2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
18 static int default_show_root
= 1;
20 /* this is in builtin-diff.c */
21 void add_head(struct rev_info
*revs
);
23 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
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
);
33 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
35 struct object
*obj
= parse_object(sha1
);
38 add_name_decoration("", refname
, obj
);
39 while (obj
->type
== OBJ_TAG
) {
40 obj
= ((struct tag
*)obj
)->tagged
;
43 add_name_decoration("tag: ", refname
, obj
);
48 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
54 rev
->abbrev
= DEFAULT_ABBREV
;
55 rev
->commit_format
= CMIT_FMT_DEFAULT
;
56 rev
->verbose_header
= 1;
57 rev
->show_root_diff
= default_show_root
;
58 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
59 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
60 rev
->always_show_header
= 0;
61 if (rev
->diffopt
.follow_renames
) {
62 rev
->always_show_header
= 0;
63 if (rev
->diffopt
.nr_paths
!= 1)
64 usage("git logs can only follow renames on one pathname at a time");
66 for (i
= 1; i
< argc
; i
++) {
67 const char *arg
= argv
[i
];
68 if (!strcmp(arg
, "--decorate")) {
70 for_each_ref(add_ref_decoration
, NULL
);
73 die("unrecognized argument: %s", arg
);
77 static int cmd_log_walk(struct rev_info
*rev
)
79 struct commit
*commit
;
81 prepare_revision_walk(rev
);
82 while ((commit
= get_revision(rev
)) != NULL
) {
83 log_tree_commit(rev
, commit
);
84 if (!rev
->reflog_info
) {
85 /* we allow cycles in reflog ancestry */
87 commit
->buffer
= NULL
;
89 free_commit_list(commit
->parents
);
90 commit
->parents
= NULL
;
95 static int git_log_config(const char *var
, const char *value
)
97 if (!strcmp(var
, "log.showroot")) {
98 default_show_root
= git_config_bool(var
, value
);
101 return git_diff_ui_config(var
, value
);
104 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
108 git_config(git_log_config
);
109 init_revisions(&rev
, prefix
);
111 rev
.diffopt
.recursive
= 1;
112 rev
.simplify_history
= 0;
113 cmd_log_init(argc
, argv
, prefix
, &rev
);
114 if (!rev
.diffopt
.output_format
)
115 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
116 return cmd_log_walk(&rev
);
119 static int show_object(const unsigned char *sha1
, int suppress_header
)
122 enum object_type type
;
123 char *buf
= read_sha1_file(sha1
, &type
, &size
);
127 return error("Could not read object %s", sha1_to_hex(sha1
));
130 while (offset
< size
&& buf
[offset
++] != '\n') {
131 int new_offset
= offset
;
132 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
138 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
143 static int show_tree_object(const unsigned char *sha1
,
144 const char *base
, int baselen
,
145 const char *pathname
, unsigned mode
, int stage
)
147 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
151 int cmd_show(int argc
, const char **argv
, const char *prefix
)
154 struct object_array_entry
*objects
;
155 int i
, count
, ret
= 0;
157 git_config(git_log_config
);
158 init_revisions(&rev
, prefix
);
160 rev
.diffopt
.recursive
= 1;
161 rev
.combine_merges
= 1;
162 rev
.dense_combined_merges
= 1;
163 rev
.always_show_header
= 1;
164 rev
.ignore_merges
= 0;
166 cmd_log_init(argc
, argv
, prefix
, &rev
);
168 count
= rev
.pending
.nr
;
169 objects
= rev
.pending
.objects
;
170 for (i
= 0; i
< count
&& !ret
; i
++) {
171 struct object
*o
= objects
[i
].item
;
172 const char *name
= objects
[i
].name
;
175 ret
= show_object(o
->sha1
, 0);
178 struct tag
*t
= (struct tag
*)o
;
180 printf("%stag %s%s\n\n",
181 diff_get_color(rev
.diffopt
.color_diff
,
184 diff_get_color(rev
.diffopt
.color_diff
,
186 ret
= show_object(o
->sha1
, 1);
187 objects
[i
].item
= (struct object
*)t
->tagged
;
192 printf("%stree %s%s\n\n",
193 diff_get_color(rev
.diffopt
.color_diff
,
196 diff_get_color(rev
.diffopt
.color_diff
,
198 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
202 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
203 rev
.pending
.objects
= NULL
;
204 add_object_array(o
, name
, &rev
.pending
);
205 ret
= cmd_log_walk(&rev
);
208 ret
= error("Unknown type: %d", o
->type
);
216 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
218 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
222 git_config(git_log_config
);
223 init_revisions(&rev
, prefix
);
224 init_reflog_walk(&rev
.reflog_info
);
225 rev
.abbrev_commit
= 1;
226 rev
.verbose_header
= 1;
227 cmd_log_init(argc
, argv
, prefix
, &rev
);
230 * This means that we override whatever commit format the user gave
231 * on the cmd line. Sad, but cmd_log_init() currently doesn't
232 * allow us to set a different default.
234 rev
.commit_format
= CMIT_FMT_ONELINE
;
235 rev
.always_show_header
= 1;
238 * We get called through "git reflog", so unlike the other log
239 * routines, we need to set up our pager manually..
243 return cmd_log_walk(&rev
);
246 int cmd_log(int argc
, const char **argv
, const char *prefix
)
250 git_config(git_log_config
);
251 init_revisions(&rev
, prefix
);
252 rev
.always_show_header
= 1;
253 cmd_log_init(argc
, argv
, prefix
, &rev
);
254 return cmd_log_walk(&rev
);
258 #define FORMAT_PATCH_NAME_MAX 64
260 static int istitlechar(char c
)
262 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
263 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
266 static char *extra_headers
= NULL
;
267 static int extra_headers_size
= 0;
268 static const char *fmt_patch_subject_prefix
= "PATCH";
269 static const char *fmt_patch_suffix
= ".patch";
271 static int git_format_config(const char *var
, const char *value
)
273 if (!strcmp(var
, "format.headers")) {
277 die("format.headers without value");
279 extra_headers_size
+= len
+ 1;
280 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
281 extra_headers
[extra_headers_size
- len
- 1] = 0;
282 strcat(extra_headers
, value
);
285 if (!strcmp(var
, "format.suffix")) {
287 die("format.suffix without value");
288 fmt_patch_suffix
= xstrdup(value
);
291 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
294 if (!strcmp(var
, "format.subjectprefix")) {
296 die("format.subjectprefix without value");
297 fmt_patch_subject_prefix
= xstrdup(value
);
301 return git_log_config(var
, value
);
305 static FILE *realstdout
= NULL
;
306 static const char *output_directory
= NULL
;
308 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
,
311 char filename
[PATH_MAX
];
314 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
316 if (output_directory
) {
317 if (strlen(output_directory
) >=
318 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
319 return error("name of output directory is too long");
320 strlcpy(filename
, output_directory
, sizeof(filename
) - suffix_len
);
321 len
= strlen(filename
);
322 if (filename
[len
- 1] != '/')
323 filename
[len
++] = '/';
326 if (numbered_files
) {
327 sprintf(filename
+ len
, "%d", nr
);
328 len
= strlen(filename
);
331 sprintf(filename
+ len
, "%04d", nr
);
332 len
= strlen(filename
);
334 sol
= strstr(commit
->buffer
, "\n\n");
339 /* strip [PATCH] or [PATCH blabla] */
340 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
341 char *eos
= strchr(sol
+ 6, ']');
343 while (isspace(*eos
))
350 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
351 len
< sizeof(filename
) - suffix_len
&&
352 sol
[j
] && sol
[j
] != '\n';
354 if (istitlechar(sol
[j
])) {
356 filename
[len
++] = '-';
359 filename
[len
++] = sol
[j
];
361 while (sol
[j
+ 1] == '.')
366 while (filename
[len
- 1] == '.'
367 || filename
[len
- 1] == '-')
371 if (len
+ suffix_len
>= sizeof(filename
))
372 return error("Patch pathname too long");
373 strcpy(filename
+ len
, fmt_patch_suffix
);
376 fprintf(realstdout
, "%s\n", filename
);
377 if (freopen(filename
, "w", stdout
) == NULL
)
378 return error("Cannot open patch file %s",filename
);
383 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
385 struct rev_info check_rev
;
386 struct commit
*commit
;
387 struct object
*o1
, *o2
;
388 unsigned flags1
, flags2
;
390 if (rev
->pending
.nr
!= 2)
391 die("Need exactly one range.");
393 o1
= rev
->pending
.objects
[0].item
;
395 o2
= rev
->pending
.objects
[1].item
;
398 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
403 /* given a range a..b get all patch ids for b..a */
404 init_revisions(&check_rev
, prefix
);
405 o1
->flags
^= UNINTERESTING
;
406 o2
->flags
^= UNINTERESTING
;
407 add_pending_object(&check_rev
, o1
, "o1");
408 add_pending_object(&check_rev
, o2
, "o2");
409 prepare_revision_walk(&check_rev
);
411 while ((commit
= get_revision(&check_rev
)) != NULL
) {
413 if (commit
->parents
&& commit
->parents
->next
)
416 add_commit_patch_id(commit
, ids
);
419 /* reset for next revision walk */
420 clear_commit_marks((struct commit
*)o1
,
421 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
422 clear_commit_marks((struct commit
*)o2
,
423 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
428 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
430 const char *committer
= git_committer_info(-1);
431 const char *email_start
= strrchr(committer
, '<');
432 const char *email_end
= strrchr(committer
, '>');
433 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
434 die("Could not extract email from committer identity.");
435 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
436 (unsigned long) time(NULL
),
437 (int)(email_end
- email_start
- 1), email_start
+ 1);
440 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
442 struct commit
*commit
;
443 struct commit
**list
= NULL
;
445 int nr
= 0, total
, i
, j
;
448 int start_number
= -1;
449 int keep_subject
= 0;
450 int numbered_files
= 0; /* _just_ numbers */
451 int subject_prefix
= 0;
452 int ignore_if_in_upstream
= 0;
454 const char *in_reply_to
= NULL
;
455 struct patch_ids ids
;
456 char *add_signoff
= NULL
;
457 char message_id
[1024];
458 char ref_message_id
[1024];
460 git_config(git_format_config
);
461 init_revisions(&rev
, prefix
);
462 rev
.commit_format
= CMIT_FMT_EMAIL
;
463 rev
.verbose_header
= 1;
465 rev
.combine_merges
= 0;
466 rev
.ignore_merges
= 1;
467 rev
.diffopt
.msg_sep
= "";
468 rev
.diffopt
.recursive
= 1;
470 rev
.subject_prefix
= fmt_patch_subject_prefix
;
471 rev
.extra_headers
= extra_headers
;
474 * Parse the arguments before setup_revisions(), or something
475 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
476 * possibly a valid SHA1.
478 for (i
= 1, j
= 1; i
< argc
; i
++) {
479 if (!strcmp(argv
[i
], "--stdout"))
481 else if (!strcmp(argv
[i
], "-n") ||
482 !strcmp(argv
[i
], "--numbered"))
484 else if (!prefixcmp(argv
[i
], "--start-number="))
485 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
486 else if (!strcmp(argv
[i
], "--numbered-files"))
488 else if (!strcmp(argv
[i
], "--start-number")) {
491 die("Need a number for --start-number");
492 start_number
= strtol(argv
[i
], NULL
, 10);
494 else if (!strcmp(argv
[i
], "-k") ||
495 !strcmp(argv
[i
], "--keep-subject")) {
499 else if (!strcmp(argv
[i
], "--output-directory") ||
500 !strcmp(argv
[i
], "-o")) {
503 die("Which directory?");
504 if (output_directory
)
505 die("Two output directories?");
506 output_directory
= argv
[i
];
508 else if (!strcmp(argv
[i
], "--signoff") ||
509 !strcmp(argv
[i
], "-s")) {
510 const char *committer
;
512 committer
= git_committer_info(1);
513 endpos
= strchr(committer
, '>');
515 die("bogos committer info %s\n", committer
);
516 add_signoff
= xmalloc(endpos
- committer
+ 2);
517 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
518 add_signoff
[endpos
- committer
+ 1] = 0;
520 else if (!strcmp(argv
[i
], "--attach")) {
521 rev
.mime_boundary
= git_version_string
;
524 else if (!prefixcmp(argv
[i
], "--attach=")) {
525 rev
.mime_boundary
= argv
[i
] + 9;
528 else if (!strcmp(argv
[i
], "--inline")) {
529 rev
.mime_boundary
= git_version_string
;
532 else if (!prefixcmp(argv
[i
], "--inline=")) {
533 rev
.mime_boundary
= argv
[i
] + 9;
536 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
537 ignore_if_in_upstream
= 1;
538 else if (!strcmp(argv
[i
], "--thread"))
540 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
541 in_reply_to
= argv
[i
] + 14;
542 else if (!strcmp(argv
[i
], "--in-reply-to")) {
545 die("Need a Message-Id for --in-reply-to");
546 in_reply_to
= argv
[i
];
547 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
549 rev
.subject_prefix
= argv
[i
] + 17;
550 } else if (!prefixcmp(argv
[i
], "--suffix="))
551 fmt_patch_suffix
= argv
[i
] + 9;
557 if (start_number
< 0)
559 if (numbered
&& keep_subject
)
560 die ("-n and -k are mutually exclusive.");
561 if (keep_subject
&& subject_prefix
)
562 die ("--subject-prefix and -k are mutually exclusive.");
563 if (numbered_files
&& use_stdout
)
564 die ("--numbered-files and --stdout are mutually exclusive.");
566 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
568 die ("unrecognized argument: %s", argv
[1]);
570 if (!rev
.diffopt
.output_format
)
571 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
573 if (!rev
.diffopt
.text
)
574 rev
.diffopt
.binary
= 1;
576 if (!output_directory
&& !use_stdout
)
577 output_directory
= prefix
;
579 if (output_directory
) {
581 die("standard output, or directory, which one?");
582 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
583 die("Could not create directory %s",
587 if (rev
.pending
.nr
== 1) {
588 if (rev
.max_count
< 0) {
589 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
592 /* Otherwise, it is "format-patch -22 HEAD", and
593 * get_revision() would return only the specified count.
597 if (ignore_if_in_upstream
)
598 get_patch_ids(&rev
, &ids
, prefix
);
601 realstdout
= xfdopen(xdup(1), "w");
603 prepare_revision_walk(&rev
);
604 while ((commit
= get_revision(&rev
)) != NULL
) {
606 if (commit
->parents
&& commit
->parents
->next
)
609 if (ignore_if_in_upstream
&&
610 has_commit_patch_id(commit
, &ids
))
614 list
= xrealloc(list
, nr
* sizeof(list
[0]));
615 list
[nr
- 1] = commit
;
619 rev
.total
= total
+ start_number
- 1;
620 rev
.add_signoff
= add_signoff
;
621 rev
.ref_message_id
= in_reply_to
;
625 rev
.nr
= total
- nr
+ (start_number
- 1);
626 /* Make the second and subsequent mails replies to the first */
628 if (nr
== (total
- 2)) {
629 strncpy(ref_message_id
, message_id
,
630 sizeof(ref_message_id
));
631 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
632 rev
.ref_message_id
= ref_message_id
;
634 gen_message_id(message_id
, sizeof(message_id
),
635 sha1_to_hex(commit
->object
.sha1
));
636 rev
.message_id
= message_id
;
639 if (reopen_stdout(commit
, rev
.nr
, keep_subject
,
641 die("Failed to create output files");
642 shown
= log_tree_commit(&rev
, commit
);
643 free(commit
->buffer
);
644 commit
->buffer
= NULL
;
646 /* We put one extra blank line between formatted
647 * patches and this flag is used by log-tree code
648 * to see if it needs to emit a LF before showing
649 * the log; when using one file per patch, we do
650 * not want the extra blank line.
655 if (rev
.mime_boundary
)
656 printf("\n--%s%s--\n\n\n",
657 mime_boundary_leader
,
660 printf("-- \n%s\n\n", git_version_string
);
666 if (ignore_if_in_upstream
)
667 free_patch_ids(&ids
);
671 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
673 unsigned char sha1
[20];
674 if (get_sha1(arg
, sha1
) == 0) {
675 struct commit
*commit
= lookup_commit_reference(sha1
);
677 commit
->object
.flags
|= flags
;
678 add_pending_object(revs
, &commit
->object
, arg
);
685 static const char cherry_usage
[] =
686 "git-cherry [-v] <upstream> [<head>] [<limit>]";
687 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
689 struct rev_info revs
;
690 struct patch_ids ids
;
691 struct commit
*commit
;
692 struct commit_list
*list
= NULL
;
693 const char *upstream
;
694 const char *head
= "HEAD";
695 const char *limit
= NULL
;
698 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
718 init_revisions(&revs
, prefix
);
720 revs
.combine_merges
= 0;
721 revs
.ignore_merges
= 1;
722 revs
.diffopt
.recursive
= 1;
724 if (add_pending_commit(head
, &revs
, 0))
725 die("Unknown commit %s", head
);
726 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
727 die("Unknown commit %s", upstream
);
729 /* Don't say anything if head and upstream are the same. */
730 if (revs
.pending
.nr
== 2) {
731 struct object_array_entry
*o
= revs
.pending
.objects
;
732 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
736 get_patch_ids(&revs
, &ids
, prefix
);
738 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
739 die("Unknown commit %s", limit
);
741 /* reverse the list of commits */
742 prepare_revision_walk(&revs
);
743 while ((commit
= get_revision(&revs
)) != NULL
) {
745 if (commit
->parents
&& commit
->parents
->next
)
748 commit_list_insert(commit
, &list
);
755 if (has_commit_patch_id(commit
, &ids
))
760 unsigned long buflen
= 0;
761 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
762 &buf
, &buflen
, 0, NULL
, NULL
, 0);
763 printf("%c %s %s\n", sign
,
764 sha1_to_hex(commit
->object
.sha1
), buf
);
768 printf("%c %s\n", sign
,
769 sha1_to_hex(commit
->object
.sha1
));
775 free_patch_ids(&ids
);