Merge branch 'lt/diff'
authorJunio C Hamano <junkio@cox.net>
Tue, 16 May 2006 01:09:15 +0000 (18:09 -0700)
committerJunio C Hamano <junkio@cox.net>
Tue, 16 May 2006 01:09:15 +0000 (18:09 -0700)
* lt/diff:
  git diff: support "-U" and "--unified" options properly

1  2 
diff.c

diff --combined diff.c
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -232,16 -232,11 +232,16 @@@ static char *pprint_rename(const char *
         * name-a => name-b
         */
        if (pfx_length + sfx_length) {
 +              int a_midlen = len_a - pfx_length - sfx_length;
 +              int b_midlen = len_b - pfx_length - sfx_length;
 +              if (a_midlen < 0) a_midlen = 0;
 +              if (b_midlen < 0) b_midlen = 0;
 +
                name = xmalloc(len_a + len_b - pfx_length - sfx_length + 7);
                sprintf(name, "%.*s{%.*s => %.*s}%s",
                        pfx_length, a,
 -                      len_a - pfx_length - sfx_length, a + pfx_length,
 -                      len_b - pfx_length - sfx_length, b + pfx_length,
 +                      a_midlen, a + pfx_length,
 +                      b_midlen, b + pfx_length,
                        a + len_a - sfx_length);
        }
        else {
@@@ -563,7 -558,7 +563,7 @@@ static void builtin_diff(const char *na
  
                ecbdata.label_path = lbl;
                xpp.flags = XDF_NEED_MINIMAL;
-               xecfg.ctxlen = 3;
+               xecfg.ctxlen = o->context;
                xecfg.flags = XDL_EMIT_FUNCNAMES;
                if (!diffopts)
                        ;
@@@ -1187,6 -1182,7 +1187,7 @@@ void diff_setup(struct diff_options *op
        options->line_termination = '\n';
        options->break_opt = -1;
        options->rename_limit = -1;
+       options->context = 3;
  
        options->change = diff_change;
        options->add_remove = diff_addremove;
@@@ -1227,11 -1223,60 +1228,60 @@@ int diff_setup_done(struct diff_option
        return 0;
  }
  
+ int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
+ {
+       char c, *eq;
+       int len;
+       if (*arg != '-')
+               return 0;
+       c = *++arg;
+       if (!c)
+               return 0;
+       if (c == arg_short) {
+               c = *++arg;
+               if (!c)
+                       return 1;
+               if (val && isdigit(c)) {
+                       char *end;
+                       int n = strtoul(arg, &end, 10);
+                       if (*end)
+                               return 0;
+                       *val = n;
+                       return 1;
+               }
+               return 0;
+       }
+       if (c != '-')
+               return 0;
+       arg++;
+       eq = strchr(arg, '=');
+       if (eq)
+               len = eq - arg;
+       else
+               len = strlen(arg);
+       if (!len || strncmp(arg, arg_long, len))
+               return 0;
+       if (eq) {
+               int n;
+               char *end;
+               if (!isdigit(*++eq))
+                       return 0;
+               n = strtoul(eq, &end, 10);
+               if (*end)
+                       return 0;
+               *val = n;
+       }
+       return 1;
+ }
  int diff_opt_parse(struct diff_options *options, const char **av, int ac)
  {
        const char *arg = av[0];
        if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
                options->output_format = DIFF_FORMAT_PATCH;
+       else if (opt_arg(arg, 'U', "unified", &options->context))
+               options->output_format = DIFF_FORMAT_PATCH;
        else if (!strcmp(arg, "--patch-with-raw")) {
                options->output_format = DIFF_FORMAT_PATCH;
                options->with_raw = 1;