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