2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
15 #include "reflog-walk.h"
16 #include "patch-ids.h"
18 #include "run-command.h"
21 /* Set a default date-time format for git log ("log.date" config variable) */
22 static const char *default_date_mode
= NULL
;
24 static int default_show_root
= 1;
25 static const char *fmt_patch_subject_prefix
= "PATCH";
26 static const char *fmt_pretty
;
28 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
30 int plen
= strlen(prefix
);
31 int nlen
= strlen(name
);
32 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + plen
+ nlen
);
33 memcpy(res
->name
, prefix
, plen
);
34 memcpy(res
->name
+ plen
, name
, nlen
+ 1);
35 res
->next
= add_decoration(&name_decoration
, obj
, res
);
38 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
40 struct object
*obj
= parse_object(sha1
);
43 add_name_decoration("", refname
, obj
);
44 while (obj
->type
== OBJ_TAG
) {
45 obj
= ((struct tag
*)obj
)->tagged
;
48 add_name_decoration("tag: ", refname
, obj
);
53 void load_ref_decorations(void)
58 for_each_ref(add_ref_decoration
, NULL
);
62 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
68 rev
->abbrev
= DEFAULT_ABBREV
;
69 rev
->commit_format
= CMIT_FMT_DEFAULT
;
71 get_commit_format(fmt_pretty
, rev
);
72 rev
->verbose_header
= 1;
73 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
74 rev
->show_root_diff
= default_show_root
;
75 rev
->subject_prefix
= fmt_patch_subject_prefix
;
77 if (default_date_mode
)
78 rev
->date_mode
= parse_date_format(default_date_mode
);
80 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
82 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
83 rev
->always_show_header
= 0;
84 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
85 rev
->always_show_header
= 0;
86 if (rev
->diffopt
.nr_paths
!= 1)
87 usage("git logs can only follow renames on one pathname at a time");
89 for (i
= 1; i
< argc
; i
++) {
90 const char *arg
= argv
[i
];
91 if (!strcmp(arg
, "--decorate")) {
92 load_ref_decorations();
95 die("unrecognized argument: %s", arg
);
100 * This gives a rough estimate for how many commits we
101 * will print out in the list.
103 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
108 struct commit
*commit
= list
->item
;
109 unsigned int flags
= commit
->object
.flags
;
111 if (!(flags
& (TREESAME
| UNINTERESTING
)))
117 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
119 if (rev
->shown_one
) {
121 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
122 putchar(rev
->diffopt
.line_termination
);
124 printf("Final output: %d %s\n", nr
, stage
);
127 struct itimerval early_output_timer
;
129 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
131 int i
= revs
->early_output
;
134 sort_in_topological_order(&list
, revs
->lifo
);
136 struct commit
*commit
= list
->item
;
137 switch (simplify_commit(revs
, commit
)) {
140 int n
= estimate_commit_count(revs
, list
);
141 show_early_header(revs
, "incomplete", n
);
144 log_tree_commit(revs
, commit
);
155 /* Did we already get enough commits for the early output? */
160 * ..if no, then repeat it twice a second until we
163 * NOTE! We don't use "it_interval", because if the
164 * reader isn't listening, we want our output to be
165 * throttled by the writing, and not have the timer
166 * trigger every second even if we're blocked on a
169 early_output_timer
.it_value
.tv_sec
= 0;
170 early_output_timer
.it_value
.tv_usec
= 500000;
171 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
174 static void early_output(int signal
)
176 show_early_output
= log_show_early
;
179 static void setup_early_output(struct rev_info
*rev
)
184 * Set up the signal handler, minimally intrusively:
185 * we only set a single volatile integer word (not
186 * using sigatomic_t - trying to avoid unnecessary
187 * system dependencies and headers), and using
190 memset(&sa
, 0, sizeof(sa
));
191 sa
.sa_handler
= early_output
;
192 sigemptyset(&sa
.sa_mask
);
193 sa
.sa_flags
= SA_RESTART
;
194 sigaction(SIGALRM
, &sa
, NULL
);
197 * If we can get the whole output in less than a
198 * tenth of a second, don't even bother doing the
199 * early-output thing..
201 * This is a one-time-only trigger.
203 early_output_timer
.it_value
.tv_sec
= 0;
204 early_output_timer
.it_value
.tv_usec
= 100000;
205 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
208 static void finish_early_output(struct rev_info
*rev
)
210 int n
= estimate_commit_count(rev
, rev
->commits
);
211 signal(SIGALRM
, SIG_IGN
);
212 show_early_header(rev
, "done", n
);
215 static int cmd_log_walk(struct rev_info
*rev
)
217 struct commit
*commit
;
219 if (rev
->early_output
)
220 setup_early_output(rev
);
222 if (prepare_revision_walk(rev
))
223 die("revision walk setup failed");
225 if (rev
->early_output
)
226 finish_early_output(rev
);
229 * For --check and --exit-code, the exit code is based on CHECK_FAILED
230 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
231 * retain that state information if replacing rev->diffopt in this loop
233 while ((commit
= get_revision(rev
)) != NULL
) {
234 log_tree_commit(rev
, commit
);
235 if (!rev
->reflog_info
) {
236 /* we allow cycles in reflog ancestry */
237 free(commit
->buffer
);
238 commit
->buffer
= NULL
;
240 free_commit_list(commit
->parents
);
241 commit
->parents
= NULL
;
243 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
244 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
247 return diff_result_code(&rev
->diffopt
, 0);
250 static int git_log_config(const char *var
, const char *value
, void *cb
)
252 if (!strcmp(var
, "format.pretty"))
253 return git_config_string(&fmt_pretty
, var
, value
);
254 if (!strcmp(var
, "format.subjectprefix"))
255 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
256 if (!strcmp(var
, "log.date"))
257 return git_config_string(&default_date_mode
, var
, value
);
258 if (!strcmp(var
, "log.showroot")) {
259 default_show_root
= git_config_bool(var
, value
);
262 return git_diff_ui_config(var
, value
, cb
);
265 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
269 git_config(git_log_config
, NULL
);
271 if (diff_use_color_default
== -1)
272 diff_use_color_default
= git_use_color_default
;
274 init_revisions(&rev
, prefix
);
276 rev
.simplify_history
= 0;
277 cmd_log_init(argc
, argv
, prefix
, &rev
);
278 if (!rev
.diffopt
.output_format
)
279 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
280 return cmd_log_walk(&rev
);
283 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
289 email_end
= memchr(buf
, '>', len
);
295 date
= strtoul(p
, &p
, 10);
298 tz
= (int)strtol(p
, NULL
, 10);
299 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end
- buf
), buf
,
300 show_date(date
, tz
, rev
->date_mode
));
303 static int show_object(const unsigned char *sha1
, int show_tag_object
,
304 struct rev_info
*rev
)
307 enum object_type type
;
308 char *buf
= read_sha1_file(sha1
, &type
, &size
);
312 return error("Could not read object %s", sha1_to_hex(sha1
));
315 while (offset
< size
&& buf
[offset
] != '\n') {
316 int new_offset
= offset
+ 1;
317 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
319 if (!prefixcmp(buf
+ offset
, "tagger "))
320 show_tagger(buf
+ offset
+ 7,
321 new_offset
- offset
- 7, rev
);
326 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
331 static int show_tree_object(const unsigned char *sha1
,
332 const char *base
, int baselen
,
333 const char *pathname
, unsigned mode
, int stage
, void *context
)
335 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
339 int cmd_show(int argc
, const char **argv
, const char *prefix
)
342 struct object_array_entry
*objects
;
343 int i
, count
, ret
= 0;
345 git_config(git_log_config
, NULL
);
347 if (diff_use_color_default
== -1)
348 diff_use_color_default
= git_use_color_default
;
350 init_revisions(&rev
, prefix
);
352 rev
.combine_merges
= 1;
353 rev
.dense_combined_merges
= 1;
354 rev
.always_show_header
= 1;
355 rev
.ignore_merges
= 0;
357 cmd_log_init(argc
, argv
, prefix
, &rev
);
359 count
= rev
.pending
.nr
;
360 objects
= rev
.pending
.objects
;
361 for (i
= 0; i
< count
&& !ret
; i
++) {
362 struct object
*o
= objects
[i
].item
;
363 const char *name
= objects
[i
].name
;
366 ret
= show_object(o
->sha1
, 0, NULL
);
369 struct tag
*t
= (struct tag
*)o
;
371 printf("%stag %s%s\n",
372 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
374 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
375 ret
= show_object(o
->sha1
, 1, &rev
);
376 objects
[i
].item
= parse_object(t
->tagged
->sha1
);
381 printf("%stree %s%s\n\n",
382 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
384 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
385 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
386 show_tree_object
, NULL
);
389 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
390 rev
.pending
.objects
= NULL
;
391 add_object_array(o
, name
, &rev
.pending
);
392 ret
= cmd_log_walk(&rev
);
395 ret
= error("Unknown type: %d", o
->type
);
403 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
405 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
409 git_config(git_log_config
, NULL
);
411 if (diff_use_color_default
== -1)
412 diff_use_color_default
= git_use_color_default
;
414 init_revisions(&rev
, prefix
);
415 init_reflog_walk(&rev
.reflog_info
);
416 rev
.abbrev_commit
= 1;
417 rev
.verbose_header
= 1;
418 cmd_log_init(argc
, argv
, prefix
, &rev
);
421 * This means that we override whatever commit format the user gave
422 * on the cmd line. Sad, but cmd_log_init() currently doesn't
423 * allow us to set a different default.
425 rev
.commit_format
= CMIT_FMT_ONELINE
;
426 rev
.use_terminator
= 1;
427 rev
.always_show_header
= 1;
430 * We get called through "git reflog", so unlike the other log
431 * routines, we need to set up our pager manually..
435 return cmd_log_walk(&rev
);
438 int cmd_log(int argc
, const char **argv
, const char *prefix
)
442 git_config(git_log_config
, NULL
);
444 if (diff_use_color_default
== -1)
445 diff_use_color_default
= git_use_color_default
;
447 init_revisions(&rev
, prefix
);
448 rev
.always_show_header
= 1;
449 cmd_log_init(argc
, argv
, prefix
, &rev
);
450 return cmd_log_walk(&rev
);
454 #define FORMAT_PATCH_NAME_MAX 64
456 static int istitlechar(char c
)
458 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
459 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
462 static const char *fmt_patch_suffix
= ".patch";
463 static int numbered
= 0;
464 static int auto_number
= 0;
466 static char **extra_hdr
;
467 static int extra_hdr_nr
;
468 static int extra_hdr_alloc
;
470 static char **extra_to
;
471 static int extra_to_nr
;
472 static int extra_to_alloc
;
474 static char **extra_cc
;
475 static int extra_cc_nr
;
476 static int extra_cc_alloc
;
478 static void add_header(const char *value
)
480 int len
= strlen(value
);
481 while (len
&& value
[len
- 1] == '\n')
483 if (!strncasecmp(value
, "to: ", 4)) {
484 ALLOC_GROW(extra_to
, extra_to_nr
+ 1, extra_to_alloc
);
485 extra_to
[extra_to_nr
++] = xstrndup(value
+ 4, len
- 4);
488 if (!strncasecmp(value
, "cc: ", 4)) {
489 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
490 extra_cc
[extra_cc_nr
++] = xstrndup(value
+ 4, len
- 4);
493 ALLOC_GROW(extra_hdr
, extra_hdr_nr
+ 1, extra_hdr_alloc
);
494 extra_hdr
[extra_hdr_nr
++] = xstrndup(value
, len
);
497 static int git_format_config(const char *var
, const char *value
, void *cb
)
499 if (!strcmp(var
, "format.headers")) {
501 die("format.headers without value");
505 if (!strcmp(var
, "format.suffix"))
506 return git_config_string(&fmt_patch_suffix
, var
, value
);
507 if (!strcmp(var
, "format.cc")) {
509 return config_error_nonbool(var
);
510 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
511 extra_cc
[extra_cc_nr
++] = xstrdup(value
);
514 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
517 if (!strcmp(var
, "format.numbered")) {
518 if (value
&& !strcasecmp(value
, "auto")) {
522 numbered
= git_config_bool(var
, value
);
526 return git_log_config(var
, value
, cb
);
530 static const char *get_oneline_for_filename(struct commit
*commit
,
533 static char filename
[PATH_MAX
];
536 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
538 sol
= strstr(commit
->buffer
, "\n\n");
545 /* strip [PATCH] or [PATCH blabla] */
546 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
547 char *eos
= strchr(sol
+ 6, ']');
549 while (isspace(*eos
))
556 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
557 len
< sizeof(filename
) - suffix_len
&&
558 sol
[j
] && sol
[j
] != '\n';
560 if (istitlechar(sol
[j
])) {
562 filename
[len
++] = '-';
565 filename
[len
++] = sol
[j
];
567 while (sol
[j
+ 1] == '.')
572 while (filename
[len
- 1] == '.'
573 || filename
[len
- 1] == '-')
575 filename
[len
] = '\0';
580 static FILE *realstdout
= NULL
;
581 static const char *output_directory
= NULL
;
583 static int reopen_stdout(const char *oneline
, int nr
, int total
)
585 char filename
[PATH_MAX
];
587 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
589 if (output_directory
) {
590 len
= snprintf(filename
, sizeof(filename
), "%s",
593 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
594 return error("name of output directory is too long");
595 if (filename
[len
- 1] != '/')
596 filename
[len
++] = '/';
600 len
+= sprintf(filename
+ len
, "%d", nr
);
602 len
+= sprintf(filename
+ len
, "%04d-", nr
);
603 len
+= snprintf(filename
+ len
, sizeof(filename
) - len
- 1
604 - suffix_len
, "%s", oneline
);
605 strcpy(filename
+ len
, fmt_patch_suffix
);
608 fprintf(realstdout
, "%s\n", filename
);
609 if (freopen(filename
, "w", stdout
) == NULL
)
610 return error("Cannot open patch file %s",filename
);
615 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
617 struct rev_info check_rev
;
618 struct commit
*commit
;
619 struct object
*o1
, *o2
;
620 unsigned flags1
, flags2
;
622 if (rev
->pending
.nr
!= 2)
623 die("Need exactly one range.");
625 o1
= rev
->pending
.objects
[0].item
;
627 o2
= rev
->pending
.objects
[1].item
;
630 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
635 /* given a range a..b get all patch ids for b..a */
636 init_revisions(&check_rev
, prefix
);
637 o1
->flags
^= UNINTERESTING
;
638 o2
->flags
^= UNINTERESTING
;
639 add_pending_object(&check_rev
, o1
, "o1");
640 add_pending_object(&check_rev
, o2
, "o2");
641 if (prepare_revision_walk(&check_rev
))
642 die("revision walk setup failed");
644 while ((commit
= get_revision(&check_rev
)) != NULL
) {
646 if (commit
->parents
&& commit
->parents
->next
)
649 add_commit_patch_id(commit
, ids
);
652 /* reset for next revision walk */
653 clear_commit_marks((struct commit
*)o1
,
654 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
655 clear_commit_marks((struct commit
*)o2
,
656 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
661 static void gen_message_id(struct rev_info
*info
, char *base
)
663 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
664 const char *email_start
= strrchr(committer
, '<');
665 const char *email_end
= strrchr(committer
, '>');
667 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
668 die("Could not extract email from committer identity.");
669 strbuf_init(&buf
, 0);
670 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
671 (unsigned long) time(NULL
),
672 (int)(email_end
- email_start
- 1), email_start
+ 1);
673 info
->message_id
= strbuf_detach(&buf
, NULL
);
676 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
677 int numbered
, int numbered_files
,
678 struct commit
*origin
,
679 int nr
, struct commit
**list
, struct commit
*head
)
681 const char *committer
;
683 const char *subject_start
= NULL
;
684 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
686 const char *extra_headers
= rev
->extra_headers
;
690 const char *encoding
= "utf-8";
691 struct diff_options opts
;
692 int need_8bit_cte
= 0;
694 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
695 die("Cover letter needs email format");
697 if (!use_stdout
&& reopen_stdout(numbered_files
?
698 NULL
: "cover-letter", 0, rev
->total
))
701 head_sha1
= sha1_to_hex(head
->object
.sha1
);
703 log_write_email_headers(rev
, head_sha1
, &subject_start
, &extra_headers
,
706 committer
= git_committer_info(0);
710 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
712 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
713 encoding
, need_8bit_cte
);
714 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
715 printf("%s\n", sb
.buf
);
724 for (i
= 0; i
< nr
; i
++)
725 shortlog_add_commit(&log
, list
[i
]);
727 shortlog_output(&log
);
730 * We can only do diffstat with a unique reference point
735 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
736 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
738 diff_setup_done(&opts
);
740 diff_tree_sha1(origin
->tree
->object
.sha1
,
741 head
->tree
->object
.sha1
,
749 static const char *clean_message_id(const char *msg_id
)
752 const char *a
, *z
, *m
;
755 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
760 if (!isspace(ch
) && (ch
!= '>'))
765 die("insane in-reply-to: %s", msg_id
);
768 return xmemdupz(a
, z
- a
);
771 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
773 struct commit
*commit
;
774 struct commit
**list
= NULL
;
776 int nr
= 0, total
, i
, j
;
778 int start_number
= -1;
779 int keep_subject
= 0;
780 int numbered_files
= 0; /* _just_ numbers */
781 int subject_prefix
= 0;
782 int ignore_if_in_upstream
= 0;
784 int cover_letter
= 0;
785 int boundary_count
= 0;
786 int no_binary_diff
= 0;
787 struct commit
*origin
= NULL
, *head
= NULL
;
788 const char *in_reply_to
= NULL
;
789 struct patch_ids ids
;
790 char *add_signoff
= NULL
;
793 git_config(git_format_config
, NULL
);
794 init_revisions(&rev
, prefix
);
795 rev
.commit_format
= CMIT_FMT_EMAIL
;
796 rev
.verbose_header
= 1;
798 rev
.combine_merges
= 0;
799 rev
.ignore_merges
= 1;
800 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
802 rev
.subject_prefix
= fmt_patch_subject_prefix
;
805 * Parse the arguments before setup_revisions(), or something
806 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
807 * possibly a valid SHA1.
809 for (i
= 1, j
= 1; i
< argc
; i
++) {
810 if (!strcmp(argv
[i
], "--stdout"))
812 else if (!strcmp(argv
[i
], "-n") ||
813 !strcmp(argv
[i
], "--numbered"))
815 else if (!strcmp(argv
[i
], "-N") ||
816 !strcmp(argv
[i
], "--no-numbered")) {
820 else if (!prefixcmp(argv
[i
], "--start-number="))
821 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
822 else if (!strcmp(argv
[i
], "--numbered-files"))
824 else if (!strcmp(argv
[i
], "--start-number")) {
827 die("Need a number for --start-number");
828 start_number
= strtol(argv
[i
], NULL
, 10);
830 else if (!prefixcmp(argv
[i
], "--cc=")) {
831 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
832 extra_cc
[extra_cc_nr
++] = xstrdup(argv
[i
] + 5);
834 else if (!strcmp(argv
[i
], "-k") ||
835 !strcmp(argv
[i
], "--keep-subject")) {
839 else if (!strcmp(argv
[i
], "--output-directory") ||
840 !strcmp(argv
[i
], "-o")) {
843 die("Which directory?");
844 if (output_directory
)
845 die("Two output directories?");
846 output_directory
= argv
[i
];
848 else if (!strcmp(argv
[i
], "--signoff") ||
849 !strcmp(argv
[i
], "-s")) {
850 const char *committer
;
852 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
853 endpos
= strchr(committer
, '>');
855 die("bogos committer info %s\n", committer
);
856 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
858 else if (!strcmp(argv
[i
], "--attach")) {
859 rev
.mime_boundary
= git_version_string
;
862 else if (!prefixcmp(argv
[i
], "--attach=")) {
863 rev
.mime_boundary
= argv
[i
] + 9;
866 else if (!strcmp(argv
[i
], "--inline")) {
867 rev
.mime_boundary
= git_version_string
;
870 else if (!prefixcmp(argv
[i
], "--inline=")) {
871 rev
.mime_boundary
= argv
[i
] + 9;
874 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
875 ignore_if_in_upstream
= 1;
876 else if (!strcmp(argv
[i
], "--thread"))
878 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
879 in_reply_to
= argv
[i
] + 14;
880 else if (!strcmp(argv
[i
], "--in-reply-to")) {
883 die("Need a Message-Id for --in-reply-to");
884 in_reply_to
= argv
[i
];
885 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
887 rev
.subject_prefix
= argv
[i
] + 17;
888 } else if (!prefixcmp(argv
[i
], "--suffix="))
889 fmt_patch_suffix
= argv
[i
] + 9;
890 else if (!strcmp(argv
[i
], "--cover-letter"))
892 else if (!strcmp(argv
[i
], "--no-binary"))
899 strbuf_init(&buf
, 0);
901 for (i
= 0; i
< extra_hdr_nr
; i
++) {
902 strbuf_addstr(&buf
, extra_hdr
[i
]);
903 strbuf_addch(&buf
, '\n');
907 strbuf_addstr(&buf
, "To: ");
908 for (i
= 0; i
< extra_to_nr
; i
++) {
910 strbuf_addstr(&buf
, " ");
911 strbuf_addstr(&buf
, extra_to
[i
]);
912 if (i
+ 1 < extra_to_nr
)
913 strbuf_addch(&buf
, ',');
914 strbuf_addch(&buf
, '\n');
918 strbuf_addstr(&buf
, "Cc: ");
919 for (i
= 0; i
< extra_cc_nr
; i
++) {
921 strbuf_addstr(&buf
, " ");
922 strbuf_addstr(&buf
, extra_cc
[i
]);
923 if (i
+ 1 < extra_cc_nr
)
924 strbuf_addch(&buf
, ',');
925 strbuf_addch(&buf
, '\n');
928 rev
.extra_headers
= strbuf_detach(&buf
, 0);
930 if (start_number
< 0)
932 if (numbered
&& keep_subject
)
933 die ("-n and -k are mutually exclusive.");
934 if (keep_subject
&& subject_prefix
)
935 die ("--subject-prefix and -k are mutually exclusive.");
936 if (numbered_files
&& use_stdout
)
937 die ("--numbered-files and --stdout are mutually exclusive.");
939 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
941 die ("unrecognized argument: %s", argv
[1]);
943 if (!rev
.diffopt
.output_format
944 || rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
)
945 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
947 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
948 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
950 if (!output_directory
&& !use_stdout
)
951 output_directory
= prefix
;
953 if (output_directory
) {
955 die("standard output, or directory, which one?");
956 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
957 die("Could not create directory %s",
961 if (rev
.pending
.nr
== 1) {
962 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
964 * This is traditional behaviour of "git format-patch
965 * origin" that prepares what the origin side still
968 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
969 add_head_to_pending(&rev
);
972 * Otherwise, it is "format-patch -22 HEAD", and/or
973 * "format-patch --root HEAD". The user wants
974 * get_revision() to do the usual traversal.
978 /* remember the range */
980 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
981 struct object
*o
= rev
.pending
.objects
[i
].item
;
982 if (!(o
->flags
& UNINTERESTING
))
983 head
= (struct commit
*)o
;
985 /* We can't generate a cover letter without any patches */
990 if (ignore_if_in_upstream
)
991 get_patch_ids(&rev
, &ids
, prefix
);
994 realstdout
= xfdopen(xdup(1), "w");
996 if (prepare_revision_walk(&rev
))
997 die("revision walk setup failed");
999 while ((commit
= get_revision(&rev
)) != NULL
) {
1000 if (commit
->object
.flags
& BOUNDARY
) {
1002 origin
= (boundary_count
== 1) ? commit
: NULL
;
1007 if (commit
->parents
&& commit
->parents
->next
)
1010 if (ignore_if_in_upstream
&&
1011 has_commit_patch_id(commit
, &ids
))
1015 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1016 list
[nr
- 1] = commit
;
1019 if (!keep_subject
&& auto_number
&& total
> 1)
1022 rev
.total
= total
+ start_number
- 1;
1024 rev
.ref_message_id
= clean_message_id(in_reply_to
);
1027 gen_message_id(&rev
, "cover");
1028 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1029 origin
, nr
, list
, head
);
1033 rev
.add_signoff
= add_signoff
;
1037 rev
.nr
= total
- nr
+ (start_number
- 1);
1038 /* Make the second and subsequent mails replies to the first */
1040 /* Have we already had a message ID? */
1041 if (rev
.message_id
) {
1043 * If we've got the ID to be a reply
1044 * to, discard the current ID;
1045 * otherwise, make everything a reply
1048 if (rev
.ref_message_id
)
1049 free(rev
.message_id
);
1051 rev
.ref_message_id
= rev
.message_id
;
1053 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1055 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
:
1056 get_oneline_for_filename(commit
, keep_subject
),
1058 die("Failed to create output files");
1059 shown
= log_tree_commit(&rev
, commit
);
1060 free(commit
->buffer
);
1061 commit
->buffer
= NULL
;
1063 /* We put one extra blank line between formatted
1064 * patches and this flag is used by log-tree code
1065 * to see if it needs to emit a LF before showing
1066 * the log; when using one file per patch, we do
1067 * not want the extra blank line.
1072 if (rev
.mime_boundary
)
1073 printf("\n--%s%s--\n\n\n",
1074 mime_boundary_leader
,
1077 printf("-- \n%s\n\n", git_version_string
);
1083 if (ignore_if_in_upstream
)
1084 free_patch_ids(&ids
);
1088 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1090 unsigned char sha1
[20];
1091 if (get_sha1(arg
, sha1
) == 0) {
1092 struct commit
*commit
= lookup_commit_reference(sha1
);
1094 commit
->object
.flags
|= flags
;
1095 add_pending_object(revs
, &commit
->object
, arg
);
1102 static const char cherry_usage
[] =
1103 "git cherry [-v] <upstream> [<head>] [<limit>]";
1104 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1106 struct rev_info revs
;
1107 struct patch_ids ids
;
1108 struct commit
*commit
;
1109 struct commit_list
*list
= NULL
;
1110 const char *upstream
;
1111 const char *head
= "HEAD";
1112 const char *limit
= NULL
;
1115 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
1132 usage(cherry_usage
);
1135 init_revisions(&revs
, prefix
);
1137 revs
.combine_merges
= 0;
1138 revs
.ignore_merges
= 1;
1139 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1141 if (add_pending_commit(head
, &revs
, 0))
1142 die("Unknown commit %s", head
);
1143 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1144 die("Unknown commit %s", upstream
);
1146 /* Don't say anything if head and upstream are the same. */
1147 if (revs
.pending
.nr
== 2) {
1148 struct object_array_entry
*o
= revs
.pending
.objects
;
1149 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1153 get_patch_ids(&revs
, &ids
, prefix
);
1155 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1156 die("Unknown commit %s", limit
);
1158 /* reverse the list of commits */
1159 if (prepare_revision_walk(&revs
))
1160 die("revision walk setup failed");
1161 while ((commit
= get_revision(&revs
)) != NULL
) {
1163 if (commit
->parents
&& commit
->parents
->next
)
1166 commit_list_insert(commit
, &list
);
1172 commit
= list
->item
;
1173 if (has_commit_patch_id(commit
, &ids
))
1178 strbuf_init(&buf
, 0);
1179 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1180 &buf
, 0, NULL
, NULL
, 0, 0);
1181 printf("%c %s %s\n", sign
,
1182 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
1183 strbuf_release(&buf
);
1186 printf("%c %s\n", sign
,
1187 sha1_to_hex(commit
->object
.sha1
));
1193 free_patch_ids(&ids
);