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
,
23 rev
->abbrev
= DEFAULT_ABBREV
;
24 rev
->commit_format
= CMIT_FMT_DEFAULT
;
25 rev
->verbose_header
= 1;
26 rev
->show_root_diff
= default_show_root
;
27 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
28 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
29 rev
->always_show_header
= 0;
31 die("unrecognized argument: %s", argv
[1]);
34 static int cmd_log_walk(struct rev_info
*rev
)
36 struct commit
*commit
;
38 prepare_revision_walk(rev
);
39 while ((commit
= get_revision(rev
)) != NULL
) {
40 log_tree_commit(rev
, commit
);
42 commit
->buffer
= NULL
;
43 free_commit_list(commit
->parents
);
44 commit
->parents
= NULL
;
49 static int git_log_config(const char *var
, const char *value
)
51 if (!strcmp(var
, "log.showroot")) {
52 default_show_root
= git_config_bool(var
, value
);
55 return git_diff_ui_config(var
, value
);
58 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
62 git_config(git_log_config
);
63 init_revisions(&rev
, prefix
);
65 rev
.diffopt
.recursive
= 1;
66 rev
.simplify_history
= 0;
67 cmd_log_init(argc
, argv
, prefix
, &rev
);
68 if (!rev
.diffopt
.output_format
)
69 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
70 return cmd_log_walk(&rev
);
73 static int show_object(const unsigned char *sha1
, int suppress_header
)
77 char *buf
= read_sha1_file(sha1
, type
, &size
);
81 return error("Could not read object %s", sha1_to_hex(sha1
));
84 while (offset
< size
&& buf
[offset
++] != '\n') {
85 int new_offset
= offset
;
86 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
92 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
97 static int show_tree_object(const unsigned char *sha1
,
98 const char *base
, int baselen
,
99 const char *pathname
, unsigned mode
, int stage
)
101 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
105 int cmd_show(int argc
, const char **argv
, const char *prefix
)
108 struct object_array_entry
*objects
;
109 int i
, count
, ret
= 0;
111 git_config(git_log_config
);
112 init_revisions(&rev
, prefix
);
114 rev
.diffopt
.recursive
= 1;
115 rev
.combine_merges
= 1;
116 rev
.dense_combined_merges
= 1;
117 rev
.always_show_header
= 1;
118 rev
.ignore_merges
= 0;
120 cmd_log_init(argc
, argv
, prefix
, &rev
);
122 count
= rev
.pending
.nr
;
123 objects
= rev
.pending
.objects
;
124 for (i
= 0; i
< count
&& !ret
; i
++) {
125 struct object
*o
= objects
[i
].item
;
126 const char *name
= objects
[i
].name
;
129 ret
= show_object(o
->sha1
, 0);
132 struct tag
*t
= (struct tag
*)o
;
134 printf("%stag %s%s\n\n",
135 diff_get_color(rev
.diffopt
.color_diff
,
138 diff_get_color(rev
.diffopt
.color_diff
,
140 ret
= show_object(o
->sha1
, 1);
141 objects
[i
].item
= (struct object
*)t
->tagged
;
146 printf("%stree %s%s\n\n",
147 diff_get_color(rev
.diffopt
.color_diff
,
150 diff_get_color(rev
.diffopt
.color_diff
,
152 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
156 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
157 rev
.pending
.objects
= NULL
;
158 add_object_array(o
, name
, &rev
.pending
);
159 ret
= cmd_log_walk(&rev
);
162 ret
= error("Unknown type: %d", o
->type
);
169 int cmd_log(int argc
, const char **argv
, const char *prefix
)
173 git_config(git_log_config
);
174 init_revisions(&rev
, prefix
);
175 rev
.always_show_header
= 1;
176 cmd_log_init(argc
, argv
, prefix
, &rev
);
177 return cmd_log_walk(&rev
);
180 static int istitlechar(char c
)
182 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
183 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
186 static char *extra_headers
= NULL
;
187 static int extra_headers_size
= 0;
189 static int git_format_config(const char *var
, const char *value
)
191 if (!strcmp(var
, "format.headers")) {
192 int len
= strlen(value
);
193 extra_headers_size
+= len
+ 1;
194 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
195 extra_headers
[extra_headers_size
- len
- 1] = 0;
196 strcat(extra_headers
, value
);
199 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
202 return git_log_config(var
, value
);
206 static FILE *realstdout
= NULL
;
207 static const char *output_directory
= NULL
;
209 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
215 if (output_directory
) {
216 strlcpy(filename
, output_directory
, 1010);
217 len
= strlen(filename
);
218 if (filename
[len
- 1] != '/')
219 filename
[len
++] = '/';
222 sprintf(filename
+ len
, "%04d", nr
);
223 len
= strlen(filename
);
225 sol
= strstr(commit
->buffer
, "\n\n");
230 /* strip [PATCH] or [PATCH blabla] */
231 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
232 char *eos
= strchr(sol
+ 6, ']');
234 while (isspace(*eos
))
240 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
241 if (istitlechar(sol
[j
])) {
243 filename
[len
++] = '-';
246 filename
[len
++] = sol
[j
];
248 while (sol
[j
+ 1] == '.')
253 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
256 strcpy(filename
+ len
, ".txt");
257 fprintf(realstdout
, "%s\n", filename
);
258 freopen(filename
, "w", stdout
);
261 static int get_patch_id(struct commit
*commit
, struct diff_options
*options
,
265 diff_tree_sha1(commit
->parents
->item
->object
.sha1
,
266 commit
->object
.sha1
, "", options
);
268 diff_root_tree_sha1(commit
->object
.sha1
, "", options
);
269 diffcore_std(options
);
270 return diff_flush_patch_id(options
, sha1
);
273 static void get_patch_ids(struct rev_info
*rev
, struct diff_options
*options
, const char *prefix
)
275 struct rev_info check_rev
;
276 struct commit
*commit
;
277 struct object
*o1
, *o2
;
278 unsigned flags1
, flags2
;
279 unsigned char sha1
[20];
281 if (rev
->pending
.nr
!= 2)
282 die("Need exactly one range.");
284 o1
= rev
->pending
.objects
[0].item
;
286 o2
= rev
->pending
.objects
[1].item
;
289 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
293 options
->recursive
= 1;
294 if (diff_setup_done(options
) < 0)
295 die("diff_setup_done failed");
297 /* given a range a..b get all patch ids for b..a */
298 init_revisions(&check_rev
, prefix
);
299 o1
->flags
^= UNINTERESTING
;
300 o2
->flags
^= UNINTERESTING
;
301 add_pending_object(&check_rev
, o1
, "o1");
302 add_pending_object(&check_rev
, o2
, "o2");
303 prepare_revision_walk(&check_rev
);
305 while ((commit
= get_revision(&check_rev
)) != NULL
) {
307 if (commit
->parents
&& commit
->parents
->next
)
310 if (!get_patch_id(commit
, options
, sha1
))
311 created_object(sha1
, xcalloc(1, sizeof(struct object
)));
314 /* reset for next revision walk */
315 clear_commit_marks((struct commit
*)o1
,
316 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
317 clear_commit_marks((struct commit
*)o2
,
318 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
323 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
325 const char *committer
= git_committer_info(1);
326 const char *email_start
= strrchr(committer
, '<');
327 const char *email_end
= strrchr(committer
, '>');
328 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
329 die("Could not extract email from committer identity.");
330 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
331 (unsigned long) time(NULL
),
332 (int)(email_end
- email_start
- 1), email_start
+ 1);
335 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
337 struct commit
*commit
;
338 struct commit
**list
= NULL
;
340 int nr
= 0, total
, i
, j
;
343 int start_number
= -1;
344 int keep_subject
= 0;
345 int ignore_if_in_upstream
= 0;
347 const char *in_reply_to
= NULL
;
348 struct diff_options patch_id_opts
;
349 char *add_signoff
= NULL
;
350 char message_id
[1024];
351 char ref_message_id
[1024];
354 git_config(git_format_config
);
355 init_revisions(&rev
, prefix
);
356 rev
.commit_format
= CMIT_FMT_EMAIL
;
357 rev
.verbose_header
= 1;
359 rev
.combine_merges
= 0;
360 rev
.ignore_merges
= 1;
361 rev
.diffopt
.msg_sep
= "";
362 rev
.diffopt
.recursive
= 1;
364 rev
.extra_headers
= extra_headers
;
367 * Parse the arguments before setup_revisions(), or something
368 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
369 * possibly a valid SHA1.
371 for (i
= 1, j
= 1; i
< argc
; i
++) {
372 if (!strcmp(argv
[i
], "--stdout"))
374 else if (!strcmp(argv
[i
], "-n") ||
375 !strcmp(argv
[i
], "--numbered"))
377 else if (!strncmp(argv
[i
], "--start-number=", 15))
378 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
379 else if (!strcmp(argv
[i
], "--start-number")) {
382 die("Need a number for --start-number");
383 start_number
= strtol(argv
[i
], NULL
, 10);
385 else if (!strcmp(argv
[i
], "-k") ||
386 !strcmp(argv
[i
], "--keep-subject")) {
390 else if (!strcmp(argv
[i
], "--output-directory") ||
391 !strcmp(argv
[i
], "-o")) {
394 die("Which directory?");
395 if (output_directory
)
396 die("Two output directories?");
397 output_directory
= argv
[i
];
399 else if (!strcmp(argv
[i
], "--signoff") ||
400 !strcmp(argv
[i
], "-s")) {
401 const char *committer
;
403 committer
= git_committer_info(1);
404 endpos
= strchr(committer
, '>');
406 die("bogos committer info %s\n", committer
);
407 add_signoff
= xmalloc(endpos
- committer
+ 2);
408 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
409 add_signoff
[endpos
- committer
+ 1] = 0;
411 else if (!strcmp(argv
[i
], "--attach"))
412 rev
.mime_boundary
= git_version_string
;
413 else if (!strncmp(argv
[i
], "--attach=", 9))
414 rev
.mime_boundary
= argv
[i
] + 9;
415 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
416 ignore_if_in_upstream
= 1;
417 else if (!strcmp(argv
[i
], "--thread"))
419 else if (!strncmp(argv
[i
], "--in-reply-to=", 14))
420 in_reply_to
= argv
[i
] + 14;
421 else if (!strcmp(argv
[i
], "--in-reply-to")) {
424 die("Need a Message-Id for --in-reply-to");
425 in_reply_to
= argv
[i
];
432 if (start_number
< 0)
434 if (numbered
&& keep_subject
)
435 die ("-n and -k are mutually exclusive.");
437 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
439 die ("unrecognized argument: %s", argv
[1]);
441 if (!rev
.diffopt
.output_format
)
442 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
444 if (!output_directory
)
445 output_directory
= prefix
;
447 if (output_directory
) {
449 die("standard output, or directory, which one?");
450 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
451 die("Could not create directory %s",
455 if (rev
.pending
.nr
== 1) {
456 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
460 if (ignore_if_in_upstream
)
461 get_patch_ids(&rev
, &patch_id_opts
, prefix
);
464 realstdout
= fdopen(dup(1), "w");
466 prepare_revision_walk(&rev
);
467 while ((commit
= get_revision(&rev
)) != NULL
) {
468 unsigned char sha1
[20];
471 if (commit
->parents
&& commit
->parents
->next
)
474 if (ignore_if_in_upstream
&&
475 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
480 list
= xrealloc(list
, nr
* sizeof(list
[0]));
481 list
[nr
- 1] = commit
;
485 rev
.total
= total
+ start_number
- 1;
486 rev
.add_signoff
= add_signoff
;
487 rev
.ref_message_id
= in_reply_to
;
491 rev
.nr
= total
- nr
+ (start_number
- 1);
492 /* Make the second and subsequent mails replies to the first */
494 if (nr
== (total
- 2)) {
495 strncpy(ref_message_id
, message_id
,
496 sizeof(ref_message_id
));
497 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
498 rev
.ref_message_id
= ref_message_id
;
500 gen_message_id(message_id
, sizeof(message_id
),
501 sha1_to_hex(commit
->object
.sha1
));
502 rev
.message_id
= message_id
;
505 reopen_stdout(commit
, rev
.nr
, keep_subject
);
506 shown
= log_tree_commit(&rev
, commit
);
507 free(commit
->buffer
);
508 commit
->buffer
= NULL
;
510 /* We put one extra blank line between formatted
511 * patches and this flag is used by log-tree code
512 * to see if it needs to emit a LF before showing
513 * the log; when using one file per patch, we do
514 * not want the extra blank line.
519 if (rev
.mime_boundary
)
520 printf("\n--%s%s--\n\n\n",
521 mime_boundary_leader
,
524 printf("-- \n%s\n\n", git_version_string
);
533 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
535 unsigned char sha1
[20];
536 if (get_sha1(arg
, sha1
) == 0) {
537 struct commit
*commit
= lookup_commit_reference(sha1
);
539 commit
->object
.flags
|= flags
;
540 add_pending_object(revs
, &commit
->object
, arg
);
547 static const char cherry_usage
[] =
548 "git-cherry [-v] <upstream> [<head>] [<limit>]";
549 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
551 struct rev_info revs
;
552 struct diff_options patch_id_opts
;
553 struct commit
*commit
;
554 struct commit_list
*list
= NULL
;
555 const char *upstream
;
556 const char *head
= "HEAD";
557 const char *limit
= NULL
;
560 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
580 init_revisions(&revs
, prefix
);
582 revs
.combine_merges
= 0;
583 revs
.ignore_merges
= 1;
584 revs
.diffopt
.recursive
= 1;
586 if (add_pending_commit(head
, &revs
, 0))
587 die("Unknown commit %s", head
);
588 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
589 die("Unknown commit %s", upstream
);
591 /* Don't say anything if head and upstream are the same. */
592 if (revs
.pending
.nr
== 2) {
593 struct object_array_entry
*o
= revs
.pending
.objects
;
594 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
598 get_patch_ids(&revs
, &patch_id_opts
, prefix
);
600 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
601 die("Unknown commit %s", limit
);
603 /* reverse the list of commits */
604 prepare_revision_walk(&revs
);
605 while ((commit
= get_revision(&revs
)) != NULL
) {
607 if (commit
->parents
&& commit
->parents
->next
)
610 commit_list_insert(commit
, &list
);
614 unsigned char sha1
[20];
618 if (!get_patch_id(commit
, &patch_id_opts
, sha1
) &&
623 static char buf
[16384];
624 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
625 buf
, sizeof(buf
), 0, NULL
, NULL
, 0);
626 printf("%c %s %s\n", sign
,
627 sha1_to_hex(commit
->object
.sha1
), buf
);
630 printf("%c %s\n", sign
,
631 sha1_to_hex(commit
->object
.sha1
));