Merge part of 'js/fmt-patch' for RFC2822 dates into 'sp/reflog'
[git/gitweb.git] / log-tree.c
blob2bcf2dfaaa483362b3cc4beb0ae73f3e6fabe764
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
6 static void show_parents(struct commit *commit, int abbrev)
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));
15 void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
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;
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;
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.
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;
50 * Print header line of header..
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');
70 * And then the pretty-printed message itself
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);
76 int log_tree_diff_flush(struct rev_info *opt)
78 diffcore_std(&opt->diffopt);
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;
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;
94 static int diff_root_tree(struct rev_info *opt,
95 const unsigned char *new, const char *base)
97 int retval;
98 void *tree;
99 struct tree_desc empty, real;
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;
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;
114 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
116 unsigned const char *sha1 = commit->object.sha1;
118 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
119 return !opt->loginfo;
123 * Show the diff of a commit.
125 * Return true if we printed any log info messages
127 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
129 int showed_log;
130 struct commit_list *parents;
131 unsigned const char *sha1 = commit->object.sha1;
133 if (!opt->diff)
134 return 0;
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;
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);
151 /* If we show individual diffs, show the parent info */
152 log->parent = parents->item;
155 showed_log = 0;
156 for (;;) {
157 struct commit *parent = parents->item;
159 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
160 log_tree_diff_flush(opt);
162 showed_log |= !opt->loginfo;
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;
171 return showed_log;
174 int log_tree_commit(struct rev_info *opt, struct commit *commit)
176 struct log_info log;
177 int shown;
179 log.commit = commit;
180 log.parent = NULL;
181 opt->loginfo = &log;
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;
189 opt->loginfo = NULL;
190 return shown;