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