blob: a59b4acef1bc45f07f15e10c0501aa23de3eed7d [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 Torvalds70827b12006-04-21 10:27:34 -070014
Peter Baumann0f03ca92006-11-23 10:36:33 +010015static int default_show_root = 1;
16
Johannes Schindeline686eb92006-05-06 22:56:38 +020017/* this is in builtin-diff.c */
18void add_head(struct rev_info *revs);
19
Linus Torvaldsa633fca2006-07-28 22:44:25 -070020static void cmd_log_init(int argc, const char **argv, const char *prefix,
Linus Torvalds70827b12006-04-21 10:27:34 -070021 struct rev_info *rev)
22{
Junio C Hamano52883fb2006-12-25 11:48:35 -080023 int i;
24
Linus Torvalds70827b12006-04-21 10:27:34 -070025 rev->abbrev = DEFAULT_ABBREV;
26 rev->commit_format = CMIT_FMT_DEFAULT;
27 rev->verbose_header = 1;
Peter Baumann0f03ca92006-11-23 10:36:33 +010028 rev->show_root_diff = default_show_root;
Linus Torvalds70827b12006-04-21 10:27:34 -070029 argc = setup_revisions(argc, argv, rev, "HEAD");
Timo Hirvonen9dafea22006-06-25 15:39:35 +030030 if (rev->diffopt.pickaxe || rev->diffopt.filter)
31 rev->always_show_header = 0;
Junio C Hamano52883fb2006-12-25 11:48:35 -080032 for (i = 1; i < argc; i++) {
33 const char *arg = argv[i];
34 if (!strncmp(arg, "--encoding=", 11)) {
35 arg += 11;
Junio C Hamano52883fb2006-12-25 11:48:35 -080036 if (strcmp(arg, "none"))
Junio C Hamanod2c11a32006-12-27 16:41:33 -080037 git_log_output_encoding = strdup(arg);
Junio C Hamano52883fb2006-12-25 11:48:35 -080038 else
Junio C Hamanod2c11a32006-12-27 16:41:33 -080039 git_log_output_encoding = "";
Junio C Hamano52883fb2006-12-25 11:48:35 -080040 }
41 else
42 die("unrecognized argument: %s", arg);
43 }
Timo Hirvonen9dafea22006-06-25 15:39:35 +030044}
45
46static int cmd_log_walk(struct rev_info *rev)
47{
48 struct commit *commit;
Linus Torvalds70827b12006-04-21 10:27:34 -070049
50 prepare_revision_walk(rev);
Linus Torvalds70827b12006-04-21 10:27:34 -070051 while ((commit = get_revision(rev)) != NULL) {
52 log_tree_commit(rev, commit);
53 free(commit->buffer);
54 commit->buffer = NULL;
Linus Torvaldscb115742006-06-17 18:47:58 -070055 free_commit_list(commit->parents);
56 commit->parents = NULL;
Linus Torvalds70827b12006-04-21 10:27:34 -070057 }
58 return 0;
59}
60
Peter Baumann0f03ca92006-11-23 10:36:33 +010061static int git_log_config(const char *var, const char *value)
62{
63 if (!strcmp(var, "log.showroot")) {
64 default_show_root = git_config_bool(var, value);
65 return 0;
66 }
67 return git_diff_ui_config(var, value);
68}
69
Linus Torvaldsa633fca2006-07-28 22:44:25 -070070int cmd_whatchanged(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -070071{
72 struct rev_info rev;
73
Peter Baumann0f03ca92006-11-23 10:36:33 +010074 git_config(git_log_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -070075 init_revisions(&rev, prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -070076 rev.diff = 1;
77 rev.diffopt.recursive = 1;
Linus Torvalds92024342006-06-11 10:57:35 -070078 rev.simplify_history = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -070079 cmd_log_init(argc, argv, prefix, &rev);
Timo Hirvonen9dafea22006-06-25 15:39:35 +030080 if (!rev.diffopt.output_format)
81 rev.diffopt.output_format = DIFF_FORMAT_RAW;
82 return cmd_log_walk(&rev);
Linus Torvalds70827b12006-04-21 10:27:34 -070083}
84
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +010085static int show_object(const unsigned char *sha1, int suppress_header)
86{
87 unsigned long size;
88 char type[20];
89 char *buf = read_sha1_file(sha1, type, &size);
90 int offset = 0;
91
92 if (!buf)
93 return error("Could not read object %s", sha1_to_hex(sha1));
94
95 if (suppress_header)
96 while (offset < size && buf[offset++] != '\n') {
97 int new_offset = offset;
98 while (new_offset < size && buf[new_offset++] != '\n')
99 ; /* do nothing */
100 offset = new_offset;
101 }
102
103 if (offset < size)
104 fwrite(buf + offset, size - offset, 1, stdout);
105 free(buf);
106 return 0;
107}
108
109static int show_tree_object(const unsigned char *sha1,
110 const char *base, int baselen,
111 const char *pathname, unsigned mode, int stage)
112{
113 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
114 return 0;
115}
116
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700117int cmd_show(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700118{
119 struct rev_info rev;
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +0100120 struct object_array_entry *objects;
121 int i, count, ret = 0;
Linus Torvalds70827b12006-04-21 10:27:34 -0700122
Peter Baumann0f03ca92006-11-23 10:36:33 +0100123 git_config(git_log_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700124 init_revisions(&rev, prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -0700125 rev.diff = 1;
126 rev.diffopt.recursive = 1;
127 rev.combine_merges = 1;
128 rev.dense_combined_merges = 1;
129 rev.always_show_header = 1;
130 rev.ignore_merges = 0;
131 rev.no_walk = 1;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700132 cmd_log_init(argc, argv, prefix, &rev);
Johannes Schindelin5d7eeee2006-12-14 11:31:05 +0100133
134 count = rev.pending.nr;
135 objects = rev.pending.objects;
136 for (i = 0; i < count && !ret; i++) {
137 struct object *o = objects[i].item;
138 const char *name = objects[i].name;
139 switch (o->type) {
140 case OBJ_BLOB:
141 ret = show_object(o->sha1, 0);
142 break;
143 case OBJ_TAG: {
144 struct tag *t = (struct tag *)o;
145
146 printf("%stag %s%s\n\n",
147 diff_get_color(rev.diffopt.color_diff,
148 DIFF_COMMIT),
149 t->tag,
150 diff_get_color(rev.diffopt.color_diff,
151 DIFF_RESET));
152 ret = show_object(o->sha1, 1);
153 objects[i].item = (struct object *)t->tagged;
154 i--;
155 break;
156 }
157 case OBJ_TREE:
158 printf("%stree %s%s\n\n",
159 diff_get_color(rev.diffopt.color_diff,
160 DIFF_COMMIT),
161 name,
162 diff_get_color(rev.diffopt.color_diff,
163 DIFF_RESET));
164 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
165 show_tree_object);
166 break;
167 case OBJ_COMMIT:
168 rev.pending.nr = rev.pending.alloc = 0;
169 rev.pending.objects = NULL;
170 add_object_array(o, name, &rev.pending);
171 ret = cmd_log_walk(&rev);
172 break;
173 default:
174 ret = error("Unknown type: %d", o->type);
175 }
176 }
177 free(objects);
178 return ret;
Linus Torvalds70827b12006-04-21 10:27:34 -0700179}
180
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700181int cmd_log(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700182{
183 struct rev_info rev;
184
Peter Baumann0f03ca92006-11-23 10:36:33 +0100185 git_config(git_log_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700186 init_revisions(&rev, prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -0700187 rev.always_show_header = 1;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700188 cmd_log_init(argc, argv, prefix, &rev);
Timo Hirvonen9dafea22006-06-25 15:39:35 +0300189 return cmd_log_walk(&rev);
Linus Torvalds70827b12006-04-21 10:27:34 -0700190}
Junio C Hamano91efcf62006-04-21 13:19:58 -0700191
Johannes Schindelin0377db72006-05-05 01:16:40 +0200192static int istitlechar(char c)
193{
194 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
195 (c >= '0' && c <= '9') || c == '.' || c == '_';
196}
197
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200198static char *extra_headers = NULL;
199static int extra_headers_size = 0;
200
201static int git_format_config(const char *var, const char *value)
202{
203 if (!strcmp(var, "format.headers")) {
204 int len = strlen(value);
205 extra_headers_size += len + 1;
Jonas Fonseca83572c12006-08-26 16:16:18 +0200206 extra_headers = xrealloc(extra_headers, extra_headers_size);
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200207 extra_headers[extra_headers_size - len - 1] = 0;
208 strcat(extra_headers, value);
209 return 0;
210 }
Andy Parkinsa159ca02006-12-13 09:13:28 +0000211 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
Ryan Andersonf3aafa42006-07-09 02:28:21 -0400212 return 0;
213 }
Peter Baumann0f03ca92006-11-23 10:36:33 +0100214 return git_log_config(var, value);
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200215}
216
217
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200218static FILE *realstdout = NULL;
Junio C Hamanoefd02012006-06-06 08:46:23 -0700219static const char *output_directory = NULL;
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200220
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200221static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
Johannes Schindelin0377db72006-05-05 01:16:40 +0200222{
223 char filename[1024];
224 char *sol;
Johannes Schindelin24484822006-05-05 03:33:32 +0200225 int len = 0;
Johannes Schindelin0377db72006-05-05 01:16:40 +0200226
Johannes Schindelin24484822006-05-05 03:33:32 +0200227 if (output_directory) {
Peter Eriksen817151e2006-06-24 16:01:25 +0200228 strlcpy(filename, output_directory, 1010);
Johannes Schindelin24484822006-05-05 03:33:32 +0200229 len = strlen(filename);
230 if (filename[len - 1] != '/')
231 filename[len++] = '/';
232 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200233
Johannes Schindelin24484822006-05-05 03:33:32 +0200234 sprintf(filename + len, "%04d", nr);
Johannes Schindelin0377db72006-05-05 01:16:40 +0200235 len = strlen(filename);
236
237 sol = strstr(commit->buffer, "\n\n");
238 if (sol) {
239 int j, space = 1;
240
241 sol += 2;
242 /* strip [PATCH] or [PATCH blabla] */
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200243 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
Johannes Schindelin0377db72006-05-05 01:16:40 +0200244 char *eos = strchr(sol + 6, ']');
245 if (eos) {
246 while (isspace(*eos))
247 eos++;
248 sol = eos;
249 }
250 }
251
252 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
253 if (istitlechar(sol[j])) {
254 if (space) {
255 filename[len++] = '-';
256 space = 0;
257 }
258 filename[len++] = sol[j];
259 if (sol[j] == '.')
260 while (sol[j + 1] == '.')
261 j++;
262 } else
263 space = 1;
264 }
265 while (filename[len - 1] == '.' || filename[len - 1] == '-')
266 len--;
267 }
268 strcpy(filename + len, ".txt");
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200269 fprintf(realstdout, "%s\n", filename);
Johannes Schindelin0377db72006-05-05 01:16:40 +0200270 freopen(filename, "w", stdout);
271}
272
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200273static int get_patch_id(struct commit *commit, struct diff_options *options,
274 unsigned char *sha1)
275{
Rene Scharfe2b603562006-10-26 18:52:39 +0200276 if (commit->parents)
277 diff_tree_sha1(commit->parents->item->object.sha1,
278 commit->object.sha1, "", options);
279 else
280 diff_root_tree_sha1(commit->object.sha1, "", options);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200281 diffcore_std(options);
282 return diff_flush_patch_id(options, sha1);
283}
284
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700285static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200286{
287 struct rev_info check_rev;
288 struct commit *commit;
289 struct object *o1, *o2;
290 unsigned flags1, flags2;
291 unsigned char sha1[20];
292
293 if (rev->pending.nr != 2)
294 die("Need exactly one range.");
295
296 o1 = rev->pending.objects[0].item;
297 flags1 = o1->flags;
298 o2 = rev->pending.objects[1].item;
299 flags2 = o2->flags;
300
301 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
302 die("Not a range.");
303
304 diff_setup(options);
305 options->recursive = 1;
306 if (diff_setup_done(options) < 0)
307 die("diff_setup_done failed");
308
309 /* given a range a..b get all patch ids for b..a */
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700310 init_revisions(&check_rev, prefix);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200311 o1->flags ^= UNINTERESTING;
312 o2->flags ^= UNINTERESTING;
313 add_pending_object(&check_rev, o1, "o1");
314 add_pending_object(&check_rev, o2, "o2");
315 prepare_revision_walk(&check_rev);
316
317 while ((commit = get_revision(&check_rev)) != NULL) {
318 /* ignore merges */
319 if (commit->parents && commit->parents->next)
320 continue;
321
322 if (!get_patch_id(commit, options, sha1))
323 created_object(sha1, xcalloc(1, sizeof(struct object)));
324 }
325
326 /* reset for next revision walk */
Johannes Schindelin81db0942006-06-27 22:38:04 +0200327 clear_commit_marks((struct commit *)o1,
328 SEEN | UNINTERESTING | SHOWN | ADDED);
329 clear_commit_marks((struct commit *)o2,
330 SEEN | UNINTERESTING | SHOWN | ADDED);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200331 o1->flags = flags1;
332 o2->flags = flags2;
333}
334
Josh Triplettd1566f72006-07-14 17:48:51 -0700335static void gen_message_id(char *dest, unsigned int length, char *base)
336{
337 const char *committer = git_committer_info(1);
338 const char *email_start = strrchr(committer, '<');
339 const char *email_end = strrchr(committer, '>');
340 if(!email_start || !email_end || email_start > email_end - 1)
341 die("Could not extract email from committer identity.");
Junio C Hamano76af0732006-07-14 22:47:53 -0700342 snprintf(dest, length, "%s.%lu.git.%.*s", base,
343 (unsigned long) time(NULL),
344 (int)(email_end - email_start - 1), email_start + 1);
Josh Triplettd1566f72006-07-14 17:48:51 -0700345}
346
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700347int cmd_format_patch(int argc, const char **argv, const char *prefix)
Junio C Hamano91efcf62006-04-21 13:19:58 -0700348{
349 struct commit *commit;
350 struct commit **list = NULL;
351 struct rev_info rev;
Johannes Schindelin24484822006-05-05 03:33:32 +0200352 int nr = 0, total, i, j;
Johannes Schindelin0377db72006-05-05 01:16:40 +0200353 int use_stdout = 0;
Johannes Schindelin596524b2006-05-05 04:30:52 +0200354 int numbered = 0;
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200355 int start_number = -1;
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200356 int keep_subject = 0;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200357 int ignore_if_in_upstream = 0;
Josh Triplettcc35de82006-07-14 17:49:04 -0700358 int thread = 0;
Junio C Hamano76af0732006-07-14 22:47:53 -0700359 const char *in_reply_to = NULL;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200360 struct diff_options patch_id_opts;
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700361 char *add_signoff = NULL;
Josh Triplettd1566f72006-07-14 17:48:51 -0700362 char message_id[1024];
363 char ref_message_id[1024];
Junio C Hamano91efcf62006-04-21 13:19:58 -0700364
Ramsay Jones07efc6a2006-08-04 22:01:30 +0100365 setup_ident();
Eric Wong97beb812006-07-07 03:10:45 -0700366 git_config(git_format_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700367 init_revisions(&rev, prefix);
Junio C Hamano91efcf62006-04-21 13:19:58 -0700368 rev.commit_format = CMIT_FMT_EMAIL;
369 rev.verbose_header = 1;
370 rev.diff = 1;
Junio C Hamano91efcf62006-04-21 13:19:58 -0700371 rev.combine_merges = 0;
372 rev.ignore_merges = 1;
Junio C Hamano27e1b122006-06-29 00:18:52 -0700373 rev.diffopt.msg_sep = "";
374 rev.diffopt.recursive = 1;
Junio C Hamano91efcf62006-04-21 13:19:58 -0700375
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200376 rev.extra_headers = extra_headers;
377
Johannes Schindelin24484822006-05-05 03:33:32 +0200378 /*
379 * Parse the arguments before setup_revisions(), or something
380 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
381 * possibly a valid SHA1.
382 */
383 for (i = 1, j = 1; i < argc; i++) {
384 if (!strcmp(argv[i], "--stdout"))
Johannes Schindelin0377db72006-05-05 01:16:40 +0200385 use_stdout = 1;
Johannes Schindelin596524b2006-05-05 04:30:52 +0200386 else if (!strcmp(argv[i], "-n") ||
387 !strcmp(argv[i], "--numbered"))
388 numbered = 1;
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200389 else if (!strncmp(argv[i], "--start-number=", 15))
390 start_number = strtol(argv[i] + 15, NULL, 10);
391 else if (!strcmp(argv[i], "--start-number")) {
392 i++;
393 if (i == argc)
394 die("Need a number for --start-number");
395 start_number = strtol(argv[i], NULL, 10);
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700396 }
397 else if (!strcmp(argv[i], "-k") ||
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200398 !strcmp(argv[i], "--keep-subject")) {
399 keep_subject = 1;
400 rev.total = -1;
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700401 }
Junio C Hamanoefd02012006-06-06 08:46:23 -0700402 else if (!strcmp(argv[i], "--output-directory") ||
403 !strcmp(argv[i], "-o")) {
Johannes Schindelin24484822006-05-05 03:33:32 +0200404 i++;
Junio C Hamanoefd02012006-06-06 08:46:23 -0700405 if (argc <= i)
406 die("Which directory?");
407 if (output_directory)
408 die("Two output directories?");
409 output_directory = argv[i];
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200410 }
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700411 else if (!strcmp(argv[i], "--signoff") ||
412 !strcmp(argv[i], "-s")) {
Eric W. Biederman6c4cca12006-06-12 13:31:38 -0600413 const char *committer;
414 const char *endpos;
Eric W. Biederman6c4cca12006-06-12 13:31:38 -0600415 committer = git_committer_info(1);
416 endpos = strchr(committer, '>');
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700417 if (!endpos)
418 die("bogos committer info %s\n", committer);
419 add_signoff = xmalloc(endpos - committer + 2);
420 memcpy(add_signoff, committer, endpos - committer + 1);
421 add_signoff[endpos - committer + 1] = 0;
422 }
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200423 else if (!strcmp(argv[i], "--attach"))
424 rev.mime_boundary = git_version_string;
425 else if (!strncmp(argv[i], "--attach=", 9))
426 rev.mime_boundary = argv[i] + 9;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200427 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
428 ignore_if_in_upstream = 1;
Josh Triplettcc35de82006-07-14 17:49:04 -0700429 else if (!strcmp(argv[i], "--thread"))
430 thread = 1;
Josh Triplettda566452006-07-14 17:49:08 -0700431 else if (!strncmp(argv[i], "--in-reply-to=", 14))
432 in_reply_to = argv[i] + 14;
433 else if (!strcmp(argv[i], "--in-reply-to")) {
434 i++;
435 if (i == argc)
436 die("Need a Message-Id for --in-reply-to");
437 in_reply_to = argv[i];
438 }
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200439 else
Johannes Schindelin24484822006-05-05 03:33:32 +0200440 argv[j++] = argv[i];
Johannes Schindelin0377db72006-05-05 01:16:40 +0200441 }
Johannes Schindelin24484822006-05-05 03:33:32 +0200442 argc = j;
443
Junio C Hamanoadd5c8a2006-05-26 11:30:49 -0700444 if (start_number < 0)
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200445 start_number = 1;
Junio C Hamano63b398a2006-05-28 09:23:29 -0700446 if (numbered && keep_subject)
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200447 die ("-n and -k are mutually exclusive.");
448
Johannes Schindelin24484822006-05-05 03:33:32 +0200449 argc = setup_revisions(argc, argv, &rev, "HEAD");
450 if (argc > 1)
451 die ("unrecognized argument: %s", argv[1]);
Johannes Schindelin0377db72006-05-05 01:16:40 +0200452
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300453 if (!rev.diffopt.output_format)
454 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
455
Matthias Lederhofer77e565d2006-09-28 21:55:35 +0200456 if (!output_directory)
457 output_directory = prefix;
458
Junio C Hamanoefd02012006-06-06 08:46:23 -0700459 if (output_directory) {
460 if (use_stdout)
461 die("standard output, or directory, which one?");
462 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
463 die("Could not create directory %s",
464 output_directory);
465 }
466
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700467 if (rev.pending.nr == 1) {
468 rev.pending.objects[0].item->flags |= UNINTERESTING;
Johannes Schindeline686eb92006-05-06 22:56:38 +0200469 add_head(&rev);
470 }
471
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200472 if (ignore_if_in_upstream)
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700473 get_patch_ids(&rev, &patch_id_opts, prefix);
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200474
Johannes Schindelin81f3a182006-05-05 03:33:05 +0200475 if (!use_stdout)
476 realstdout = fdopen(dup(1), "w");
477
Junio C Hamano91efcf62006-04-21 13:19:58 -0700478 prepare_revision_walk(&rev);
479 while ((commit = get_revision(&rev)) != NULL) {
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200480 unsigned char sha1[20];
481
Johannes Schindelin0377db72006-05-05 01:16:40 +0200482 /* ignore merges */
483 if (commit->parents && commit->parents->next)
484 continue;
Johannes Schindelin9c6efa32006-06-25 03:52:01 +0200485
486 if (ignore_if_in_upstream &&
487 !get_patch_id(commit, &patch_id_opts, sha1) &&
488 lookup_object(sha1))
489 continue;
490
Junio C Hamano91efcf62006-04-21 13:19:58 -0700491 nr++;
Jonas Fonseca83572c12006-08-26 16:16:18 +0200492 list = xrealloc(list, nr * sizeof(list[0]));
Junio C Hamano91efcf62006-04-21 13:19:58 -0700493 list[nr - 1] = commit;
494 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200495 total = nr;
Johannes Schindelin596524b2006-05-05 04:30:52 +0200496 if (numbered)
Johannes Schindelinfa0f02d2006-05-25 23:55:11 +0200497 rev.total = total + start_number - 1;
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700498 rev.add_signoff = add_signoff;
Josh Triplettda566452006-07-14 17:49:08 -0700499 rev.ref_message_id = in_reply_to;
Junio C Hamano91efcf62006-04-21 13:19:58 -0700500 while (0 <= --nr) {
501 int shown;
502 commit = list[nr];
Junio C Hamanoadd5c8a2006-05-26 11:30:49 -0700503 rev.nr = total - nr + (start_number - 1);
Josh Triplettd1566f72006-07-14 17:48:51 -0700504 /* Make the second and subsequent mails replies to the first */
Josh Triplettcc35de82006-07-14 17:49:04 -0700505 if (thread) {
506 if (nr == (total - 2)) {
507 strncpy(ref_message_id, message_id,
508 sizeof(ref_message_id));
509 ref_message_id[sizeof(ref_message_id)-1]='\0';
510 rev.ref_message_id = ref_message_id;
511 }
512 gen_message_id(message_id, sizeof(message_id),
513 sha1_to_hex(commit->object.sha1));
514 rev.message_id = message_id;
Josh Triplettd1566f72006-07-14 17:48:51 -0700515 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200516 if (!use_stdout)
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200517 reopen_stdout(commit, rev.nr, keep_subject);
Junio C Hamano91efcf62006-04-21 13:19:58 -0700518 shown = log_tree_commit(&rev, commit);
519 free(commit->buffer);
520 commit->buffer = NULL;
Junio C Hamanoadd5c8a2006-05-26 11:30:49 -0700521
522 /* We put one extra blank line between formatted
523 * patches and this flag is used by log-tree code
524 * to see if it needs to emit a LF before showing
525 * the log; when using one file per patch, we do
526 * not want the extra blank line.
527 */
528 if (!use_stdout)
529 rev.shown_one = 0;
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200530 if (shown) {
531 if (rev.mime_boundary)
532 printf("\n--%s%s--\n\n\n",
533 mime_boundary_leader,
534 rev.mime_boundary);
535 else
536 printf("-- \n%s\n\n", git_version_string);
537 }
Johannes Schindelin0377db72006-05-05 01:16:40 +0200538 if (!use_stdout)
539 fclose(stdout);
Junio C Hamano91efcf62006-04-21 13:19:58 -0700540 }
541 free(list);
542 return 0;
543}
544
Rene Scharfee8276332006-10-24 01:01:57 +0200545static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
546{
547 unsigned char sha1[20];
548 if (get_sha1(arg, sha1) == 0) {
549 struct commit *commit = lookup_commit_reference(sha1);
550 if (commit) {
551 commit->object.flags |= flags;
552 add_pending_object(revs, &commit->object, arg);
553 return 0;
554 }
555 }
556 return -1;
557}
558
559static const char cherry_usage[] =
560"git-cherry [-v] <upstream> [<head>] [<limit>]";
561int cmd_cherry(int argc, const char **argv, const char *prefix)
562{
563 struct rev_info revs;
564 struct diff_options patch_id_opts;
565 struct commit *commit;
566 struct commit_list *list = NULL;
567 const char *upstream;
568 const char *head = "HEAD";
569 const char *limit = NULL;
570 int verbose = 0;
571
572 if (argc > 1 && !strcmp(argv[1], "-v")) {
573 verbose = 1;
574 argc--;
575 argv++;
576 }
577
578 switch (argc) {
579 case 4:
580 limit = argv[3];
581 /* FALLTHROUGH */
582 case 3:
583 head = argv[2];
584 /* FALLTHROUGH */
585 case 2:
586 upstream = argv[1];
587 break;
588 default:
589 usage(cherry_usage);
590 }
591
592 init_revisions(&revs, prefix);
593 revs.diff = 1;
594 revs.combine_merges = 0;
595 revs.ignore_merges = 1;
596 revs.diffopt.recursive = 1;
597
598 if (add_pending_commit(head, &revs, 0))
599 die("Unknown commit %s", head);
600 if (add_pending_commit(upstream, &revs, UNINTERESTING))
601 die("Unknown commit %s", upstream);
602
603 /* Don't say anything if head and upstream are the same. */
604 if (revs.pending.nr == 2) {
605 struct object_array_entry *o = revs.pending.objects;
606 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
607 return 0;
608 }
609
610 get_patch_ids(&revs, &patch_id_opts, prefix);
611
612 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
613 die("Unknown commit %s", limit);
614
615 /* reverse the list of commits */
616 prepare_revision_walk(&revs);
617 while ((commit = get_revision(&revs)) != NULL) {
618 /* ignore merges */
619 if (commit->parents && commit->parents->next)
620 continue;
621
622 commit_list_insert(commit, &list);
623 }
624
625 while (list) {
626 unsigned char sha1[20];
627 char sign = '+';
628
629 commit = list->item;
630 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
631 lookup_object(sha1))
632 sign = '-';
633
634 if (verbose) {
635 static char buf[16384];
636 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
637 buf, sizeof(buf), 0, NULL, NULL, 0);
638 printf("%c %s %s\n", sign,
639 sha1_to_hex(commit->object.sha1), buf);
640 }
641 else {
642 printf("%c %s\n", sign,
643 sha1_to_hex(commit->object.sha1));
644 }
645
646 list = list->next;
647 }
648
649 return 0;
650}