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 (!prefixcmp(arg
, "--encoding=")) {
65 if (strcmp(arg
, "none"))
66 git_log_output_encoding
= xstrdup(arg
);
68 git_log_output_encoding
= "";
69 } else if (!strcmp(arg
, "--decorate")) {
71 for_each_ref(add_ref_decoration
, NULL
);
74 die("unrecognized argument: %s", arg
);
78 static int cmd_log_walk(struct rev_info
*rev
)
80 struct commit
*commit
;
82 prepare_revision_walk(rev
);
83 while ((commit
= get_revision(rev
)) != NULL
) {
84 log_tree_commit(rev
, commit
);
85 if (!rev
->reflog_info
) {
86 /* we allow cycles in reflog ancestry */
88 commit
->buffer
= NULL
;
90 free_commit_list(commit
->parents
);
91 commit
->parents
= NULL
;
96 static int git_log_config(const char *var
, const char *value
)
98 if (!strcmp(var
, "log.showroot")) {
99 default_show_root
= git_config_bool(var
, value
);
102 return git_diff_ui_config(var
, value
);
105 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
109 git_config(git_log_config
);
110 init_revisions(&rev
, prefix
);
112 rev
.diffopt
.recursive
= 1;
113 rev
.simplify_history
= 0;
114 cmd_log_init(argc
, argv
, prefix
, &rev
);
115 if (!rev
.diffopt
.output_format
)
116 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
117 return cmd_log_walk(&rev
);
120 static int show_object(const unsigned char *sha1
, int suppress_header
)
123 enum object_type type
;
124 char *buf
= read_sha1_file(sha1
, &type
, &size
);
128 return error("Could not read object %s", sha1_to_hex(sha1
));
131 while (offset
< size
&& buf
[offset
++] != '\n') {
132 int new_offset
= offset
;
133 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
139 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
144 static int show_tree_object(const unsigned char *sha1
,
145 const char *base
, int baselen
,
146 const char *pathname
, unsigned mode
, int stage
)
148 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
152 int cmd_show(int argc
, const char **argv
, const char *prefix
)
155 struct object_array_entry
*objects
;
156 int i
, count
, ret
= 0;
158 git_config(git_log_config
);
159 init_revisions(&rev
, prefix
);
161 rev
.diffopt
.recursive
= 1;
162 rev
.combine_merges
= 1;
163 rev
.dense_combined_merges
= 1;
164 rev
.always_show_header
= 1;
165 rev
.ignore_merges
= 0;
167 cmd_log_init(argc
, argv
, prefix
, &rev
);
169 count
= rev
.pending
.nr
;
170 objects
= rev
.pending
.objects
;
171 for (i
= 0; i
< count
&& !ret
; i
++) {
172 struct object
*o
= objects
[i
].item
;
173 const char *name
= objects
[i
].name
;
176 ret
= show_object(o
->sha1
, 0);
179 struct tag
*t
= (struct tag
*)o
;
181 printf("%stag %s%s\n\n",
182 diff_get_color(rev
.diffopt
.color_diff
,
185 diff_get_color(rev
.diffopt
.color_diff
,
187 ret
= show_object(o
->sha1
, 1);
188 objects
[i
].item
= (struct object
*)t
->tagged
;
193 printf("%stree %s%s\n\n",
194 diff_get_color(rev
.diffopt
.color_diff
,
197 diff_get_color(rev
.diffopt
.color_diff
,
199 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
203 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
204 rev
.pending
.objects
= NULL
;
205 add_object_array(o
, name
, &rev
.pending
);
206 ret
= cmd_log_walk(&rev
);
209 ret
= error("Unknown type: %d", o
->type
);
217 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
219 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
223 git_config(git_log_config
);
224 init_revisions(&rev
, prefix
);
225 init_reflog_walk(&rev
.reflog_info
);
226 rev
.abbrev_commit
= 1;
227 rev
.verbose_header
= 1;
228 cmd_log_init(argc
, argv
, prefix
, &rev
);
231 * This means that we override whatever commit format the user gave
232 * on the cmd line. Sad, but cmd_log_init() currently doesn't
233 * allow us to set a different default.
235 rev
.commit_format
= CMIT_FMT_ONELINE
;
236 rev
.always_show_header
= 1;
239 * We get called through "git reflog", so unlike the other log
240 * routines, we need to set up our pager manually..
244 return cmd_log_walk(&rev
);
247 int cmd_log(int argc
, const char **argv
, const char *prefix
)
251 git_config(git_log_config
);
252 init_revisions(&rev
, prefix
);
253 rev
.always_show_header
= 1;
254 cmd_log_init(argc
, argv
, prefix
, &rev
);
255 return cmd_log_walk(&rev
);
259 #define FORMAT_PATCH_NAME_MAX 64
261 static int istitlechar(char c
)
263 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
264 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
267 static char *extra_headers
= NULL
;
268 static int extra_headers_size
= 0;
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 return git_log_config(var
, value
);
298 static FILE *realstdout
= NULL
;
299 static const char *output_directory
= NULL
;
301 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
303 char filename
[PATH_MAX
];
306 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
308 if (output_directory
) {
309 if (strlen(output_directory
) >=
310 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
311 return error("name of output directory is too long");
312 strlcpy(filename
, output_directory
, sizeof(filename
) - suffix_len
);
313 len
= strlen(filename
);
314 if (filename
[len
- 1] != '/')
315 filename
[len
++] = '/';
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] == '.' || filename
[len
- 1] == '-')
357 if (len
+ suffix_len
>= sizeof(filename
))
358 return error("Patch pathname too long");
359 strcpy(filename
+ len
, fmt_patch_suffix
);
360 fprintf(realstdout
, "%s\n", filename
);
361 if (freopen(filename
, "w", stdout
) == NULL
)
362 return error("Cannot open patch file %s",filename
);
367 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
369 struct rev_info check_rev
;
370 struct commit
*commit
;
371 struct object
*o1
, *o2
;
372 unsigned flags1
, flags2
;
374 if (rev
->pending
.nr
!= 2)
375 die("Need exactly one range.");
377 o1
= rev
->pending
.objects
[0].item
;
379 o2
= rev
->pending
.objects
[1].item
;
382 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
387 /* given a range a..b get all patch ids for b..a */
388 init_revisions(&check_rev
, prefix
);
389 o1
->flags
^= UNINTERESTING
;
390 o2
->flags
^= UNINTERESTING
;
391 add_pending_object(&check_rev
, o1
, "o1");
392 add_pending_object(&check_rev
, o2
, "o2");
393 prepare_revision_walk(&check_rev
);
395 while ((commit
= get_revision(&check_rev
)) != NULL
) {
397 if (commit
->parents
&& commit
->parents
->next
)
400 add_commit_patch_id(commit
, ids
);
403 /* reset for next revision walk */
404 clear_commit_marks((struct commit
*)o1
,
405 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
406 clear_commit_marks((struct commit
*)o2
,
407 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
412 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
414 const char *committer
= git_committer_info(-1);
415 const char *email_start
= strrchr(committer
, '<');
416 const char *email_end
= strrchr(committer
, '>');
417 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
418 die("Could not extract email from committer identity.");
419 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
420 (unsigned long) time(NULL
),
421 (int)(email_end
- email_start
- 1), email_start
+ 1);
424 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
426 struct commit
*commit
;
427 struct commit
**list
= NULL
;
429 int nr
= 0, total
, i
, j
;
432 int start_number
= -1;
433 int keep_subject
= 0;
434 int subject_prefix
= 0;
435 int ignore_if_in_upstream
= 0;
437 const char *in_reply_to
= NULL
;
438 struct patch_ids ids
;
439 char *add_signoff
= NULL
;
440 char message_id
[1024];
441 char ref_message_id
[1024];
443 git_config(git_format_config
);
444 init_revisions(&rev
, prefix
);
445 rev
.commit_format
= CMIT_FMT_EMAIL
;
446 rev
.verbose_header
= 1;
448 rev
.combine_merges
= 0;
449 rev
.ignore_merges
= 1;
450 rev
.diffopt
.msg_sep
= "";
451 rev
.diffopt
.recursive
= 1;
453 rev
.extra_headers
= extra_headers
;
456 * Parse the arguments before setup_revisions(), or something
457 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
458 * possibly a valid SHA1.
460 for (i
= 1, j
= 1; i
< argc
; i
++) {
461 if (!strcmp(argv
[i
], "--stdout"))
463 else if (!strcmp(argv
[i
], "-n") ||
464 !strcmp(argv
[i
], "--numbered"))
466 else if (!prefixcmp(argv
[i
], "--start-number="))
467 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
468 else if (!strcmp(argv
[i
], "--start-number")) {
471 die("Need a number for --start-number");
472 start_number
= strtol(argv
[i
], NULL
, 10);
474 else if (!strcmp(argv
[i
], "-k") ||
475 !strcmp(argv
[i
], "--keep-subject")) {
479 else if (!strcmp(argv
[i
], "--output-directory") ||
480 !strcmp(argv
[i
], "-o")) {
483 die("Which directory?");
484 if (output_directory
)
485 die("Two output directories?");
486 output_directory
= argv
[i
];
488 else if (!strcmp(argv
[i
], "--signoff") ||
489 !strcmp(argv
[i
], "-s")) {
490 const char *committer
;
492 committer
= git_committer_info(1);
493 endpos
= strchr(committer
, '>');
495 die("bogos committer info %s\n", committer
);
496 add_signoff
= xmalloc(endpos
- committer
+ 2);
497 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
498 add_signoff
[endpos
- committer
+ 1] = 0;
500 else if (!strcmp(argv
[i
], "--attach")) {
501 rev
.mime_boundary
= git_version_string
;
504 else if (!prefixcmp(argv
[i
], "--attach=")) {
505 rev
.mime_boundary
= argv
[i
] + 9;
508 else if (!strcmp(argv
[i
], "--inline")) {
509 rev
.mime_boundary
= git_version_string
;
512 else if (!prefixcmp(argv
[i
], "--inline=")) {
513 rev
.mime_boundary
= argv
[i
] + 9;
516 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
517 ignore_if_in_upstream
= 1;
518 else if (!strcmp(argv
[i
], "--thread"))
520 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
521 in_reply_to
= argv
[i
] + 14;
522 else if (!strcmp(argv
[i
], "--in-reply-to")) {
525 die("Need a Message-Id for --in-reply-to");
526 in_reply_to
= argv
[i
];
527 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
529 rev
.subject_prefix
= argv
[i
] + 17;
530 } else if (!prefixcmp(argv
[i
], "--suffix="))
531 fmt_patch_suffix
= argv
[i
] + 9;
537 if (start_number
< 0)
539 if (numbered
&& keep_subject
)
540 die ("-n and -k are mutually exclusive.");
541 if (keep_subject
&& subject_prefix
)
542 die ("--subject-prefix and -k are mutually exclusive.");
544 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
546 die ("unrecognized argument: %s", argv
[1]);
548 if (!rev
.diffopt
.output_format
)
549 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
551 if (!rev
.diffopt
.text
)
552 rev
.diffopt
.binary
= 1;
554 if (!output_directory
&& !use_stdout
)
555 output_directory
= prefix
;
557 if (output_directory
) {
559 die("standard output, or directory, which one?");
560 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
561 die("Could not create directory %s",
565 if (rev
.pending
.nr
== 1) {
566 if (rev
.max_count
< 0) {
567 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
570 /* Otherwise, it is "format-patch -22 HEAD", and
571 * get_revision() would return only the specified count.
575 if (ignore_if_in_upstream
)
576 get_patch_ids(&rev
, &ids
, prefix
);
579 realstdout
= fdopen(dup(1), "w");
581 prepare_revision_walk(&rev
);
582 while ((commit
= get_revision(&rev
)) != NULL
) {
584 if (commit
->parents
&& commit
->parents
->next
)
587 if (ignore_if_in_upstream
&&
588 has_commit_patch_id(commit
, &ids
))
592 list
= xrealloc(list
, nr
* sizeof(list
[0]));
593 list
[nr
- 1] = commit
;
597 rev
.total
= total
+ start_number
- 1;
598 rev
.add_signoff
= add_signoff
;
599 rev
.ref_message_id
= in_reply_to
;
603 rev
.nr
= total
- nr
+ (start_number
- 1);
604 /* Make the second and subsequent mails replies to the first */
606 if (nr
== (total
- 2)) {
607 strncpy(ref_message_id
, message_id
,
608 sizeof(ref_message_id
));
609 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
610 rev
.ref_message_id
= ref_message_id
;
612 gen_message_id(message_id
, sizeof(message_id
),
613 sha1_to_hex(commit
->object
.sha1
));
614 rev
.message_id
= message_id
;
617 if (reopen_stdout(commit
, rev
.nr
, keep_subject
))
618 die("Failed to create output files");
619 shown
= log_tree_commit(&rev
, commit
);
620 free(commit
->buffer
);
621 commit
->buffer
= NULL
;
623 /* We put one extra blank line between formatted
624 * patches and this flag is used by log-tree code
625 * to see if it needs to emit a LF before showing
626 * the log; when using one file per patch, we do
627 * not want the extra blank line.
632 if (rev
.mime_boundary
)
633 printf("\n--%s%s--\n\n\n",
634 mime_boundary_leader
,
637 printf("-- \n%s\n\n", git_version_string
);
643 if (ignore_if_in_upstream
)
644 free_patch_ids(&ids
);
648 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
650 unsigned char sha1
[20];
651 if (get_sha1(arg
, sha1
) == 0) {
652 struct commit
*commit
= lookup_commit_reference(sha1
);
654 commit
->object
.flags
|= flags
;
655 add_pending_object(revs
, &commit
->object
, arg
);
662 static const char cherry_usage
[] =
663 "git-cherry [-v] <upstream> [<head>] [<limit>]";
664 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
666 struct rev_info revs
;
667 struct patch_ids ids
;
668 struct commit
*commit
;
669 struct commit_list
*list
= NULL
;
670 const char *upstream
;
671 const char *head
= "HEAD";
672 const char *limit
= NULL
;
675 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
695 init_revisions(&revs
, prefix
);
697 revs
.combine_merges
= 0;
698 revs
.ignore_merges
= 1;
699 revs
.diffopt
.recursive
= 1;
701 if (add_pending_commit(head
, &revs
, 0))
702 die("Unknown commit %s", head
);
703 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
704 die("Unknown commit %s", upstream
);
706 /* Don't say anything if head and upstream are the same. */
707 if (revs
.pending
.nr
== 2) {
708 struct object_array_entry
*o
= revs
.pending
.objects
;
709 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
713 get_patch_ids(&revs
, &ids
, prefix
);
715 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
716 die("Unknown commit %s", limit
);
718 /* reverse the list of commits */
719 prepare_revision_walk(&revs
);
720 while ((commit
= get_revision(&revs
)) != NULL
) {
722 if (commit
->parents
&& commit
->parents
->next
)
725 commit_list_insert(commit
, &list
);
732 if (has_commit_patch_id(commit
, &ids
))
736 static char buf
[16384];
737 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
738 buf
, sizeof(buf
), 0, NULL
, NULL
, 0);
739 printf("%c %s %s\n", sign
,
740 sha1_to_hex(commit
->object
.sha1
), buf
);
743 printf("%c %s\n", sign
,
744 sha1_to_hex(commit
->object
.sha1
));
750 free_patch_ids(&ids
);