2
#include "builtin.h"
3
#include "config.h"
4
#include "diff.h"
5
+#include "parse-options.h"
6
7
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
8
{
58
}
59
60
static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
60
- struct strbuf *line_buf, int stable)
61
+ struct strbuf *line_buf, int stable, int verbatim)
62
{
63
int patchlen = 0, found_next = 0;
64
int before = -1, after = -1;
77
if (!skip_prefix(line, "diff-tree ", &p) &&
78
!skip_prefix(line, "commit ", &p) &&
79
!skip_prefix(line, "From ", &p) &&
79
- starts_with(line, "\\ ") && 12 < strlen(line))
80
+ starts_with(line, "\\ ") && 12 < strlen(line)) {
81
+ if (verbatim)
82
+ the_hash_algo->update_fn(&ctx, line, strlen(line));
83
continue;
84
+ }
85
86
if (!get_oid_hex(p, next_oid)) {
87
found_next = 1;
156
if (line[0] == '+' || line[0] == ' ')
157
after--;
158
155
- /* Compute the sha without whitespace */
156
- len = remove_space(line);
159
+ /* Add line to hash algo (possibly removing whitespace) */
160
+ len = verbatim ? strlen(line) : remove_space(line);
161
patchlen += len;
162
the_hash_algo->update_fn(&ctx, line, len);
163
}
170
return patchlen;
171
}
172
169
-static void generate_id_list(int stable)
173
+static void generate_id_list(int stable, int verbatim)
174
{
175
struct object_id oid, n, result;
176
int patchlen;
178
179
oidclr(&oid);
180
while (!feof(stdin)) {
177
- patchlen = get_one_patchid(&n, &result, &line_buf, stable);
181
+ patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim);
182
flush_current_id(patchlen, &oid, &result);
183
oidcpy(&oid, &n);
184
}
185
strbuf_release(&line_buf);
186
}
187
184
-static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
188
+static const char *const patch_id_usage[] = {
189
+ N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
190
+};
191
+
192
+struct patch_id_opts {
193
+ int stable;
194
+ int verbatim;
195
+};
196
197
static int git_patch_id_config(const char *var, const char *value, void *cb)
198
{
188
- int *stable = cb;
199
+ struct patch_id_opts *opts = cb;
200
201
if (!strcmp(var, "patchid.stable")) {
191
- *stable = git_config_bool(var, value);
202
+ opts->stable = git_config_bool(var, value);
203
+ return 0;
204
+ }
205
+ if (!strcmp(var, "patchid.verbatim")) {
206
+ opts->verbatim = git_config_bool(var, value);
207
return 0;
208
}
209
212
213
int cmd_patch_id(int argc, const char **argv, const char *prefix)
214
{
200
- int stable = -1;
201
-
202
- git_config(git_patch_id_config, &stable);
203
-
204
- /* If nothing is set, default to unstable. */
205
- if (stable < 0)
206
- stable = 0;
207
-
208
- if (argc == 2 && !strcmp(argv[1], "--stable"))
209
- stable = 1;
210
- else if (argc == 2 && !strcmp(argv[1], "--unstable"))
211
- stable = 0;
212
- else if (argc != 1)
213
- usage(patch_id_usage);
214
-
215
- generate_id_list(stable);
215
+ /* if nothing is set, default to unstable */
216
+ struct patch_id_opts config = {0, 0};
217
+ int opts = 0;
218
+ struct option builtin_patch_id_options[] = {
219
+ OPT_CMDMODE(0, "unstable", &opts,
220
+ N_("use the unstable patch-id algorithm"), 1),
221
+ OPT_CMDMODE(0, "stable", &opts,
222
+ N_("use the stable patch-id algorithm"), 2),
223
+ OPT_CMDMODE(0, "verbatim", &opts,
224
+ N_("don't strip whitespace from the patch"), 3),
225
+ OPT_END()
226
+ };
227
+
228
+ git_config(git_patch_id_config, &config);
229
+
230
+ /* verbatim implies stable */
231
+ if (config.verbatim)
232
+ config.stable = 1;
233
+
234
+ argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
235
+ patch_id_usage, 0);
236
+
237
+ generate_id_list(opts ? opts > 1 : config.stable,
238
+ opts ? opts == 3 : config.verbatim);
239
return 0;
240
}