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;
422 rev
.subject_prefix
= "PATCH";
424 rev
.extra_headers
= extra_headers
;
427 * Parse the arguments before setup_revisions(), or something
428 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
429 * possibly a valid SHA1.
431 for (i
= 1, j
= 1; i
< argc
; i
++) {
432 if (!strcmp(argv
[i
], "--stdout"))
434 else if (!strcmp(argv
[i
], "-n") ||
435 !strcmp(argv
[i
], "--numbered"))
437 else if (!prefixcmp(argv
[i
], "--start-number="))
438 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
439 else if (!strcmp(argv
[i
], "--start-number")) {
442 die("Need a number for --start-number");
443 start_number
= strtol(argv
[i
], NULL
, 10);
445 else if (!strcmp(argv
[i
], "-k") ||
446 !strcmp(argv
[i
], "--keep-subject")) {
450 else if (!strcmp(argv
[i
], "--output-directory") ||
451 !strcmp(argv
[i
], "-o")) {
454 die("Which directory?");
455 if (output_directory
)
456 die("Two output directories?");
457 output_directory
= argv
[i
];
459 else if (!strcmp(argv
[i
], "--signoff") ||
460 !strcmp(argv
[i
], "-s")) {
461 const char *committer
;
463 committer
= git_committer_info(1);
464 endpos
= strchr(committer
, '>');
466 die("bogos committer info %s\n", committer
);
467 add_signoff
= xmalloc(endpos
- committer
+ 2);
468 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
469 add_signoff
[endpos
- committer
+ 1] = 0;
471 else if (!strcmp(argv
[i
], "--attach")) {
472 rev
.mime_boundary
= git_version_string
;
475 else if (!prefixcmp(argv
[i
], "--attach=")) {
476 rev
.mime_boundary
= argv
[i
] + 9;
479 else if (!strcmp(argv
[i
], "--inline")) {
480 rev
.mime_boundary
= git_version_string
;
483 else if (!prefixcmp(argv
[i
], "--inline=")) {
484 rev
.mime_boundary
= argv
[i
] + 9;
487 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
488 ignore_if_in_upstream
= 1;
489 else if (!strcmp(argv
[i
], "--thread"))
491 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
492 in_reply_to
= argv
[i
] + 14;
493 else if (!strcmp(argv
[i
], "--in-reply-to")) {
496 die("Need a Message-Id for --in-reply-to");
497 in_reply_to
= argv
[i
];
498 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
500 rev
.subject_prefix
= argv
[i
] + 17;
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.");
512 if (keep_subject
&& subject_prefix
)
513 die ("--subject-prefix and -k are mutually exclusive.");
515 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
517 die ("unrecognized argument: %s", argv
[1]);
519 if (!rev
.diffopt
.output_format
)
520 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
522 if (!rev
.diffopt
.text
)
523 rev
.diffopt
.binary
= 1;
525 if (!output_directory
&& !use_stdout
)
526 output_directory
= prefix
;
528 if (output_directory
) {
530 die("standard output, or directory, which one?");
531 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
532 die("Could not create directory %s",
536 if (rev
.pending
.nr
== 1) {
537 if (rev
.max_count
< 0) {
538 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
541 /* Otherwise, it is "format-patch -22 HEAD", and
542 * get_revision() would return only the specified count.
546 if (ignore_if_in_upstream
)
547 get_patch_ids(&rev
, &ids
, prefix
);
550 realstdout
= fdopen(dup(1), "w");
552 prepare_revision_walk(&rev
);
553 while ((commit
= get_revision(&rev
)) != NULL
) {
555 if (commit
->parents
&& commit
->parents
->next
)
558 if (ignore_if_in_upstream
&&
559 has_commit_patch_id(commit
, &ids
))
563 list
= xrealloc(list
, nr
* sizeof(list
[0]));
564 list
[nr
- 1] = commit
;
568 rev
.total
= total
+ start_number
- 1;
569 rev
.add_signoff
= add_signoff
;
570 rev
.ref_message_id
= in_reply_to
;
574 rev
.nr
= total
- nr
+ (start_number
- 1);
575 /* Make the second and subsequent mails replies to the first */
577 if (nr
== (total
- 2)) {
578 strncpy(ref_message_id
, message_id
,
579 sizeof(ref_message_id
));
580 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
581 rev
.ref_message_id
= ref_message_id
;
583 gen_message_id(message_id
, sizeof(message_id
),
584 sha1_to_hex(commit
->object
.sha1
));
585 rev
.message_id
= message_id
;
588 if (reopen_stdout(commit
, rev
.nr
, keep_subject
))
589 die("Failed to create output files");
590 shown
= log_tree_commit(&rev
, commit
);
591 free(commit
->buffer
);
592 commit
->buffer
= NULL
;
594 /* We put one extra blank line between formatted
595 * patches and this flag is used by log-tree code
596 * to see if it needs to emit a LF before showing
597 * the log; when using one file per patch, we do
598 * not want the extra blank line.
603 if (rev
.mime_boundary
)
604 printf("\n--%s%s--\n\n\n",
605 mime_boundary_leader
,
608 printf("-- \n%s\n\n", git_version_string
);
614 if (ignore_if_in_upstream
)
615 free_patch_ids(&ids
);
619 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
621 unsigned char sha1
[20];
622 if (get_sha1(arg
, sha1
) == 0) {
623 struct commit
*commit
= lookup_commit_reference(sha1
);
625 commit
->object
.flags
|= flags
;
626 add_pending_object(revs
, &commit
->object
, arg
);
633 static const char cherry_usage
[] =
634 "git-cherry [-v] <upstream> [<head>] [<limit>]";
635 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
637 struct rev_info revs
;
638 struct patch_ids ids
;
639 struct commit
*commit
;
640 struct commit_list
*list
= NULL
;
641 const char *upstream
;
642 const char *head
= "HEAD";
643 const char *limit
= NULL
;
646 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
666 init_revisions(&revs
, prefix
);
668 revs
.combine_merges
= 0;
669 revs
.ignore_merges
= 1;
670 revs
.diffopt
.recursive
= 1;
672 if (add_pending_commit(head
, &revs
, 0))
673 die("Unknown commit %s", head
);
674 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
675 die("Unknown commit %s", upstream
);
677 /* Don't say anything if head and upstream are the same. */
678 if (revs
.pending
.nr
== 2) {
679 struct object_array_entry
*o
= revs
.pending
.objects
;
680 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
684 get_patch_ids(&revs
, &ids
, prefix
);
686 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
687 die("Unknown commit %s", limit
);
689 /* reverse the list of commits */
690 prepare_revision_walk(&revs
);
691 while ((commit
= get_revision(&revs
)) != NULL
) {
693 if (commit
->parents
&& commit
->parents
->next
)
696 commit_list_insert(commit
, &list
);
703 if (has_commit_patch_id(commit
, &ids
))
707 static char buf
[16384];
708 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
709 buf
, sizeof(buf
), 0, NULL
, NULL
, 0);
710 printf("%c %s %s\n", sign
,
711 sha1_to_hex(commit
->object
.sha1
), buf
);
714 printf("%c %s\n", sign
,
715 sha1_to_hex(commit
->object
.sha1
));
721 free_patch_ids(&ids
);