]>
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" |
cf39f54e | 14 | #include "reflog-walk.h" |
5d23e133 | 15 | #include "patch-ids.h" |
ca135e7a | 16 | #include "refs.h" |
70827b15 | 17 | |
0f03ca94 | 18 | static int default_show_root = 1; |
d54276f2 | 19 | static const char *fmt_patch_subject_prefix = "PATCH"; |
0f03ca94 | 20 | |
ca135e7a LT |
21 | static void add_name_decoration(const char *prefix, const char *name, struct object *obj) |
22 | { | |
23 | int plen = strlen(prefix); | |
24 | int nlen = strlen(name); | |
25 | struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen); | |
26 | memcpy(res->name, prefix, plen); | |
27 | memcpy(res->name + plen, name, nlen + 1); | |
28 | res->next = add_decoration(&name_decoration, obj, res); | |
29 | } | |
30 | ||
31 | static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data) | |
32 | { | |
33 | struct object *obj = parse_object(sha1); | |
34 | if (!obj) | |
35 | return 0; | |
36 | add_name_decoration("", refname, obj); | |
37 | while (obj->type == OBJ_TAG) { | |
38 | obj = ((struct tag *)obj)->tagged; | |
39 | if (!obj) | |
40 | break; | |
41 | add_name_decoration("tag: ", refname, obj); | |
42 | } | |
43 | return 0; | |
44 | } | |
45 | ||
a633fca0 | 46 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
70827b15 LT |
47 | struct rev_info *rev) |
48 | { | |
52883fbd | 49 | int i; |
ca135e7a | 50 | int decorate = 0; |
52883fbd | 51 | |
70827b15 LT |
52 | rev->abbrev = DEFAULT_ABBREV; |
53 | rev->commit_format = CMIT_FMT_DEFAULT; | |
54 | rev->verbose_header = 1; | |
8f67f8ae | 55 | DIFF_OPT_SET(&rev->diffopt, RECURSIVE); |
0f03ca94 | 56 | rev->show_root_diff = default_show_root; |
d54276f2 | 57 | rev->subject_prefix = fmt_patch_subject_prefix; |
70827b15 | 58 | argc = setup_revisions(argc, argv, rev, "HEAD"); |
9dafea26 TH |
59 | if (rev->diffopt.pickaxe || rev->diffopt.filter) |
60 | rev->always_show_header = 0; | |
8f67f8ae | 61 | if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) { |
750f7b66 LT |
62 | rev->always_show_header = 0; |
63 | if (rev->diffopt.nr_paths != 1) | |
64 | usage("git logs can only follow renames on one pathname at a time"); | |
65 | } | |
52883fbd JH |
66 | for (i = 1; i < argc; i++) { |
67 | const char *arg = argv[i]; | |
66688334 | 68 | if (!strcmp(arg, "--decorate")) { |
ca135e7a LT |
69 | if (!decorate) |
70 | for_each_ref(add_ref_decoration, NULL); | |
71 | decorate = 1; | |
72 | } else | |
52883fbd JH |
73 | die("unrecognized argument: %s", arg); |
74 | } | |
9dafea26 TH |
75 | } |
76 | ||
252a7c02 LT |
77 | /* |
78 | * This gives a rough estimate for how many commits we | |
79 | * will print out in the list. | |
80 | */ | |
81 | static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) | |
82 | { | |
83 | int n = 0; | |
84 | ||
85 | while (list) { | |
86 | struct commit *commit = list->item; | |
87 | unsigned int flags = commit->object.flags; | |
252a7c02 | 88 | list = list->next; |
7dc0fe3b | 89 | if (!(flags & (TREESAME | UNINTERESTING))) |
53b2c823 | 90 | n++; |
252a7c02 LT |
91 | } |
92 | return n; | |
93 | } | |
94 | ||
95 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) | |
96 | { | |
97 | if (rev->shown_one) { | |
98 | rev->shown_one = 0; | |
99 | if (rev->commit_format != CMIT_FMT_ONELINE) | |
100 | putchar(rev->diffopt.line_termination); | |
101 | } | |
102 | printf("Final output: %d %s\n", nr, stage); | |
103 | } | |
104 | ||
105 | struct itimerval early_output_timer; | |
106 | ||
cdcefbc9 LT |
107 | static void log_show_early(struct rev_info *revs, struct commit_list *list) |
108 | { | |
109 | int i = revs->early_output; | |
252a7c02 | 110 | int show_header = 1; |
cdcefbc9 LT |
111 | |
112 | sort_in_topological_order(&list, revs->lifo); | |
113 | while (list && i) { | |
114 | struct commit *commit = list->item; | |
252a7c02 LT |
115 | switch (simplify_commit(revs, commit)) { |
116 | case commit_show: | |
117 | if (show_header) { | |
118 | int n = estimate_commit_count(revs, list); | |
119 | show_early_header(revs, "incomplete", n); | |
120 | show_header = 0; | |
121 | } | |
122 | log_tree_commit(revs, commit); | |
123 | i--; | |
124 | break; | |
125 | case commit_ignore: | |
126 | break; | |
127 | case commit_error: | |
128 | return; | |
129 | } | |
cdcefbc9 | 130 | list = list->next; |
cdcefbc9 | 131 | } |
252a7c02 LT |
132 | |
133 | /* Did we already get enough commits for the early output? */ | |
134 | if (!i) | |
135 | return; | |
136 | ||
137 | /* | |
138 | * ..if no, then repeat it twice a second until we | |
139 | * do. | |
140 | * | |
141 | * NOTE! We don't use "it_interval", because if the | |
142 | * reader isn't listening, we want our output to be | |
143 | * throttled by the writing, and not have the timer | |
144 | * trigger every second even if we're blocked on a | |
145 | * reader! | |
146 | */ | |
147 | early_output_timer.it_value.tv_sec = 0; | |
148 | early_output_timer.it_value.tv_usec = 500000; | |
149 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
150 | } |
151 | ||
152 | static void early_output(int signal) | |
153 | { | |
154 | show_early_output = log_show_early; | |
155 | } | |
156 | ||
157 | static void setup_early_output(struct rev_info *rev) | |
158 | { | |
159 | struct sigaction sa; | |
cdcefbc9 LT |
160 | |
161 | /* | |
162 | * Set up the signal handler, minimally intrusively: | |
163 | * we only set a single volatile integer word (not | |
164 | * using sigatomic_t - trying to avoid unnecessary | |
165 | * system dependencies and headers), and using | |
166 | * SA_RESTART. | |
167 | */ | |
168 | memset(&sa, 0, sizeof(sa)); | |
169 | sa.sa_handler = early_output; | |
170 | sigemptyset(&sa.sa_mask); | |
171 | sa.sa_flags = SA_RESTART; | |
172 | sigaction(SIGALRM, &sa, NULL); | |
173 | ||
174 | /* | |
175 | * If we can get the whole output in less than a | |
176 | * tenth of a second, don't even bother doing the | |
177 | * early-output thing.. | |
178 | * | |
179 | * This is a one-time-only trigger. | |
180 | */ | |
252a7c02 LT |
181 | early_output_timer.it_value.tv_sec = 0; |
182 | early_output_timer.it_value.tv_usec = 100000; | |
183 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
184 | } |
185 | ||
186 | static void finish_early_output(struct rev_info *rev) | |
187 | { | |
252a7c02 | 188 | int n = estimate_commit_count(rev, rev->commits); |
cdcefbc9 | 189 | signal(SIGALRM, SIG_IGN); |
252a7c02 | 190 | show_early_header(rev, "done", n); |
cdcefbc9 LT |
191 | } |
192 | ||
9dafea26 TH |
193 | static int cmd_log_walk(struct rev_info *rev) |
194 | { | |
195 | struct commit *commit; | |
70827b15 | 196 | |
cdcefbc9 LT |
197 | if (rev->early_output) |
198 | setup_early_output(rev); | |
199 | ||
3d51e1b5 MK |
200 | if (prepare_revision_walk(rev)) |
201 | die("revision walk setup failed"); | |
cdcefbc9 LT |
202 | |
203 | if (rev->early_output) | |
204 | finish_early_output(rev); | |
205 | ||
70827b15 LT |
206 | while ((commit = get_revision(rev)) != NULL) { |
207 | log_tree_commit(rev, commit); | |
a6c73064 JS |
208 | if (!rev->reflog_info) { |
209 | /* we allow cycles in reflog ancestry */ | |
210 | free(commit->buffer); | |
211 | commit->buffer = NULL; | |
212 | } | |
cb115748 LT |
213 | free_commit_list(commit->parents); |
214 | commit->parents = NULL; | |
70827b15 LT |
215 | } |
216 | return 0; | |
217 | } | |
218 | ||
0f03ca94 PB |
219 | static int git_log_config(const char *var, const char *value) |
220 | { | |
d54276f2 AR |
221 | if (!strcmp(var, "format.subjectprefix")) { |
222 | if (!value) | |
995c4527 | 223 | config_error_nonbool(var); |
d54276f2 AR |
224 | fmt_patch_subject_prefix = xstrdup(value); |
225 | return 0; | |
226 | } | |
0f03ca94 PB |
227 | if (!strcmp(var, "log.showroot")) { |
228 | default_show_root = git_config_bool(var, value); | |
229 | return 0; | |
230 | } | |
231 | return git_diff_ui_config(var, value); | |
232 | } | |
233 | ||
a633fca0 | 234 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
70827b15 LT |
235 | { |
236 | struct rev_info rev; | |
237 | ||
0f03ca94 | 238 | git_config(git_log_config); |
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, | |
298 | const char *pathname, unsigned mode, int stage) | |
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 | |
0f03ca94 | 310 | git_config(git_log_config); |
db6296a5 | 311 | init_revisions(&rev, prefix); |
70827b15 | 312 | rev.diff = 1; |
70827b15 LT |
313 | rev.combine_merges = 1; |
314 | rev.dense_combined_merges = 1; | |
315 | rev.always_show_header = 1; | |
316 | rev.ignore_merges = 0; | |
317 | rev.no_walk = 1; | |
a633fca0 | 318 | cmd_log_init(argc, argv, prefix, &rev); |
5d7eeee2 JS |
319 | |
320 | count = rev.pending.nr; | |
321 | objects = rev.pending.objects; | |
322 | for (i = 0; i < count && !ret; i++) { | |
323 | struct object *o = objects[i].item; | |
324 | const char *name = objects[i].name; | |
325 | switch (o->type) { | |
326 | case OBJ_BLOB: | |
56122ed8 | 327 | ret = show_object(o->sha1, 0, NULL); |
5d7eeee2 JS |
328 | break; |
329 | case OBJ_TAG: { | |
330 | struct tag *t = (struct tag *)o; | |
331 | ||
56122ed8 | 332 | printf("%stag %s%s\n", |
8f67f8ae | 333 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 334 | t->tag, |
8f67f8ae | 335 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
56122ed8 | 336 | ret = show_object(o->sha1, 1, &rev); |
5d7eeee2 JS |
337 | objects[i].item = (struct object *)t->tagged; |
338 | i--; | |
339 | break; | |
340 | } | |
341 | case OBJ_TREE: | |
342 | printf("%stree %s%s\n\n", | |
8f67f8ae | 343 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 344 | name, |
8f67f8ae | 345 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
5d7eeee2 JS |
346 | read_tree_recursive((struct tree *)o, "", 0, 0, NULL, |
347 | show_tree_object); | |
348 | break; | |
349 | case OBJ_COMMIT: | |
350 | rev.pending.nr = rev.pending.alloc = 0; | |
351 | rev.pending.objects = NULL; | |
352 | add_object_array(o, name, &rev.pending); | |
353 | ret = cmd_log_walk(&rev); | |
354 | break; | |
355 | default: | |
356 | ret = error("Unknown type: %d", o->type); | |
357 | } | |
358 | } | |
359 | free(objects); | |
360 | return ret; | |
70827b15 LT |
361 | } |
362 | ||
cf39f54e LT |
363 | /* |
364 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" | |
365 | */ | |
366 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) | |
367 | { | |
368 | struct rev_info rev; | |
369 | ||
370 | git_config(git_log_config); | |
371 | init_revisions(&rev, prefix); | |
372 | init_reflog_walk(&rev.reflog_info); | |
373 | rev.abbrev_commit = 1; | |
374 | rev.verbose_header = 1; | |
375 | cmd_log_init(argc, argv, prefix, &rev); | |
376 | ||
377 | /* | |
378 | * This means that we override whatever commit format the user gave | |
379 | * on the cmd line. Sad, but cmd_log_init() currently doesn't | |
380 | * allow us to set a different default. | |
381 | */ | |
382 | rev.commit_format = CMIT_FMT_ONELINE; | |
383 | rev.always_show_header = 1; | |
384 | ||
385 | /* | |
386 | * We get called through "git reflog", so unlike the other log | |
387 | * routines, we need to set up our pager manually.. | |
388 | */ | |
389 | setup_pager(); | |
390 | ||
391 | return cmd_log_walk(&rev); | |
392 | } | |
393 | ||
a633fca0 | 394 | int cmd_log(int argc, const char **argv, const char *prefix) |
70827b15 LT |
395 | { |
396 | struct rev_info rev; | |
397 | ||
0f03ca94 | 398 | git_config(git_log_config); |
db6296a5 | 399 | init_revisions(&rev, prefix); |
70827b15 | 400 | rev.always_show_header = 1; |
a633fca0 | 401 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 | 402 | return cmd_log_walk(&rev); |
70827b15 | 403 | } |
91efcf60 | 404 | |
c06d2daa RR |
405 | /* format-patch */ |
406 | #define FORMAT_PATCH_NAME_MAX 64 | |
407 | ||
0377db77 JS |
408 | static int istitlechar(char c) |
409 | { | |
410 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
411 | (c >= '0' && c <= '9') || c == '.' || c == '_'; | |
412 | } | |
413 | ||
20ff0680 JS |
414 | static char *extra_headers = NULL; |
415 | static int extra_headers_size = 0; | |
917a8f89 | 416 | static const char *fmt_patch_suffix = ".patch"; |
49604a4d BG |
417 | static int numbered = 0; |
418 | static int auto_number = 0; | |
20ff0680 JS |
419 | |
420 | static int git_format_config(const char *var, const char *value) | |
421 | { | |
422 | if (!strcmp(var, "format.headers")) { | |
d7fb91c6 JH |
423 | int len; |
424 | ||
425 | if (!value) | |
426 | die("format.headers without value"); | |
427 | len = strlen(value); | |
20ff0680 | 428 | extra_headers_size += len + 1; |
83572c1a | 429 | extra_headers = xrealloc(extra_headers, extra_headers_size); |
20ff0680 JS |
430 | extra_headers[extra_headers_size - len - 1] = 0; |
431 | strcat(extra_headers, value); | |
432 | return 0; | |
433 | } | |
03eeaeae JH |
434 | if (!strcmp(var, "format.suffix")) { |
435 | if (!value) | |
90f5c186 | 436 | return config_error_nonbool(var); |
03eeaeae JH |
437 | fmt_patch_suffix = xstrdup(value); |
438 | return 0; | |
439 | } | |
a159ca0c | 440 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
f3aafa4d RA |
441 | return 0; |
442 | } | |
49604a4d | 443 | if (!strcmp(var, "format.numbered")) { |
90f5c186 | 444 | if (value && !strcasecmp(value, "auto")) { |
49604a4d BG |
445 | auto_number = 1; |
446 | return 0; | |
447 | } | |
49604a4d BG |
448 | numbered = git_config_bool(var, value); |
449 | return 0; | |
450 | } | |
dbd21447 | 451 | |
0f03ca94 | 452 | return git_log_config(var, value); |
20ff0680 JS |
453 | } |
454 | ||
455 | ||
81f3a188 | 456 | static FILE *realstdout = NULL; |
efd02016 | 457 | static const char *output_directory = NULL; |
81f3a188 | 458 | |
e6ff0f42 JL |
459 | static int reopen_stdout(struct commit *commit, int nr, int keep_subject, |
460 | int numbered_files) | |
0377db77 | 461 | { |
c06d2daa | 462 | char filename[PATH_MAX]; |
0377db77 | 463 | char *sol; |
2448482b | 464 | int len = 0; |
c06d2daa | 465 | int suffix_len = strlen(fmt_patch_suffix) + 1; |
0377db77 | 466 | |
2448482b | 467 | if (output_directory) { |
c06d2daa RR |
468 | if (strlen(output_directory) >= |
469 | sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len) | |
470 | return error("name of output directory is too long"); | |
471 | strlcpy(filename, output_directory, sizeof(filename) - suffix_len); | |
2448482b JS |
472 | len = strlen(filename); |
473 | if (filename[len - 1] != '/') | |
474 | filename[len++] = '/'; | |
475 | } | |
0377db77 | 476 | |
e6ff0f42 JL |
477 | if (numbered_files) { |
478 | sprintf(filename + len, "%d", nr); | |
479 | len = strlen(filename); | |
0377db77 | 480 | |
e6ff0f42 JL |
481 | } else { |
482 | sprintf(filename + len, "%04d", nr); | |
483 | len = strlen(filename); | |
484 | ||
485 | sol = strstr(commit->buffer, "\n\n"); | |
486 | if (sol) { | |
487 | int j, space = 1; | |
488 | ||
489 | sol += 2; | |
490 | /* strip [PATCH] or [PATCH blabla] */ | |
491 | if (!keep_subject && !prefixcmp(sol, "[PATCH")) { | |
492 | char *eos = strchr(sol + 6, ']'); | |
493 | if (eos) { | |
494 | while (isspace(*eos)) | |
495 | eos++; | |
496 | sol = eos; | |
0377db77 | 497 | } |
e6ff0f42 JL |
498 | } |
499 | ||
500 | for (j = 0; | |
501 | j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 && | |
502 | len < sizeof(filename) - suffix_len && | |
503 | sol[j] && sol[j] != '\n'; | |
504 | j++) { | |
505 | if (istitlechar(sol[j])) { | |
506 | if (space) { | |
507 | filename[len++] = '-'; | |
508 | space = 0; | |
509 | } | |
510 | filename[len++] = sol[j]; | |
511 | if (sol[j] == '.') | |
512 | while (sol[j + 1] == '.') | |
513 | j++; | |
514 | } else | |
515 | space = 1; | |
516 | } | |
517 | while (filename[len - 1] == '.' | |
518 | || filename[len - 1] == '-') | |
519 | len--; | |
520 | filename[len] = 0; | |
0377db77 | 521 | } |
e6ff0f42 JL |
522 | if (len + suffix_len >= sizeof(filename)) |
523 | return error("Patch pathname too long"); | |
524 | strcpy(filename + len, fmt_patch_suffix); | |
0377db77 | 525 | } |
e6ff0f42 | 526 | |
81f3a188 | 527 | fprintf(realstdout, "%s\n", filename); |
c06d2daa RR |
528 | if (freopen(filename, "w", stdout) == NULL) |
529 | return error("Cannot open patch file %s",filename); | |
c06d2daa | 530 | |
e6ff0f42 | 531 | return 0; |
0377db77 JS |
532 | } |
533 | ||
5d23e133 | 534 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix) |
9c6efa36 JS |
535 | { |
536 | struct rev_info check_rev; | |
537 | struct commit *commit; | |
538 | struct object *o1, *o2; | |
539 | unsigned flags1, flags2; | |
9c6efa36 JS |
540 | |
541 | if (rev->pending.nr != 2) | |
542 | die("Need exactly one range."); | |
543 | ||
544 | o1 = rev->pending.objects[0].item; | |
545 | flags1 = o1->flags; | |
546 | o2 = rev->pending.objects[1].item; | |
547 | flags2 = o2->flags; | |
548 | ||
549 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
550 | die("Not a range."); | |
551 | ||
5d23e133 | 552 | init_patch_ids(ids); |
9c6efa36 JS |
553 | |
554 | /* given a range a..b get all patch ids for b..a */ | |
db6296a5 | 555 | init_revisions(&check_rev, prefix); |
9c6efa36 JS |
556 | o1->flags ^= UNINTERESTING; |
557 | o2->flags ^= UNINTERESTING; | |
558 | add_pending_object(&check_rev, o1, "o1"); | |
559 | add_pending_object(&check_rev, o2, "o2"); | |
3d51e1b5 MK |
560 | if (prepare_revision_walk(&check_rev)) |
561 | die("revision walk setup failed"); | |
9c6efa36 JS |
562 | |
563 | while ((commit = get_revision(&check_rev)) != NULL) { | |
564 | /* ignore merges */ | |
565 | if (commit->parents && commit->parents->next) | |
566 | continue; | |
567 | ||
5d23e133 | 568 | add_commit_patch_id(commit, ids); |
9c6efa36 JS |
569 | } |
570 | ||
571 | /* reset for next revision walk */ | |
81db0941 JS |
572 | clear_commit_marks((struct commit *)o1, |
573 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
574 | clear_commit_marks((struct commit *)o2, | |
575 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
9c6efa36 JS |
576 | o1->flags = flags1; |
577 | o2->flags = flags2; | |
578 | } | |
579 | ||
d1566f78 JT |
580 | static void gen_message_id(char *dest, unsigned int length, char *base) |
581 | { | |
774751a8 | 582 | const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME); |
d1566f78 JT |
583 | const char *email_start = strrchr(committer, '<'); |
584 | const char *email_end = strrchr(committer, '>'); | |
585 | if(!email_start || !email_end || email_start > email_end - 1) | |
586 | die("Could not extract email from committer identity."); | |
76af0734 JH |
587 | snprintf(dest, length, "%s.%lu.git.%.*s", base, |
588 | (unsigned long) time(NULL), | |
589 | (int)(email_end - email_start - 1), email_start + 1); | |
d1566f78 JT |
590 | } |
591 | ||
8419d2ee JH |
592 | static const char *clean_message_id(const char *msg_id) |
593 | { | |
594 | char ch; | |
595 | const char *a, *z, *m; | |
8419d2ee JH |
596 | |
597 | m = msg_id; | |
598 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) | |
599 | m++; | |
600 | a = m; | |
601 | z = NULL; | |
602 | while ((ch = *m)) { | |
603 | if (!isspace(ch) && (ch != '>')) | |
604 | z = m; | |
605 | m++; | |
606 | } | |
607 | if (!z) | |
608 | die("insane in-reply-to: %s", msg_id); | |
609 | if (++z == m) | |
610 | return a; | |
182af834 | 611 | return xmemdupz(a, z - a); |
8419d2ee JH |
612 | } |
613 | ||
a633fca0 | 614 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
91efcf60 JH |
615 | { |
616 | struct commit *commit; | |
617 | struct commit **list = NULL; | |
618 | struct rev_info rev; | |
2448482b | 619 | int nr = 0, total, i, j; |
0377db77 | 620 | int use_stdout = 0; |
fa0f02df | 621 | int start_number = -1; |
8ac80a57 | 622 | int keep_subject = 0; |
e6ff0f42 | 623 | int numbered_files = 0; /* _just_ numbers */ |
2d9e4a47 | 624 | int subject_prefix = 0; |
9c6efa36 | 625 | int ignore_if_in_upstream = 0; |
cc35de84 | 626 | int thread = 0; |
76af0734 | 627 | const char *in_reply_to = NULL; |
5d23e133 | 628 | struct patch_ids ids; |
cf2251b6 | 629 | char *add_signoff = NULL; |
d1566f78 JT |
630 | char message_id[1024]; |
631 | char ref_message_id[1024]; | |
91efcf60 | 632 | |
97beb812 | 633 | git_config(git_format_config); |
db6296a5 | 634 | init_revisions(&rev, prefix); |
91efcf60 JH |
635 | rev.commit_format = CMIT_FMT_EMAIL; |
636 | rev.verbose_header = 1; | |
637 | rev.diff = 1; | |
91efcf60 JH |
638 | rev.combine_merges = 0; |
639 | rev.ignore_merges = 1; | |
27e1b127 | 640 | rev.diffopt.msg_sep = ""; |
8f67f8ae | 641 | DIFF_OPT_SET(&rev.diffopt, RECURSIVE); |
91efcf60 | 642 | |
dbd21447 | 643 | rev.subject_prefix = fmt_patch_subject_prefix; |
20ff0680 JS |
644 | rev.extra_headers = extra_headers; |
645 | ||
2448482b JS |
646 | /* |
647 | * Parse the arguments before setup_revisions(), or something | |
2dc189a3 | 648 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
2448482b JS |
649 | * possibly a valid SHA1. |
650 | */ | |
651 | for (i = 1, j = 1; i < argc; i++) { | |
652 | if (!strcmp(argv[i], "--stdout")) | |
0377db77 | 653 | use_stdout = 1; |
596524b3 JS |
654 | else if (!strcmp(argv[i], "-n") || |
655 | !strcmp(argv[i], "--numbered")) | |
656 | numbered = 1; | |
49604a4d BG |
657 | else if (!strcmp(argv[i], "-N") || |
658 | !strcmp(argv[i], "--no-numbered")) { | |
659 | numbered = 0; | |
660 | auto_number = 0; | |
661 | } | |
cc44c765 | 662 | else if (!prefixcmp(argv[i], "--start-number=")) |
fa0f02df | 663 | start_number = strtol(argv[i] + 15, NULL, 10); |
e6ff0f42 JL |
664 | else if (!strcmp(argv[i], "--numbered-files")) |
665 | numbered_files = 1; | |
fa0f02df JS |
666 | else if (!strcmp(argv[i], "--start-number")) { |
667 | i++; | |
668 | if (i == argc) | |
669 | die("Need a number for --start-number"); | |
670 | start_number = strtol(argv[i], NULL, 10); | |
cf2251b6 JH |
671 | } |
672 | else if (!strcmp(argv[i], "-k") || | |
8ac80a57 JS |
673 | !strcmp(argv[i], "--keep-subject")) { |
674 | keep_subject = 1; | |
675 | rev.total = -1; | |
cf2251b6 | 676 | } |
efd02016 JH |
677 | else if (!strcmp(argv[i], "--output-directory") || |
678 | !strcmp(argv[i], "-o")) { | |
2448482b | 679 | i++; |
efd02016 JH |
680 | if (argc <= i) |
681 | die("Which directory?"); | |
682 | if (output_directory) | |
683 | die("Two output directories?"); | |
684 | output_directory = argv[i]; | |
698ce6f8 | 685 | } |
cf2251b6 JH |
686 | else if (!strcmp(argv[i], "--signoff") || |
687 | !strcmp(argv[i], "-s")) { | |
6c4cca1c EB |
688 | const char *committer; |
689 | const char *endpos; | |
774751a8 | 690 | committer = git_committer_info(IDENT_ERROR_ON_NO_NAME); |
6c4cca1c | 691 | endpos = strchr(committer, '>'); |
cf2251b6 JH |
692 | if (!endpos) |
693 | die("bogos committer info %s\n", committer); | |
182af834 | 694 | add_signoff = xmemdupz(committer, endpos - committer + 1); |
cf2251b6 | 695 | } |
c112f689 | 696 | else if (!strcmp(argv[i], "--attach")) { |
698ce6f8 | 697 | rev.mime_boundary = git_version_string; |
c112f689 JS |
698 | rev.no_inline = 1; |
699 | } | |
700 | else if (!prefixcmp(argv[i], "--attach=")) { | |
701 | rev.mime_boundary = argv[i] + 9; | |
702 | rev.no_inline = 1; | |
703 | } | |
704 | else if (!strcmp(argv[i], "--inline")) { | |
705 | rev.mime_boundary = git_version_string; | |
706 | rev.no_inline = 0; | |
707 | } | |
708 | else if (!prefixcmp(argv[i], "--inline=")) { | |
698ce6f8 | 709 | rev.mime_boundary = argv[i] + 9; |
c112f689 JS |
710 | rev.no_inline = 0; |
711 | } | |
9c6efa36 JS |
712 | else if (!strcmp(argv[i], "--ignore-if-in-upstream")) |
713 | ignore_if_in_upstream = 1; | |
cc35de84 JT |
714 | else if (!strcmp(argv[i], "--thread")) |
715 | thread = 1; | |
cc44c765 | 716 | else if (!prefixcmp(argv[i], "--in-reply-to=")) |
da56645d JT |
717 | in_reply_to = argv[i] + 14; |
718 | else if (!strcmp(argv[i], "--in-reply-to")) { | |
719 | i++; | |
720 | if (i == argc) | |
721 | die("Need a Message-Id for --in-reply-to"); | |
722 | in_reply_to = argv[i]; | |
2d9e4a47 RJ |
723 | } else if (!prefixcmp(argv[i], "--subject-prefix=")) { |
724 | subject_prefix = 1; | |
725 | rev.subject_prefix = argv[i] + 17; | |
726 | } else if (!prefixcmp(argv[i], "--suffix=")) | |
03eeaeae | 727 | fmt_patch_suffix = argv[i] + 9; |
698ce6f8 | 728 | else |
2448482b | 729 | argv[j++] = argv[i]; |
0377db77 | 730 | } |
2448482b JS |
731 | argc = j; |
732 | ||
add5c8a5 | 733 | if (start_number < 0) |
fa0f02df | 734 | start_number = 1; |
63b398a4 | 735 | if (numbered && keep_subject) |
8ac80a57 | 736 | die ("-n and -k are mutually exclusive."); |
2d9e4a47 RJ |
737 | if (keep_subject && subject_prefix) |
738 | die ("--subject-prefix and -k are mutually exclusive."); | |
e6ff0f42 JL |
739 | if (numbered_files && use_stdout) |
740 | die ("--numbered-files and --stdout are mutually exclusive."); | |
8ac80a57 | 741 | |
2448482b JS |
742 | argc = setup_revisions(argc, argv, &rev, "HEAD"); |
743 | if (argc > 1) | |
744 | die ("unrecognized argument: %s", argv[1]); | |
0377db77 | 745 | |
c9b5ef99 | 746 | if (!rev.diffopt.output_format) |
c261e438 | 747 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH; |
c9b5ef99 | 748 | |
8f67f8ae PH |
749 | if (!DIFF_OPT_TST(&rev.diffopt, TEXT)) |
750 | DIFF_OPT_SET(&rev.diffopt, BINARY); | |
e47f306d | 751 | |
90f70a91 | 752 | if (!output_directory && !use_stdout) |
77e565d8 ML |
753 | output_directory = prefix; |
754 | ||
efd02016 JH |
755 | if (output_directory) { |
756 | if (use_stdout) | |
757 | die("standard output, or directory, which one?"); | |
758 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) | |
759 | die("Could not create directory %s", | |
760 | output_directory); | |
761 | } | |
762 | ||
1f1e895f | 763 | if (rev.pending.nr == 1) { |
8a1d076e JH |
764 | if (rev.max_count < 0 && !rev.show_root_diff) { |
765 | /* | |
766 | * This is traditional behaviour of "git format-patch | |
767 | * origin" that prepares what the origin side still | |
768 | * does not have. | |
769 | */ | |
7c496280 | 770 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
3384a2df | 771 | add_head_to_pending(&rev); |
7c496280 | 772 | } |
8a1d076e JH |
773 | /* |
774 | * Otherwise, it is "format-patch -22 HEAD", and/or | |
775 | * "format-patch --root HEAD". The user wants | |
776 | * get_revision() to do the usual traversal. | |
7c496280 | 777 | */ |
e686eb98 JS |
778 | } |
779 | ||
9c6efa36 | 780 | if (ignore_if_in_upstream) |
5d23e133 | 781 | get_patch_ids(&rev, &ids, prefix); |
9c6efa36 | 782 | |
81f3a188 | 783 | if (!use_stdout) |
f5788250 | 784 | realstdout = xfdopen(xdup(1), "w"); |
81f3a188 | 785 | |
3d51e1b5 MK |
786 | if (prepare_revision_walk(&rev)) |
787 | die("revision walk setup failed"); | |
91efcf60 | 788 | while ((commit = get_revision(&rev)) != NULL) { |
0377db77 JS |
789 | /* ignore merges */ |
790 | if (commit->parents && commit->parents->next) | |
791 | continue; | |
9c6efa36 JS |
792 | |
793 | if (ignore_if_in_upstream && | |
5d23e133 | 794 | has_commit_patch_id(commit, &ids)) |
9c6efa36 JS |
795 | continue; |
796 | ||
91efcf60 | 797 | nr++; |
83572c1a | 798 | list = xrealloc(list, nr * sizeof(list[0])); |
91efcf60 JH |
799 | list[nr - 1] = commit; |
800 | } | |
0377db77 | 801 | total = nr; |
49604a4d BG |
802 | if (!keep_subject && auto_number && total > 1) |
803 | numbered = 1; | |
596524b3 | 804 | if (numbered) |
fa0f02df | 805 | rev.total = total + start_number - 1; |
cf2251b6 | 806 | rev.add_signoff = add_signoff; |
8419d2ee JH |
807 | if (in_reply_to) |
808 | rev.ref_message_id = clean_message_id(in_reply_to); | |
91efcf60 JH |
809 | while (0 <= --nr) { |
810 | int shown; | |
811 | commit = list[nr]; | |
add5c8a5 | 812 | rev.nr = total - nr + (start_number - 1); |
d1566f78 | 813 | /* Make the second and subsequent mails replies to the first */ |
cc35de84 JT |
814 | if (thread) { |
815 | if (nr == (total - 2)) { | |
816 | strncpy(ref_message_id, message_id, | |
817 | sizeof(ref_message_id)); | |
818 | ref_message_id[sizeof(ref_message_id)-1]='\0'; | |
819 | rev.ref_message_id = ref_message_id; | |
820 | } | |
821 | gen_message_id(message_id, sizeof(message_id), | |
822 | sha1_to_hex(commit->object.sha1)); | |
823 | rev.message_id = message_id; | |
d1566f78 | 824 | } |
0377db77 | 825 | if (!use_stdout) |
e6ff0f42 JL |
826 | if (reopen_stdout(commit, rev.nr, keep_subject, |
827 | numbered_files)) | |
c06d2daa | 828 | die("Failed to create output files"); |
91efcf60 JH |
829 | shown = log_tree_commit(&rev, commit); |
830 | free(commit->buffer); | |
831 | commit->buffer = NULL; | |
add5c8a5 JH |
832 | |
833 | /* We put one extra blank line between formatted | |
834 | * patches and this flag is used by log-tree code | |
835 | * to see if it needs to emit a LF before showing | |
836 | * the log; when using one file per patch, we do | |
837 | * not want the extra blank line. | |
838 | */ | |
839 | if (!use_stdout) | |
840 | rev.shown_one = 0; | |
698ce6f8 JS |
841 | if (shown) { |
842 | if (rev.mime_boundary) | |
843 | printf("\n--%s%s--\n\n\n", | |
844 | mime_boundary_leader, | |
845 | rev.mime_boundary); | |
846 | else | |
847 | printf("-- \n%s\n\n", git_version_string); | |
848 | } | |
0377db77 JS |
849 | if (!use_stdout) |
850 | fclose(stdout); | |
91efcf60 JH |
851 | } |
852 | free(list); | |
5d23e133 JH |
853 | if (ignore_if_in_upstream) |
854 | free_patch_ids(&ids); | |
91efcf60 JH |
855 | return 0; |
856 | } | |
857 | ||
e827633a RS |
858 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
859 | { | |
860 | unsigned char sha1[20]; | |
861 | if (get_sha1(arg, sha1) == 0) { | |
862 | struct commit *commit = lookup_commit_reference(sha1); | |
863 | if (commit) { | |
864 | commit->object.flags |= flags; | |
865 | add_pending_object(revs, &commit->object, arg); | |
866 | return 0; | |
867 | } | |
868 | } | |
869 | return -1; | |
870 | } | |
871 | ||
872 | static const char cherry_usage[] = | |
873 | "git-cherry [-v] <upstream> [<head>] [<limit>]"; | |
874 | int cmd_cherry(int argc, const char **argv, const char *prefix) | |
875 | { | |
876 | struct rev_info revs; | |
5d23e133 | 877 | struct patch_ids ids; |
e827633a RS |
878 | struct commit *commit; |
879 | struct commit_list *list = NULL; | |
880 | const char *upstream; | |
881 | const char *head = "HEAD"; | |
882 | const char *limit = NULL; | |
883 | int verbose = 0; | |
884 | ||
885 | if (argc > 1 && !strcmp(argv[1], "-v")) { | |
886 | verbose = 1; | |
887 | argc--; | |
888 | argv++; | |
889 | } | |
890 | ||
891 | switch (argc) { | |
892 | case 4: | |
893 | limit = argv[3]; | |
894 | /* FALLTHROUGH */ | |
895 | case 3: | |
896 | head = argv[2]; | |
897 | /* FALLTHROUGH */ | |
898 | case 2: | |
899 | upstream = argv[1]; | |
900 | break; | |
901 | default: | |
902 | usage(cherry_usage); | |
903 | } | |
904 | ||
905 | init_revisions(&revs, prefix); | |
906 | revs.diff = 1; | |
907 | revs.combine_merges = 0; | |
908 | revs.ignore_merges = 1; | |
8f67f8ae | 909 | DIFF_OPT_SET(&revs.diffopt, RECURSIVE); |
e827633a RS |
910 | |
911 | if (add_pending_commit(head, &revs, 0)) | |
912 | die("Unknown commit %s", head); | |
913 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) | |
914 | die("Unknown commit %s", upstream); | |
915 | ||
916 | /* Don't say anything if head and upstream are the same. */ | |
917 | if (revs.pending.nr == 2) { | |
918 | struct object_array_entry *o = revs.pending.objects; | |
919 | if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) | |
920 | return 0; | |
921 | } | |
922 | ||
5d23e133 | 923 | get_patch_ids(&revs, &ids, prefix); |
e827633a RS |
924 | |
925 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
926 | die("Unknown commit %s", limit); | |
927 | ||
928 | /* reverse the list of commits */ | |
3d51e1b5 MK |
929 | if (prepare_revision_walk(&revs)) |
930 | die("revision walk setup failed"); | |
e827633a RS |
931 | while ((commit = get_revision(&revs)) != NULL) { |
932 | /* ignore merges */ | |
933 | if (commit->parents && commit->parents->next) | |
934 | continue; | |
935 | ||
936 | commit_list_insert(commit, &list); | |
937 | } | |
938 | ||
939 | while (list) { | |
e827633a RS |
940 | char sign = '+'; |
941 | ||
942 | commit = list->item; | |
5d23e133 | 943 | if (has_commit_patch_id(commit, &ids)) |
e827633a RS |
944 | sign = '-'; |
945 | ||
946 | if (verbose) { | |
674d1727 PH |
947 | struct strbuf buf; |
948 | strbuf_init(&buf, 0); | |
949 | pretty_print_commit(CMIT_FMT_ONELINE, commit, | |
4593fb84 | 950 | &buf, 0, NULL, NULL, 0, 0); |
e827633a | 951 | printf("%c %s %s\n", sign, |
674d1727 PH |
952 | sha1_to_hex(commit->object.sha1), buf.buf); |
953 | strbuf_release(&buf); | |
e827633a RS |
954 | } |
955 | else { | |
956 | printf("%c %s\n", sign, | |
957 | sha1_to_hex(commit->object.sha1)); | |
958 | } | |
959 | ||
960 | list = list->next; | |
961 | } | |
962 | ||
5d23e133 | 963 | free_patch_ids(&ids); |
e827633a RS |
964 | return 0; |
965 | } |