Merge branch 'jk/diff-graph-cleanup'
authorJunio C Hamano <gitster@pobox.com>
Thu, 14 Feb 2013 18:29:59 +0000 (10:29 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 Feb 2013 18:29:59 +0000 (10:29 -0800)
Refactors a lot of repetitive code sequence from the graph drawing
code and adds it to the combined diff output.

* jk/diff-graph-cleanup:
combine-diff.c: teach combined diffs about line prefix
diff.c: use diff_line_prefix() where applicable
diff: add diff_line_prefix function
diff.c: make constant string arguments const
diff: write prefix to the correct file
graph: output padding for merge subsequent parents

1  2 
combine-diff.c
diff.c
diff --combined combine-diff.c
index 7f6187f9cd2fc44540cddbcf4bb853231c57397c,35c817bb2c52399c6eaaebb23dc6c3ba05c0ea2b..35d41cd56d2ddb8df416e5e422552ec473294055
@@@ -526,7 -526,8 +526,8 @@@ static void show_line_to_eol(const cha
               saw_cr_at_eol ? "\r" : "");
  }
  
- static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
+ static void dump_sline(struct sline *sline, const char *line_prefix,
+                      unsigned long cnt, int num_parent,
                       int use_color, int result_deleted)
  {
        unsigned long mark = (1UL<<num_parent);
                        rlines -= null_context;
                }
  
-               fputs(c_frag, stdout);
+               printf("%s%s", line_prefix, c_frag);
                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
                for (i = 0; i < num_parent; i++)
                        show_parent_lno(sline, lno, hunk_end, i, null_context);
                        struct sline *sl = &sline[lno++];
                        ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
                        while (ll) {
-                               fputs(c_old, stdout);
+                               printf("%s%s", line_prefix, c_old);
                                for (j = 0; j < num_parent; j++) {
                                        if (ll->parent_map & (1UL<<j))
                                                putchar('-');
                        if (cnt < lno)
                                break;
                        p_mask = 1;
+                       fputs(line_prefix, stdout);
                        if (!(sl->flag & (mark-1))) {
                                /*
                                 * This sline was here to hang the
@@@ -680,11 -682,13 +682,13 @@@ static void reuse_combine_diff(struct s
  static void dump_quoted_path(const char *head,
                             const char *prefix,
                             const char *path,
+                            const char *line_prefix,
                             const char *c_meta, const char *c_reset)
  {
        static struct strbuf buf = STRBUF_INIT;
  
        strbuf_reset(&buf);
+       strbuf_addstr(&buf, line_prefix);
        strbuf_addstr(&buf, c_meta);
        strbuf_addstr(&buf, head);
        quote_two_c_style(&buf, prefix, path, 0);
@@@ -696,6 -700,7 +700,7 @@@ static void show_combined_header(struc
                                 int num_parent,
                                 int dense,
                                 struct rev_info *rev,
+                                const char *line_prefix,
                                 int mode_differs,
                                 int show_file_header)
  {
                show_log(rev);
  
        dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
-                        "", elem->path, c_meta, c_reset);
-       printf("%sindex ", c_meta);
+                        "", elem->path, line_prefix, c_meta, c_reset);
+       printf("%s%sindex ", line_prefix, c_meta);
        for (i = 0; i < num_parent; i++) {
                abb = find_unique_abbrev(elem->parent[i].sha1,
                                         abbrev);
                            DIFF_STATUS_ADDED)
                                added = 0;
                if (added)
-                       printf("%snew file mode %06o",
-                              c_meta, elem->mode);
+                       printf("%s%snew file mode %06o",
+                              line_prefix, c_meta, elem->mode);
                else {
                        if (deleted)
-                               printf("%sdeleted file ", c_meta);
+                               printf("%s%sdeleted file ",
+                                      line_prefix, c_meta);
                        printf("mode ");
                        for (i = 0; i < num_parent; i++) {
                                printf("%s%06o", i ? "," : "",
  
        if (added)
                dump_quoted_path("--- ", "", "/dev/null",
-                                c_meta, c_reset);
+                                line_prefix, c_meta, c_reset);
        else
                dump_quoted_path("--- ", a_prefix, elem->path,
-                                c_meta, c_reset);
+                                line_prefix, c_meta, c_reset);
        if (deleted)
                dump_quoted_path("+++ ", "", "/dev/null",
-                                c_meta, c_reset);
+                                line_prefix, c_meta, c_reset);
        else
                dump_quoted_path("+++ ", b_prefix, elem->path,
-                                c_meta, c_reset);
+                                line_prefix, c_meta, c_reset);
  }
  
  static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
        struct userdiff_driver *userdiff;
        struct userdiff_driver *textconv = NULL;
        int is_binary;
+       const char *line_prefix = diff_line_prefix(opt);
  
        context = opt->context;
        userdiff = userdiff_find_by_path(elem->path);
        }
        if (is_binary) {
                show_combined_header(elem, num_parent, dense, rev,
-                                    mode_differs, 0);
+                                    line_prefix, mode_differs, 0);
                printf("Binary files differ\n");
                free(result);
                return;
  
        if (show_hunks || mode_differs || working_tree_file) {
                show_combined_header(elem, num_parent, dense, rev,
-                                    mode_differs, 1);
-               dump_sline(sline, cnt, num_parent,
+                                    line_prefix, mode_differs, 1);
+               dump_sline(sline, line_prefix, cnt, num_parent,
                           opt->use_color, result_deleted);
        }
        free(result);
        free(sline);
  }
  
 -#define COLONS "::::::::::::::::::::::::::::::::"
 -
  static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
  {
        struct diff_options *opt = &rev->diffopt;
 -      int i, offset;
 -      const char *prefix;
 -      int line_termination, inter_name_termination;
 +      int line_termination, inter_name_termination, i;
+       const char *line_prefix = diff_line_prefix(opt);
  
        line_termination = opt->line_termination;
        inter_name_termination = '\t';
        if (rev->loginfo && !rev->no_commit_id)
                show_log(rev);
  
++
        if (opt->output_format & DIFF_FORMAT_RAW) {
 -              offset = strlen(COLONS) - num_parent;
 -              if (offset < 0)
 -                      offset = 0;
 -              prefix = COLONS + offset;
+               printf("%s", line_prefix);
++
 +              /* As many colons as there are parents */
 +              for (i = 0; i < num_parent; i++)
 +                      putchar(':');
  
                /* Show the modes */
 -              for (i = 0; i < num_parent; i++) {
 -                      printf("%s%06o", prefix, p->parent[i].mode);
 -                      prefix = " ";
 -              }
 -              printf("%s%06o", prefix, p->mode);
 +              for (i = 0; i < num_parent; i++)
 +                      printf("%06o ", p->parent[i].mode);
 +              printf("%06o", p->mode);
  
                /* Show sha1's */
                for (i = 0; i < num_parent; i++)
@@@ -1033,6 -1049,7 +1044,7 @@@ void show_combined_diff(struct combine_
                       struct rev_info *rev)
  {
        struct diff_options *opt = &rev->diffopt;
        if (!p->len)
                return;
        if (opt->output_format & (DIFF_FORMAT_RAW |
@@@ -1143,8 -1160,10 +1155,10 @@@ void diff_tree_combined(const unsigned 
  
                if (show_log_first && i == 0) {
                        show_log(rev);
                        if (rev->verbose_header && opt->output_format)
-                               putchar(opt->line_termination);
+                               printf("%s%c", diff_line_prefix(opt),
+                                      opt->line_termination);
                }
                diff_flush(&diffopts);
        }
  
                if (opt->output_format & DIFF_FORMAT_PATCH) {
                        if (needsep)
-                               putchar(opt->line_termination);
+                               printf("%s%c", diff_line_prefix(opt),
+                                      opt->line_termination);
                        for (p = paths; p; p = p->next) {
                                if (p->len)
                                        show_patch_diff(p, num_parent, dense,
diff --combined diff.c
index 348f71b46256657860a2f7f79598e1d9b19d6c54,170ada5c3cc3f81f7e0e4e41b0a7a983cfeba28e..f441f6c7f8790a9601163a7e3280239e63036755
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -402,12 -402,7 +402,7 @@@ static void emit_line_0(struct diff_opt
        int nofirst;
        FILE *file = o->file;
  
-       if (o->output_prefix) {
-               struct strbuf *msg = NULL;
-               msg = o->output_prefix(o, o->output_prefix_data);
-               assert(msg);
-               fwrite(msg->buf, msg->len, 1, file);
-       }
+       fputs(diff_line_prefix(o), file);
  
        if (len == 0) {
                has_trailing_newline = (first == '\n');
@@@ -625,13 -620,7 +620,7 @@@ static void emit_rewrite_diff(const cha
        char *data_one, *data_two;
        size_t size_one, size_two;
        struct emit_callback ecbdata;
-       char *line_prefix = "";
-       struct strbuf *msgbuf;
-       if (o && o->output_prefix) {
-               msgbuf = o->output_prefix(o, o->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
+       const char *line_prefix = diff_line_prefix(o);
  
        if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
                a_prefix = o->b_prefix;
@@@ -827,18 -816,14 +816,14 @@@ static void fn_out_diff_words_aux(void 
        int minus_first, minus_len, plus_first, plus_len;
        const char *minus_begin, *minus_end, *plus_begin, *plus_end;
        struct diff_options *opt = diff_words->opt;
-       struct strbuf *msgbuf;
-       char *line_prefix = "";
+       const char *line_prefix;
  
        if (line[0] != '@' || parse_hunk_header(line, len,
                        &minus_first, &minus_len, &plus_first, &plus_len))
                return;
  
        assert(opt);
-       if (opt->output_prefix) {
-               msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
+       line_prefix = diff_line_prefix(opt);
  
        /* POSIX requires that first be decremented by one if len == 0... */
        if (minus_len) {
@@@ -962,14 -947,10 +947,10 @@@ static void diff_words_show(struct diff
        struct diff_words_style *style = diff_words->style;
  
        struct diff_options *opt = diff_words->opt;
-       struct strbuf *msgbuf;
-       char *line_prefix = "";
+       const char *line_prefix;
  
        assert(opt);
-       if (opt->output_prefix) {
-               msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
+       line_prefix = diff_line_prefix(opt);
  
        /* special case: only removal */
        if (!diff_words->plus.text.size) {
@@@ -1105,6 -1086,16 +1086,16 @@@ const char *diff_get_color(int diff_use
        return "";
  }
  
+ const char *diff_line_prefix(struct diff_options *opt)
+ {
+       struct strbuf *msgbuf;
+       if (!opt->output_prefix)
+               return "";
+       msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
+       return msgbuf->buf;
+ }
  static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
  {
        const char *cp;
@@@ -1145,13 -1136,7 +1136,7 @@@ static void fn_out_consume(void *priv, 
        const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
        struct diff_options *o = ecbdata->opt;
-       char *line_prefix = "";
-       struct strbuf *msgbuf;
-       if (o && o->output_prefix) {
-               msgbuf = o->output_prefix(o, o->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
+       const char *line_prefix = diff_line_prefix(o);
  
        if (ecbdata->header) {
                fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
@@@ -1475,16 -1460,11 +1460,11 @@@ static void show_stats(struct diffstat_
        const char *reset, *add_c, *del_c;
        const char *line_prefix = "";
        int extra_shown = 0;
-       struct strbuf *msg = NULL;
  
        if (data->nr == 0)
                return;
  
-       if (options->output_prefix) {
-               msg = options->output_prefix(options, options->output_prefix_data);
-               line_prefix = msg->buf;
-       }
+       line_prefix = diff_line_prefix(options);
        count = options->stat_count ? options->stat_count : data->nr;
  
        reset = diff_get_color_opt(options, DIFF_RESET);
@@@ -1736,12 -1716,7 +1716,7 @@@ static void show_shortstats(struct diff
                        dels += deleted;
                }
        }
-       if (options->output_prefix) {
-               struct strbuf *msg = NULL;
-               msg = options->output_prefix(options,
-                               options->output_prefix_data);
-               fprintf(options->file, "%s", msg->buf);
-       }
+       fprintf(options->file, "%s", diff_line_prefix(options));
        print_stat_summary(options->file, total_files, adds, dels);
  }
  
@@@ -1755,12 -1730,7 +1730,7 @@@ static void show_numstat(struct diffsta
        for (i = 0; i < data->nr; i++) {
                struct diffstat_file *file = data->files[i];
  
-               if (options->output_prefix) {
-                       struct strbuf *msg = NULL;
-                       msg = options->output_prefix(options,
-                                       options->output_prefix_data);
-                       fprintf(options->file, "%s", msg->buf);
-               }
+               fprintf(options->file, "%s", diff_line_prefix(options));
  
                if (file->is_binary)
                        fprintf(options->file, "-\t-\t");
@@@ -1802,13 -1772,7 +1772,7 @@@ static long gather_dirstat(struct diff_
  {
        unsigned long this_dir = 0;
        unsigned int sources = 0;
-       const char *line_prefix = "";
-       struct strbuf *msg = NULL;
-       if (opt->output_prefix) {
-               msg = opt->output_prefix(opt, opt->output_prefix_data);
-               line_prefix = msg->buf;
-       }
+       const char *line_prefix = diff_line_prefix(opt);
  
        while (dir->nr) {
                struct dirstat_file *f = dir->files;
@@@ -2058,15 -2022,10 +2022,10 @@@ static void checkdiff_consume(void *pri
        const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
        const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
        char *err;
-       char *line_prefix = "";
-       struct strbuf *msgbuf;
+       const char *line_prefix;
  
        assert(data->o);
-       if (data->o->output_prefix) {
-               msgbuf = data->o->output_prefix(data->o,
-                       data->o->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
+       line_prefix = diff_line_prefix(data->o);
  
        if (line[0] == '+') {
                unsigned bad;
@@@ -2123,7 -2082,8 +2082,8 @@@ static unsigned char *deflate_it(char *
        return deflated;
  }
  
- static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, char *prefix)
+ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
+                                 const char *prefix)
  {
        void *cp;
        void *delta;
        free(data);
  }
  
- static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two, char *prefix)
+ static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two,
+                            const char *prefix)
  {
        fprintf(file, "%sGIT binary patch\n", prefix);
        emit_binary_diff_body(file, one, two, prefix);
@@@ -2251,13 -2212,7 +2212,7 @@@ static void builtin_diff(const char *na
        struct userdiff_driver *textconv_one = NULL;
        struct userdiff_driver *textconv_two = NULL;
        struct strbuf header = STRBUF_INIT;
-       struct strbuf *msgbuf;
-       char *line_prefix = "";
-       if (o->output_prefix) {
-               msgbuf = o->output_prefix(o, o->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
+       const char *line_prefix = diff_line_prefix(o);
  
        if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
                        (!one->mode || S_ISGITLINK(one->mode)) &&
@@@ -2956,14 -2911,9 +2911,9 @@@ static void fill_metainfo(struct strbu
  {
        const char *set = diff_get_color(use_color, DIFF_METAINFO);
        const char *reset = diff_get_color(use_color, DIFF_RESET);
-       struct strbuf *msgbuf;
-       char *line_prefix = "";
+       const char *line_prefix = diff_line_prefix(o);
  
        *must_show_header = 1;
-       if (o->output_prefix) {
-               msgbuf = o->output_prefix(o, o->output_prefix_data);
-               line_prefix = msgbuf->buf;
-       }
        strbuf_init(msg, PATH_MAX * 2 + 300);
        switch (p->status) {
        case DIFF_STATUS_COPIED:
@@@ -3626,8 -3576,6 +3576,8 @@@ int diff_opt_parse(struct diff_options 
                DIFF_OPT_SET(options, FIND_COPIES_HARDER);
        else if (!strcmp(arg, "--follow"))
                DIFF_OPT_SET(options, FOLLOW_RENAMES);
 +      else if (!strcmp(arg, "--no-follow"))
 +              DIFF_OPT_CLR(options, FOLLOW_RENAMES);
        else if (!strcmp(arg, "--color"))
                options->use_color = 1;
        else if (!prefixcmp(arg, "--color=")) {
@@@ -3900,12 -3848,8 +3850,8 @@@ static void diff_flush_raw(struct diff_
  {
        int line_termination = opt->line_termination;
        int inter_name_termination = line_termination ? '\t' : '\0';
-       if (opt->output_prefix) {
-               struct strbuf *msg = NULL;
-               msg = opt->output_prefix(opt, opt->output_prefix_data);
-               fprintf(opt->file, "%s", msg->buf);
-       }
  
+       fprintf(opt->file, "%s", diff_line_prefix(opt));
        if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
                fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
                        diff_unique_abbrev(p->one->sha1, opt->abbrev));
@@@ -4175,12 -4119,7 +4121,7 @@@ static void show_rename_copy(FILE *file
  static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
  {
        FILE *file = opt->file;
-       char *line_prefix = "";
-       if (opt->output_prefix) {
-               struct strbuf *buf = opt->output_prefix(opt, opt->output_prefix_data);
-               line_prefix = buf->buf;
-       }
+       const char *line_prefix = diff_line_prefix(opt);
  
        switch(p->status) {
        case DIFF_STATUS_DELETED:
@@@ -4481,13 -4420,9 +4422,9 @@@ void diff_flush(struct diff_options *op
  
        if (output_format & DIFF_FORMAT_PATCH) {
                if (separator) {
-                       if (options->output_prefix) {
-                               struct strbuf *msg = NULL;
-                               msg = options->output_prefix(options,
-                                       options->output_prefix_data);
-                               fwrite(msg->buf, msg->len, 1, stdout);
-                       }
-                       putc(options->line_termination, options->file);
+                       fprintf(options->file, "%s%c",
+                               diff_line_prefix(options),
+                               options->line_termination);
                        if (options->stat_sep) {
                                /* attach patch instead of inline */
                                fputs(options->stat_sep, options->file);