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"
17 static int default_show_root
= 1;
19 /* this is in builtin-diff.c */
20 void add_head(struct rev_info
*revs
);
22 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
27 rev
->abbrev
= DEFAULT_ABBREV
;
28 rev
->commit_format
= CMIT_FMT_DEFAULT
;
29 rev
->verbose_header
= 1;
30 rev
->show_root_diff
= default_show_root
;
31 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
32 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
33 rev
->always_show_header
= 0;
34 for (i
= 1; i
< argc
; i
++) {
35 const char *arg
= argv
[i
];
36 if (!prefixcmp(arg
, "--encoding=")) {
38 if (strcmp(arg
, "none"))
39 git_log_output_encoding
= xstrdup(arg
);
41 git_log_output_encoding
= "";
44 die("unrecognized argument: %s", arg
);
48 static int cmd_log_walk(struct rev_info
*rev
)
50 struct commit
*commit
;
52 prepare_revision_walk(rev
);
53 while ((commit
= get_revision(rev
)) != NULL
) {
54 log_tree_commit(rev
, commit
);
55 if (!rev
->reflog_info
) {
56 /* we allow cycles in reflog ancestry */
58 commit
->buffer
= NULL
;
60 free_commit_list(commit
->parents
);
61 commit
->parents
= NULL
;
66 static int git_log_config(const char *var
, const char *value
)
68 if (!strcmp(var
, "log.showroot")) {
69 default_show_root
= git_config_bool(var
, value
);
72 return git_diff_ui_config(var
, value
);
75 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
79 git_config(git_log_config
);
80 init_revisions(&rev
, prefix
);
82 rev
.diffopt
.recursive
= 1;
83 rev
.simplify_history
= 0;
84 cmd_log_init(argc
, argv
, prefix
, &rev
);
85 if (!rev
.diffopt
.output_format
)
86 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
87 return cmd_log_walk(&rev
);
90 static int show_object(const unsigned char *sha1
, int suppress_header
)
93 enum object_type type
;
94 char *buf
= read_sha1_file(sha1
, &type
, &size
);
98 return error("Could not read object %s", sha1_to_hex(sha1
));
101 while (offset
< size
&& buf
[offset
++] != '\n') {
102 int new_offset
= offset
;
103 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
109 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
114 static int show_tree_object(const unsigned char *sha1
,
115 const char *base
, int baselen
,
116 const char *pathname
, unsigned mode
, int stage
)
118 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
122 int cmd_show(int argc
, const char **argv
, const char *prefix
)
125 struct object_array_entry
*objects
;
126 int i
, count
, ret
= 0;
128 git_config(git_log_config
);
129 init_revisions(&rev
, prefix
);
131 rev
.diffopt
.recursive
= 1;
132 rev
.combine_merges
= 1;
133 rev
.dense_combined_merges
= 1;
134 rev
.always_show_header
= 1;
135 rev
.ignore_merges
= 0;
137 cmd_log_init(argc
, argv
, prefix
, &rev
);
139 count
= rev
.pending
.nr
;
140 objects
= rev
.pending
.objects
;
141 for (i
= 0; i
< count
&& !ret
; i
++) {
142 struct object
*o
= objects
[i
].item
;
143 const char *name
= objects
[i
].name
;
146 ret
= show_object(o
->sha1
, 0);
149 struct tag
*t
= (struct tag
*)o
;
151 printf("%stag %s%s\n\n",
152 diff_get_color(rev
.diffopt
.color_diff
,
155 diff_get_color(rev
.diffopt
.color_diff
,
157 ret
= show_object(o
->sha1
, 1);
158 objects
[i
].item
= (struct object
*)t
->tagged
;
163 printf("%stree %s%s\n\n",
164 diff_get_color(rev
.diffopt
.color_diff
,
167 diff_get_color(rev
.diffopt
.color_diff
,
169 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
173 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
174 rev
.pending
.objects
= NULL
;
175 add_object_array(o
, name
, &rev
.pending
);
176 ret
= cmd_log_walk(&rev
);
179 ret
= error("Unknown type: %d", o
->type
);
187 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
189 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
193 git_config(git_log_config
);
194 init_revisions(&rev
, prefix
);
195 init_reflog_walk(&rev
.reflog_info
);
196 rev
.abbrev_commit
= 1;
197 rev
.verbose_header
= 1;
198 cmd_log_init(argc
, argv
, prefix
, &rev
);
201 * This means that we override whatever commit format the user gave
202 * on the cmd line. Sad, but cmd_log_init() currently doesn't
203 * allow us to set a different default.
205 rev
.commit_format
= CMIT_FMT_ONELINE
;
206 rev
.always_show_header
= 1;
209 * We get called through "git reflog", so unlike the other log
210 * routines, we need to set up our pager manually..
214 return cmd_log_walk(&rev
);
217 int cmd_log(int argc
, const char **argv
, const char *prefix
)
221 git_config(git_log_config
);
222 init_revisions(&rev
, prefix
);
223 rev
.always_show_header
= 1;
224 cmd_log_init(argc
, argv
, prefix
, &rev
);
225 return cmd_log_walk(&rev
);
229 #define FORMAT_PATCH_NAME_MAX 64
231 static int istitlechar(char c
)
233 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
234 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
237 static char *extra_headers
= NULL
;
238 static int extra_headers_size
= 0;
239 static const char *fmt_patch_suffix
= ".patch";
241 static int git_format_config(const char *var
, const char *value
)
243 if (!strcmp(var
, "format.headers")) {
247 die("format.headers without value");
249 extra_headers_size
+= len
+ 1;
250 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
251 extra_headers
[extra_headers_size
- len
- 1] = 0;
252 strcat(extra_headers
, value
);
255 if (!strcmp(var
, "format.suffix")) {
257 die("format.suffix without value");
258 fmt_patch_suffix
= xstrdup(value
);
261 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
264 return git_log_config(var
, value
);
268 static FILE *realstdout
= NULL
;
269 static const char *output_directory
= NULL
;
271 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
273 char filename
[PATH_MAX
];
276 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
278 if (output_directory
) {
279 if (strlen(output_directory
) >=
280 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
281 return error("name of output directory is too long");
282 strlcpy(filename
, output_directory
, sizeof(filename
) - suffix_len
);
283 len
= strlen(filename
);
284 if (filename
[len
- 1] != '/')
285 filename
[len
++] = '/';
288 sprintf(filename
+ len
, "%04d", nr
);
289 len
= strlen(filename
);
291 sol
= strstr(commit
->buffer
, "\n\n");
296 /* strip [PATCH] or [PATCH blabla] */
297 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
298 char *eos
= strchr(sol
+ 6, ']');
300 while (isspace(*eos
))
307 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
308 len
< sizeof(filename
) - suffix_len
&&
309 sol
[j
] && sol
[j
] != '\n';
311 if (istitlechar(sol
[j
])) {
313 filename
[len
++] = '-';
316 filename
[len
++] = sol
[j
];
318 while (sol
[j
+ 1] == '.')
323 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
327 if (len
+ suffix_len
>= sizeof(filename
))
328 return error("Patch pathname too long");
329 strcpy(filename
+ len
, fmt_patch_suffix
);
330 fprintf(realstdout
, "%s\n", filename
);
331 if (freopen(filename
, "w", stdout
) == NULL
)
332 return error("Cannot open patch file %s",filename
);
337 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
339 struct rev_info check_rev
;
340 struct commit
*commit
;
341 struct object
*o1
, *o2
;
342 unsigned flags1
, flags2
;
344 if (rev
->pending
.nr
!= 2)
345 die("Need exactly one range.");
347 o1
= rev
->pending
.objects
[0].item
;
349 o2
= rev
->pending
.objects
[1].item
;
352 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
357 /* given a range a..b get all patch ids for b..a */
358 init_revisions(&check_rev
, prefix
);
359 o1
->flags
^= UNINTERESTING
;
360 o2
->flags
^= UNINTERESTING
;
361 add_pending_object(&check_rev
, o1
, "o1");
362 add_pending_object(&check_rev
, o2
, "o2");
363 prepare_revision_walk(&check_rev
);
365 while ((commit
= get_revision(&check_rev
)) != NULL
) {
367 if (commit
->parents
&& commit
->parents
->next
)
370 add_commit_patch_id(commit
, ids
);
373 /* reset for next revision walk */
374 clear_commit_marks((struct commit
*)o1
,
375 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
376 clear_commit_marks((struct commit
*)o2
,
377 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
382 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
384 const char *committer
= git_committer_info(-1);
385 const char *email_start
= strrchr(committer
, '<');
386 const char *email_end
= strrchr(committer
, '>');
387 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
388 die("Could not extract email from committer identity.");
389 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
390 (unsigned long) time(NULL
),
391 (int)(email_end
- email_start
- 1), email_start
+ 1);
394 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
396 struct commit
*commit
;
397 struct commit
**list
= NULL
;
399 int nr
= 0, total
, i
, j
;
402 int start_number
= -1;
403 int keep_subject
= 0;
404 int subject_prefix
= 0;
405 int ignore_if_in_upstream
= 0;
407 const char *in_reply_to
= NULL
;
408 struct patch_ids ids
;
409 char *add_signoff
= NULL
;
410 char message_id
[1024];
411 char ref_message_id
[1024];
413 git_config(git_format_config
);
414 init_revisions(&rev
, prefix
);
415 rev
.commit_format
= CMIT_FMT_EMAIL
;
416 rev
.verbose_header
= 1;
418 rev
.combine_merges
= 0;
419 rev
.ignore_merges
= 1;
420 rev
.diffopt
.msg_sep
= "";
421 rev
.diffopt
.recursive
= 1;
423 rev
.extra_headers
= extra_headers
;
426 * Parse the arguments before setup_revisions(), or something
427 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
428 * possibly a valid SHA1.
430 for (i
= 1, j
= 1; i
< argc
; i
++) {
431 if (!strcmp(argv
[i
], "--stdout"))
433 else if (!strcmp(argv
[i
], "-n") ||
434 !strcmp(argv
[i
], "--numbered"))
436 else if (!prefixcmp(argv
[i
], "--start-number="))
437 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
438 else if (!strcmp(argv
[i
], "--start-number")) {
441 die("Need a number for --start-number");
442 start_number
= strtol(argv
[i
], NULL
, 10);
444 else if (!strcmp(argv
[i
], "-k") ||
445 !strcmp(argv
[i
], "--keep-subject")) {
449 else if (!strcmp(argv
[i
], "--output-directory") ||
450 !strcmp(argv
[i
], "-o")) {
453 die("Which directory?");
454 if (output_directory
)
455 die("Two output directories?");
456 output_directory
= argv
[i
];
458 else if (!strcmp(argv
[i
], "--signoff") ||
459 !strcmp(argv
[i
], "-s")) {
460 const char *committer
;
462 committer
= git_committer_info(1);
463 endpos
= strchr(committer
, '>');
465 die("bogos committer info %s\n", committer
);
466 add_signoff
= xmalloc(endpos
- committer
+ 2);
467 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
468 add_signoff
[endpos
- committer
+ 1] = 0;
470 else if (!strcmp(argv
[i
], "--attach")) {
471 rev
.mime_boundary
= git_version_string
;
474 else if (!prefixcmp(argv
[i
], "--attach=")) {
475 rev
.mime_boundary
= argv
[i
] + 9;
478 else if (!strcmp(argv
[i
], "--inline")) {
479 rev
.mime_boundary
= git_version_string
;
482 else if (!prefixcmp(argv
[i
], "--inline=")) {
483 rev
.mime_boundary
= argv
[i
] + 9;
486 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
487 ignore_if_in_upstream
= 1;
488 else if (!strcmp(argv
[i
], "--thread"))
490 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
491 in_reply_to
= argv
[i
] + 14;
492 else if (!strcmp(argv
[i
], "--in-reply-to")) {
495 die("Need a Message-Id for --in-reply-to");
496 in_reply_to
= argv
[i
];
497 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
499 rev
.subject_prefix
= argv
[i
] + 17;
500 } else if (!prefixcmp(argv
[i
], "--suffix="))
501 fmt_patch_suffix
= argv
[i
] + 9;
507 if (start_number
< 0)
509 if (numbered
&& keep_subject
)
510 die ("-n and -k are mutually exclusive.");
511 if (keep_subject
&& subject_prefix
)
512 die ("--subject-prefix and -k are mutually exclusive.");
514 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
516 die ("unrecognized argument: %s", argv
[1]);
518 if (!rev
.diffopt
.output_format
)
519 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
521 if (!rev
.diffopt
.text
)
522 rev
.diffopt
.binary
= 1;
524 if (!output_directory
&& !use_stdout
)
525 output_directory
= prefix
;
527 if (output_directory
) {
529 die("standard output, or directory, which one?");
530 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
531 die("Could not create directory %s",
535 if (rev
.pending
.nr
== 1) {
536 if (rev
.max_count
< 0) {
537 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
540 /* Otherwise, it is "format-patch -22 HEAD", and
541 * get_revision() would return only the specified count.
545 if (ignore_if_in_upstream
)
546 get_patch_ids(&rev
, &ids
, prefix
);
549 realstdout
= fdopen(dup(1), "w");
551 prepare_revision_walk(&rev
);
552 while ((commit
= get_revision(&rev
)) != NULL
) {
554 if (commit
->parents
&& commit
->parents
->next
)
557 if (ignore_if_in_upstream
&&
558 has_commit_patch_id(commit
, &ids
))
562 list
= xrealloc(list
, nr
* sizeof(list
[0]));
563 list
[nr
- 1] = commit
;
567 rev
.total
= total
+ start_number
- 1;
568 rev
.add_signoff
= add_signoff
;
569 rev
.ref_message_id
= in_reply_to
;
573 rev
.nr
= total
- nr
+ (start_number
- 1);
574 /* Make the second and subsequent mails replies to the first */
576 if (nr
== (total
- 2)) {
577 strncpy(ref_message_id
, message_id
,
578 sizeof(ref_message_id
));
579 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
580 rev
.ref_message_id
= ref_message_id
;
582 gen_message_id(message_id
, sizeof(message_id
),
583 sha1_to_hex(commit
->object
.sha1
));
584 rev
.message_id
= message_id
;
587 if (reopen_stdout(commit
, rev
.nr
, keep_subject
))
588 die("Failed to create output files");
589 shown
= log_tree_commit(&rev
, commit
);
590 free(commit
->buffer
);
591 commit
->buffer
= NULL
;
593 /* We put one extra blank line between formatted
594 * patches and this flag is used by log-tree code
595 * to see if it needs to emit a LF before showing
596 * the log; when using one file per patch, we do
597 * not want the extra blank line.
602 if (rev
.mime_boundary
)
603 printf("\n--%s%s--\n\n\n",
604 mime_boundary_leader
,
607 printf("-- \n%s\n\n", git_version_string
);
613 if (ignore_if_in_upstream
)
614 free_patch_ids(&ids
);
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 patch_ids ids
;
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
, &ids
, 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
);
702 if (has_commit_patch_id(commit
, &ids
))
706 static char buf
[16384];
707 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
708 buf
, sizeof(buf
), 0, NULL
, NULL
, 0);
709 printf("%c %s %s\n", sign
,
710 sha1_to_hex(commit
->object
.sha1
), buf
);
713 printf("%c %s\n", sign
,
714 sha1_to_hex(commit
->object
.sha1
));
720 free_patch_ids(&ids
);