blob: af2de54371cf7fc294c17431622f759e8e6066db [file] [log] [blame]
Linus Torvalds70827b12006-04-21 10:27:34 -07001/*
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"
Junio C Hamano91efcf62006-04-21 13:19:58 -070012#include "builtin.h"
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +010013#include "tag.h"
Linus Torvaldscf39f542007-02-08 09:51:56 -080014#include "reflog-walk.h"
Linus Torvalds70827b12006-04-21 10:27:34 -070015
Peter Baumann0f03ca92006-11-23 10:36:33 +010016static int default_show_root = 1;
17
Johannes Schindeline686eb92006-05-06 22:56:38 +020018/* this is in builtin-diff.c */
19void add_head(struct rev_info *revs);
20
Linus Torvaldsa633fca2006-07-28 22:44:25 -070021static void cmd_log_init(int argc, const char **argv, const char *prefix,
Linus Torvalds70827b12006-04-21 10:27:34 -070022 struct rev_info *rev)
23{
Junio C Hamano52883fb2006-12-25 11:48:35 -080024 int i;
25
Linus Torvalds70827b12006-04-21 10:27:34 -070026 rev->abbrev = DEFAULT_ABBREV;
27 rev->commit_format = CMIT_FMT_DEFAULT;
28 rev->verbose_header = 1;
Peter Baumann0f03ca92006-11-23 10:36:33 +010029 rev->show_root_diff = default_show_root;
Linus Torvalds70827b12006-04-21 10:27:34 -070030 argc = setup_revisions(argc, argv, rev, "HEAD");
Timo Hirvonen9dafea22006-06-25 15:39:35 +030031 if (rev->diffopt.pickaxe || rev->diffopt.filter)
32 rev->always_show_header = 0;
Junio C Hamano52883fb2006-12-25 11:48:35 -080033 for (i = 1; i < argc; i++) {
34 const char *arg = argv[i];
35 if (!strncmp(arg, "--encoding=", 11)) {
36 arg += 11;
Junio C Hamano52883fb2006-12-25 11:48:35 -080037 if (strcmp(arg, "none"))
Junio C Hamanod2c11a32006-12-27 16:41:33 -080038 git_log_output_encoding = strdup(arg);
Junio C Hamano52883fb2006-12-25 11:48:35 -080039 else
Junio C Hamanod2c11a32006-12-27 16:41:33 -080040 git_log_output_encoding = "";
Junio C Hamano52883fb2006-12-25 11:48:35 -080041 }
42 else
43 die("unrecognized argument: %s", arg);
44 }
Timo Hirvonen9dafea22006-06-25 15:39:35 +030045}
46
47static int cmd_log_walk(struct rev_info *rev)
48{
49 struct commit *commit;
Linus Torvalds70827b12006-04-21 10:27:34 -070050
51 prepare_revision_walk(rev);
Linus Torvalds70827b12006-04-21 10:27:34 -070052 while ((commit = get_revision(rev)) != NULL) {
53 log_tree_commit(rev, commit);
Johannes Schindelina6c73062007-01-20 22:28:16 +010054 if (!rev->reflog_info) {
55 /* we allow cycles in reflog ancestry */
56 free(commit->buffer);
57 commit->buffer = NULL;
58 }
Linus Torvaldscb115742006-06-17 18:47:58 -070059 free_commit_list(commit->parents);
60 commit->parents = NULL;
Linus Torvalds70827b12006-04-21 10:27:34 -070061 }
62 return 0;
63}
64
Peter Baumann0f03ca92006-11-23 10:36:33 +010065static int git_log_config(const char *var, const char *value)
66{
67 if (!strcmp(var, "log.showroot")) {
68 default_show_root = git_config_bool(var, value);
69 return 0;
70 }
71 return git_diff_ui_config(var, value);
72}
73
Linus Torvaldsa633fca2006-07-28 22:44:25 -070074int cmd_whatchanged(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -070075{
76 struct rev_info rev;
77
Peter Baumann0f03ca92006-11-23 10:36:33 +010078 git_config(git_log_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -070079 init_revisions(&rev, prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -070080 rev.diff = 1;
81 rev.diffopt.recursive = 1;
Linus Torvalds92024342006-06-11 10:57:35 -070082 rev.simplify_history = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -070083 cmd_log_init(argc, argv, prefix, &rev);
Timo Hirvonen9dafea22006-06-25 15:39:35 +030084 if (!rev.diffopt.output_format)
85 rev.diffopt.output_format = DIFF_FORMAT_RAW;
86 return cmd_log_walk(&rev);
Linus Torvalds70827b12006-04-21 10:27:34 -070087}
88
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +010089static int show_object(const unsigned char *sha1, int suppress_header)
90{
91 unsigned long size;
92 char type[20];
93 char *buf = read_sha1_file(sha1, type, &size);
94 int offset = 0;
95
96 if (!buf)
97 return error("Could not read object %s", sha1_to_hex(sha1));
98
99 if (suppress_header)
100 while (offset < size && buf[offset++] != '\n') {
101 int new_offset = offset;
102 while (new_offset < size && buf[new_offset++] != '\n')
103 ; /* do nothing */
104 offset = new_offset;
105 }
106
107 if (offset < size)
108 fwrite(buf + offset, size - offset, 1, stdout);
109 free(buf);
110 return 0;
111}
112
113static int show_tree_object(const unsigned char *sha1,
114 const char *base, int baselen,
115 const char *pathname, unsigned mode, int stage)
116{
117 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
118 return 0;
119}
120
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700121int cmd_show(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700122{
123 struct rev_info rev;
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +0100124 struct object_array_entry *objects;
125 int i, count, ret = 0;
Linus Torvalds70827b12006-04-21 10:27:34 -0700126
Peter Baumann0f03ca92006-11-23 10:36:33 +0100127 git_config(git_log_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700128 init_revisions(&rev, prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -0700129 rev.diff = 1;
130 rev.diffopt.recursive = 1;
131 rev.combine_merges = 1;
132 rev.dense_combined_merges = 1;
133 rev.always_show_header = 1;
134 rev.ignore_merges = 0;
135 rev.no_walk = 1;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700136 cmd_log_init(argc, argv, prefix, &rev);
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +0100137
138 count = rev.pending.nr;
139 objects = rev.pending.objects;
140 for (i = 0; i < count && !ret; i++) {
141 struct object *o = objects[i].item;
142 const char *name = objects[i].name;
143 switch (o->type) {
144 case OBJ_BLOB:
145 ret = show_object(o->sha1, 0);
146 break;
147 case OBJ_TAG: {
148 struct tag *t = (struct tag *)o;
149
150 printf("%stag %s%s\n\n",
151 diff_get_color(rev.diffopt.color_diff,
152 DIFF_COMMIT),
153 t->tag,
154 diff_get_color(rev.diffopt.color_diff,
155 DIFF_RESET));
156 ret = show_object(o->sha1, 1);
157 objects[i].item = (struct object *)t->tagged;
158 i--;
159 break;
160 }
161 case OBJ_TREE:
162 printf("%stree %s%s\n\n",
163 diff_get_color(rev.diffopt.color_diff,
164 DIFF_COMMIT),
165 name,
166 diff_get_color(rev.diffopt.color_diff,
167 DIFF_RESET));
168 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
169 show_tree_object);
170 break;
171 case OBJ_COMMIT:
172 rev.pending.nr = rev.pending.alloc = 0;
173 rev.pending.objects = NULL;
174 add_object_array(o, name, &rev.pending);
175 ret = cmd_log_walk(&rev);
176 break;
177 default:
178 ret = error("Unknown type: %d", o->type);
179 }
180 }
181 free(objects);
182 return ret;
Linus Torvalds70827b12006-04-21 10:27:34 -0700183}
184
Linus Torvaldscf39f542007-02-08 09:51:56 -0800185/*
186 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
187 */
188int cmd_log_reflog(int argc, const char **argv, const char *prefix)
189{
190 struct rev_info rev;
191
192 git_config(git_log_config);
193 init_revisions(&rev, prefix);
194 init_reflog_walk(&rev.reflog_info);
195 rev.abbrev_commit = 1;
196 rev.verbose_header = 1;
197 cmd_log_init(argc, argv, prefix, &rev);
198
199 /*
200 * This means that we override whatever commit format the user gave
201 * on the cmd line. Sad, but cmd_log_init() currently doesn't
202 * allow us to set a different default.
203 */
204 rev.commit_format = CMIT_FMT_ONELINE;
205 rev.always_show_header = 1;
206
207 /*
208 * We get called through "git reflog", so unlike the other log
209 * routines, we need to set up our pager manually..
210 */
211 setup_pager();
212
213 return cmd_log_walk(&rev);
214}
215
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700216int cmd_log(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700217{
218 struct rev_info rev;
219
Peter Baumann0f03ca92006-11-23 10:36:33 +0100220 git_config(git_log_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700221 init_revisions(&rev, prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -0700222 rev.always_show_header = 1;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700223 cmd_log_init(argc, argv, prefix, &rev);
Timo Hirvonen9dafea22006-06-25 15:39:35 +0300224 return cmd_log_walk(&rev);
Linus Torvalds70827b12006-04-21 10:27:34 -0700225}
Junio C Hamano91efcf62006-04-21 13:19:58 -0700226
Johannes Schindelin0377db72006-05-05 01:16:40 +0200227static int istitlechar(char c)
228{
229 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
230 (c >= '0' && c <= '9') || c == '.' || c == '_';
231}
232
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200233static char *extra_headers = NULL;
234static int extra_headers_size = 0;
Junio C Hamano917a8f82007-01-17 15:03:39 -0800235static const char *fmt_patch_suffix = ".patch";
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200236
237static int git_format_config(const char *var, const char *value)
238{
239 if (!strcmp(var, "format.headers")) {
Junio C Hamanod7fb91c2007-01-17 11:13:02 -0800240 int len;
241
242 if (!value)
243 die("format.headers without value");
244 len = strlen(value);
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200245 extra_headers_size += len + 1;
Jonas Fonseca83572c12006-08-26 16:16:18 +0200246 extra_headers = xrealloc(extra_headers, extra_headers_size);
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200247 extra_headers[extra_headers_size - len - 1] = 0;
248 strcat(extra_headers, value);
249 return 0;
250 }
Junio C Hamano03eeaea2007-01-17 11:12:03 -0800251 if (!strcmp(var, "format.suffix")) {
252 if (!value)
253 die("format.suffix without value");
254 fmt_patch_suffix = xstrdup(value);
255 return 0;
256 }
Andy Parkinsa159ca02006-12-13 09:13:28 +0000257 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
Ryan Andersonf3aafa42006-07-09 02:28:21 -0400258 return 0;
259 }
Peter Baumann0f03ca92006-11-23 10:36:33 +0100260 return git_log_config(var, value);
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200261}
262
263
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200264static FILE *realstdout = NULL;
Junio C Hamanoefd02012006-06-06 08:46:23 -0700265static const char *output_directory = NULL;
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200266
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200267static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
Johannes Schindelin0377db72006-05-05 01:16:40 +0200268{
269 char filename[1024];
270 char *sol;
Johannes Schindelin24484822006-05-05 03:33:32 +0200271 int len = 0;
Junio C Hamano03eeaea2007-01-17 11:12:03 -0800272 int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */
Johannes Schindelin0377db72006-05-05 01:16:40 +0200273
Johannes Schindelin24484822006-05-05 03:33:32 +0200274 if (output_directory) {
Junio C Hamano03eeaea2007-01-17 11:12:03 -0800275 strlcpy(filename, output_directory, 1000);
Johannes Schindelin24484822006-05-05 03:33:32 +0200276 len = strlen(filename);
277 if (filename[len - 1] != '/')
278 filename[len++] = '/';
279 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200280
Johannes Schindelin24484822006-05-05 03:33:32 +0200281 sprintf(filename + len, "%04d", nr);
Johannes Schindelin0377db72006-05-05 01:16:40 +0200282 len = strlen(filename);
283
284 sol = strstr(commit->buffer, "\n\n");
285 if (sol) {
286 int j, space = 1;
287
288 sol += 2;
289 /* strip [PATCH] or [PATCH blabla] */
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200290 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
Johannes Schindelin0377db72006-05-05 01:16:40 +0200291 char *eos = strchr(sol + 6, ']');
292 if (eos) {
293 while (isspace(*eos))
294 eos++;
295 sol = eos;
296 }
297 }
298
Junio C Hamano03eeaea2007-01-17 11:12:03 -0800299 for (j = 0;
300 len < sizeof(filename) - suffix_len &&
301 sol[j] && sol[j] != '\n';
302 j++) {
Johannes Schindelin0377db72006-05-05 01:16:40 +0200303 if (istitlechar(sol[j])) {
304 if (space) {
305 filename[len++] = '-';
306 space = 0;
307 }
308 filename[len++] = sol[j];
309 if (sol[j] == '.')
310 while (sol[j + 1] == '.')
311 j++;
312 } else
313 space = 1;
314 }
315 while (filename[len - 1] == '.' || filename[len - 1] == '-')
316 len--;
317 }
Junio C Hamano03eeaea2007-01-17 11:12:03 -0800318 strcpy(filename + len, fmt_patch_suffix);
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200319 fprintf(realstdout, "%s\n", filename);
Johannes Schindelin0377db72006-05-05 01:16:40 +0200320 freopen(filename, "w", stdout);
321}
322
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200323static int get_patch_id(struct commit *commit, struct diff_options *options,
324 unsigned char *sha1)
325{
Rene Scharfe2b603562006-10-26 18:52:39 +0200326 if (commit->parents)
327 diff_tree_sha1(commit->parents->item->object.sha1,
328 commit->object.sha1, "", options);
329 else
330 diff_root_tree_sha1(commit->object.sha1, "", options);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200331 diffcore_std(options);
332 return diff_flush_patch_id(options, sha1);
333}
334
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700335static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200336{
337 struct rev_info check_rev;
338 struct commit *commit;
339 struct object *o1, *o2;
340 unsigned flags1, flags2;
341 unsigned char sha1[20];
342
343 if (rev->pending.nr != 2)
344 die("Need exactly one range.");
345
346 o1 = rev->pending.objects[0].item;
347 flags1 = o1->flags;
348 o2 = rev->pending.objects[1].item;
349 flags2 = o2->flags;
350
351 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
352 die("Not a range.");
353
354 diff_setup(options);
355 options->recursive = 1;
356 if (diff_setup_done(options) < 0)
357 die("diff_setup_done failed");
358
359 /* given a range a..b get all patch ids for b..a */
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700360 init_revisions(&check_rev, prefix);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200361 o1->flags ^= UNINTERESTING;
362 o2->flags ^= UNINTERESTING;
363 add_pending_object(&check_rev, o1, "o1");
364 add_pending_object(&check_rev, o2, "o2");
365 prepare_revision_walk(&check_rev);
366
367 while ((commit = get_revision(&check_rev)) != NULL) {
368 /* ignore merges */
369 if (commit->parents && commit->parents->next)
370 continue;
371
372 if (!get_patch_id(commit, options, sha1))
373 created_object(sha1, xcalloc(1, sizeof(struct object)));
374 }
375
376 /* reset for next revision walk */
Johannes Schindelin81db0942006-06-27 22:38:04 +0200377 clear_commit_marks((struct commit *)o1,
378 SEEN | UNINTERESTING | SHOWN | ADDED);
379 clear_commit_marks((struct commit *)o2,
380 SEEN | UNINTERESTING | SHOWN | ADDED);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200381 o1->flags = flags1;
382 o2->flags = flags2;
383}
384
Josh Triplettd1566f72006-07-14 17:48:51 -0700385static void gen_message_id(char *dest, unsigned int length, char *base)
386{
Junio C Hamanocb280e12007-01-25 19:05:01 -0800387 const char *committer = git_committer_info(-1);
Josh Triplettd1566f72006-07-14 17:48:51 -0700388 const char *email_start = strrchr(committer, '<');
389 const char *email_end = strrchr(committer, '>');
390 if(!email_start || !email_end || email_start > email_end - 1)
391 die("Could not extract email from committer identity.");
Junio C Hamano76af0732006-07-14 22:47:53 -0700392 snprintf(dest, length, "%s.%lu.git.%.*s", base,
393 (unsigned long) time(NULL),
394 (int)(email_end - email_start - 1), email_start + 1);
Josh Triplettd1566f72006-07-14 17:48:51 -0700395}
396
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700397int cmd_format_patch(int argc, const char **argv, const char *prefix)
Junio C Hamano91efcf62006-04-21 13:19:58 -0700398{
399 struct commit *commit;
400 struct commit **list = NULL;
401 struct rev_info rev;
Johannes Schindelin24484822006-05-05 03:33:32 +0200402 int nr = 0, total, i, j;
Johannes Schindelin0377db72006-05-05 01:16:40 +0200403 int use_stdout = 0;
Johannes Schindelin596524b2006-05-05 04:30:52 +0200404 int numbered = 0;
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200405 int start_number = -1;
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200406 int keep_subject = 0;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200407 int ignore_if_in_upstream = 0;
Josh Triplettcc35de82006-07-14 17:49:04 -0700408 int thread = 0;
Junio C Hamano76af0732006-07-14 22:47:53 -0700409 const char *in_reply_to = NULL;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200410 struct diff_options patch_id_opts;
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700411 char *add_signoff = NULL;
Josh Triplettd1566f72006-07-14 17:48:51 -0700412 char message_id[1024];
413 char ref_message_id[1024];
Junio C Hamano91efcf62006-04-21 13:19:58 -0700414
Eric Wong97beb812006-07-07 03:10:45 -0700415 git_config(git_format_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700416 init_revisions(&rev, prefix);
Junio C Hamano91efcf62006-04-21 13:19:58 -0700417 rev.commit_format = CMIT_FMT_EMAIL;
418 rev.verbose_header = 1;
419 rev.diff = 1;
Junio C Hamano91efcf62006-04-21 13:19:58 -0700420 rev.combine_merges = 0;
421 rev.ignore_merges = 1;
Junio C Hamano27e1b122006-06-29 00:18:52 -0700422 rev.diffopt.msg_sep = "";
423 rev.diffopt.recursive = 1;
Junio C Hamano91efcf62006-04-21 13:19:58 -0700424
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200425 rev.extra_headers = extra_headers;
426
Johannes Schindelin24484822006-05-05 03:33:32 +0200427 /*
428 * Parse the arguments before setup_revisions(), or something
429 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
430 * possibly a valid SHA1.
431 */
432 for (i = 1, j = 1; i < argc; i++) {
433 if (!strcmp(argv[i], "--stdout"))
Johannes Schindelin0377db72006-05-05 01:16:40 +0200434 use_stdout = 1;
Johannes Schindelin596524b2006-05-05 04:30:52 +0200435 else if (!strcmp(argv[i], "-n") ||
436 !strcmp(argv[i], "--numbered"))
437 numbered = 1;
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200438 else if (!strncmp(argv[i], "--start-number=", 15))
439 start_number = strtol(argv[i] + 15, NULL, 10);
440 else if (!strcmp(argv[i], "--start-number")) {
441 i++;
442 if (i == argc)
443 die("Need a number for --start-number");
444 start_number = strtol(argv[i], NULL, 10);
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700445 }
446 else if (!strcmp(argv[i], "-k") ||
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200447 !strcmp(argv[i], "--keep-subject")) {
448 keep_subject = 1;
449 rev.total = -1;
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700450 }
Junio C Hamanoefd02012006-06-06 08:46:23 -0700451 else if (!strcmp(argv[i], "--output-directory") ||
452 !strcmp(argv[i], "-o")) {
Johannes Schindelin24484822006-05-05 03:33:32 +0200453 i++;
Junio C Hamanoefd02012006-06-06 08:46:23 -0700454 if (argc <= i)
455 die("Which directory?");
456 if (output_directory)
457 die("Two output directories?");
458 output_directory = argv[i];
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200459 }
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700460 else if (!strcmp(argv[i], "--signoff") ||
461 !strcmp(argv[i], "-s")) {
Eric W. Biederman6c4cca12006-06-12 13:31:38 -0600462 const char *committer;
463 const char *endpos;
Eric W. Biederman6c4cca12006-06-12 13:31:38 -0600464 committer = git_committer_info(1);
465 endpos = strchr(committer, '>');
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700466 if (!endpos)
467 die("bogos committer info %s\n", committer);
468 add_signoff = xmalloc(endpos - committer + 2);
469 memcpy(add_signoff, committer, endpos - committer + 1);
470 add_signoff[endpos - committer + 1] = 0;
471 }
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200472 else if (!strcmp(argv[i], "--attach"))
473 rev.mime_boundary = git_version_string;
474 else if (!strncmp(argv[i], "--attach=", 9))
475 rev.mime_boundary = argv[i] + 9;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200476 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
477 ignore_if_in_upstream = 1;
Josh Triplettcc35de82006-07-14 17:49:04 -0700478 else if (!strcmp(argv[i], "--thread"))
479 thread = 1;
Josh Triplettda566452006-07-14 17:49:08 -0700480 else if (!strncmp(argv[i], "--in-reply-to=", 14))
481 in_reply_to = argv[i] + 14;
482 else if (!strcmp(argv[i], "--in-reply-to")) {
483 i++;
484 if (i == argc)
485 die("Need a Message-Id for --in-reply-to");
486 in_reply_to = argv[i];
487 }
Junio C Hamano03eeaea2007-01-17 11:12:03 -0800488 else if (!strncmp(argv[i], "--suffix=", 9))
489 fmt_patch_suffix = argv[i] + 9;
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200490 else
Johannes Schindelin24484822006-05-05 03:33:32 +0200491 argv[j++] = argv[i];
Johannes Schindelin0377db72006-05-05 01:16:40 +0200492 }
Johannes Schindelin24484822006-05-05 03:33:32 +0200493 argc = j;
494
Junio C Hamanoadd5c8a2006-05-26 11:30:49 -0700495 if (start_number < 0)
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200496 start_number = 1;
Junio C Hamano63b398a2006-05-28 09:23:29 -0700497 if (numbered && keep_subject)
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200498 die ("-n and -k are mutually exclusive.");
499
Johannes Schindelin24484822006-05-05 03:33:32 +0200500 argc = setup_revisions(argc, argv, &rev, "HEAD");
501 if (argc > 1)
502 die ("unrecognized argument: %s", argv[1]);
Johannes Schindelin0377db72006-05-05 01:16:40 +0200503
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300504 if (!rev.diffopt.output_format)
Junio C Hamanoc261e432007-01-17 13:51:44 -0800505 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300506
Junio C Hamanoe47f3062007-01-17 14:32:52 -0800507 if (!rev.diffopt.text)
508 rev.diffopt.binary = 1;
509
Jeff King90f70a92007-01-22 22:38:28 -0500510 if (!output_directory && !use_stdout)
Matthias Lederhofer77e565d2006-09-28 21:55:35 +0200511 output_directory = prefix;
512
Junio C Hamanoefd02012006-06-06 08:46:23 -0700513 if (output_directory) {
514 if (use_stdout)
515 die("standard output, or directory, which one?");
516 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
517 die("Could not create directory %s",
518 output_directory);
519 }
520
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700521 if (rev.pending.nr == 1) {
Junio C Hamano7c496282007-01-17 13:35:13 -0800522 if (rev.max_count < 0) {
523 rev.pending.objects[0].item->flags |= UNINTERESTING;
524 add_head(&rev);
525 }
526 /* Otherwise, it is "format-patch -22 HEAD", and
527 * get_revision() would return only the specified count.
528 */
Johannes Schindeline686eb92006-05-06 22:56:38 +0200529 }
530
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200531 if (ignore_if_in_upstream)
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700532 get_patch_ids(&rev, &patch_id_opts, prefix);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200533
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200534 if (!use_stdout)
535 realstdout = fdopen(dup(1), "w");
536
Junio C Hamano91efcf62006-04-21 13:19:58 -0700537 prepare_revision_walk(&rev);
538 while ((commit = get_revision(&rev)) != NULL) {
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200539 unsigned char sha1[20];
540
Johannes Schindelin0377db72006-05-05 01:16:40 +0200541 /* ignore merges */
542 if (commit->parents && commit->parents->next)
543 continue;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200544
545 if (ignore_if_in_upstream &&
546 !get_patch_id(commit, &patch_id_opts, sha1) &&
547 lookup_object(sha1))
548 continue;
549
Junio C Hamano91efcf62006-04-21 13:19:58 -0700550 nr++;
Jonas Fonseca83572c12006-08-26 16:16:18 +0200551 list = xrealloc(list, nr * sizeof(list[0]));
Junio C Hamano91efcf62006-04-21 13:19:58 -0700552 list[nr - 1] = commit;
553 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200554 total = nr;
Johannes Schindelin596524b2006-05-05 04:30:52 +0200555 if (numbered)
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200556 rev.total = total + start_number - 1;
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700557 rev.add_signoff = add_signoff;
Josh Triplettda566452006-07-14 17:49:08 -0700558 rev.ref_message_id = in_reply_to;
Junio C Hamano91efcf62006-04-21 13:19:58 -0700559 while (0 <= --nr) {
560 int shown;
561 commit = list[nr];
Junio C Hamanoadd5c8a2006-05-26 11:30:49 -0700562 rev.nr = total - nr + (start_number - 1);
Josh Triplettd1566f72006-07-14 17:48:51 -0700563 /* Make the second and subsequent mails replies to the first */
Josh Triplettcc35de82006-07-14 17:49:04 -0700564 if (thread) {
565 if (nr == (total - 2)) {
566 strncpy(ref_message_id, message_id,
567 sizeof(ref_message_id));
568 ref_message_id[sizeof(ref_message_id)-1]='\0';
569 rev.ref_message_id = ref_message_id;
570 }
571 gen_message_id(message_id, sizeof(message_id),
572 sha1_to_hex(commit->object.sha1));
573 rev.message_id = message_id;
Josh Triplettd1566f72006-07-14 17:48:51 -0700574 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200575 if (!use_stdout)
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200576 reopen_stdout(commit, rev.nr, keep_subject);
Junio C Hamano91efcf62006-04-21 13:19:58 -0700577 shown = log_tree_commit(&rev, commit);
578 free(commit->buffer);
579 commit->buffer = NULL;
Junio C Hamanoadd5c8a2006-05-26 11:30:49 -0700580
581 /* We put one extra blank line between formatted
582 * patches and this flag is used by log-tree code
583 * to see if it needs to emit a LF before showing
584 * the log; when using one file per patch, we do
585 * not want the extra blank line.
586 */
587 if (!use_stdout)
588 rev.shown_one = 0;
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200589 if (shown) {
590 if (rev.mime_boundary)
591 printf("\n--%s%s--\n\n\n",
592 mime_boundary_leader,
593 rev.mime_boundary);
594 else
595 printf("-- \n%s\n\n", git_version_string);
596 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200597 if (!use_stdout)
598 fclose(stdout);
Junio C Hamano91efcf62006-04-21 13:19:58 -0700599 }
600 free(list);
601 return 0;
602}
603
Rene Scharfee8276332006-10-24 01:01:57 +0200604static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
605{
606 unsigned char sha1[20];
607 if (get_sha1(arg, sha1) == 0) {
608 struct commit *commit = lookup_commit_reference(sha1);
609 if (commit) {
610 commit->object.flags |= flags;
611 add_pending_object(revs, &commit->object, arg);
612 return 0;
613 }
614 }
615 return -1;
616}
617
618static const char cherry_usage[] =
619"git-cherry [-v] <upstream> [<head>] [<limit>]";
620int cmd_cherry(int argc, const char **argv, const char *prefix)
621{
622 struct rev_info revs;
623 struct diff_options patch_id_opts;
624 struct commit *commit;
625 struct commit_list *list = NULL;
626 const char *upstream;
627 const char *head = "HEAD";
628 const char *limit = NULL;
629 int verbose = 0;
630
631 if (argc > 1 && !strcmp(argv[1], "-v")) {
632 verbose = 1;
633 argc--;
634 argv++;
635 }
636
637 switch (argc) {
638 case 4:
639 limit = argv[3];
640 /* FALLTHROUGH */
641 case 3:
642 head = argv[2];
643 /* FALLTHROUGH */
644 case 2:
645 upstream = argv[1];
646 break;
647 default:
648 usage(cherry_usage);
649 }
650
651 init_revisions(&revs, prefix);
652 revs.diff = 1;
653 revs.combine_merges = 0;
654 revs.ignore_merges = 1;
655 revs.diffopt.recursive = 1;
656
657 if (add_pending_commit(head, &revs, 0))
658 die("Unknown commit %s", head);
659 if (add_pending_commit(upstream, &revs, UNINTERESTING))
660 die("Unknown commit %s", upstream);
661
662 /* Don't say anything if head and upstream are the same. */
663 if (revs.pending.nr == 2) {
664 struct object_array_entry *o = revs.pending.objects;
665 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
666 return 0;
667 }
668
669 get_patch_ids(&revs, &patch_id_opts, prefix);
670
671 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
672 die("Unknown commit %s", limit);
673
674 /* reverse the list of commits */
675 prepare_revision_walk(&revs);
676 while ((commit = get_revision(&revs)) != NULL) {
677 /* ignore merges */
678 if (commit->parents && commit->parents->next)
679 continue;
680
681 commit_list_insert(commit, &list);
682 }
683
684 while (list) {
685 unsigned char sha1[20];
686 char sign = '+';
687
688 commit = list->item;
689 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
690 lookup_object(sha1))
691 sign = '-';
692
693 if (verbose) {
694 static char buf[16384];
695 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
696 buf, sizeof(buf), 0, NULL, NULL, 0);
697 printf("%c %s %s\n", sign,
698 sha1_to_hex(commit->object.sha1), buf);
699 }
700 else {
701 printf("%c %s\n", sign,
702 sha1_to_hex(commit->object.sha1));
703 }
704
705 list = list->next;
706 }
707
708 return 0;
709}