Merge part of 'js/fmt-patch' for RFC2822 dates into 'sp/reflog'
[git.git] / log-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5
6 static void show_parents(struct commit *commit, int abbrev)
7 {
8         struct commit_list *p;
9         for (p = commit->parents; p ; p = p->next) {
10                 struct commit *parent = p->item;
11                 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
12         }
13 }
14
15 void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
16 {
17         static char this_header[16384];
18         struct commit *commit = log->commit, *parent = log->parent;
19         int abbrev = opt->diffopt.abbrev;
20         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
21         const char *extra;
22         int len;
23
24         opt->loginfo = NULL;
25         if (!opt->verbose_header) {
26                 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
27                 if (opt->parents)
28                         show_parents(commit, abbrev_commit);
29                 putchar('\n');
30                 return;
31         }
32
33         /*
34          * The "oneline" format has several special cases:
35          *  - The pretty-printed commit lacks a newline at the end
36          *    of the buffer, but we do want to make sure that we
37          *    have a newline there. If the separator isn't already
38          *    a newline, add an extra one.
39          *  - unlike other log messages, the one-line format does
40          *    not have an empty line between entries.
41          */
42         extra = "";
43         if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
44                 extra = "\n";
45         if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
46                 putchar('\n');
47         opt->shown_one = 1;
48
49         /*
50          * Print header line of header..
51          */
52
53         if (opt->commit_format == CMIT_FMT_EMAIL)
54                 printf("From %s  Thu Apr 7 15:13:13 2005\n",
55                        sha1_to_hex(commit->object.sha1));
56         else {
57                 printf("%s%s",
58                        opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
59                        diff_unique_abbrev(commit->object.sha1, abbrev_commit));
60                 if (opt->parents)
61                         show_parents(commit, abbrev_commit);
62                 if (parent)
63                         printf(" (from %s)",
64                                diff_unique_abbrev(parent->object.sha1,
65                                                   abbrev_commit));
66                 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
67         }
68
69         /*
70          * And then the pretty-printed message itself
71          */
72         len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev);
73         printf("%s%s%s", this_header, extra, sep);
74 }
75
76 int log_tree_diff_flush(struct rev_info *opt)
77 {
78         diffcore_std(&opt->diffopt);
79
80         if (diff_queue_is_empty()) {
81                 int saved_fmt = opt->diffopt.output_format;
82                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
83                 diff_flush(&opt->diffopt);
84                 opt->diffopt.output_format = saved_fmt;
85                 return 0;
86         }
87
88         if (opt->loginfo && !opt->no_commit_id)
89                 show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
90         diff_flush(&opt->diffopt);
91         return 1;
92 }
93
94 static int diff_root_tree(struct rev_info *opt,
95                           const unsigned char *new, const char *base)
96 {
97         int retval;
98         void *tree;
99         struct tree_desc empty, real;
100
101         tree = read_object_with_reference(new, tree_type, &real.size, NULL);
102         if (!tree)
103                 die("unable to read root tree (%s)", sha1_to_hex(new));
104         real.buf = tree;
105
106         empty.buf = "";
107         empty.size = 0;
108         retval = diff_tree(&empty, &real, base, &opt->diffopt);
109         free(tree);
110         log_tree_diff_flush(opt);
111         return retval;
112 }
113
114 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
115 {
116         unsigned const char *sha1 = commit->object.sha1;
117
118         diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
119         return !opt->loginfo;
120 }
121
122 /*
123  * Show the diff of a commit.
124  *
125  * Return true if we printed any log info messages
126  */
127 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
128 {
129         int showed_log;
130         struct commit_list *parents;
131         unsigned const char *sha1 = commit->object.sha1;
132
133         if (!opt->diff)
134                 return 0;
135
136         /* Root commit? */
137         parents = commit->parents;
138         if (!parents) {
139                 if (opt->show_root_diff)
140                         diff_root_tree(opt, sha1, "");
141                 return !opt->loginfo;
142         }
143
144         /* More than one parent? */
145         if (parents && parents->next) {
146                 if (opt->ignore_merges)
147                         return 0;
148                 else if (opt->combine_merges)
149                         return do_diff_combined(opt, commit);
150
151                 /* If we show individual diffs, show the parent info */
152                 log->parent = parents->item;
153         }
154
155         showed_log = 0;
156         for (;;) {
157                 struct commit *parent = parents->item;
158
159                 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
160                 log_tree_diff_flush(opt);
161
162                 showed_log |= !opt->loginfo;
163
164                 /* Set up the log info for the next parent, if any.. */
165                 parents = parents->next;
166                 if (!parents)
167                         break;
168                 log->parent = parents->item;
169                 opt->loginfo = log;
170         }
171         return showed_log;
172 }
173
174 int log_tree_commit(struct rev_info *opt, struct commit *commit)
175 {
176         struct log_info log;
177         int shown;
178
179         log.commit = commit;
180         log.parent = NULL;
181         opt->loginfo = &log;
182
183         shown = log_tree_diff(opt, commit, &log);
184         if (!shown && opt->loginfo && opt->always_show_header) {
185                 log.parent = NULL;
186                 show_log(opt, opt->loginfo, "");
187                 shown = 1;
188         }
189         opt->loginfo = NULL;
190         return shown;
191 }