]>
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" | |
6b2f2d98 | 8 | #include "color.h" |
70827b15 LT |
9 | #include "commit.h" |
10 | #include "diff.h" | |
11 | #include "revision.h" | |
12 | #include "log-tree.h" | |
91efcf60 | 13 | #include "builtin.h" |
5d7eeee2 | 14 | #include "tag.h" |
cf39f54e | 15 | #include "reflog-walk.h" |
5d23e133 | 16 | #include "patch-ids.h" |
a5a27c79 | 17 | #include "run-command.h" |
2bda2cf4 | 18 | #include "shortlog.h" |
70827b15 | 19 | |
dd0ffd5b HO |
20 | /* Set a default date-time format for git log ("log.date" config variable) */ |
21 | static const char *default_date_mode = NULL; | |
22 | ||
0f03ca94 | 23 | static int default_show_root = 1; |
d54276f2 | 24 | static const char *fmt_patch_subject_prefix = "PATCH"; |
94c22a5e | 25 | static const char *fmt_pretty; |
0f03ca94 | 26 | |
a633fca0 | 27 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
70827b15 LT |
28 | struct rev_info *rev) |
29 | { | |
52883fbd | 30 | int i; |
ca135e7a | 31 | int decorate = 0; |
52883fbd | 32 | |
70827b15 LT |
33 | rev->abbrev = DEFAULT_ABBREV; |
34 | rev->commit_format = CMIT_FMT_DEFAULT; | |
94c22a5e | 35 | if (fmt_pretty) |
4da45bef | 36 | get_commit_format(fmt_pretty, rev); |
70827b15 | 37 | rev->verbose_header = 1; |
8f67f8ae | 38 | DIFF_OPT_SET(&rev->diffopt, RECURSIVE); |
0f03ca94 | 39 | rev->show_root_diff = default_show_root; |
d54276f2 | 40 | rev->subject_prefix = fmt_patch_subject_prefix; |
dd0ffd5b HO |
41 | |
42 | if (default_date_mode) | |
43 | rev->date_mode = parse_date_format(default_date_mode); | |
44 | ||
70827b15 | 45 | argc = setup_revisions(argc, argv, rev, "HEAD"); |
dd0ffd5b | 46 | |
9dafea26 TH |
47 | if (rev->diffopt.pickaxe || rev->diffopt.filter) |
48 | rev->always_show_header = 0; | |
8f67f8ae | 49 | if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) { |
750f7b66 LT |
50 | rev->always_show_header = 0; |
51 | if (rev->diffopt.nr_paths != 1) | |
52 | usage("git logs can only follow renames on one pathname at a time"); | |
53 | } | |
52883fbd JH |
54 | for (i = 1; i < argc; i++) { |
55 | const char *arg = argv[i]; | |
66688334 | 56 | if (!strcmp(arg, "--decorate")) { |
5e9bf11c | 57 | load_ref_decorations(); |
ca135e7a LT |
58 | decorate = 1; |
59 | } else | |
52883fbd JH |
60 | die("unrecognized argument: %s", arg); |
61 | } | |
9dafea26 TH |
62 | } |
63 | ||
252a7c02 LT |
64 | /* |
65 | * This gives a rough estimate for how many commits we | |
66 | * will print out in the list. | |
67 | */ | |
68 | static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) | |
69 | { | |
70 | int n = 0; | |
71 | ||
72 | while (list) { | |
73 | struct commit *commit = list->item; | |
74 | unsigned int flags = commit->object.flags; | |
252a7c02 | 75 | list = list->next; |
7dc0fe3b | 76 | if (!(flags & (TREESAME | UNINTERESTING))) |
53b2c823 | 77 | n++; |
252a7c02 LT |
78 | } |
79 | return n; | |
80 | } | |
81 | ||
82 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) | |
83 | { | |
84 | if (rev->shown_one) { | |
85 | rev->shown_one = 0; | |
86 | if (rev->commit_format != CMIT_FMT_ONELINE) | |
87 | putchar(rev->diffopt.line_termination); | |
88 | } | |
89 | printf("Final output: %d %s\n", nr, stage); | |
90 | } | |
91 | ||
92 | struct itimerval early_output_timer; | |
93 | ||
cdcefbc9 LT |
94 | static void log_show_early(struct rev_info *revs, struct commit_list *list) |
95 | { | |
96 | int i = revs->early_output; | |
252a7c02 | 97 | int show_header = 1; |
cdcefbc9 LT |
98 | |
99 | sort_in_topological_order(&list, revs->lifo); | |
100 | while (list && i) { | |
101 | struct commit *commit = list->item; | |
252a7c02 LT |
102 | switch (simplify_commit(revs, commit)) { |
103 | case commit_show: | |
104 | if (show_header) { | |
105 | int n = estimate_commit_count(revs, list); | |
106 | show_early_header(revs, "incomplete", n); | |
107 | show_header = 0; | |
108 | } | |
109 | log_tree_commit(revs, commit); | |
110 | i--; | |
111 | break; | |
112 | case commit_ignore: | |
113 | break; | |
114 | case commit_error: | |
115 | return; | |
116 | } | |
cdcefbc9 | 117 | list = list->next; |
cdcefbc9 | 118 | } |
252a7c02 LT |
119 | |
120 | /* Did we already get enough commits for the early output? */ | |
121 | if (!i) | |
122 | return; | |
123 | ||
124 | /* | |
125 | * ..if no, then repeat it twice a second until we | |
126 | * do. | |
127 | * | |
128 | * NOTE! We don't use "it_interval", because if the | |
129 | * reader isn't listening, we want our output to be | |
130 | * throttled by the writing, and not have the timer | |
131 | * trigger every second even if we're blocked on a | |
132 | * reader! | |
133 | */ | |
134 | early_output_timer.it_value.tv_sec = 0; | |
135 | early_output_timer.it_value.tv_usec = 500000; | |
136 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
137 | } |
138 | ||
139 | static void early_output(int signal) | |
140 | { | |
141 | show_early_output = log_show_early; | |
142 | } | |
143 | ||
144 | static void setup_early_output(struct rev_info *rev) | |
145 | { | |
146 | struct sigaction sa; | |
cdcefbc9 LT |
147 | |
148 | /* | |
149 | * Set up the signal handler, minimally intrusively: | |
150 | * we only set a single volatile integer word (not | |
151 | * using sigatomic_t - trying to avoid unnecessary | |
152 | * system dependencies and headers), and using | |
153 | * SA_RESTART. | |
154 | */ | |
155 | memset(&sa, 0, sizeof(sa)); | |
156 | sa.sa_handler = early_output; | |
157 | sigemptyset(&sa.sa_mask); | |
158 | sa.sa_flags = SA_RESTART; | |
159 | sigaction(SIGALRM, &sa, NULL); | |
160 | ||
161 | /* | |
162 | * If we can get the whole output in less than a | |
163 | * tenth of a second, don't even bother doing the | |
164 | * early-output thing.. | |
165 | * | |
166 | * This is a one-time-only trigger. | |
167 | */ | |
252a7c02 LT |
168 | early_output_timer.it_value.tv_sec = 0; |
169 | early_output_timer.it_value.tv_usec = 100000; | |
170 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
171 | } |
172 | ||
173 | static void finish_early_output(struct rev_info *rev) | |
174 | { | |
252a7c02 | 175 | int n = estimate_commit_count(rev, rev->commits); |
cdcefbc9 | 176 | signal(SIGALRM, SIG_IGN); |
252a7c02 | 177 | show_early_header(rev, "done", n); |
cdcefbc9 LT |
178 | } |
179 | ||
9dafea26 TH |
180 | static int cmd_log_walk(struct rev_info *rev) |
181 | { | |
182 | struct commit *commit; | |
70827b15 | 183 | |
cdcefbc9 LT |
184 | if (rev->early_output) |
185 | setup_early_output(rev); | |
186 | ||
3d51e1b5 MK |
187 | if (prepare_revision_walk(rev)) |
188 | die("revision walk setup failed"); | |
cdcefbc9 LT |
189 | |
190 | if (rev->early_output) | |
191 | finish_early_output(rev); | |
192 | ||
036d17fe | 193 | /* |
84102a33 PVM |
194 | * For --check and --exit-code, the exit code is based on CHECK_FAILED |
195 | * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to | |
196 | * retain that state information if replacing rev->diffopt in this loop | |
036d17fe | 197 | */ |
70827b15 LT |
198 | while ((commit = get_revision(rev)) != NULL) { |
199 | log_tree_commit(rev, commit); | |
a6c73064 JS |
200 | if (!rev->reflog_info) { |
201 | /* we allow cycles in reflog ancestry */ | |
202 | free(commit->buffer); | |
203 | commit->buffer = NULL; | |
204 | } | |
cb115748 LT |
205 | free_commit_list(commit->parents); |
206 | commit->parents = NULL; | |
70827b15 | 207 | } |
036d17fe PVM |
208 | if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && |
209 | DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) { | |
210 | return 02; | |
211 | } | |
84102a33 | 212 | return diff_result_code(&rev->diffopt, 0); |
70827b15 LT |
213 | } |
214 | ||
ef90d6d4 | 215 | static int git_log_config(const char *var, const char *value, void *cb) |
0f03ca94 | 216 | { |
94c22a5e CR |
217 | if (!strcmp(var, "format.pretty")) |
218 | return git_config_string(&fmt_pretty, var, value); | |
70cff3ac BH |
219 | if (!strcmp(var, "format.subjectprefix")) |
220 | return git_config_string(&fmt_patch_subject_prefix, var, value); | |
dd0ffd5b HO |
221 | if (!strcmp(var, "log.date")) |
222 | return git_config_string(&default_date_mode, var, value); | |
0f03ca94 PB |
223 | if (!strcmp(var, "log.showroot")) { |
224 | default_show_root = git_config_bool(var, value); | |
225 | return 0; | |
226 | } | |
ef90d6d4 | 227 | return git_diff_ui_config(var, value, cb); |
0f03ca94 PB |
228 | } |
229 | ||
a633fca0 | 230 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
70827b15 LT |
231 | { |
232 | struct rev_info rev; | |
233 | ||
ef90d6d4 | 234 | git_config(git_log_config, NULL); |
6b2f2d98 MK |
235 | |
236 | if (diff_use_color_default == -1) | |
237 | diff_use_color_default = git_use_color_default; | |
238 | ||
db6296a5 | 239 | init_revisions(&rev, prefix); |
70827b15 | 240 | rev.diff = 1; |
9202434c | 241 | rev.simplify_history = 0; |
a633fca0 | 242 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 TH |
243 | if (!rev.diffopt.output_format) |
244 | rev.diffopt.output_format = DIFF_FORMAT_RAW; | |
245 | return cmd_log_walk(&rev); | |
70827b15 LT |
246 | } |
247 | ||
56122ed8 JS |
248 | static void show_tagger(char *buf, int len, struct rev_info *rev) |
249 | { | |
250 | char *email_end, *p; | |
251 | unsigned long date; | |
252 | int tz; | |
253 | ||
254 | email_end = memchr(buf, '>', len); | |
255 | if (!email_end) | |
256 | return; | |
257 | p = ++email_end; | |
258 | while (isspace(*p)) | |
259 | p++; | |
260 | date = strtoul(p, &p, 10); | |
261 | while (isspace(*p)) | |
262 | p++; | |
263 | tz = (int)strtol(p, NULL, 10); | |
264 | printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf, | |
265 | show_date(date, tz, rev->date_mode)); | |
266 | } | |
267 | ||
268 | static int show_object(const unsigned char *sha1, int show_tag_object, | |
269 | struct rev_info *rev) | |
5d7eeee2 JS |
270 | { |
271 | unsigned long size; | |
21666f1a NP |
272 | enum object_type type; |
273 | char *buf = read_sha1_file(sha1, &type, &size); | |
5d7eeee2 JS |
274 | int offset = 0; |
275 | ||
276 | if (!buf) | |
277 | return error("Could not read object %s", sha1_to_hex(sha1)); | |
278 | ||
56122ed8 JS |
279 | if (show_tag_object) |
280 | while (offset < size && buf[offset] != '\n') { | |
281 | int new_offset = offset + 1; | |
5d7eeee2 JS |
282 | while (new_offset < size && buf[new_offset++] != '\n') |
283 | ; /* do nothing */ | |
56122ed8 JS |
284 | if (!prefixcmp(buf + offset, "tagger ")) |
285 | show_tagger(buf + offset + 7, | |
286 | new_offset - offset - 7, rev); | |
5d7eeee2 JS |
287 | offset = new_offset; |
288 | } | |
289 | ||
290 | if (offset < size) | |
291 | fwrite(buf + offset, size - offset, 1, stdout); | |
292 | free(buf); | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static int show_tree_object(const unsigned char *sha1, | |
297 | const char *base, int baselen, | |
671f0707 | 298 | const char *pathname, unsigned mode, int stage, void *context) |
5d7eeee2 JS |
299 | { |
300 | printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); | |
301 | return 0; | |
302 | } | |
303 | ||
a633fca0 | 304 | int cmd_show(int argc, const char **argv, const char *prefix) |
70827b15 LT |
305 | { |
306 | struct rev_info rev; | |
5d7eeee2 JS |
307 | struct object_array_entry *objects; |
308 | int i, count, ret = 0; | |
70827b15 | 309 | |
ef90d6d4 | 310 | git_config(git_log_config, NULL); |
6b2f2d98 MK |
311 | |
312 | if (diff_use_color_default == -1) | |
313 | diff_use_color_default = git_use_color_default; | |
314 | ||
db6296a5 | 315 | init_revisions(&rev, prefix); |
70827b15 | 316 | rev.diff = 1; |
70827b15 LT |
317 | rev.combine_merges = 1; |
318 | rev.dense_combined_merges = 1; | |
319 | rev.always_show_header = 1; | |
320 | rev.ignore_merges = 0; | |
321 | rev.no_walk = 1; | |
a633fca0 | 322 | cmd_log_init(argc, argv, prefix, &rev); |
5d7eeee2 JS |
323 | |
324 | count = rev.pending.nr; | |
325 | objects = rev.pending.objects; | |
326 | for (i = 0; i < count && !ret; i++) { | |
327 | struct object *o = objects[i].item; | |
328 | const char *name = objects[i].name; | |
329 | switch (o->type) { | |
330 | case OBJ_BLOB: | |
56122ed8 | 331 | ret = show_object(o->sha1, 0, NULL); |
5d7eeee2 JS |
332 | break; |
333 | case OBJ_TAG: { | |
334 | struct tag *t = (struct tag *)o; | |
335 | ||
56122ed8 | 336 | printf("%stag %s%s\n", |
8f67f8ae | 337 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 338 | t->tag, |
8f67f8ae | 339 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
56122ed8 | 340 | ret = show_object(o->sha1, 1, &rev); |
4f3dcc27 | 341 | objects[i].item = parse_object(t->tagged->sha1); |
5d7eeee2 JS |
342 | i--; |
343 | break; | |
344 | } | |
345 | case OBJ_TREE: | |
346 | printf("%stree %s%s\n\n", | |
8f67f8ae | 347 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 348 | name, |
8f67f8ae | 349 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
5d7eeee2 | 350 | read_tree_recursive((struct tree *)o, "", 0, 0, NULL, |
671f0707 | 351 | show_tree_object, NULL); |
5d7eeee2 JS |
352 | break; |
353 | case OBJ_COMMIT: | |
354 | rev.pending.nr = rev.pending.alloc = 0; | |
355 | rev.pending.objects = NULL; | |
356 | add_object_array(o, name, &rev.pending); | |
357 | ret = cmd_log_walk(&rev); | |
358 | break; | |
359 | default: | |
360 | ret = error("Unknown type: %d", o->type); | |
361 | } | |
362 | } | |
363 | free(objects); | |
364 | return ret; | |
70827b15 LT |
365 | } |
366 | ||
cf39f54e LT |
367 | /* |
368 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" | |
369 | */ | |
370 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) | |
371 | { | |
372 | struct rev_info rev; | |
373 | ||
ef90d6d4 | 374 | git_config(git_log_config, NULL); |
6b2f2d98 MK |
375 | |
376 | if (diff_use_color_default == -1) | |
377 | diff_use_color_default = git_use_color_default; | |
378 | ||
cf39f54e LT |
379 | init_revisions(&rev, prefix); |
380 | init_reflog_walk(&rev.reflog_info); | |
381 | rev.abbrev_commit = 1; | |
382 | rev.verbose_header = 1; | |
383 | cmd_log_init(argc, argv, prefix, &rev); | |
384 | ||
385 | /* | |
386 | * This means that we override whatever commit format the user gave | |
387 | * on the cmd line. Sad, but cmd_log_init() currently doesn't | |
388 | * allow us to set a different default. | |
389 | */ | |
390 | rev.commit_format = CMIT_FMT_ONELINE; | |
4da45bef | 391 | rev.use_terminator = 1; |
cf39f54e LT |
392 | rev.always_show_header = 1; |
393 | ||
394 | /* | |
395 | * We get called through "git reflog", so unlike the other log | |
396 | * routines, we need to set up our pager manually.. | |
397 | */ | |
398 | setup_pager(); | |
399 | ||
400 | return cmd_log_walk(&rev); | |
401 | } | |
402 | ||
a633fca0 | 403 | int cmd_log(int argc, const char **argv, const char *prefix) |
70827b15 LT |
404 | { |
405 | struct rev_info rev; | |
406 | ||
ef90d6d4 | 407 | git_config(git_log_config, NULL); |
6b2f2d98 MK |
408 | |
409 | if (diff_use_color_default == -1) | |
410 | diff_use_color_default = git_use_color_default; | |
411 | ||
db6296a5 | 412 | init_revisions(&rev, prefix); |
70827b15 | 413 | rev.always_show_header = 1; |
a633fca0 | 414 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 | 415 | return cmd_log_walk(&rev); |
70827b15 | 416 | } |
91efcf60 | 417 | |
c06d2daa RR |
418 | /* format-patch */ |
419 | #define FORMAT_PATCH_NAME_MAX 64 | |
420 | ||
0377db77 JS |
421 | static int istitlechar(char c) |
422 | { | |
423 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
424 | (c >= '0' && c <= '9') || c == '.' || c == '_'; | |
425 | } | |
426 | ||
917a8f89 | 427 | static const char *fmt_patch_suffix = ".patch"; |
49604a4d BG |
428 | static int numbered = 0; |
429 | static int auto_number = 0; | |
20ff0680 | 430 | |
3ee79d9f DB |
431 | static char **extra_hdr; |
432 | static int extra_hdr_nr; | |
433 | static int extra_hdr_alloc; | |
434 | ||
435 | static char **extra_to; | |
436 | static int extra_to_nr; | |
437 | static int extra_to_alloc; | |
438 | ||
439 | static char **extra_cc; | |
440 | static int extra_cc_nr; | |
441 | static int extra_cc_alloc; | |
442 | ||
443 | static void add_header(const char *value) | |
444 | { | |
445 | int len = strlen(value); | |
c8c4450e | 446 | while (len && value[len - 1] == '\n') |
3ee79d9f DB |
447 | len--; |
448 | if (!strncasecmp(value, "to: ", 4)) { | |
449 | ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc); | |
450 | extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4); | |
451 | return; | |
452 | } | |
453 | if (!strncasecmp(value, "cc: ", 4)) { | |
454 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); | |
455 | extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4); | |
456 | return; | |
457 | } | |
458 | ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc); | |
459 | extra_hdr[extra_hdr_nr++] = xstrndup(value, len); | |
460 | } | |
461 | ||
ef90d6d4 | 462 | static int git_format_config(const char *var, const char *value, void *cb) |
20ff0680 JS |
463 | { |
464 | if (!strcmp(var, "format.headers")) { | |
d7fb91c6 JH |
465 | if (!value) |
466 | die("format.headers without value"); | |
3ee79d9f | 467 | add_header(value); |
20ff0680 JS |
468 | return 0; |
469 | } | |
70cff3ac BH |
470 | if (!strcmp(var, "format.suffix")) |
471 | return git_config_string(&fmt_patch_suffix, var, value); | |
fe8928e6 MV |
472 | if (!strcmp(var, "format.cc")) { |
473 | if (!value) | |
474 | return config_error_nonbool(var); | |
475 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); | |
476 | extra_cc[extra_cc_nr++] = xstrdup(value); | |
477 | return 0; | |
478 | } | |
a159ca0c | 479 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
f3aafa4d RA |
480 | return 0; |
481 | } | |
49604a4d | 482 | if (!strcmp(var, "format.numbered")) { |
90f5c186 | 483 | if (value && !strcasecmp(value, "auto")) { |
49604a4d BG |
484 | auto_number = 1; |
485 | return 0; | |
486 | } | |
49604a4d BG |
487 | numbered = git_config_bool(var, value); |
488 | return 0; | |
489 | } | |
dbd21447 | 490 | |
ef90d6d4 | 491 | return git_log_config(var, value, cb); |
20ff0680 JS |
492 | } |
493 | ||
494 | ||
a5a27c79 DB |
495 | static const char *get_oneline_for_filename(struct commit *commit, |
496 | int keep_subject) | |
497 | { | |
498 | static char filename[PATH_MAX]; | |
499 | char *sol; | |
500 | int len = 0; | |
501 | int suffix_len = strlen(fmt_patch_suffix) + 1; | |
502 | ||
503 | sol = strstr(commit->buffer, "\n\n"); | |
504 | if (!sol) | |
505 | filename[0] = '\0'; | |
506 | else { | |
507 | int j, space = 0; | |
508 | ||
509 | sol += 2; | |
510 | /* strip [PATCH] or [PATCH blabla] */ | |
511 | if (!keep_subject && !prefixcmp(sol, "[PATCH")) { | |
512 | char *eos = strchr(sol + 6, ']'); | |
513 | if (eos) { | |
514 | while (isspace(*eos)) | |
515 | eos++; | |
516 | sol = eos; | |
517 | } | |
518 | } | |
519 | ||
520 | for (j = 0; | |
521 | j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 && | |
522 | len < sizeof(filename) - suffix_len && | |
523 | sol[j] && sol[j] != '\n'; | |
524 | j++) { | |
525 | if (istitlechar(sol[j])) { | |
526 | if (space) { | |
527 | filename[len++] = '-'; | |
528 | space = 0; | |
529 | } | |
530 | filename[len++] = sol[j]; | |
531 | if (sol[j] == '.') | |
532 | while (sol[j + 1] == '.') | |
533 | j++; | |
534 | } else | |
535 | space = 1; | |
536 | } | |
537 | while (filename[len - 1] == '.' | |
538 | || filename[len - 1] == '-') | |
539 | len--; | |
540 | filename[len] = '\0'; | |
541 | } | |
542 | return filename; | |
543 | } | |
544 | ||
81f3a188 | 545 | static FILE *realstdout = NULL; |
efd02016 | 546 | static const char *output_directory = NULL; |
81f3a188 | 547 | |
a5a27c79 | 548 | static int reopen_stdout(const char *oneline, int nr, int total) |
0377db77 | 549 | { |
c06d2daa | 550 | char filename[PATH_MAX]; |
2448482b | 551 | int len = 0; |
c06d2daa | 552 | int suffix_len = strlen(fmt_patch_suffix) + 1; |
0377db77 | 553 | |
2448482b | 554 | if (output_directory) { |
a5a27c79 DB |
555 | len = snprintf(filename, sizeof(filename), "%s", |
556 | output_directory); | |
557 | if (len >= | |
c06d2daa RR |
558 | sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len) |
559 | return error("name of output directory is too long"); | |
2448482b JS |
560 | if (filename[len - 1] != '/') |
561 | filename[len++] = '/'; | |
562 | } | |
0377db77 | 563 | |
a5a27c79 DB |
564 | if (!oneline) |
565 | len += sprintf(filename + len, "%d", nr); | |
566 | else { | |
567 | len += sprintf(filename + len, "%04d-", nr); | |
568 | len += snprintf(filename + len, sizeof(filename) - len - 1 | |
569 | - suffix_len, "%s", oneline); | |
e6ff0f42 | 570 | strcpy(filename + len, fmt_patch_suffix); |
0377db77 | 571 | } |
e6ff0f42 | 572 | |
81f3a188 | 573 | fprintf(realstdout, "%s\n", filename); |
c06d2daa RR |
574 | if (freopen(filename, "w", stdout) == NULL) |
575 | return error("Cannot open patch file %s",filename); | |
c06d2daa | 576 | |
e6ff0f42 | 577 | return 0; |
0377db77 JS |
578 | } |
579 | ||
5d23e133 | 580 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix) |
9c6efa36 JS |
581 | { |
582 | struct rev_info check_rev; | |
583 | struct commit *commit; | |
584 | struct object *o1, *o2; | |
585 | unsigned flags1, flags2; | |
9c6efa36 JS |
586 | |
587 | if (rev->pending.nr != 2) | |
588 | die("Need exactly one range."); | |
589 | ||
590 | o1 = rev->pending.objects[0].item; | |
591 | flags1 = o1->flags; | |
592 | o2 = rev->pending.objects[1].item; | |
593 | flags2 = o2->flags; | |
594 | ||
595 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
596 | die("Not a range."); | |
597 | ||
5d23e133 | 598 | init_patch_ids(ids); |
9c6efa36 JS |
599 | |
600 | /* given a range a..b get all patch ids for b..a */ | |
db6296a5 | 601 | init_revisions(&check_rev, prefix); |
9c6efa36 JS |
602 | o1->flags ^= UNINTERESTING; |
603 | o2->flags ^= UNINTERESTING; | |
604 | add_pending_object(&check_rev, o1, "o1"); | |
605 | add_pending_object(&check_rev, o2, "o2"); | |
3d51e1b5 MK |
606 | if (prepare_revision_walk(&check_rev)) |
607 | die("revision walk setup failed"); | |
9c6efa36 JS |
608 | |
609 | while ((commit = get_revision(&check_rev)) != NULL) { | |
610 | /* ignore merges */ | |
611 | if (commit->parents && commit->parents->next) | |
612 | continue; | |
613 | ||
5d23e133 | 614 | add_commit_patch_id(commit, ids); |
9c6efa36 JS |
615 | } |
616 | ||
617 | /* reset for next revision walk */ | |
81db0941 JS |
618 | clear_commit_marks((struct commit *)o1, |
619 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
620 | clear_commit_marks((struct commit *)o2, | |
621 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
9c6efa36 JS |
622 | o1->flags = flags1; |
623 | o2->flags = flags2; | |
624 | } | |
625 | ||
e1a37346 | 626 | static void gen_message_id(struct rev_info *info, char *base) |
d1566f78 | 627 | { |
774751a8 | 628 | const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME); |
d1566f78 JT |
629 | const char *email_start = strrchr(committer, '<'); |
630 | const char *email_end = strrchr(committer, '>'); | |
e1a37346 DB |
631 | struct strbuf buf; |
632 | if (!email_start || !email_end || email_start > email_end - 1) | |
d1566f78 | 633 | die("Could not extract email from committer identity."); |
e1a37346 DB |
634 | strbuf_init(&buf, 0); |
635 | strbuf_addf(&buf, "%s.%lu.git.%.*s", base, | |
636 | (unsigned long) time(NULL), | |
637 | (int)(email_end - email_start - 1), email_start + 1); | |
638 | info->message_id = strbuf_detach(&buf, NULL); | |
d1566f78 JT |
639 | } |
640 | ||
2bda2cf4 DB |
641 | static void make_cover_letter(struct rev_info *rev, int use_stdout, |
642 | int numbered, int numbered_files, | |
643 | struct commit *origin, | |
644 | int nr, struct commit **list, struct commit *head) | |
a5a27c79 DB |
645 | { |
646 | const char *committer; | |
39fe578b | 647 | char *head_sha1; |
a5a27c79 DB |
648 | const char *subject_start = NULL; |
649 | const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; | |
650 | const char *msg; | |
651 | const char *extra_headers = rev->extra_headers; | |
2bda2cf4 | 652 | struct shortlog log; |
a5a27c79 | 653 | struct strbuf sb; |
2bda2cf4 | 654 | int i; |
a5a27c79 | 655 | const char *encoding = "utf-8"; |
39fe578b | 656 | struct diff_options opts; |
267123b4 | 657 | int need_8bit_cte = 0; |
a5a27c79 DB |
658 | |
659 | if (rev->commit_format != CMIT_FMT_EMAIL) | |
660 | die("Cover letter needs email format"); | |
661 | ||
662 | if (!use_stdout && reopen_stdout(numbered_files ? | |
663 | NULL : "cover-letter", 0, rev->total)) | |
664 | return; | |
665 | ||
a5a27c79 DB |
666 | head_sha1 = sha1_to_hex(head->object.sha1); |
667 | ||
267123b4 JH |
668 | log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers, |
669 | &need_8bit_cte); | |
a5a27c79 DB |
670 | |
671 | committer = git_committer_info(0); | |
672 | ||
673 | msg = body; | |
674 | strbuf_init(&sb, 0); | |
675 | pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822, | |
676 | encoding); | |
677 | pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers, | |
267123b4 | 678 | encoding, need_8bit_cte); |
a5a27c79 DB |
679 | pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0); |
680 | printf("%s\n", sb.buf); | |
681 | ||
682 | strbuf_release(&sb); | |
683 | ||
2bda2cf4 | 684 | shortlog_init(&log); |
859c4fbe JS |
685 | log.wrap_lines = 1; |
686 | log.wrap = 72; | |
687 | log.in1 = 2; | |
688 | log.in2 = 4; | |
2bda2cf4 DB |
689 | for (i = 0; i < nr; i++) |
690 | shortlog_add_commit(&log, list[i]); | |
691 | ||
692 | shortlog_output(&log); | |
693 | ||
a5a27c79 | 694 | /* |
2bda2cf4 | 695 | * We can only do diffstat with a unique reference point |
a5a27c79 DB |
696 | */ |
697 | if (!origin) | |
698 | return; | |
699 | ||
5d02294c JS |
700 | memcpy(&opts, &rev->diffopt, sizeof(opts)); |
701 | opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
a5a27c79 | 702 | |
39fe578b DB |
703 | diff_setup_done(&opts); |
704 | ||
705 | diff_tree_sha1(origin->tree->object.sha1, | |
706 | head->tree->object.sha1, | |
707 | "", &opts); | |
708 | diffcore_std(&opts); | |
709 | diff_flush(&opts); | |
a5a27c79 | 710 | |
a5a27c79 | 711 | printf("\n"); |
d1566f78 JT |
712 | } |
713 | ||
8419d2ee JH |
714 | static const char *clean_message_id(const char *msg_id) |
715 | { | |
716 | char ch; | |
717 | const char *a, *z, *m; | |
8419d2ee JH |
718 | |
719 | m = msg_id; | |
720 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) | |
721 | m++; | |
722 | a = m; | |
723 | z = NULL; | |
724 | while ((ch = *m)) { | |
725 | if (!isspace(ch) && (ch != '>')) | |
726 | z = m; | |
727 | m++; | |
728 | } | |
729 | if (!z) | |
730 | die("insane in-reply-to: %s", msg_id); | |
731 | if (++z == m) | |
732 | return a; | |
182af834 | 733 | return xmemdupz(a, z - a); |
8419d2ee JH |
734 | } |
735 | ||
a633fca0 | 736 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
91efcf60 JH |
737 | { |
738 | struct commit *commit; | |
739 | struct commit **list = NULL; | |
740 | struct rev_info rev; | |
2448482b | 741 | int nr = 0, total, i, j; |
0377db77 | 742 | int use_stdout = 0; |
fa0f02df | 743 | int start_number = -1; |
8ac80a57 | 744 | int keep_subject = 0; |
e6ff0f42 | 745 | int numbered_files = 0; /* _just_ numbers */ |
2d9e4a47 | 746 | int subject_prefix = 0; |
9c6efa36 | 747 | int ignore_if_in_upstream = 0; |
cc35de84 | 748 | int thread = 0; |
a5a27c79 | 749 | int cover_letter = 0; |
2bda2cf4 | 750 | int boundary_count = 0; |
37c22a4b | 751 | int no_binary_diff = 0; |
a5a27c79 | 752 | struct commit *origin = NULL, *head = NULL; |
76af0734 | 753 | const char *in_reply_to = NULL; |
5d23e133 | 754 | struct patch_ids ids; |
cf2251b6 | 755 | char *add_signoff = NULL; |
3ee79d9f | 756 | struct strbuf buf; |
91efcf60 | 757 | |
ef90d6d4 | 758 | git_config(git_format_config, NULL); |
db6296a5 | 759 | init_revisions(&rev, prefix); |
91efcf60 JH |
760 | rev.commit_format = CMIT_FMT_EMAIL; |
761 | rev.verbose_header = 1; | |
762 | rev.diff = 1; | |
91efcf60 JH |
763 | rev.combine_merges = 0; |
764 | rev.ignore_merges = 1; | |
8f67f8ae | 765 | DIFF_OPT_SET(&rev.diffopt, RECURSIVE); |
91efcf60 | 766 | |
dbd21447 | 767 | rev.subject_prefix = fmt_patch_subject_prefix; |
20ff0680 | 768 | |
2448482b JS |
769 | /* |
770 | * Parse the arguments before setup_revisions(), or something | |
2dc189a3 | 771 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
2448482b JS |
772 | * possibly a valid SHA1. |
773 | */ | |
774 | for (i = 1, j = 1; i < argc; i++) { | |
775 | if (!strcmp(argv[i], "--stdout")) | |
0377db77 | 776 | use_stdout = 1; |
596524b3 JS |
777 | else if (!strcmp(argv[i], "-n") || |
778 | !strcmp(argv[i], "--numbered")) | |
779 | numbered = 1; | |
49604a4d BG |
780 | else if (!strcmp(argv[i], "-N") || |
781 | !strcmp(argv[i], "--no-numbered")) { | |
782 | numbered = 0; | |
783 | auto_number = 0; | |
784 | } | |
cc44c765 | 785 | else if (!prefixcmp(argv[i], "--start-number=")) |
fa0f02df | 786 | start_number = strtol(argv[i] + 15, NULL, 10); |
e6ff0f42 JL |
787 | else if (!strcmp(argv[i], "--numbered-files")) |
788 | numbered_files = 1; | |
fa0f02df JS |
789 | else if (!strcmp(argv[i], "--start-number")) { |
790 | i++; | |
791 | if (i == argc) | |
792 | die("Need a number for --start-number"); | |
793 | start_number = strtol(argv[i], NULL, 10); | |
cf2251b6 | 794 | } |
736cc67d DB |
795 | else if (!prefixcmp(argv[i], "--cc=")) { |
796 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); | |
797 | extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5); | |
798 | } | |
cf2251b6 | 799 | else if (!strcmp(argv[i], "-k") || |
8ac80a57 JS |
800 | !strcmp(argv[i], "--keep-subject")) { |
801 | keep_subject = 1; | |
802 | rev.total = -1; | |
cf2251b6 | 803 | } |
efd02016 JH |
804 | else if (!strcmp(argv[i], "--output-directory") || |
805 | !strcmp(argv[i], "-o")) { | |
2448482b | 806 | i++; |
efd02016 JH |
807 | if (argc <= i) |
808 | die("Which directory?"); | |
809 | if (output_directory) | |
810 | die("Two output directories?"); | |
811 | output_directory = argv[i]; | |
698ce6f8 | 812 | } |
cf2251b6 JH |
813 | else if (!strcmp(argv[i], "--signoff") || |
814 | !strcmp(argv[i], "-s")) { | |
6c4cca1c EB |
815 | const char *committer; |
816 | const char *endpos; | |
774751a8 | 817 | committer = git_committer_info(IDENT_ERROR_ON_NO_NAME); |
6c4cca1c | 818 | endpos = strchr(committer, '>'); |
cf2251b6 | 819 | if (!endpos) |
597faa00 | 820 | die("bogus committer info %s\n", committer); |
182af834 | 821 | add_signoff = xmemdupz(committer, endpos - committer + 1); |
cf2251b6 | 822 | } |
c112f689 | 823 | else if (!strcmp(argv[i], "--attach")) { |
698ce6f8 | 824 | rev.mime_boundary = git_version_string; |
c112f689 JS |
825 | rev.no_inline = 1; |
826 | } | |
827 | else if (!prefixcmp(argv[i], "--attach=")) { | |
828 | rev.mime_boundary = argv[i] + 9; | |
829 | rev.no_inline = 1; | |
830 | } | |
831 | else if (!strcmp(argv[i], "--inline")) { | |
832 | rev.mime_boundary = git_version_string; | |
833 | rev.no_inline = 0; | |
834 | } | |
835 | else if (!prefixcmp(argv[i], "--inline=")) { | |
698ce6f8 | 836 | rev.mime_boundary = argv[i] + 9; |
c112f689 JS |
837 | rev.no_inline = 0; |
838 | } | |
9c6efa36 JS |
839 | else if (!strcmp(argv[i], "--ignore-if-in-upstream")) |
840 | ignore_if_in_upstream = 1; | |
cc35de84 JT |
841 | else if (!strcmp(argv[i], "--thread")) |
842 | thread = 1; | |
cc44c765 | 843 | else if (!prefixcmp(argv[i], "--in-reply-to=")) |
da56645d JT |
844 | in_reply_to = argv[i] + 14; |
845 | else if (!strcmp(argv[i], "--in-reply-to")) { | |
846 | i++; | |
847 | if (i == argc) | |
848 | die("Need a Message-Id for --in-reply-to"); | |
849 | in_reply_to = argv[i]; | |
2d9e4a47 RJ |
850 | } else if (!prefixcmp(argv[i], "--subject-prefix=")) { |
851 | subject_prefix = 1; | |
852 | rev.subject_prefix = argv[i] + 17; | |
853 | } else if (!prefixcmp(argv[i], "--suffix=")) | |
03eeaeae | 854 | fmt_patch_suffix = argv[i] + 9; |
a5a27c79 DB |
855 | else if (!strcmp(argv[i], "--cover-letter")) |
856 | cover_letter = 1; | |
37c22a4b CMOF |
857 | else if (!strcmp(argv[i], "--no-binary")) |
858 | no_binary_diff = 1; | |
698ce6f8 | 859 | else |
2448482b | 860 | argv[j++] = argv[i]; |
0377db77 | 861 | } |
2448482b JS |
862 | argc = j; |
863 | ||
3ee79d9f DB |
864 | strbuf_init(&buf, 0); |
865 | ||
866 | for (i = 0; i < extra_hdr_nr; i++) { | |
867 | strbuf_addstr(&buf, extra_hdr[i]); | |
868 | strbuf_addch(&buf, '\n'); | |
869 | } | |
870 | ||
871 | if (extra_to_nr) | |
872 | strbuf_addstr(&buf, "To: "); | |
873 | for (i = 0; i < extra_to_nr; i++) { | |
874 | if (i) | |
875 | strbuf_addstr(&buf, " "); | |
876 | strbuf_addstr(&buf, extra_to[i]); | |
877 | if (i + 1 < extra_to_nr) | |
878 | strbuf_addch(&buf, ','); | |
879 | strbuf_addch(&buf, '\n'); | |
880 | } | |
881 | ||
882 | if (extra_cc_nr) | |
883 | strbuf_addstr(&buf, "Cc: "); | |
884 | for (i = 0; i < extra_cc_nr; i++) { | |
885 | if (i) | |
886 | strbuf_addstr(&buf, " "); | |
887 | strbuf_addstr(&buf, extra_cc[i]); | |
888 | if (i + 1 < extra_cc_nr) | |
889 | strbuf_addch(&buf, ','); | |
890 | strbuf_addch(&buf, '\n'); | |
891 | } | |
892 | ||
893 | rev.extra_headers = strbuf_detach(&buf, 0); | |
894 | ||
add5c8a5 | 895 | if (start_number < 0) |
fa0f02df | 896 | start_number = 1; |
63b398a4 | 897 | if (numbered && keep_subject) |
8ac80a57 | 898 | die ("-n and -k are mutually exclusive."); |
2d9e4a47 RJ |
899 | if (keep_subject && subject_prefix) |
900 | die ("--subject-prefix and -k are mutually exclusive."); | |
e6ff0f42 JL |
901 | if (numbered_files && use_stdout) |
902 | die ("--numbered-files and --stdout are mutually exclusive."); | |
8ac80a57 | 903 | |
2448482b JS |
904 | argc = setup_revisions(argc, argv, &rev, "HEAD"); |
905 | if (argc > 1) | |
906 | die ("unrecognized argument: %s", argv[1]); | |
0377db77 | 907 | |
68daa64d JK |
908 | if (!rev.diffopt.output_format |
909 | || rev.diffopt.output_format == DIFF_FORMAT_PATCH) | |
c261e438 | 910 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH; |
c9b5ef99 | 911 | |
37c22a4b | 912 | if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff) |
8f67f8ae | 913 | DIFF_OPT_SET(&rev.diffopt, BINARY); |
e47f306d | 914 | |
90f70a91 | 915 | if (!output_directory && !use_stdout) |
77e565d8 ML |
916 | output_directory = prefix; |
917 | ||
efd02016 JH |
918 | if (output_directory) { |
919 | if (use_stdout) | |
920 | die("standard output, or directory, which one?"); | |
921 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) | |
922 | die("Could not create directory %s", | |
923 | output_directory); | |
924 | } | |
925 | ||
1f1e895f | 926 | if (rev.pending.nr == 1) { |
8a1d076e JH |
927 | if (rev.max_count < 0 && !rev.show_root_diff) { |
928 | /* | |
929 | * This is traditional behaviour of "git format-patch | |
930 | * origin" that prepares what the origin side still | |
931 | * does not have. | |
932 | */ | |
7c496280 | 933 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
3384a2df | 934 | add_head_to_pending(&rev); |
7c496280 | 935 | } |
8a1d076e JH |
936 | /* |
937 | * Otherwise, it is "format-patch -22 HEAD", and/or | |
938 | * "format-patch --root HEAD". The user wants | |
939 | * get_revision() to do the usual traversal. | |
7c496280 | 940 | */ |
e686eb98 | 941 | } |
a5a27c79 DB |
942 | if (cover_letter) { |
943 | /* remember the range */ | |
a5a27c79 DB |
944 | int i; |
945 | for (i = 0; i < rev.pending.nr; i++) { | |
946 | struct object *o = rev.pending.objects[i].item; | |
2bda2cf4 | 947 | if (!(o->flags & UNINTERESTING)) |
a5a27c79 DB |
948 | head = (struct commit *)o; |
949 | } | |
a5a27c79 DB |
950 | /* We can't generate a cover letter without any patches */ |
951 | if (!head) | |
952 | return 0; | |
953 | } | |
e686eb98 | 954 | |
9c6efa36 | 955 | if (ignore_if_in_upstream) |
5d23e133 | 956 | get_patch_ids(&rev, &ids, prefix); |
9c6efa36 | 957 | |
81f3a188 | 958 | if (!use_stdout) |
f5788250 | 959 | realstdout = xfdopen(xdup(1), "w"); |
81f3a188 | 960 | |
3d51e1b5 MK |
961 | if (prepare_revision_walk(&rev)) |
962 | die("revision walk setup failed"); | |
2bda2cf4 | 963 | rev.boundary = 1; |
91efcf60 | 964 | while ((commit = get_revision(&rev)) != NULL) { |
2bda2cf4 | 965 | if (commit->object.flags & BOUNDARY) { |
2bda2cf4 DB |
966 | boundary_count++; |
967 | origin = (boundary_count == 1) ? commit : NULL; | |
968 | continue; | |
969 | } | |
970 | ||
0377db77 JS |
971 | /* ignore merges */ |
972 | if (commit->parents && commit->parents->next) | |
973 | continue; | |
9c6efa36 JS |
974 | |
975 | if (ignore_if_in_upstream && | |
5d23e133 | 976 | has_commit_patch_id(commit, &ids)) |
9c6efa36 JS |
977 | continue; |
978 | ||
91efcf60 | 979 | nr++; |
83572c1a | 980 | list = xrealloc(list, nr * sizeof(list[0])); |
91efcf60 JH |
981 | list[nr - 1] = commit; |
982 | } | |
0377db77 | 983 | total = nr; |
49604a4d BG |
984 | if (!keep_subject && auto_number && total > 1) |
985 | numbered = 1; | |
596524b3 | 986 | if (numbered) |
fa0f02df | 987 | rev.total = total + start_number - 1; |
8419d2ee JH |
988 | if (in_reply_to) |
989 | rev.ref_message_id = clean_message_id(in_reply_to); | |
a5a27c79 DB |
990 | if (cover_letter) { |
991 | if (thread) | |
992 | gen_message_id(&rev, "cover"); | |
993 | make_cover_letter(&rev, use_stdout, numbered, numbered_files, | |
2bda2cf4 | 994 | origin, nr, list, head); |
a5a27c79 DB |
995 | total++; |
996 | start_number--; | |
997 | } | |
998 | rev.add_signoff = add_signoff; | |
91efcf60 JH |
999 | while (0 <= --nr) { |
1000 | int shown; | |
1001 | commit = list[nr]; | |
add5c8a5 | 1002 | rev.nr = total - nr + (start_number - 1); |
d1566f78 | 1003 | /* Make the second and subsequent mails replies to the first */ |
cc35de84 | 1004 | if (thread) { |
a5a27c79 | 1005 | /* Have we already had a message ID? */ |
e1a37346 | 1006 | if (rev.message_id) { |
a5a27c79 DB |
1007 | /* |
1008 | * If we've got the ID to be a reply | |
1009 | * to, discard the current ID; | |
1010 | * otherwise, make everything a reply | |
1011 | * to that. | |
1012 | */ | |
e1a37346 DB |
1013 | if (rev.ref_message_id) |
1014 | free(rev.message_id); | |
1015 | else | |
1016 | rev.ref_message_id = rev.message_id; | |
cc35de84 | 1017 | } |
e1a37346 | 1018 | gen_message_id(&rev, sha1_to_hex(commit->object.sha1)); |
d1566f78 | 1019 | } |
a5a27c79 DB |
1020 | if (!use_stdout && reopen_stdout(numbered_files ? NULL : |
1021 | get_oneline_for_filename(commit, keep_subject), | |
1022 | rev.nr, rev.total)) | |
1023 | die("Failed to create output files"); | |
91efcf60 JH |
1024 | shown = log_tree_commit(&rev, commit); |
1025 | free(commit->buffer); | |
1026 | commit->buffer = NULL; | |
add5c8a5 JH |
1027 | |
1028 | /* We put one extra blank line between formatted | |
1029 | * patches and this flag is used by log-tree code | |
1030 | * to see if it needs to emit a LF before showing | |
1031 | * the log; when using one file per patch, we do | |
1032 | * not want the extra blank line. | |
1033 | */ | |
1034 | if (!use_stdout) | |
1035 | rev.shown_one = 0; | |
698ce6f8 JS |
1036 | if (shown) { |
1037 | if (rev.mime_boundary) | |
1038 | printf("\n--%s%s--\n\n\n", | |
1039 | mime_boundary_leader, | |
1040 | rev.mime_boundary); | |
1041 | else | |
1042 | printf("-- \n%s\n\n", git_version_string); | |
1043 | } | |
0377db77 JS |
1044 | if (!use_stdout) |
1045 | fclose(stdout); | |
91efcf60 JH |
1046 | } |
1047 | free(list); | |
5d23e133 JH |
1048 | if (ignore_if_in_upstream) |
1049 | free_patch_ids(&ids); | |
91efcf60 JH |
1050 | return 0; |
1051 | } | |
1052 | ||
e827633a RS |
1053 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
1054 | { | |
1055 | unsigned char sha1[20]; | |
1056 | if (get_sha1(arg, sha1) == 0) { | |
1057 | struct commit *commit = lookup_commit_reference(sha1); | |
1058 | if (commit) { | |
1059 | commit->object.flags |= flags; | |
1060 | add_pending_object(revs, &commit->object, arg); | |
1061 | return 0; | |
1062 | } | |
1063 | } | |
1064 | return -1; | |
1065 | } | |
1066 | ||
1067 | static const char cherry_usage[] = | |
1b1dd23f | 1068 | "git cherry [-v] <upstream> [<head>] [<limit>]"; |
e827633a RS |
1069 | int cmd_cherry(int argc, const char **argv, const char *prefix) |
1070 | { | |
1071 | struct rev_info revs; | |
5d23e133 | 1072 | struct patch_ids ids; |
e827633a RS |
1073 | struct commit *commit; |
1074 | struct commit_list *list = NULL; | |
1075 | const char *upstream; | |
1076 | const char *head = "HEAD"; | |
1077 | const char *limit = NULL; | |
1078 | int verbose = 0; | |
1079 | ||
1080 | if (argc > 1 && !strcmp(argv[1], "-v")) { | |
1081 | verbose = 1; | |
1082 | argc--; | |
1083 | argv++; | |
1084 | } | |
1085 | ||
1086 | switch (argc) { | |
1087 | case 4: | |
1088 | limit = argv[3]; | |
1089 | /* FALLTHROUGH */ | |
1090 | case 3: | |
1091 | head = argv[2]; | |
1092 | /* FALLTHROUGH */ | |
1093 | case 2: | |
1094 | upstream = argv[1]; | |
1095 | break; | |
1096 | default: | |
1097 | usage(cherry_usage); | |
1098 | } | |
1099 | ||
1100 | init_revisions(&revs, prefix); | |
1101 | revs.diff = 1; | |
1102 | revs.combine_merges = 0; | |
1103 | revs.ignore_merges = 1; | |
8f67f8ae | 1104 | DIFF_OPT_SET(&revs.diffopt, RECURSIVE); |
e827633a RS |
1105 | |
1106 | if (add_pending_commit(head, &revs, 0)) | |
1107 | die("Unknown commit %s", head); | |
1108 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) | |
1109 | die("Unknown commit %s", upstream); | |
1110 | ||
1111 | /* Don't say anything if head and upstream are the same. */ | |
1112 | if (revs.pending.nr == 2) { | |
1113 | struct object_array_entry *o = revs.pending.objects; | |
1114 | if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) | |
1115 | return 0; | |
1116 | } | |
1117 | ||
5d23e133 | 1118 | get_patch_ids(&revs, &ids, prefix); |
e827633a RS |
1119 | |
1120 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
1121 | die("Unknown commit %s", limit); | |
1122 | ||
1123 | /* reverse the list of commits */ | |
3d51e1b5 MK |
1124 | if (prepare_revision_walk(&revs)) |
1125 | die("revision walk setup failed"); | |
e827633a RS |
1126 | while ((commit = get_revision(&revs)) != NULL) { |
1127 | /* ignore merges */ | |
1128 | if (commit->parents && commit->parents->next) | |
1129 | continue; | |
1130 | ||
1131 | commit_list_insert(commit, &list); | |
1132 | } | |
1133 | ||
1134 | while (list) { | |
e827633a RS |
1135 | char sign = '+'; |
1136 | ||
1137 | commit = list->item; | |
5d23e133 | 1138 | if (has_commit_patch_id(commit, &ids)) |
e827633a RS |
1139 | sign = '-'; |
1140 | ||
1141 | if (verbose) { | |
674d1727 PH |
1142 | struct strbuf buf; |
1143 | strbuf_init(&buf, 0); | |
1144 | pretty_print_commit(CMIT_FMT_ONELINE, commit, | |
4593fb84 | 1145 | &buf, 0, NULL, NULL, 0, 0); |
e827633a | 1146 | printf("%c %s %s\n", sign, |
674d1727 PH |
1147 | sha1_to_hex(commit->object.sha1), buf.buf); |
1148 | strbuf_release(&buf); | |
e827633a RS |
1149 | } |
1150 | else { | |
1151 | printf("%c %s\n", sign, | |
1152 | sha1_to_hex(commit->object.sha1)); | |
1153 | } | |
1154 | ||
1155 | list = list->next; | |
1156 | } | |
1157 | ||
5d23e133 | 1158 | free_patch_ids(&ids); |
e827633a RS |
1159 | return 0; |
1160 | } |