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 for (i
= 1; i
< argc
; i
++) {
62 const char *arg
= argv
[i
];
63 if (!strcmp(arg
, "--decorate")) {
65 for_each_ref(add_ref_decoration
, NULL
);
68 die("unrecognized argument: %s", arg
);
72 static int cmd_log_walk(struct rev_info
*rev
)
74 struct commit
*commit
;
76 prepare_revision_walk(rev
);
77 while ((commit
= get_revision(rev
)) != NULL
) {
78 log_tree_commit(rev
, commit
);
79 if (!rev
->reflog_info
) {
80 /* we allow cycles in reflog ancestry */
82 commit
->buffer
= NULL
;
84 free_commit_list(commit
->parents
);
85 commit
->parents
= NULL
;
90 static int git_log_config(const char *var
, const char *value
)
92 if (!strcmp(var
, "log.showroot")) {
93 default_show_root
= git_config_bool(var
, value
);
96 return git_diff_ui_config(var
, value
);
99 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
103 git_config(git_log_config
);
104 init_revisions(&rev
, prefix
);
106 rev
.diffopt
.recursive
= 1;
107 rev
.simplify_history
= 0;
108 cmd_log_init(argc
, argv
, prefix
, &rev
);
109 if (!rev
.diffopt
.output_format
)
110 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
111 return cmd_log_walk(&rev
);
114 static int show_object(const unsigned char *sha1
, int suppress_header
)
117 enum object_type type
;
118 char *buf
= read_sha1_file(sha1
, &type
, &size
);
122 return error("Could not read object %s", sha1_to_hex(sha1
));
125 while (offset
< size
&& buf
[offset
++] != '\n') {
126 int new_offset
= offset
;
127 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
133 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
138 static int show_tree_object(const unsigned char *sha1
,
139 const char *base
, int baselen
,
140 const char *pathname
, unsigned mode
, int stage
)
142 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
146 int cmd_show(int argc
, const char **argv
, const char *prefix
)
149 struct object_array_entry
*objects
;
150 int i
, count
, ret
= 0;
152 git_config(git_log_config
);
153 init_revisions(&rev
, prefix
);
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;
161 cmd_log_init(argc
, argv
, prefix
, &rev
);
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
;
170 ret
= show_object(o
->sha1
, 0);
173 struct tag
*t
= (struct tag
*)o
;
175 printf("%stag %s%s\n\n",
176 diff_get_color(rev
.diffopt
.color_diff
,
179 diff_get_color(rev
.diffopt
.color_diff
,
181 ret
= show_object(o
->sha1
, 1);
182 objects
[i
].item
= (struct object
*)t
->tagged
;
187 printf("%stree %s%s\n\n",
188 diff_get_color(rev
.diffopt
.color_diff
,
191 diff_get_color(rev
.diffopt
.color_diff
,
193 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
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
);
203 ret
= error("Unknown type: %d", o
->type
);
211 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
213 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
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
);
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.
229 rev
.commit_format
= CMIT_FMT_ONELINE
;
230 rev
.always_show_header
= 1;
233 * We get called through "git reflog", so unlike the other log
234 * routines, we need to set up our pager manually..
238 return cmd_log_walk(&rev
);
241 int cmd_log(int argc
, const char **argv
, const char *prefix
)
245 git_config(git_log_config
);
246 init_revisions(&rev
, prefix
);
247 rev
.always_show_header
= 1;
248 cmd_log_init(argc
, argv
, prefix
, &rev
);
249 return cmd_log_walk(&rev
);
253 #define FORMAT_PATCH_NAME_MAX 64
255 static int istitlechar(char c
)
257 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
258 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
261 static char *extra_headers
= NULL
;
262 static int extra_headers_size
= 0;
263 static const char *fmt_patch_suffix
= ".patch";
265 static int git_format_config(const char *var
, const char *value
)
267 if (!strcmp(var
, "format.headers")) {
271 die("format.headers without value");
273 extra_headers_size
+= len
+ 1;
274 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
275 extra_headers
[extra_headers_size
- len
- 1] = 0;
276 strcat(extra_headers
, value
);
279 if (!strcmp(var
, "format.suffix")) {
281 die("format.suffix without value");
282 fmt_patch_suffix
= xstrdup(value
);
285 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
288 return git_log_config(var
, value
);
292 static FILE *realstdout
= NULL
;
293 static const char *output_directory
= NULL
;
295 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
,
298 char filename
[PATH_MAX
];
301 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
303 if (output_directory
) {
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
);
308 len
= strlen(filename
);
309 if (filename
[len
- 1] != '/')
310 filename
[len
++] = '/';
313 if (numbered_files
) {
314 sprintf(filename
+ len
, "%d", nr
);
315 len
= strlen(filename
);
318 sprintf(filename
+ len
, "%04d", nr
);
319 len
= strlen(filename
);
321 sol
= strstr(commit
->buffer
, "\n\n");
326 /* strip [PATCH] or [PATCH blabla] */
327 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
328 char *eos
= strchr(sol
+ 6, ']');
330 while (isspace(*eos
))
337 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
338 len
< sizeof(filename
) - suffix_len
&&
339 sol
[j
] && sol
[j
] != '\n';
341 if (istitlechar(sol
[j
])) {
343 filename
[len
++] = '-';
346 filename
[len
++] = sol
[j
];
348 while (sol
[j
+ 1] == '.')
353 while (filename
[len
- 1] == '.'
354 || filename
[len
- 1] == '-')
358 if (len
+ suffix_len
>= sizeof(filename
))
359 return error("Patch pathname too long");
360 strcpy(filename
+ len
, fmt_patch_suffix
);
363 fprintf(realstdout
, "%s\n", filename
);
364 if (freopen(filename
, "w", stdout
) == NULL
)
365 return error("Cannot open patch file %s",filename
);
370 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
372 struct rev_info check_rev
;
373 struct commit
*commit
;
374 struct object
*o1
, *o2
;
375 unsigned flags1
, flags2
;
377 if (rev
->pending
.nr
!= 2)
378 die("Need exactly one range.");
380 o1
= rev
->pending
.objects
[0].item
;
382 o2
= rev
->pending
.objects
[1].item
;
385 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
390 /* given a range a..b get all patch ids for b..a */
391 init_revisions(&check_rev
, prefix
);
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
);
398 while ((commit
= get_revision(&check_rev
)) != NULL
) {
400 if (commit
->parents
&& commit
->parents
->next
)
403 add_commit_patch_id(commit
, ids
);
406 /* reset for next revision walk */
407 clear_commit_marks((struct commit
*)o1
,
408 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
409 clear_commit_marks((struct commit
*)o2
,
410 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
415 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
417 const char *committer
= git_committer_info(-1);
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.");
422 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
423 (unsigned long) time(NULL
),
424 (int)(email_end
- email_start
- 1), email_start
+ 1);
427 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
429 struct commit
*commit
;
430 struct commit
**list
= NULL
;
432 int nr
= 0, total
, i
, j
;
435 int start_number
= -1;
436 int keep_subject
= 0;
437 int numbered_files
= 0; /* _just_ numbers */
438 int subject_prefix
= 0;
439 int ignore_if_in_upstream
= 0;
441 const char *in_reply_to
= NULL
;
442 struct patch_ids ids
;
443 char *add_signoff
= NULL
;
444 char message_id
[1024];
445 char ref_message_id
[1024];
447 git_config(git_format_config
);
448 init_revisions(&rev
, prefix
);
449 rev
.commit_format
= CMIT_FMT_EMAIL
;
450 rev
.verbose_header
= 1;
452 rev
.combine_merges
= 0;
453 rev
.ignore_merges
= 1;
454 rev
.diffopt
.msg_sep
= "";
455 rev
.diffopt
.recursive
= 1;
457 rev
.extra_headers
= extra_headers
;
460 * Parse the arguments before setup_revisions(), or something
461 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
462 * possibly a valid SHA1.
464 for (i
= 1, j
= 1; i
< argc
; i
++) {
465 if (!strcmp(argv
[i
], "--stdout"))
467 else if (!strcmp(argv
[i
], "-n") ||
468 !strcmp(argv
[i
], "--numbered"))
470 else if (!prefixcmp(argv
[i
], "--start-number="))
471 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
472 else if (!strcmp(argv
[i
], "--numbered-files"))
474 else if (!strcmp(argv
[i
], "--start-number")) {
477 die("Need a number for --start-number");
478 start_number
= strtol(argv
[i
], NULL
, 10);
480 else if (!strcmp(argv
[i
], "-k") ||
481 !strcmp(argv
[i
], "--keep-subject")) {
485 else if (!strcmp(argv
[i
], "--output-directory") ||
486 !strcmp(argv
[i
], "-o")) {
489 die("Which directory?");
490 if (output_directory
)
491 die("Two output directories?");
492 output_directory
= argv
[i
];
494 else if (!strcmp(argv
[i
], "--signoff") ||
495 !strcmp(argv
[i
], "-s")) {
496 const char *committer
;
498 committer
= git_committer_info(1);
499 endpos
= strchr(committer
, '>');
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;
506 else if (!strcmp(argv
[i
], "--attach")) {
507 rev
.mime_boundary
= git_version_string
;
510 else if (!prefixcmp(argv
[i
], "--attach=")) {
511 rev
.mime_boundary
= argv
[i
] + 9;
514 else if (!strcmp(argv
[i
], "--inline")) {
515 rev
.mime_boundary
= git_version_string
;
518 else if (!prefixcmp(argv
[i
], "--inline=")) {
519 rev
.mime_boundary
= argv
[i
] + 9;
522 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
523 ignore_if_in_upstream
= 1;
524 else if (!strcmp(argv
[i
], "--thread"))
526 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
527 in_reply_to
= argv
[i
] + 14;
528 else if (!strcmp(argv
[i
], "--in-reply-to")) {
531 die("Need a Message-Id for --in-reply-to");
532 in_reply_to
= argv
[i
];
533 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
535 rev
.subject_prefix
= argv
[i
] + 17;
536 } else if (!prefixcmp(argv
[i
], "--suffix="))
537 fmt_patch_suffix
= argv
[i
] + 9;
543 if (start_number
< 0)
545 if (numbered
&& keep_subject
)
546 die ("-n and -k are mutually exclusive.");
547 if (keep_subject
&& subject_prefix
)
548 die ("--subject-prefix and -k are mutually exclusive.");
549 if (numbered_files
&& use_stdout
)
550 die ("--numbered-files and --stdout are mutually exclusive.");
552 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
554 die ("unrecognized argument: %s", argv
[1]);
556 if (!rev
.diffopt
.output_format
)
557 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
559 if (!rev
.diffopt
.text
)
560 rev
.diffopt
.binary
= 1;
562 if (!output_directory
&& !use_stdout
)
563 output_directory
= prefix
;
565 if (output_directory
) {
567 die("standard output, or directory, which one?");
568 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
569 die("Could not create directory %s",
573 if (rev
.pending
.nr
== 1) {
574 if (rev
.max_count
< 0) {
575 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
578 /* Otherwise, it is "format-patch -22 HEAD", and
579 * get_revision() would return only the specified count.
583 if (ignore_if_in_upstream
)
584 get_patch_ids(&rev
, &ids
, prefix
);
587 realstdout
= fdopen(dup(1), "w");
589 prepare_revision_walk(&rev
);
590 while ((commit
= get_revision(&rev
)) != NULL
) {
592 if (commit
->parents
&& commit
->parents
->next
)
595 if (ignore_if_in_upstream
&&
596 has_commit_patch_id(commit
, &ids
))
600 list
= xrealloc(list
, nr
* sizeof(list
[0]));
601 list
[nr
- 1] = commit
;
605 rev
.total
= total
+ start_number
- 1;
606 rev
.add_signoff
= add_signoff
;
607 rev
.ref_message_id
= in_reply_to
;
611 rev
.nr
= total
- nr
+ (start_number
- 1);
612 /* Make the second and subsequent mails replies to the first */
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
;
620 gen_message_id(message_id
, sizeof(message_id
),
621 sha1_to_hex(commit
->object
.sha1
));
622 rev
.message_id
= message_id
;
625 if (reopen_stdout(commit
, rev
.nr
, keep_subject
,
627 die("Failed to create output files");
628 shown
= log_tree_commit(&rev
, commit
);
629 free(commit
->buffer
);
630 commit
->buffer
= NULL
;
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.
641 if (rev
.mime_boundary
)
642 printf("\n--%s%s--\n\n\n",
643 mime_boundary_leader
,
646 printf("-- \n%s\n\n", git_version_string
);
652 if (ignore_if_in_upstream
)
653 free_patch_ids(&ids
);
657 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
659 unsigned char sha1
[20];
660 if (get_sha1(arg
, sha1
) == 0) {
661 struct commit
*commit
= lookup_commit_reference(sha1
);
663 commit
->object
.flags
|= flags
;
664 add_pending_object(revs
, &commit
->object
, arg
);
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
)
675 struct rev_info revs
;
676 struct patch_ids ids
;
677 struct commit
*commit
;
678 struct commit_list
*list
= NULL
;
679 const char *upstream
;
680 const char *head
= "HEAD";
681 const char *limit
= NULL
;
684 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
704 init_revisions(&revs
, prefix
);
706 revs
.combine_merges
= 0;
707 revs
.ignore_merges
= 1;
708 revs
.diffopt
.recursive
= 1;
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
);
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)
722 get_patch_ids(&revs
, &ids
, prefix
);
724 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
725 die("Unknown commit %s", limit
);
727 /* reverse the list of commits */
728 prepare_revision_walk(&revs
);
729 while ((commit
= get_revision(&revs
)) != NULL
) {
731 if (commit
->parents
&& commit
->parents
->next
)
734 commit_list_insert(commit
, &list
);
741 if (has_commit_patch_id(commit
, &ids
))
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
);
752 printf("%c %s\n", sign
,
753 sha1_to_hex(commit
->object
.sha1
));
759 free_patch_ids(&ids
);