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
);
228 #define FORMAT_PATCH_NAME_MAX 64
230 static int istitlechar(char c
)
232 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
233 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
236 static char *extra_headers
= NULL
;
237 static int extra_headers_size
= 0;
238 static const char *fmt_patch_suffix
= ".patch";
240 static int git_format_config(const char *var
, const char *value
)
242 if (!strcmp(var
, "format.headers")) {
246 die("format.headers without value");
248 extra_headers_size
+= len
+ 1;
249 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
250 extra_headers
[extra_headers_size
- len
- 1] = 0;
251 strcat(extra_headers
, value
);
254 if (!strcmp(var
, "format.suffix")) {
256 die("format.suffix without value");
257 fmt_patch_suffix
= xstrdup(value
);
260 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
263 return git_log_config(var
, value
);
267 static FILE *realstdout
= NULL
;
268 static const char *output_directory
= NULL
;
270 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
272 char filename
[PATH_MAX
];
275 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
277 if (output_directory
) {
278 if (strlen(output_directory
) >=
279 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
280 return error("name of output directory is too long");
281 strlcpy(filename
, output_directory
, sizeof(filename
) - suffix_len
);
282 len
= strlen(filename
);
283 if (filename
[len
- 1] != '/')
284 filename
[len
++] = '/';
287 sprintf(filename
+ len
, "%04d", nr
);
288 len
= strlen(filename
);
290 sol
= strstr(commit
->buffer
, "\n\n");
295 /* strip [PATCH] or [PATCH blabla] */
296 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
297 char *eos
= strchr(sol
+ 6, ']');
299 while (isspace(*eos
))
306 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
307 len
< sizeof(filename
) - suffix_len
&&
308 sol
[j
] && sol
[j
] != '\n';
310 if (istitlechar(sol
[j
])) {
312 filename
[len
++] = '-';
315 filename
[len
++] = sol
[j
];
317 while (sol
[j
+ 1] == '.')
322 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
326 if (len
+ suffix_len
>= sizeof(filename
))
327 return error("Patch pathname too long");
328 strcpy(filename
+ len
, fmt_patch_suffix
);
329 fprintf(realstdout
, "%s\n", filename
);
330 if (freopen(filename
, "w", stdout
) == NULL
)
331 return error("Cannot open patch file %s",filename
);
336 static int get_patch_id(struct commit
*commit
, struct diff_options
*options
,
340 diff_tree_sha1(commit
->parents
->item
->object
.sha1
,
341 commit
->object
.sha1
, "", options
);
343 diff_root_tree_sha1(commit
->object
.sha1
, "", options
);
344 diffcore_std(options
);
345 return diff_flush_patch_id(options
, sha1
);
348 static void get_patch_ids(struct rev_info
*rev
, struct diff_options
*options
, const char *prefix
)
350 struct rev_info check_rev
;
351 struct commit
*commit
;
352 struct object
*o1
, *o2
;
353 unsigned flags1
, flags2
;
354 unsigned char sha1
[20];
356 if (rev
->pending
.nr
!= 2)
357 die("Need exactly one range.");
359 o1
= rev
->pending
.objects
[0].item
;
361 o2
= rev
->pending
.objects
[1].item
;
364 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
368 options
->recursive
= 1;
369 if (diff_setup_done(options
) < 0)
370 die("diff_setup_done failed");
372 /* given a range a..b get all patch ids for b..a */
373 init_revisions(&check_rev
, prefix
);
374 o1
->flags
^= UNINTERESTING
;
375 o2
->flags
^= UNINTERESTING
;
376 add_pending_object(&check_rev
, o1
, "o1");
377 add_pending_object(&check_rev
, o2
, "o2");
378 prepare_revision_walk(&check_rev
);
380 while ((commit
= get_revision(&check_rev
)) != NULL
) {
382 if (commit
->parents
&& commit
->parents
->next
)
385 if (!get_patch_id(commit
, options
, sha1
))
386 created_object(sha1
, xcalloc(1, sizeof(struct object
)));
389 /* reset for next revision walk */
390 clear_commit_marks((struct commit
*)o1
,
391 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
392 clear_commit_marks((struct commit
*)o2
,
393 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
398 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
400 const char *committer
= git_committer_info(-1);
401 const char *email_start
= strrchr(committer
, '<');
402 const char *email_end
= strrchr(committer
, '>');
403 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
404 die("Could not extract email from committer identity.");
405 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
406 (unsigned long) time(NULL
),
407 (int)(email_end
- email_start
- 1), email_start
+ 1);
410 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
412 struct commit
*commit
;
413 struct commit
**list
= NULL
;
415 int nr
= 0, total
, i
, j
;
418 int start_number
= -1;
419 int keep_subject
= 0;
420 int ignore_if_in_upstream
= 0;
422 const char *in_reply_to
= NULL
;
423 struct diff_options patch_id_opts
;
424 char *add_signoff
= NULL
;
425 char message_id
[1024];
426 char ref_message_id
[1024];
428 git_config(git_format_config
);
429 init_revisions(&rev
, prefix
);
430 rev
.commit_format
= CMIT_FMT_EMAIL
;
431 rev
.verbose_header
= 1;
433 rev
.combine_merges
= 0;
434 rev
.ignore_merges
= 1;
435 rev
.diffopt
.msg_sep
= "";
436 rev
.diffopt
.recursive
= 1;
438 rev
.extra_headers
= extra_headers
;
441 * Parse the arguments before setup_revisions(), or something
442 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
443 * possibly a valid SHA1.
445 for (i
= 1, j
= 1; i
< argc
; i
++) {
446 if (!strcmp(argv
[i
], "--stdout"))
448 else if (!strcmp(argv
[i
], "-n") ||
449 !strcmp(argv
[i
], "--numbered"))
451 else if (!prefixcmp(argv
[i
], "--start-number="))
452 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
453 else if (!strcmp(argv
[i
], "--start-number")) {
456 die("Need a number for --start-number");
457 start_number
= strtol(argv
[i
], NULL
, 10);
459 else if (!strcmp(argv
[i
], "-k") ||
460 !strcmp(argv
[i
], "--keep-subject")) {
464 else if (!strcmp(argv
[i
], "--output-directory") ||
465 !strcmp(argv
[i
], "-o")) {
468 die("Which directory?");
469 if (output_directory
)
470 die("Two output directories?");
471 output_directory
= argv
[i
];
473 else if (!strcmp(argv
[i
], "--signoff") ||
474 !strcmp(argv
[i
], "-s")) {
475 const char *committer
;
477 committer
= git_committer_info(1);
478 endpos
= strchr(committer
, '>');
480 die("bogos committer info %s\n", committer
);
481 add_signoff
= xmalloc(endpos
- committer
+ 2);
482 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
483 add_signoff
[endpos
- committer
+ 1] = 0;
485 else if (!strcmp(argv
[i
], "--attach"))
486 rev
.mime_boundary
= git_version_string
;
487 else if (!prefixcmp(argv
[i
], "--attach="))
488 rev
.mime_boundary
= argv
[i
] + 9;
489 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
490 ignore_if_in_upstream
= 1;
491 else if (!strcmp(argv
[i
], "--thread"))
493 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
494 in_reply_to
= argv
[i
] + 14;
495 else if (!strcmp(argv
[i
], "--in-reply-to")) {
498 die("Need a Message-Id for --in-reply-to");
499 in_reply_to
= argv
[i
];
501 else if (!prefixcmp(argv
[i
], "--suffix="))
502 fmt_patch_suffix
= argv
[i
] + 9;
508 if (start_number
< 0)
510 if (numbered
&& keep_subject
)
511 die ("-n and -k are mutually exclusive.");
513 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
515 die ("unrecognized argument: %s", argv
[1]);
517 if (!rev
.diffopt
.output_format
)
518 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
520 if (!rev
.diffopt
.text
)
521 rev
.diffopt
.binary
= 1;
523 if (!output_directory
&& !use_stdout
)
524 output_directory
= prefix
;
526 if (output_directory
) {
528 die("standard output, or directory, which one?");
529 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
530 die("Could not create directory %s",
534 if (rev
.pending
.nr
== 1) {
535 if (rev
.max_count
< 0) {
536 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
539 /* Otherwise, it is "format-patch -22 HEAD", and
540 * get_revision() would return only the specified count.
544 if (ignore_if_in_upstream
)
545 get_patch_ids(&rev
, &patch_id_opts
, prefix
);
548 realstdout
= fdopen(dup(1), "w");
550 prepare_revision_walk(&rev
);
551 while ((commit
= get_revision(&rev
)) != NULL
) {
552 unsigned char sha1
[20];
555 if (commit
->parents
&& commit
->parents
->next
)
558 if (ignore_if_in_upstream
&&
559 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
564 list
= xrealloc(list
, nr
* sizeof(list
[0]));
565 list
[nr
- 1] = commit
;
569 rev
.total
= total
+ start_number
- 1;
570 rev
.add_signoff
= add_signoff
;
571 rev
.ref_message_id
= in_reply_to
;
575 rev
.nr
= total
- nr
+ (start_number
- 1);
576 /* Make the second and subsequent mails replies to the first */
578 if (nr
== (total
- 2)) {
579 strncpy(ref_message_id
, message_id
,
580 sizeof(ref_message_id
));
581 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
582 rev
.ref_message_id
= ref_message_id
;
584 gen_message_id(message_id
, sizeof(message_id
),
585 sha1_to_hex(commit
->object
.sha1
));
586 rev
.message_id
= message_id
;
589 if (reopen_stdout(commit
, rev
.nr
, keep_subject
))
590 die("Failed to create output files");
591 shown
= log_tree_commit(&rev
, commit
);
592 free(commit
->buffer
);
593 commit
->buffer
= NULL
;
595 /* We put one extra blank line between formatted
596 * patches and this flag is used by log-tree code
597 * to see if it needs to emit a LF before showing
598 * the log; when using one file per patch, we do
599 * not want the extra blank line.
604 if (rev
.mime_boundary
)
605 printf("\n--%s%s--\n\n\n",
606 mime_boundary_leader
,
609 printf("-- \n%s\n\n", git_version_string
);
618 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
620 unsigned char sha1
[20];
621 if (get_sha1(arg
, sha1
) == 0) {
622 struct commit
*commit
= lookup_commit_reference(sha1
);
624 commit
->object
.flags
|= flags
;
625 add_pending_object(revs
, &commit
->object
, arg
);
632 static const char cherry_usage
[] =
633 "git-cherry [-v] <upstream> [<head>] [<limit>]";
634 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
636 struct rev_info revs
;
637 struct diff_options patch_id_opts
;
638 struct commit
*commit
;
639 struct commit_list
*list
= NULL
;
640 const char *upstream
;
641 const char *head
= "HEAD";
642 const char *limit
= NULL
;
645 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
665 init_revisions(&revs
, prefix
);
667 revs
.combine_merges
= 0;
668 revs
.ignore_merges
= 1;
669 revs
.diffopt
.recursive
= 1;
671 if (add_pending_commit(head
, &revs
, 0))
672 die("Unknown commit %s", head
);
673 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
674 die("Unknown commit %s", upstream
);
676 /* Don't say anything if head and upstream are the same. */
677 if (revs
.pending
.nr
== 2) {
678 struct object_array_entry
*o
= revs
.pending
.objects
;
679 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
683 get_patch_ids(&revs
, &patch_id_opts
, prefix
);
685 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
686 die("Unknown commit %s", limit
);
688 /* reverse the list of commits */
689 prepare_revision_walk(&revs
);
690 while ((commit
= get_revision(&revs
)) != NULL
) {
692 if (commit
->parents
&& commit
->parents
->next
)
695 commit_list_insert(commit
, &list
);
699 unsigned char sha1
[20];
703 if (!get_patch_id(commit
, &patch_id_opts
, sha1
) &&
708 static char buf
[16384];
709 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
710 buf
, sizeof(buf
), 0, NULL
, NULL
, 0);
711 printf("%c %s %s\n", sign
,
712 sha1_to_hex(commit
->object
.sha1
), buf
);
715 printf("%c %s\n", sign
,
716 sha1_to_hex(commit
->object
.sha1
));