Log message printout cleanups (#2)
[git] / log-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5
6 void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
7 {
8         static char this_header[16384];
9         struct commit *commit = log->commit, *parent = log->parent;
10         int abbrev = opt->diffopt.abbrev;
11         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
12         int len;
13
14         opt->loginfo = NULL;
15         if (!opt->verbose_header) {
16                 puts(sha1_to_hex(commit->object.sha1));
17                 return;
18         }
19
20         /*
21          * Whitespace between commit messages, unless we are oneline
22          */
23         if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
24                 putchar('\n');
25         opt->shown_one = 1;
26
27         /*
28          * Print header line of header..
29          */
30         printf("%s%s",
31                 opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
32                 diff_unique_abbrev(commit->object.sha1, abbrev_commit));
33         if (parent) 
34                 printf(" (from %s)", diff_unique_abbrev(parent->object.sha1, abbrev_commit));
35         putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
36
37         /*
38          * And then the pretty-printed message itself
39          */
40         len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev);
41         printf("%s%s", this_header, sep);
42 }
43
44 int log_tree_diff_flush(struct rev_info *opt)
45 {
46         diffcore_std(&opt->diffopt);
47
48         if (diff_queue_is_empty()) {
49                 int saved_fmt = opt->diffopt.output_format;
50                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
51                 diff_flush(&opt->diffopt);
52                 opt->diffopt.output_format = saved_fmt;
53                 return 0;
54         }
55
56         if (opt->loginfo && !opt->no_commit_id)
57                 show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
58         diff_flush(&opt->diffopt);
59         return 1;
60 }
61
62 static int diff_root_tree(struct rev_info *opt,
63                           const unsigned char *new, const char *base)
64 {
65         int retval;
66         void *tree;
67         struct tree_desc empty, real;
68
69         tree = read_object_with_reference(new, tree_type, &real.size, NULL);
70         if (!tree)
71                 die("unable to read root tree (%s)", sha1_to_hex(new));
72         real.buf = tree;
73
74         empty.buf = "";
75         empty.size = 0;
76         retval = diff_tree(&empty, &real, base, &opt->diffopt);
77         free(tree);
78         log_tree_diff_flush(opt);
79         return retval;
80 }
81
82 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
83 {
84         unsigned const char *sha1 = commit->object.sha1;
85
86         diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
87         return !opt->loginfo;
88 }
89
90 /*
91  * Show the diff of a commit.
92  *
93  * Return true if we printed any log info messages
94  */
95 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
96 {
97         int showed_log;
98         struct commit_list *parents;
99         unsigned const char *sha1 = commit->object.sha1;
100
101         if (!opt->diff)
102                 return 0;
103
104         /* Root commit? */
105         parents = commit->parents;
106         if (!parents) {
107                 if (opt->show_root_diff)
108                         diff_root_tree(opt, sha1, "");
109                 return !opt->loginfo;
110         }
111
112         /* More than one parent? */
113         if (parents && parents->next) {
114                 if (opt->ignore_merges)
115                         return 0;
116                 else if (opt->combine_merges)
117                         return do_diff_combined(opt, commit);
118
119                 /* If we show individual diffs, show the parent info */
120                 log->parent = parents->item;
121         }
122
123         showed_log = 0;
124         for (;;) {
125                 struct commit *parent = parents->item;
126
127                 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
128                 log_tree_diff_flush(opt);
129
130                 showed_log |= !opt->loginfo;
131
132                 /* Set up the log info for the next parent, if any.. */
133                 parents = parents->next;
134                 if (!parents)
135                         break;
136                 log->parent = parents->item;
137                 opt->loginfo = log;
138         }
139         return showed_log;
140 }
141
142 int log_tree_commit(struct rev_info *opt, struct commit *commit)
143 {
144         struct log_info log;
145
146         log.commit = commit;
147         log.parent = NULL;
148         opt->loginfo = &log;
149
150         if (!log_tree_diff(opt, commit, &log) && opt->loginfo && opt->always_show_header) {
151                 log.parent = NULL;
152                 show_log(opt, opt->loginfo, "");
153         }
154         opt->loginfo = NULL;
155         return 0;
156 }