2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 #include "reflog-walk.h"
16 static int default_show_root
= 1;
18 /* this is in builtin-diff.c */
19 void add_head(struct rev_info
*revs
);
21 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
26 rev
->abbrev
= DEFAULT_ABBREV
;
27 rev
->commit_format
= CMIT_FMT_DEFAULT
;
28 rev
->verbose_header
= 1;
29 rev
->show_root_diff
= default_show_root
;
30 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
31 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
32 rev
->always_show_header
= 0;
33 for (i
= 1; i
< argc
; i
++) {
34 const char *arg
= argv
[i
];
35 if (!prefixcmp(arg
, "--encoding=")) {
37 if (strcmp(arg
, "none"))
38 git_log_output_encoding
= strdup(arg
);
40 git_log_output_encoding
= "";
43 die("unrecognized argument: %s", arg
);
47 static int cmd_log_walk(struct rev_info
*rev
)
49 struct commit
*commit
;
51 prepare_revision_walk(rev
);
52 while ((commit
= get_revision(rev
)) != NULL
) {
53 log_tree_commit(rev
, commit
);
54 if (!rev
->reflog_info
) {
55 /* we allow cycles in reflog ancestry */
57 commit
->buffer
= NULL
;
59 free_commit_list(commit
->parents
);
60 commit
->parents
= NULL
;
65 static int git_log_config(const char *var
, const char *value
)
67 if (!strcmp(var
, "log.showroot")) {
68 default_show_root
= git_config_bool(var
, value
);
71 return git_diff_ui_config(var
, value
);
74 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
78 git_config(git_log_config
);
79 init_revisions(&rev
, prefix
);
81 rev
.diffopt
.recursive
= 1;
82 rev
.simplify_history
= 0;
83 cmd_log_init(argc
, argv
, prefix
, &rev
);
84 if (!rev
.diffopt
.output_format
)
85 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
86 return cmd_log_walk(&rev
);
89 static int show_object(const unsigned char *sha1
, int suppress_header
)
93 char *buf
= read_sha1_file(sha1
, type
, &size
);
97 return error("Could not read object %s", sha1_to_hex(sha1
));
100 while (offset
< size
&& buf
[offset
++] != '\n') {
101 int new_offset
= offset
;
102 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
108 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
113 static int show_tree_object(const unsigned char *sha1
,
114 const char *base
, int baselen
,
115 const char *pathname
, unsigned mode
, int stage
)
117 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
121 int cmd_show(int argc
, const char **argv
, const char *prefix
)
124 struct object_array_entry
*objects
;
125 int i
, count
, ret
= 0;
127 git_config(git_log_config
);
128 init_revisions(&rev
, prefix
);
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;
136 cmd_log_init(argc
, argv
, prefix
, &rev
);
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
;
145 ret
= show_object(o
->sha1
, 0);
148 struct tag
*t
= (struct tag
*)o
;
150 printf("%stag %s%s\n\n",
151 diff_get_color(rev
.diffopt
.color_diff
,
154 diff_get_color(rev
.diffopt
.color_diff
,
156 ret
= show_object(o
->sha1
, 1);
157 objects
[i
].item
= (struct object
*)t
->tagged
;
162 printf("%stree %s%s\n\n",
163 diff_get_color(rev
.diffopt
.color_diff
,
166 diff_get_color(rev
.diffopt
.color_diff
,
168 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
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
);
178 ret
= error("Unknown type: %d", o
->type
);
186 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
188 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
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
);
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.
204 rev
.commit_format
= CMIT_FMT_ONELINE
;
205 rev
.always_show_header
= 1;
208 * We get called through "git reflog", so unlike the other log
209 * routines, we need to set up our pager manually..
213 return cmd_log_walk(&rev
);
216 int cmd_log(int argc
, const char **argv
, const char *prefix
)
220 git_config(git_log_config
);
221 init_revisions(&rev
, prefix
);
222 rev
.always_show_header
= 1;
223 cmd_log_init(argc
, argv
, prefix
, &rev
);
224 return cmd_log_walk(&rev
);
227 static int istitlechar(char c
)
229 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
230 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
233 static char *extra_headers
= NULL
;
234 static int extra_headers_size
= 0;
235 static const char *fmt_patch_suffix
= ".patch";
237 static int git_format_config(const char *var
, const char *value
)
239 if (!strcmp(var
, "format.headers")) {
243 die("format.headers without value");
245 extra_headers_size
+= len
+ 1;
246 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
247 extra_headers
[extra_headers_size
- len
- 1] = 0;
248 strcat(extra_headers
, value
);
251 if (!strcmp(var
, "format.suffix")) {
253 die("format.suffix without value");
254 fmt_patch_suffix
= xstrdup(value
);
257 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
260 return git_log_config(var
, value
);
264 static FILE *realstdout
= NULL
;
265 static const char *output_directory
= NULL
;
267 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
272 int suffix_len
= strlen(fmt_patch_suffix
) + 10; /* ., NUL and slop */
274 if (output_directory
) {
275 strlcpy(filename
, output_directory
, 1000);
276 len
= strlen(filename
);
277 if (filename
[len
- 1] != '/')
278 filename
[len
++] = '/';
281 sprintf(filename
+ len
, "%04d", nr
);
282 len
= strlen(filename
);
284 sol
= strstr(commit
->buffer
, "\n\n");
289 /* strip [PATCH] or [PATCH blabla] */
290 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
291 char *eos
= strchr(sol
+ 6, ']');
293 while (isspace(*eos
))
300 len
< sizeof(filename
) - suffix_len
&&
301 sol
[j
] && sol
[j
] != '\n';
303 if (istitlechar(sol
[j
])) {
305 filename
[len
++] = '-';
308 filename
[len
++] = sol
[j
];
310 while (sol
[j
+ 1] == '.')
315 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
318 strcpy(filename
+ len
, fmt_patch_suffix
);
319 fprintf(realstdout
, "%s\n", filename
);
320 freopen(filename
, "w", stdout
);
323 static int get_patch_id(struct commit
*commit
, struct diff_options
*options
,
327 diff_tree_sha1(commit
->parents
->item
->object
.sha1
,
328 commit
->object
.sha1
, "", options
);
330 diff_root_tree_sha1(commit
->object
.sha1
, "", options
);
331 diffcore_std(options
);
332 return diff_flush_patch_id(options
, sha1
);
335 static void get_patch_ids(struct rev_info
*rev
, struct diff_options
*options
, const char *prefix
)
337 struct rev_info check_rev
;
338 struct commit
*commit
;
339 struct object
*o1
, *o2
;
340 unsigned flags1
, flags2
;
341 unsigned char sha1
[20];
343 if (rev
->pending
.nr
!= 2)
344 die("Need exactly one range.");
346 o1
= rev
->pending
.objects
[0].item
;
348 o2
= rev
->pending
.objects
[1].item
;
351 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
355 options
->recursive
= 1;
356 if (diff_setup_done(options
) < 0)
357 die("diff_setup_done failed");
359 /* given a range a..b get all patch ids for b..a */
360 init_revisions(&check_rev
, prefix
);
361 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
);
367 while ((commit
= get_revision(&check_rev
)) != NULL
) {
369 if (commit
->parents
&& commit
->parents
->next
)
372 if (!get_patch_id(commit
, options
, sha1
))
373 created_object(sha1
, xcalloc(1, sizeof(struct object
)));
376 /* reset for next revision walk */
377 clear_commit_marks((struct commit
*)o1
,
378 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
379 clear_commit_marks((struct commit
*)o2
,
380 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
385 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
387 const char *committer
= git_committer_info(-1);
388 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.");
392 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
393 (unsigned long) time(NULL
),
394 (int)(email_end
- email_start
- 1), email_start
+ 1);
397 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
399 struct commit
*commit
;
400 struct commit
**list
= NULL
;
402 int nr
= 0, total
, i
, j
;
405 int start_number
= -1;
406 int keep_subject
= 0;
407 int ignore_if_in_upstream
= 0;
409 const char *in_reply_to
= NULL
;
410 struct diff_options patch_id_opts
;
411 char *add_signoff
= NULL
;
412 char message_id
[1024];
413 char ref_message_id
[1024];
415 git_config(git_format_config
);
416 init_revisions(&rev
, prefix
);
417 rev
.commit_format
= CMIT_FMT_EMAIL
;
418 rev
.verbose_header
= 1;
420 rev
.combine_merges
= 0;
421 rev
.ignore_merges
= 1;
422 rev
.diffopt
.msg_sep
= "";
423 rev
.diffopt
.recursive
= 1;
425 rev
.extra_headers
= extra_headers
;
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.
432 for (i
= 1, j
= 1; i
< argc
; i
++) {
433 if (!strcmp(argv
[i
], "--stdout"))
435 else if (!strcmp(argv
[i
], "-n") ||
436 !strcmp(argv
[i
], "--numbered"))
438 else if (!prefixcmp(argv
[i
], "--start-number="))
439 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
440 else if (!strcmp(argv
[i
], "--start-number")) {
443 die("Need a number for --start-number");
444 start_number
= strtol(argv
[i
], NULL
, 10);
446 else if (!strcmp(argv
[i
], "-k") ||
447 !strcmp(argv
[i
], "--keep-subject")) {
451 else if (!strcmp(argv
[i
], "--output-directory") ||
452 !strcmp(argv
[i
], "-o")) {
455 die("Which directory?");
456 if (output_directory
)
457 die("Two output directories?");
458 output_directory
= argv
[i
];
460 else if (!strcmp(argv
[i
], "--signoff") ||
461 !strcmp(argv
[i
], "-s")) {
462 const char *committer
;
464 committer
= git_committer_info(1);
465 endpos
= strchr(committer
, '>');
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;
472 else if (!strcmp(argv
[i
], "--attach"))
473 rev
.mime_boundary
= git_version_string
;
474 else if (!prefixcmp(argv
[i
], "--attach="))
475 rev
.mime_boundary
= argv
[i
] + 9;
476 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
477 ignore_if_in_upstream
= 1;
478 else if (!strcmp(argv
[i
], "--thread"))
480 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
481 in_reply_to
= argv
[i
] + 14;
482 else if (!strcmp(argv
[i
], "--in-reply-to")) {
485 die("Need a Message-Id for --in-reply-to");
486 in_reply_to
= argv
[i
];
488 else if (!prefixcmp(argv
[i
], "--suffix="))
489 fmt_patch_suffix
= argv
[i
] + 9;
495 if (start_number
< 0)
497 if (numbered
&& keep_subject
)
498 die ("-n and -k are mutually exclusive.");
500 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
502 die ("unrecognized argument: %s", argv
[1]);
504 if (!rev
.diffopt
.output_format
)
505 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
507 if (!rev
.diffopt
.text
)
508 rev
.diffopt
.binary
= 1;
510 if (!output_directory
&& !use_stdout
)
511 output_directory
= prefix
;
513 if (output_directory
) {
515 die("standard output, or directory, which one?");
516 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
517 die("Could not create directory %s",
521 if (rev
.pending
.nr
== 1) {
522 if (rev
.max_count
< 0) {
523 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
526 /* Otherwise, it is "format-patch -22 HEAD", and
527 * get_revision() would return only the specified count.
531 if (ignore_if_in_upstream
)
532 get_patch_ids(&rev
, &patch_id_opts
, prefix
);
535 realstdout
= fdopen(dup(1), "w");
537 prepare_revision_walk(&rev
);
538 while ((commit
= get_revision(&rev
)) != NULL
) {
539 unsigned char sha1
[20];
542 if (commit
->parents
&& commit
->parents
->next
)
545 if (ignore_if_in_upstream
&&
546 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
551 list
= xrealloc(list
, nr
* sizeof(list
[0]));
552 list
[nr
- 1] = commit
;
556 rev
.total
= total
+ start_number
- 1;
557 rev
.add_signoff
= add_signoff
;
558 rev
.ref_message_id
= in_reply_to
;
562 rev
.nr
= total
- nr
+ (start_number
- 1);
563 /* Make the second and subsequent mails replies to the first */
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
;
571 gen_message_id(message_id
, sizeof(message_id
),
572 sha1_to_hex(commit
->object
.sha1
));
573 rev
.message_id
= message_id
;
576 reopen_stdout(commit
, rev
.nr
, keep_subject
);
577 shown
= log_tree_commit(&rev
, commit
);
578 free(commit
->buffer
);
579 commit
->buffer
= NULL
;
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.
590 if (rev
.mime_boundary
)
591 printf("\n--%s%s--\n\n\n",
592 mime_boundary_leader
,
595 printf("-- \n%s\n\n", git_version_string
);
604 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
606 unsigned char sha1
[20];
607 if (get_sha1(arg
, sha1
) == 0) {
608 struct commit
*commit
= lookup_commit_reference(sha1
);
610 commit
->object
.flags
|= flags
;
611 add_pending_object(revs
, &commit
->object
, arg
);
618 static const char cherry_usage
[] =
619 "git-cherry [-v] <upstream> [<head>] [<limit>]";
620 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
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
;
631 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
651 init_revisions(&revs
, prefix
);
653 revs
.combine_merges
= 0;
654 revs
.ignore_merges
= 1;
655 revs
.diffopt
.recursive
= 1;
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
);
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)
669 get_patch_ids(&revs
, &patch_id_opts
, prefix
);
671 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
672 die("Unknown commit %s", limit
);
674 /* reverse the list of commits */
675 prepare_revision_walk(&revs
);
676 while ((commit
= get_revision(&revs
)) != NULL
) {
678 if (commit
->parents
&& commit
->parents
->next
)
681 commit_list_insert(commit
, &list
);
685 unsigned char sha1
[20];
689 if (!get_patch_id(commit
, &patch_id_opts
, sha1
) &&
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
);
701 printf("%c %s\n", sign
,
702 sha1_to_hex(commit
->object
.sha1
));