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