]>
Commit | Line | Data |
---|---|---|
70827b15 LT |
1 | /* |
2 | * Builtin "git log" and related commands (show, whatchanged) | |
3 | * | |
4 | * (C) Copyright 2006 Linus Torvalds | |
5 | * 2006 Junio Hamano | |
6 | */ | |
7 | #include "cache.h" | |
8 | #include "commit.h" | |
9 | #include "diff.h" | |
10 | #include "revision.h" | |
11 | #include "log-tree.h" | |
91efcf60 | 12 | #include "builtin.h" |
5d7eeee2 | 13 | #include "tag.h" |
70827b15 | 14 | |
0f03ca94 PB |
15 | static int default_show_root = 1; |
16 | ||
e686eb98 JS |
17 | /* this is in builtin-diff.c */ |
18 | void add_head(struct rev_info *revs); | |
19 | ||
a633fca0 | 20 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
70827b15 LT |
21 | struct rev_info *rev) |
22 | { | |
52883fbd JH |
23 | int i; |
24 | ||
70827b15 LT |
25 | rev->abbrev = DEFAULT_ABBREV; |
26 | rev->commit_format = CMIT_FMT_DEFAULT; | |
27 | rev->verbose_header = 1; | |
0f03ca94 | 28 | rev->show_root_diff = default_show_root; |
70827b15 | 29 | argc = setup_revisions(argc, argv, rev, "HEAD"); |
9dafea26 TH |
30 | if (rev->diffopt.pickaxe || rev->diffopt.filter) |
31 | rev->always_show_header = 0; | |
52883fbd JH |
32 | for (i = 1; i < argc; i++) { |
33 | const char *arg = argv[i]; | |
34 | if (!strncmp(arg, "--encoding=", 11)) { | |
35 | arg += 11; | |
52883fbd | 36 | if (strcmp(arg, "none")) |
d2c11a38 | 37 | git_log_output_encoding = strdup(arg); |
52883fbd | 38 | else |
d2c11a38 | 39 | git_log_output_encoding = ""; |
52883fbd JH |
40 | } |
41 | else | |
42 | die("unrecognized argument: %s", arg); | |
43 | } | |
9dafea26 TH |
44 | } |
45 | ||
46 | static int cmd_log_walk(struct rev_info *rev) | |
47 | { | |
48 | struct commit *commit; | |
70827b15 LT |
49 | |
50 | prepare_revision_walk(rev); | |
70827b15 LT |
51 | while ((commit = get_revision(rev)) != NULL) { |
52 | log_tree_commit(rev, commit); | |
53 | free(commit->buffer); | |
54 | commit->buffer = NULL; | |
cb115748 LT |
55 | free_commit_list(commit->parents); |
56 | commit->parents = NULL; | |
70827b15 LT |
57 | } |
58 | return 0; | |
59 | } | |
60 | ||
0f03ca94 PB |
61 | static int git_log_config(const char *var, const char *value) |
62 | { | |
63 | if (!strcmp(var, "log.showroot")) { | |
64 | default_show_root = git_config_bool(var, value); | |
65 | return 0; | |
66 | } | |
67 | return git_diff_ui_config(var, value); | |
68 | } | |
69 | ||
a633fca0 | 70 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
70827b15 LT |
71 | { |
72 | struct rev_info rev; | |
73 | ||
0f03ca94 | 74 | git_config(git_log_config); |
db6296a5 | 75 | init_revisions(&rev, prefix); |
70827b15 LT |
76 | rev.diff = 1; |
77 | rev.diffopt.recursive = 1; | |
9202434c | 78 | rev.simplify_history = 0; |
a633fca0 | 79 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 TH |
80 | if (!rev.diffopt.output_format) |
81 | rev.diffopt.output_format = DIFF_FORMAT_RAW; | |
82 | return cmd_log_walk(&rev); | |
70827b15 LT |
83 | } |
84 | ||
5d7eeee2 JS |
85 | static int show_object(const unsigned char *sha1, int suppress_header) |
86 | { | |
87 | unsigned long size; | |
88 | char type[20]; | |
89 | char *buf = read_sha1_file(sha1, type, &size); | |
90 | int offset = 0; | |
91 | ||
92 | if (!buf) | |
93 | return error("Could not read object %s", sha1_to_hex(sha1)); | |
94 | ||
95 | if (suppress_header) | |
96 | while (offset < size && buf[offset++] != '\n') { | |
97 | int new_offset = offset; | |
98 | while (new_offset < size && buf[new_offset++] != '\n') | |
99 | ; /* do nothing */ | |
100 | offset = new_offset; | |
101 | } | |
102 | ||
103 | if (offset < size) | |
104 | fwrite(buf + offset, size - offset, 1, stdout); | |
105 | free(buf); | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static int show_tree_object(const unsigned char *sha1, | |
110 | const char *base, int baselen, | |
111 | const char *pathname, unsigned mode, int stage) | |
112 | { | |
113 | printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); | |
114 | return 0; | |
115 | } | |
116 | ||
a633fca0 | 117 | int cmd_show(int argc, const char **argv, const char *prefix) |
70827b15 LT |
118 | { |
119 | struct rev_info rev; | |
5d7eeee2 JS |
120 | struct object_array_entry *objects; |
121 | int i, count, ret = 0; | |
70827b15 | 122 | |
0f03ca94 | 123 | git_config(git_log_config); |
db6296a5 | 124 | init_revisions(&rev, prefix); |
70827b15 LT |
125 | rev.diff = 1; |
126 | rev.diffopt.recursive = 1; | |
127 | rev.combine_merges = 1; | |
128 | rev.dense_combined_merges = 1; | |
129 | rev.always_show_header = 1; | |
130 | rev.ignore_merges = 0; | |
131 | rev.no_walk = 1; | |
a633fca0 | 132 | cmd_log_init(argc, argv, prefix, &rev); |
5d7eeee2 JS |
133 | |
134 | count = rev.pending.nr; | |
135 | objects = rev.pending.objects; | |
136 | for (i = 0; i < count && !ret; i++) { | |
137 | struct object *o = objects[i].item; | |
138 | const char *name = objects[i].name; | |
139 | switch (o->type) { | |
140 | case OBJ_BLOB: | |
141 | ret = show_object(o->sha1, 0); | |
142 | break; | |
143 | case OBJ_TAG: { | |
144 | struct tag *t = (struct tag *)o; | |
145 | ||
146 | printf("%stag %s%s\n\n", | |
147 | diff_get_color(rev.diffopt.color_diff, | |
148 | DIFF_COMMIT), | |
149 | t->tag, | |
150 | diff_get_color(rev.diffopt.color_diff, | |
151 | DIFF_RESET)); | |
152 | ret = show_object(o->sha1, 1); | |
153 | objects[i].item = (struct object *)t->tagged; | |
154 | i--; | |
155 | break; | |
156 | } | |
157 | case OBJ_TREE: | |
158 | printf("%stree %s%s\n\n", | |
159 | diff_get_color(rev.diffopt.color_diff, | |
160 | DIFF_COMMIT), | |
161 | name, | |
162 | diff_get_color(rev.diffopt.color_diff, | |
163 | DIFF_RESET)); | |
164 | read_tree_recursive((struct tree *)o, "", 0, 0, NULL, | |
165 | show_tree_object); | |
166 | break; | |
167 | case OBJ_COMMIT: | |
168 | rev.pending.nr = rev.pending.alloc = 0; | |
169 | rev.pending.objects = NULL; | |
170 | add_object_array(o, name, &rev.pending); | |
171 | ret = cmd_log_walk(&rev); | |
172 | break; | |
173 | default: | |
174 | ret = error("Unknown type: %d", o->type); | |
175 | } | |
176 | } | |
177 | free(objects); | |
178 | return ret; | |
70827b15 LT |
179 | } |
180 | ||
a633fca0 | 181 | int cmd_log(int argc, const char **argv, const char *prefix) |
70827b15 LT |
182 | { |
183 | struct rev_info rev; | |
184 | ||
0f03ca94 | 185 | git_config(git_log_config); |
db6296a5 | 186 | init_revisions(&rev, prefix); |
70827b15 | 187 | rev.always_show_header = 1; |
a633fca0 | 188 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 | 189 | return cmd_log_walk(&rev); |
70827b15 | 190 | } |
91efcf60 | 191 | |
0377db77 JS |
192 | static int istitlechar(char c) |
193 | { | |
194 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
195 | (c >= '0' && c <= '9') || c == '.' || c == '_'; | |
196 | } | |
197 | ||
20ff0680 JS |
198 | static char *extra_headers = NULL; |
199 | static int extra_headers_size = 0; | |
917a8f89 | 200 | static const char *fmt_patch_suffix = ".patch"; |
20ff0680 JS |
201 | |
202 | static int git_format_config(const char *var, const char *value) | |
203 | { | |
204 | if (!strcmp(var, "format.headers")) { | |
d7fb91c6 JH |
205 | int len; |
206 | ||
207 | if (!value) | |
208 | die("format.headers without value"); | |
209 | len = strlen(value); | |
20ff0680 | 210 | extra_headers_size += len + 1; |
83572c1a | 211 | extra_headers = xrealloc(extra_headers, extra_headers_size); |
20ff0680 JS |
212 | extra_headers[extra_headers_size - len - 1] = 0; |
213 | strcat(extra_headers, value); | |
214 | return 0; | |
215 | } | |
03eeaeae JH |
216 | if (!strcmp(var, "format.suffix")) { |
217 | if (!value) | |
218 | die("format.suffix without value"); | |
219 | fmt_patch_suffix = xstrdup(value); | |
220 | return 0; | |
221 | } | |
a159ca0c | 222 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
f3aafa4d RA |
223 | return 0; |
224 | } | |
0f03ca94 | 225 | return git_log_config(var, value); |
20ff0680 JS |
226 | } |
227 | ||
228 | ||
81f3a188 | 229 | static FILE *realstdout = NULL; |
efd02016 | 230 | static const char *output_directory = NULL; |
81f3a188 | 231 | |
8ac80a57 | 232 | static void reopen_stdout(struct commit *commit, int nr, int keep_subject) |
0377db77 JS |
233 | { |
234 | char filename[1024]; | |
235 | char *sol; | |
2448482b | 236 | int len = 0; |
03eeaeae | 237 | int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */ |
0377db77 | 238 | |
2448482b | 239 | if (output_directory) { |
03eeaeae | 240 | strlcpy(filename, output_directory, 1000); |
2448482b JS |
241 | len = strlen(filename); |
242 | if (filename[len - 1] != '/') | |
243 | filename[len++] = '/'; | |
244 | } | |
0377db77 | 245 | |
2448482b | 246 | sprintf(filename + len, "%04d", nr); |
0377db77 JS |
247 | len = strlen(filename); |
248 | ||
249 | sol = strstr(commit->buffer, "\n\n"); | |
250 | if (sol) { | |
251 | int j, space = 1; | |
252 | ||
253 | sol += 2; | |
254 | /* strip [PATCH] or [PATCH blabla] */ | |
8ac80a57 | 255 | if (!keep_subject && !strncmp(sol, "[PATCH", 6)) { |
0377db77 JS |
256 | char *eos = strchr(sol + 6, ']'); |
257 | if (eos) { | |
258 | while (isspace(*eos)) | |
259 | eos++; | |
260 | sol = eos; | |
261 | } | |
262 | } | |
263 | ||
03eeaeae JH |
264 | for (j = 0; |
265 | len < sizeof(filename) - suffix_len && | |
266 | sol[j] && sol[j] != '\n'; | |
267 | j++) { | |
0377db77 JS |
268 | if (istitlechar(sol[j])) { |
269 | if (space) { | |
270 | filename[len++] = '-'; | |
271 | space = 0; | |
272 | } | |
273 | filename[len++] = sol[j]; | |
274 | if (sol[j] == '.') | |
275 | while (sol[j + 1] == '.') | |
276 | j++; | |
277 | } else | |
278 | space = 1; | |
279 | } | |
280 | while (filename[len - 1] == '.' || filename[len - 1] == '-') | |
281 | len--; | |
282 | } | |
03eeaeae | 283 | strcpy(filename + len, fmt_patch_suffix); |
81f3a188 | 284 | fprintf(realstdout, "%s\n", filename); |
0377db77 JS |
285 | freopen(filename, "w", stdout); |
286 | } | |
287 | ||
9c6efa36 JS |
288 | static int get_patch_id(struct commit *commit, struct diff_options *options, |
289 | unsigned char *sha1) | |
290 | { | |
2b60356d RS |
291 | if (commit->parents) |
292 | diff_tree_sha1(commit->parents->item->object.sha1, | |
293 | commit->object.sha1, "", options); | |
294 | else | |
295 | diff_root_tree_sha1(commit->object.sha1, "", options); | |
9c6efa36 JS |
296 | diffcore_std(options); |
297 | return diff_flush_patch_id(options, sha1); | |
298 | } | |
299 | ||
a633fca0 | 300 | static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix) |
9c6efa36 JS |
301 | { |
302 | struct rev_info check_rev; | |
303 | struct commit *commit; | |
304 | struct object *o1, *o2; | |
305 | unsigned flags1, flags2; | |
306 | unsigned char sha1[20]; | |
307 | ||
308 | if (rev->pending.nr != 2) | |
309 | die("Need exactly one range."); | |
310 | ||
311 | o1 = rev->pending.objects[0].item; | |
312 | flags1 = o1->flags; | |
313 | o2 = rev->pending.objects[1].item; | |
314 | flags2 = o2->flags; | |
315 | ||
316 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
317 | die("Not a range."); | |
318 | ||
319 | diff_setup(options); | |
320 | options->recursive = 1; | |
321 | if (diff_setup_done(options) < 0) | |
322 | die("diff_setup_done failed"); | |
323 | ||
324 | /* given a range a..b get all patch ids for b..a */ | |
db6296a5 | 325 | init_revisions(&check_rev, prefix); |
9c6efa36 JS |
326 | o1->flags ^= UNINTERESTING; |
327 | o2->flags ^= UNINTERESTING; | |
328 | add_pending_object(&check_rev, o1, "o1"); | |
329 | add_pending_object(&check_rev, o2, "o2"); | |
330 | prepare_revision_walk(&check_rev); | |
331 | ||
332 | while ((commit = get_revision(&check_rev)) != NULL) { | |
333 | /* ignore merges */ | |
334 | if (commit->parents && commit->parents->next) | |
335 | continue; | |
336 | ||
337 | if (!get_patch_id(commit, options, sha1)) | |
338 | created_object(sha1, xcalloc(1, sizeof(struct object))); | |
339 | } | |
340 | ||
341 | /* reset for next revision walk */ | |
81db0941 JS |
342 | clear_commit_marks((struct commit *)o1, |
343 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
344 | clear_commit_marks((struct commit *)o2, | |
345 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
9c6efa36 JS |
346 | o1->flags = flags1; |
347 | o2->flags = flags2; | |
348 | } | |
349 | ||
d1566f78 JT |
350 | static void gen_message_id(char *dest, unsigned int length, char *base) |
351 | { | |
352 | const char *committer = git_committer_info(1); | |
353 | const char *email_start = strrchr(committer, '<'); | |
354 | const char *email_end = strrchr(committer, '>'); | |
355 | if(!email_start || !email_end || email_start > email_end - 1) | |
356 | die("Could not extract email from committer identity."); | |
76af0734 JH |
357 | snprintf(dest, length, "%s.%lu.git.%.*s", base, |
358 | (unsigned long) time(NULL), | |
359 | (int)(email_end - email_start - 1), email_start + 1); | |
d1566f78 JT |
360 | } |
361 | ||
a633fca0 | 362 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
91efcf60 JH |
363 | { |
364 | struct commit *commit; | |
365 | struct commit **list = NULL; | |
366 | struct rev_info rev; | |
2448482b | 367 | int nr = 0, total, i, j; |
0377db77 | 368 | int use_stdout = 0; |
596524b3 | 369 | int numbered = 0; |
fa0f02df | 370 | int start_number = -1; |
8ac80a57 | 371 | int keep_subject = 0; |
9c6efa36 | 372 | int ignore_if_in_upstream = 0; |
cc35de84 | 373 | int thread = 0; |
76af0734 | 374 | const char *in_reply_to = NULL; |
9c6efa36 | 375 | struct diff_options patch_id_opts; |
cf2251b6 | 376 | char *add_signoff = NULL; |
d1566f78 JT |
377 | char message_id[1024]; |
378 | char ref_message_id[1024]; | |
91efcf60 | 379 | |
07efc6a6 | 380 | setup_ident(); |
97beb812 | 381 | git_config(git_format_config); |
db6296a5 | 382 | init_revisions(&rev, prefix); |
91efcf60 JH |
383 | rev.commit_format = CMIT_FMT_EMAIL; |
384 | rev.verbose_header = 1; | |
385 | rev.diff = 1; | |
91efcf60 JH |
386 | rev.combine_merges = 0; |
387 | rev.ignore_merges = 1; | |
27e1b127 JH |
388 | rev.diffopt.msg_sep = ""; |
389 | rev.diffopt.recursive = 1; | |
91efcf60 | 390 | |
20ff0680 JS |
391 | rev.extra_headers = extra_headers; |
392 | ||
2448482b JS |
393 | /* |
394 | * Parse the arguments before setup_revisions(), or something | |
395 | * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is | |
396 | * possibly a valid SHA1. | |
397 | */ | |
398 | for (i = 1, j = 1; i < argc; i++) { | |
399 | if (!strcmp(argv[i], "--stdout")) | |
0377db77 | 400 | use_stdout = 1; |
596524b3 JS |
401 | else if (!strcmp(argv[i], "-n") || |
402 | !strcmp(argv[i], "--numbered")) | |
403 | numbered = 1; | |
fa0f02df JS |
404 | else if (!strncmp(argv[i], "--start-number=", 15)) |
405 | start_number = strtol(argv[i] + 15, NULL, 10); | |
406 | else if (!strcmp(argv[i], "--start-number")) { | |
407 | i++; | |
408 | if (i == argc) | |
409 | die("Need a number for --start-number"); | |
410 | start_number = strtol(argv[i], NULL, 10); | |
cf2251b6 JH |
411 | } |
412 | else if (!strcmp(argv[i], "-k") || | |
8ac80a57 JS |
413 | !strcmp(argv[i], "--keep-subject")) { |
414 | keep_subject = 1; | |
415 | rev.total = -1; | |
cf2251b6 | 416 | } |
efd02016 JH |
417 | else if (!strcmp(argv[i], "--output-directory") || |
418 | !strcmp(argv[i], "-o")) { | |
2448482b | 419 | i++; |
efd02016 JH |
420 | if (argc <= i) |
421 | die("Which directory?"); | |
422 | if (output_directory) | |
423 | die("Two output directories?"); | |
424 | output_directory = argv[i]; | |
698ce6f8 | 425 | } |
cf2251b6 JH |
426 | else if (!strcmp(argv[i], "--signoff") || |
427 | !strcmp(argv[i], "-s")) { | |
6c4cca1c EB |
428 | const char *committer; |
429 | const char *endpos; | |
6c4cca1c EB |
430 | committer = git_committer_info(1); |
431 | endpos = strchr(committer, '>'); | |
cf2251b6 JH |
432 | if (!endpos) |
433 | die("bogos committer info %s\n", committer); | |
434 | add_signoff = xmalloc(endpos - committer + 2); | |
435 | memcpy(add_signoff, committer, endpos - committer + 1); | |
436 | add_signoff[endpos - committer + 1] = 0; | |
437 | } | |
698ce6f8 JS |
438 | else if (!strcmp(argv[i], "--attach")) |
439 | rev.mime_boundary = git_version_string; | |
440 | else if (!strncmp(argv[i], "--attach=", 9)) | |
441 | rev.mime_boundary = argv[i] + 9; | |
9c6efa36 JS |
442 | else if (!strcmp(argv[i], "--ignore-if-in-upstream")) |
443 | ignore_if_in_upstream = 1; | |
cc35de84 JT |
444 | else if (!strcmp(argv[i], "--thread")) |
445 | thread = 1; | |
da56645d JT |
446 | else if (!strncmp(argv[i], "--in-reply-to=", 14)) |
447 | in_reply_to = argv[i] + 14; | |
448 | else if (!strcmp(argv[i], "--in-reply-to")) { | |
449 | i++; | |
450 | if (i == argc) | |
451 | die("Need a Message-Id for --in-reply-to"); | |
452 | in_reply_to = argv[i]; | |
453 | } | |
03eeaeae JH |
454 | else if (!strncmp(argv[i], "--suffix=", 9)) |
455 | fmt_patch_suffix = argv[i] + 9; | |
698ce6f8 | 456 | else |
2448482b | 457 | argv[j++] = argv[i]; |
0377db77 | 458 | } |
2448482b JS |
459 | argc = j; |
460 | ||
add5c8a5 | 461 | if (start_number < 0) |
fa0f02df | 462 | start_number = 1; |
63b398a4 | 463 | if (numbered && keep_subject) |
8ac80a57 JS |
464 | die ("-n and -k are mutually exclusive."); |
465 | ||
2448482b JS |
466 | argc = setup_revisions(argc, argv, &rev, "HEAD"); |
467 | if (argc > 1) | |
468 | die ("unrecognized argument: %s", argv[1]); | |
0377db77 | 469 | |
c9b5ef99 | 470 | if (!rev.diffopt.output_format) |
c261e438 | 471 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH; |
c9b5ef99 | 472 | |
e47f306d JH |
473 | if (!rev.diffopt.text) |
474 | rev.diffopt.binary = 1; | |
475 | ||
77e565d8 ML |
476 | if (!output_directory) |
477 | output_directory = prefix; | |
478 | ||
efd02016 JH |
479 | if (output_directory) { |
480 | if (use_stdout) | |
481 | die("standard output, or directory, which one?"); | |
482 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) | |
483 | die("Could not create directory %s", | |
484 | output_directory); | |
485 | } | |
486 | ||
1f1e895f | 487 | if (rev.pending.nr == 1) { |
7c496280 JH |
488 | if (rev.max_count < 0) { |
489 | rev.pending.objects[0].item->flags |= UNINTERESTING; | |
490 | add_head(&rev); | |
491 | } | |
492 | /* Otherwise, it is "format-patch -22 HEAD", and | |
493 | * get_revision() would return only the specified count. | |
494 | */ | |
e686eb98 JS |
495 | } |
496 | ||
9c6efa36 | 497 | if (ignore_if_in_upstream) |
a633fca0 | 498 | get_patch_ids(&rev, &patch_id_opts, prefix); |
9c6efa36 | 499 | |
81f3a188 JS |
500 | if (!use_stdout) |
501 | realstdout = fdopen(dup(1), "w"); | |
502 | ||
91efcf60 JH |
503 | prepare_revision_walk(&rev); |
504 | while ((commit = get_revision(&rev)) != NULL) { | |
9c6efa36 JS |
505 | unsigned char sha1[20]; |
506 | ||
0377db77 JS |
507 | /* ignore merges */ |
508 | if (commit->parents && commit->parents->next) | |
509 | continue; | |
9c6efa36 JS |
510 | |
511 | if (ignore_if_in_upstream && | |
512 | !get_patch_id(commit, &patch_id_opts, sha1) && | |
513 | lookup_object(sha1)) | |
514 | continue; | |
515 | ||
91efcf60 | 516 | nr++; |
83572c1a | 517 | list = xrealloc(list, nr * sizeof(list[0])); |
91efcf60 JH |
518 | list[nr - 1] = commit; |
519 | } | |
0377db77 | 520 | total = nr; |
596524b3 | 521 | if (numbered) |
fa0f02df | 522 | rev.total = total + start_number - 1; |
cf2251b6 | 523 | rev.add_signoff = add_signoff; |
da56645d | 524 | rev.ref_message_id = in_reply_to; |
91efcf60 JH |
525 | while (0 <= --nr) { |
526 | int shown; | |
527 | commit = list[nr]; | |
add5c8a5 | 528 | rev.nr = total - nr + (start_number - 1); |
d1566f78 | 529 | /* Make the second and subsequent mails replies to the first */ |
cc35de84 JT |
530 | if (thread) { |
531 | if (nr == (total - 2)) { | |
532 | strncpy(ref_message_id, message_id, | |
533 | sizeof(ref_message_id)); | |
534 | ref_message_id[sizeof(ref_message_id)-1]='\0'; | |
535 | rev.ref_message_id = ref_message_id; | |
536 | } | |
537 | gen_message_id(message_id, sizeof(message_id), | |
538 | sha1_to_hex(commit->object.sha1)); | |
539 | rev.message_id = message_id; | |
d1566f78 | 540 | } |
0377db77 | 541 | if (!use_stdout) |
8ac80a57 | 542 | reopen_stdout(commit, rev.nr, keep_subject); |
91efcf60 JH |
543 | shown = log_tree_commit(&rev, commit); |
544 | free(commit->buffer); | |
545 | commit->buffer = NULL; | |
add5c8a5 JH |
546 | |
547 | /* We put one extra blank line between formatted | |
548 | * patches and this flag is used by log-tree code | |
549 | * to see if it needs to emit a LF before showing | |
550 | * the log; when using one file per patch, we do | |
551 | * not want the extra blank line. | |
552 | */ | |
553 | if (!use_stdout) | |
554 | rev.shown_one = 0; | |
698ce6f8 JS |
555 | if (shown) { |
556 | if (rev.mime_boundary) | |
557 | printf("\n--%s%s--\n\n\n", | |
558 | mime_boundary_leader, | |
559 | rev.mime_boundary); | |
560 | else | |
561 | printf("-- \n%s\n\n", git_version_string); | |
562 | } | |
0377db77 JS |
563 | if (!use_stdout) |
564 | fclose(stdout); | |
91efcf60 JH |
565 | } |
566 | free(list); | |
567 | return 0; | |
568 | } | |
569 | ||
e827633a RS |
570 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
571 | { | |
572 | unsigned char sha1[20]; | |
573 | if (get_sha1(arg, sha1) == 0) { | |
574 | struct commit *commit = lookup_commit_reference(sha1); | |
575 | if (commit) { | |
576 | commit->object.flags |= flags; | |
577 | add_pending_object(revs, &commit->object, arg); | |
578 | return 0; | |
579 | } | |
580 | } | |
581 | return -1; | |
582 | } | |
583 | ||
584 | static const char cherry_usage[] = | |
585 | "git-cherry [-v] <upstream> [<head>] [<limit>]"; | |
586 | int cmd_cherry(int argc, const char **argv, const char *prefix) | |
587 | { | |
588 | struct rev_info revs; | |
589 | struct diff_options patch_id_opts; | |
590 | struct commit *commit; | |
591 | struct commit_list *list = NULL; | |
592 | const char *upstream; | |
593 | const char *head = "HEAD"; | |
594 | const char *limit = NULL; | |
595 | int verbose = 0; | |
596 | ||
597 | if (argc > 1 && !strcmp(argv[1], "-v")) { | |
598 | verbose = 1; | |
599 | argc--; | |
600 | argv++; | |
601 | } | |
602 | ||
603 | switch (argc) { | |
604 | case 4: | |
605 | limit = argv[3]; | |
606 | /* FALLTHROUGH */ | |
607 | case 3: | |
608 | head = argv[2]; | |
609 | /* FALLTHROUGH */ | |
610 | case 2: | |
611 | upstream = argv[1]; | |
612 | break; | |
613 | default: | |
614 | usage(cherry_usage); | |
615 | } | |
616 | ||
617 | init_revisions(&revs, prefix); | |
618 | revs.diff = 1; | |
619 | revs.combine_merges = 0; | |
620 | revs.ignore_merges = 1; | |
621 | revs.diffopt.recursive = 1; | |
622 | ||
623 | if (add_pending_commit(head, &revs, 0)) | |
624 | die("Unknown commit %s", head); | |
625 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) | |
626 | die("Unknown commit %s", upstream); | |
627 | ||
628 | /* Don't say anything if head and upstream are the same. */ | |
629 | if (revs.pending.nr == 2) { | |
630 | struct object_array_entry *o = revs.pending.objects; | |
631 | if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) | |
632 | return 0; | |
633 | } | |
634 | ||
635 | get_patch_ids(&revs, &patch_id_opts, prefix); | |
636 | ||
637 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
638 | die("Unknown commit %s", limit); | |
639 | ||
640 | /* reverse the list of commits */ | |
641 | prepare_revision_walk(&revs); | |
642 | while ((commit = get_revision(&revs)) != NULL) { | |
643 | /* ignore merges */ | |
644 | if (commit->parents && commit->parents->next) | |
645 | continue; | |
646 | ||
647 | commit_list_insert(commit, &list); | |
648 | } | |
649 | ||
650 | while (list) { | |
651 | unsigned char sha1[20]; | |
652 | char sign = '+'; | |
653 | ||
654 | commit = list->item; | |
655 | if (!get_patch_id(commit, &patch_id_opts, sha1) && | |
656 | lookup_object(sha1)) | |
657 | sign = '-'; | |
658 | ||
659 | if (verbose) { | |
660 | static char buf[16384]; | |
661 | pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, | |
662 | buf, sizeof(buf), 0, NULL, NULL, 0); | |
663 | printf("%c %s %s\n", sign, | |
664 | sha1_to_hex(commit->object.sha1), buf); | |
665 | } | |
666 | else { | |
667 | printf("%c %s\n", sign, | |
668 | sha1_to_hex(commit->object.sha1)); | |
669 | } | |
670 | ||
671 | list = list->next; | |
672 | } | |
673 | ||
674 | return 0; | |
675 | } |