2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
15 static int default_show_root
= 1;
17 /* this is in builtin-diff.c */
18 void add_head(struct rev_info
*revs
);
20 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
25 rev
->abbrev
= DEFAULT_ABBREV
;
26 rev
->commit_format
= CMIT_FMT_DEFAULT
;
27 rev
->verbose_header
= 1;
28 rev
->show_root_diff
= default_show_root
;
29 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
30 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
31 rev
->always_show_header
= 0;
32 for (i
= 1; i
< argc
; i
++) {
33 const char *arg
= argv
[i
];
34 if (!strncmp(arg
, "--encoding=", 11)) {
36 if (strcmp(arg
, "none"))
37 git_log_output_encoding
= strdup(arg
);
39 git_log_output_encoding
= "";
42 die("unrecognized argument: %s", arg
);
46 static int cmd_log_walk(struct rev_info
*rev
)
48 struct commit
*commit
;
50 prepare_revision_walk(rev
);
51 while ((commit
= get_revision(rev
)) != NULL
) {
52 log_tree_commit(rev
, commit
);
54 commit
->buffer
= NULL
;
55 free_commit_list(commit
->parents
);
56 commit
->parents
= NULL
;
61 static int git_log_config(const char *var
, const char *value
)
63 if (!strcmp(var
, "log.showroot")) {
64 default_show_root
= git_config_bool(var
, value
);
67 return git_diff_ui_config(var
, value
);
70 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
74 git_config(git_log_config
);
75 init_revisions(&rev
, prefix
);
77 rev
.diffopt
.recursive
= 1;
78 rev
.simplify_history
= 0;
79 cmd_log_init(argc
, argv
, prefix
, &rev
);
80 if (!rev
.diffopt
.output_format
)
81 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
82 return cmd_log_walk(&rev
);
85 static int show_object(const unsigned char *sha1
, int suppress_header
)
89 char *buf
= read_sha1_file(sha1
, type
, &size
);
93 return error("Could not read object %s", sha1_to_hex(sha1
));
96 while (offset
< size
&& buf
[offset
++] != '\n') {
97 int new_offset
= offset
;
98 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
104 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
109 static int show_tree_object(const unsigned char *sha1
,
110 const char *base
, int baselen
,
111 const char *pathname
, unsigned mode
, int stage
)
113 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
117 int cmd_show(int argc
, const char **argv
, const char *prefix
)
120 struct object_array_entry
*objects
;
121 int i
, count
, ret
= 0;
123 git_config(git_log_config
);
124 init_revisions(&rev
, prefix
);
126 rev
.diffopt
.recursive
= 1;
127 rev
.combine_merges
= 1;
128 rev
.dense_combined_merges
= 1;
129 rev
.always_show_header
= 1;
130 rev
.ignore_merges
= 0;
132 cmd_log_init(argc
, argv
, prefix
, &rev
);
134 count
= rev
.pending
.nr
;
135 objects
= rev
.pending
.objects
;
136 for (i
= 0; i
< count
&& !ret
; i
++) {
137 struct object
*o
= objects
[i
].item
;
138 const char *name
= objects
[i
].name
;
141 ret
= show_object(o
->sha1
, 0);
144 struct tag
*t
= (struct tag
*)o
;
146 printf("%stag %s%s\n\n",
147 diff_get_color(rev
.diffopt
.color_diff
,
150 diff_get_color(rev
.diffopt
.color_diff
,
152 ret
= show_object(o
->sha1
, 1);
153 objects
[i
].item
= (struct object
*)t
->tagged
;
158 printf("%stree %s%s\n\n",
159 diff_get_color(rev
.diffopt
.color_diff
,
162 diff_get_color(rev
.diffopt
.color_diff
,
164 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
168 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
169 rev
.pending
.objects
= NULL
;
170 add_object_array(o
, name
, &rev
.pending
);
171 ret
= cmd_log_walk(&rev
);
174 ret
= error("Unknown type: %d", o
->type
);
181 int cmd_log(int argc
, const char **argv
, const char *prefix
)
185 git_config(git_log_config
);
186 init_revisions(&rev
, prefix
);
187 rev
.always_show_header
= 1;
188 cmd_log_init(argc
, argv
, prefix
, &rev
);
189 return cmd_log_walk(&rev
);
192 static int istitlechar(char c
)
194 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
195 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
198 static char *extra_headers
= NULL
;
199 static int extra_headers_size
= 0;
201 static int git_format_config(const char *var
, const char *value
)
203 if (!strcmp(var
, "format.headers")) {
204 int len
= strlen(value
);
205 extra_headers_size
+= len
+ 1;
206 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
207 extra_headers
[extra_headers_size
- len
- 1] = 0;
208 strcat(extra_headers
, value
);
211 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
214 return git_log_config(var
, value
);
218 static FILE *realstdout
= NULL
;
219 static const char *output_directory
= NULL
;
221 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
227 if (output_directory
) {
228 strlcpy(filename
, output_directory
, 1010);
229 len
= strlen(filename
);
230 if (filename
[len
- 1] != '/')
231 filename
[len
++] = '/';
234 sprintf(filename
+ len
, "%04d", nr
);
235 len
= strlen(filename
);
237 sol
= strstr(commit
->buffer
, "\n\n");
242 /* strip [PATCH] or [PATCH blabla] */
243 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
244 char *eos
= strchr(sol
+ 6, ']');
246 while (isspace(*eos
))
252 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
253 if (istitlechar(sol
[j
])) {
255 filename
[len
++] = '-';
258 filename
[len
++] = sol
[j
];
260 while (sol
[j
+ 1] == '.')
265 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
268 strcpy(filename
+ len
, ".txt");
269 fprintf(realstdout
, "%s\n", filename
);
270 freopen(filename
, "w", stdout
);
273 static int get_patch_id(struct commit
*commit
, struct diff_options
*options
,
277 diff_tree_sha1(commit
->parents
->item
->object
.sha1
,
278 commit
->object
.sha1
, "", options
);
280 diff_root_tree_sha1(commit
->object
.sha1
, "", options
);
281 diffcore_std(options
);
282 return diff_flush_patch_id(options
, sha1
);
285 static void get_patch_ids(struct rev_info
*rev
, struct diff_options
*options
, const char *prefix
)
287 struct rev_info check_rev
;
288 struct commit
*commit
;
289 struct object
*o1
, *o2
;
290 unsigned flags1
, flags2
;
291 unsigned char sha1
[20];
293 if (rev
->pending
.nr
!= 2)
294 die("Need exactly one range.");
296 o1
= rev
->pending
.objects
[0].item
;
298 o2
= rev
->pending
.objects
[1].item
;
301 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
305 options
->recursive
= 1;
306 if (diff_setup_done(options
) < 0)
307 die("diff_setup_done failed");
309 /* given a range a..b get all patch ids for b..a */
310 init_revisions(&check_rev
, prefix
);
311 o1
->flags
^= UNINTERESTING
;
312 o2
->flags
^= UNINTERESTING
;
313 add_pending_object(&check_rev
, o1
, "o1");
314 add_pending_object(&check_rev
, o2
, "o2");
315 prepare_revision_walk(&check_rev
);
317 while ((commit
= get_revision(&check_rev
)) != NULL
) {
319 if (commit
->parents
&& commit
->parents
->next
)
322 if (!get_patch_id(commit
, options
, sha1
))
323 created_object(sha1
, xcalloc(1, sizeof(struct object
)));
326 /* reset for next revision walk */
327 clear_commit_marks((struct commit
*)o1
,
328 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
329 clear_commit_marks((struct commit
*)o2
,
330 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
335 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
337 const char *committer
= git_committer_info(1);
338 const char *email_start
= strrchr(committer
, '<');
339 const char *email_end
= strrchr(committer
, '>');
340 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
341 die("Could not extract email from committer identity.");
342 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
343 (unsigned long) time(NULL
),
344 (int)(email_end
- email_start
- 1), email_start
+ 1);
347 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
349 struct commit
*commit
;
350 struct commit
**list
= NULL
;
352 int nr
= 0, total
, i
, j
;
355 int start_number
= -1;
356 int keep_subject
= 0;
357 int ignore_if_in_upstream
= 0;
359 const char *in_reply_to
= NULL
;
360 struct diff_options patch_id_opts
;
361 char *add_signoff
= NULL
;
362 char message_id
[1024];
363 char ref_message_id
[1024];
366 git_config(git_format_config
);
367 init_revisions(&rev
, prefix
);
368 rev
.commit_format
= CMIT_FMT_EMAIL
;
369 rev
.verbose_header
= 1;
371 rev
.combine_merges
= 0;
372 rev
.ignore_merges
= 1;
373 rev
.diffopt
.msg_sep
= "";
374 rev
.diffopt
.recursive
= 1;
376 rev
.extra_headers
= extra_headers
;
379 * Parse the arguments before setup_revisions(), or something
380 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
381 * possibly a valid SHA1.
383 for (i
= 1, j
= 1; i
< argc
; i
++) {
384 if (!strcmp(argv
[i
], "--stdout"))
386 else if (!strcmp(argv
[i
], "-n") ||
387 !strcmp(argv
[i
], "--numbered"))
389 else if (!strncmp(argv
[i
], "--start-number=", 15))
390 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
391 else if (!strcmp(argv
[i
], "--start-number")) {
394 die("Need a number for --start-number");
395 start_number
= strtol(argv
[i
], NULL
, 10);
397 else if (!strcmp(argv
[i
], "-k") ||
398 !strcmp(argv
[i
], "--keep-subject")) {
402 else if (!strcmp(argv
[i
], "--output-directory") ||
403 !strcmp(argv
[i
], "-o")) {
406 die("Which directory?");
407 if (output_directory
)
408 die("Two output directories?");
409 output_directory
= argv
[i
];
411 else if (!strcmp(argv
[i
], "--signoff") ||
412 !strcmp(argv
[i
], "-s")) {
413 const char *committer
;
415 committer
= git_committer_info(1);
416 endpos
= strchr(committer
, '>');
418 die("bogos committer info %s\n", committer
);
419 add_signoff
= xmalloc(endpos
- committer
+ 2);
420 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
421 add_signoff
[endpos
- committer
+ 1] = 0;
423 else if (!strcmp(argv
[i
], "--attach"))
424 rev
.mime_boundary
= git_version_string
;
425 else if (!strncmp(argv
[i
], "--attach=", 9))
426 rev
.mime_boundary
= argv
[i
] + 9;
427 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
428 ignore_if_in_upstream
= 1;
429 else if (!strcmp(argv
[i
], "--thread"))
431 else if (!strncmp(argv
[i
], "--in-reply-to=", 14))
432 in_reply_to
= argv
[i
] + 14;
433 else if (!strcmp(argv
[i
], "--in-reply-to")) {
436 die("Need a Message-Id for --in-reply-to");
437 in_reply_to
= argv
[i
];
444 if (start_number
< 0)
446 if (numbered
&& keep_subject
)
447 die ("-n and -k are mutually exclusive.");
449 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
451 die ("unrecognized argument: %s", argv
[1]);
453 if (!rev
.diffopt
.output_format
)
454 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
456 if (!output_directory
)
457 output_directory
= prefix
;
459 if (output_directory
) {
461 die("standard output, or directory, which one?");
462 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
463 die("Could not create directory %s",
467 if (rev
.pending
.nr
== 1) {
468 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
472 if (ignore_if_in_upstream
)
473 get_patch_ids(&rev
, &patch_id_opts
, prefix
);
476 realstdout
= fdopen(dup(1), "w");
478 prepare_revision_walk(&rev
);
479 while ((commit
= get_revision(&rev
)) != NULL
) {
480 unsigned char sha1
[20];
483 if (commit
->parents
&& commit
->parents
->next
)
486 if (ignore_if_in_upstream
&&
487 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
492 list
= xrealloc(list
, nr
* sizeof(list
[0]));
493 list
[nr
- 1] = commit
;
497 rev
.total
= total
+ start_number
- 1;
498 rev
.add_signoff
= add_signoff
;
499 rev
.ref_message_id
= in_reply_to
;
503 rev
.nr
= total
- nr
+ (start_number
- 1);
504 /* Make the second and subsequent mails replies to the first */
506 if (nr
== (total
- 2)) {
507 strncpy(ref_message_id
, message_id
,
508 sizeof(ref_message_id
));
509 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
510 rev
.ref_message_id
= ref_message_id
;
512 gen_message_id(message_id
, sizeof(message_id
),
513 sha1_to_hex(commit
->object
.sha1
));
514 rev
.message_id
= message_id
;
517 reopen_stdout(commit
, rev
.nr
, keep_subject
);
518 shown
= log_tree_commit(&rev
, commit
);
519 free(commit
->buffer
);
520 commit
->buffer
= NULL
;
522 /* We put one extra blank line between formatted
523 * patches and this flag is used by log-tree code
524 * to see if it needs to emit a LF before showing
525 * the log; when using one file per patch, we do
526 * not want the extra blank line.
531 if (rev
.mime_boundary
)
532 printf("\n--%s%s--\n\n\n",
533 mime_boundary_leader
,
536 printf("-- \n%s\n\n", git_version_string
);
545 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
547 unsigned char sha1
[20];
548 if (get_sha1(arg
, sha1
) == 0) {
549 struct commit
*commit
= lookup_commit_reference(sha1
);
551 commit
->object
.flags
|= flags
;
552 add_pending_object(revs
, &commit
->object
, arg
);
559 static const char cherry_usage
[] =
560 "git-cherry [-v] <upstream> [<head>] [<limit>]";
561 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
563 struct rev_info revs
;
564 struct diff_options patch_id_opts
;
565 struct commit
*commit
;
566 struct commit_list
*list
= NULL
;
567 const char *upstream
;
568 const char *head
= "HEAD";
569 const char *limit
= NULL
;
572 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
592 init_revisions(&revs
, prefix
);
594 revs
.combine_merges
= 0;
595 revs
.ignore_merges
= 1;
596 revs
.diffopt
.recursive
= 1;
598 if (add_pending_commit(head
, &revs
, 0))
599 die("Unknown commit %s", head
);
600 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
601 die("Unknown commit %s", upstream
);
603 /* Don't say anything if head and upstream are the same. */
604 if (revs
.pending
.nr
== 2) {
605 struct object_array_entry
*o
= revs
.pending
.objects
;
606 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
610 get_patch_ids(&revs
, &patch_id_opts
, prefix
);
612 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
613 die("Unknown commit %s", limit
);
615 /* reverse the list of commits */
616 prepare_revision_walk(&revs
);
617 while ((commit
= get_revision(&revs
)) != NULL
) {
619 if (commit
->parents
&& commit
->parents
->next
)
622 commit_list_insert(commit
, &list
);
626 unsigned char sha1
[20];
630 if (!get_patch_id(commit
, &patch_id_opts
, sha1
) &&
635 static char buf
[16384];
636 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
637 buf
, sizeof(buf
), 0, NULL
, NULL
, 0);
638 printf("%c %s %s\n", sign
,
639 sha1_to_hex(commit
->object
.sha1
), buf
);
642 printf("%c %s\n", sign
,
643 sha1_to_hex(commit
->object
.sha1
));