]>
Commit | Line | Data |
---|---|---|
5f1c3f07 JH |
1 | #include "cache.h" |
2 | #include "diff.h" | |
3 | #include "commit.h" | |
4 | #include "log-tree.h" | |
5 | ||
c8c893c6 LT |
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 | ||
cf2251b6 JH |
15 | static int append_signoff(char *buf, int buf_sz, int at, const char *signoff) |
16 | { | |
17 | int signoff_len = strlen(signoff); | |
18 | static const char signed_off_by[] = "Signed-off-by: "; | |
19 | char *cp = buf; | |
20 | ||
21 | /* Do we have enough space to add it? */ | |
22 | if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 2) | |
23 | return at; | |
24 | ||
25 | /* First see if we already have the sign-off by the signer */ | |
26 | while (1) { | |
27 | cp = strstr(cp, signed_off_by); | |
28 | if (!cp) | |
29 | break; | |
30 | cp += strlen(signed_off_by); | |
31 | if ((cp + signoff_len < buf + at) && | |
32 | !strncmp(cp, signoff, signoff_len) && | |
33 | isspace(cp[signoff_len])) | |
34 | return at; /* we already have him */ | |
35 | } | |
36 | ||
37 | strcpy(buf + at, signed_off_by); | |
38 | at += strlen(signed_off_by); | |
39 | strcpy(buf + at, signoff); | |
40 | at += signoff_len; | |
41 | buf[at++] = '\n'; | |
42 | buf[at] = 0; | |
43 | return at; | |
44 | } | |
45 | ||
91539833 LT |
46 | void show_log(struct rev_info *opt, struct log_info *log, const char *sep) |
47 | { | |
48 | static char this_header[16384]; | |
49 | struct commit *commit = log->commit, *parent = log->parent; | |
50 | int abbrev = opt->diffopt.abbrev; | |
51 | int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40; | |
a4d34e2d | 52 | const char *extra; |
91539833 | 53 | int len; |
20ff0680 | 54 | const char *subject = NULL, *extra_headers = opt->extra_headers; |
91539833 LT |
55 | |
56 | opt->loginfo = NULL; | |
57 | if (!opt->verbose_header) { | |
c8c893c6 LT |
58 | fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout); |
59 | if (opt->parents) | |
60 | show_parents(commit, abbrev_commit); | |
61 | putchar('\n'); | |
91539833 LT |
62 | return; |
63 | } | |
64 | ||
65 | /* | |
a4d34e2d LT |
66 | * The "oneline" format has several special cases: |
67 | * - The pretty-printed commit lacks a newline at the end | |
68 | * of the buffer, but we do want to make sure that we | |
69 | * have a newline there. If the separator isn't already | |
70 | * a newline, add an extra one. | |
71 | * - unlike other log messages, the one-line format does | |
72 | * not have an empty line between entries. | |
91539833 | 73 | */ |
a4d34e2d LT |
74 | extra = ""; |
75 | if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE) | |
76 | extra = "\n"; | |
91539833 LT |
77 | if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE) |
78 | putchar('\n'); | |
79 | opt->shown_one = 1; | |
80 | ||
81 | /* | |
82 | * Print header line of header.. | |
83 | */ | |
3eefc189 | 84 | |
596524b3 | 85 | if (opt->commit_format == CMIT_FMT_EMAIL) { |
698ce6f8 | 86 | char *sha1 = sha1_to_hex(commit->object.sha1); |
596524b3 JS |
87 | if (opt->total > 0) { |
88 | static char buffer[64]; | |
89 | snprintf(buffer, sizeof(buffer), | |
90 | "Subject: [PATCH %d/%d] ", | |
91 | opt->nr, opt->total); | |
92 | subject = buffer; | |
8ac80a57 | 93 | } else if (opt->total == 0) |
596524b3 | 94 | subject = "Subject: [PATCH] "; |
8ac80a57 JS |
95 | else |
96 | subject = "Subject: "; | |
97 | ||
698ce6f8 JS |
98 | printf("From %s Mon Sep 17 00:00:00 2001\n", sha1); |
99 | if (opt->mime_boundary) { | |
100 | static char subject_buffer[1024]; | |
101 | static char buffer[1024]; | |
102 | snprintf(subject_buffer, sizeof(subject_buffer) - 1, | |
20ff0680 | 103 | "%s" |
698ce6f8 JS |
104 | "MIME-Version: 1.0\n" |
105 | "Content-Type: multipart/mixed;\n" | |
106 | " boundary=\"%s%s\"\n" | |
107 | "\n" | |
108 | "This is a multi-part message in MIME " | |
109 | "format.\n" | |
110 | "--%s%s\n" | |
111 | "Content-Type: text/plain; " | |
112 | "charset=UTF-8; format=fixed\n" | |
113 | "Content-Transfer-Encoding: 8bit\n\n", | |
20ff0680 | 114 | extra_headers ? extra_headers : "", |
698ce6f8 JS |
115 | mime_boundary_leader, opt->mime_boundary, |
116 | mime_boundary_leader, opt->mime_boundary); | |
20ff0680 | 117 | extra_headers = subject_buffer; |
698ce6f8 JS |
118 | |
119 | snprintf(buffer, sizeof(buffer) - 1, | |
120 | "--%s%s\n" | |
121 | "Content-Type: text/x-patch;\n" | |
122 | " name=\"%s.diff\"\n" | |
123 | "Content-Transfer-Encoding: 8bit\n" | |
124 | "Content-Disposition: inline;\n" | |
125 | " filename=\"%s.diff\"\n\n", | |
126 | mime_boundary_leader, opt->mime_boundary, | |
127 | sha1, sha1); | |
128 | opt->diffopt.stat_sep = buffer; | |
129 | } | |
596524b3 | 130 | } else { |
3eefc189 JH |
131 | printf("%s%s", |
132 | opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ", | |
133 | diff_unique_abbrev(commit->object.sha1, abbrev_commit)); | |
c66b6c06 JH |
134 | if (opt->parents) |
135 | show_parents(commit, abbrev_commit); | |
73f0a157 | 136 | if (parent) |
3eefc189 JH |
137 | printf(" (from %s)", |
138 | diff_unique_abbrev(parent->object.sha1, | |
139 | abbrev_commit)); | |
140 | putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n'); | |
141 | } | |
91539833 LT |
142 | |
143 | /* | |
144 | * And then the pretty-printed message itself | |
145 | */ | |
20ff0680 | 146 | len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject, extra_headers); |
cf2251b6 JH |
147 | |
148 | if (opt->add_signoff) | |
149 | len = append_signoff(this_header, sizeof(this_header), len, | |
150 | opt->add_signoff); | |
a4d34e2d | 151 | printf("%s%s%s", this_header, extra, sep); |
91539833 LT |
152 | } |
153 | ||
cd2bdc53 | 154 | int log_tree_diff_flush(struct rev_info *opt) |
5f1c3f07 JH |
155 | { |
156 | diffcore_std(&opt->diffopt); | |
91539833 | 157 | |
5f1c3f07 JH |
158 | if (diff_queue_is_empty()) { |
159 | int saved_fmt = opt->diffopt.output_format; | |
160 | opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; | |
161 | diff_flush(&opt->diffopt); | |
162 | opt->diffopt.output_format = saved_fmt; | |
163 | return 0; | |
164 | } | |
91539833 | 165 | |
c6744349 TH |
166 | if (opt->loginfo && !opt->no_commit_id) { |
167 | if (opt->diffopt.output_format & DIFF_FORMAT_DIFFSTAT) { | |
168 | show_log(opt, opt->loginfo, "---\n"); | |
169 | } else { | |
170 | show_log(opt, opt->loginfo, "\n"); | |
171 | } | |
172 | } | |
5f1c3f07 JH |
173 | diff_flush(&opt->diffopt); |
174 | return 1; | |
175 | } | |
176 | ||
cd2bdc53 | 177 | static int diff_root_tree(struct rev_info *opt, |
5f1c3f07 JH |
178 | const unsigned char *new, const char *base) |
179 | { | |
180 | int retval; | |
181 | void *tree; | |
182 | struct tree_desc empty, real; | |
183 | ||
184 | tree = read_object_with_reference(new, tree_type, &real.size, NULL); | |
185 | if (!tree) | |
186 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
187 | real.buf = tree; | |
188 | ||
189 | empty.buf = ""; | |
190 | empty.size = 0; | |
191 | retval = diff_tree(&empty, &real, base, &opt->diffopt); | |
192 | free(tree); | |
193 | log_tree_diff_flush(opt); | |
194 | return retval; | |
195 | } | |
196 | ||
cd2bdc53 | 197 | static int do_diff_combined(struct rev_info *opt, struct commit *commit) |
5f1c3f07 JH |
198 | { |
199 | unsigned const char *sha1 = commit->object.sha1; | |
200 | ||
91539833 LT |
201 | diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt); |
202 | return !opt->loginfo; | |
5f1c3f07 JH |
203 | } |
204 | ||
91539833 LT |
205 | /* |
206 | * Show the diff of a commit. | |
207 | * | |
208 | * Return true if we printed any log info messages | |
209 | */ | |
210 | static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) | |
5f1c3f07 | 211 | { |
91539833 | 212 | int showed_log; |
5f1c3f07 JH |
213 | struct commit_list *parents; |
214 | unsigned const char *sha1 = commit->object.sha1; | |
215 | ||
91539833 LT |
216 | if (!opt->diff) |
217 | return 0; | |
218 | ||
5f1c3f07 | 219 | /* Root commit? */ |
91539833 LT |
220 | parents = commit->parents; |
221 | if (!parents) { | |
222 | if (opt->show_root_diff) | |
223 | diff_root_tree(opt, sha1, ""); | |
224 | return !opt->loginfo; | |
5f1c3f07 JH |
225 | } |
226 | ||
227 | /* More than one parent? */ | |
91539833 | 228 | if (parents && parents->next) { |
5f1c3f07 JH |
229 | if (opt->ignore_merges) |
230 | return 0; | |
231 | else if (opt->combine_merges) | |
232 | return do_diff_combined(opt, commit); | |
91539833 LT |
233 | |
234 | /* If we show individual diffs, show the parent info */ | |
235 | log->parent = parents->item; | |
5f1c3f07 JH |
236 | } |
237 | ||
91539833 LT |
238 | showed_log = 0; |
239 | for (;;) { | |
5f1c3f07 | 240 | struct commit *parent = parents->item; |
5f1c3f07 | 241 | |
91539833 LT |
242 | diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt); |
243 | log_tree_diff_flush(opt); | |
244 | ||
245 | showed_log |= !opt->loginfo; | |
246 | ||
247 | /* Set up the log info for the next parent, if any.. */ | |
248 | parents = parents->next; | |
249 | if (!parents) | |
250 | break; | |
251 | log->parent = parents->item; | |
252 | opt->loginfo = log; | |
253 | } | |
254 | return showed_log; | |
255 | } | |
256 | ||
257 | int log_tree_commit(struct rev_info *opt, struct commit *commit) | |
258 | { | |
259 | struct log_info log; | |
3eefc189 | 260 | int shown; |
91539833 LT |
261 | |
262 | log.commit = commit; | |
263 | log.parent = NULL; | |
264 | opt->loginfo = &log; | |
265 | ||
3eefc189 JH |
266 | shown = log_tree_diff(opt, commit, &log); |
267 | if (!shown && opt->loginfo && opt->always_show_header) { | |
91539833 LT |
268 | log.parent = NULL; |
269 | show_log(opt, opt->loginfo, ""); | |
3eefc189 | 270 | shown = 1; |
5f1c3f07 | 271 | } |
91539833 | 272 | opt->loginfo = NULL; |
3eefc189 | 273 | return shown; |
5f1c3f07 | 274 | } |