git diff: support "-U" and "--unified" options properly
We used to parse "-U" and "--unified" as part of the GIT_DIFF_OPTS environment variable, but strangely enough we would _not_ parse them as part of the normal diff command line (where we only accepted "-u"). This adds parsing of -U and --unified, both with an optional numeric argument. So now you can just say git diff --unified=5 to get a unified diff with a five-line context, instead of having to do something silly like GIT_DIFF_OPTS="--unified=5" git diff -u (that silly format does continue to still work, of course). Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
May 13, 2006 at 13:23 UTC
ee1e5412a702674d4da95a4aef73a73d2834dcc3
3 files changed
+53
-1
combine-diff.c
+1
@@ -608,6 +608,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
608
int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
609
mmfile_t result_file;
610
611
+ context = opt->context;
612
/* Read the result of merge first */
613
if (!working_tree_file)
614
result = grab_blob(elem->sha1, &result_size);
diff.c
+51
-1
@@ -558,7 +558,7 @@ static void builtin_diff(const char *name_a,
558
559
ecbdata.label_path = lbl;
560
xpp.flags = XDF_NEED_MINIMAL;
561
- xecfg.ctxlen = 3;
561
+ xecfg.ctxlen = o->context;
562
xecfg.flags = XDL_EMIT_FUNCNAMES;
563
if (!diffopts)
564
;
@@ -1182,6 +1182,7 @@ void diff_setup(struct diff_options *options)
1182
options->line_termination = '\n';
1183
options->break_opt = -1;
1184
options->rename_limit = -1;
1185
+ options->context = 3;
1186
1187
options->change = diff_change;
1188
options->add_remove = diff_addremove;
@@ -1222,11 +1223,60 @@ int diff_setup_done(struct diff_options *options)
1223
return 0;
1224
}
1225
1226
+int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1227
+{
1228
+ char c, *eq;
1229
+ int len;
1230
+
1231
+ if (*arg != '-')
1232
+ return 0;
1233
+ c = *++arg;
1234
+ if (!c)
1235
+ return 0;
1236
+ if (c == arg_short) {
1237
+ c = *++arg;
1238
+ if (!c)
1239
+ return 1;
1240
+ if (val && isdigit(c)) {
1241
+ char *end;
1242
+ int n = strtoul(arg, &end, 10);
1243
+ if (*end)
1244
+ return 0;
1245
+ *val = n;
1246
+ return 1;
1247
+ }
1248
+ return 0;
1249
+ }
1250
+ if (c != '-')
1251
+ return 0;
1252
+ arg++;
1253
+ eq = strchr(arg, '=');
1254
+ if (eq)
1255
+ len = eq - arg;
1256
+ else
1257
+ len = strlen(arg);
1258
+ if (!len || strncmp(arg, arg_long, len))
1259
+ return 0;
1260
+ if (eq) {
1261
+ int n;
1262
+ char *end;
1263
+ if (!isdigit(*++eq))
1264
+ return 0;
1265
+ n = strtoul(eq, &end, 10);
1266
+ if (*end)
1267
+ return 0;
1268
+ *val = n;
1269
+ }
1270
+ return 1;
1271
+}
1272
+
1273
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1274
{
1275
const char *arg = av[0];
1276
if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1277
options->output_format = DIFF_FORMAT_PATCH;
1278
+ else if (opt_arg(arg, 'U', "unified", &options->context))
1279
+ options->output_format = DIFF_FORMAT_PATCH;
1280
else if (!strcmp(arg, "--patch-with-raw")) {
1281
options->output_format = DIFF_FORMAT_PATCH;
1282
options->with_raw = 1;
diff.h
+1
@@ -32,6 +32,7 @@ struct diff_options {
32
full_index:1,
33
silent_on_remove:1,
34
find_copies_harder:1;
35
+ int context;
36
int break_opt;
37
int detect_rename;
38
int line_termination;