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