Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git log" and related commands (show, whatchanged) |
| 3 | * |
| 4 | * (C) Copyright 2006 Linus Torvalds |
| 5 | * 2006 Junio Hamano |
| 6 | */ |
| 7 | #include "cache.h" |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 8 | #include "color.h" |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 9 | #include "commit.h" |
| 10 | #include "diff.h" |
| 11 | #include "revision.h" |
| 12 | #include "log-tree.h" |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 13 | #include "builtin.h" |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 14 | #include "tag.h" |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 15 | #include "reflog-walk.h" |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 16 | #include "patch-ids.h" |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 17 | #include "run-command.h" |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 18 | #include "shortlog.h" |
Markus Heidelberg | f296802 | 2008-12-29 18:45:20 +0100 | [diff] [blame] | 19 | #include "remote.h" |
Thomas Rast | b079c50 | 2009-02-19 22:26:31 +0100 | [diff] [blame] | 20 | #include "string-list.h" |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 21 | #include "parse-options.h" |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 22 | |
Heikki Orsila | dd0ffd5 | 2008-05-22 18:24:07 +0300 | [diff] [blame] | 23 | /* Set a default date-time format for git log ("log.date" config variable) */ |
| 24 | static const char *default_date_mode = NULL; |
| 25 | |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 26 | static int default_show_root = 1; |
Alex Riesen | d54276f | 2007-07-04 12:37:27 +0200 | [diff] [blame] | 27 | static const char *fmt_patch_subject_prefix = "PATCH"; |
Denis Cheng | 94c22a5 | 2008-03-02 17:05:53 +0800 | [diff] [blame] | 28 | static const char *fmt_pretty; |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 29 | |
Matthieu Moy | 1c370ea | 2009-08-06 12:47:21 +0200 | [diff] [blame] | 30 | static const char * const builtin_log_usage = |
| 31 | "git log [<options>] [<since>..<until>] [[--] <path>...]\n" |
| 32 | " or: git show [options] <object>..."; |
| 33 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 34 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 35 | struct rev_info *rev) |
| 36 | { |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 37 | int i; |
Lars Hjemli | 33e7018 | 2009-08-15 16:23:12 +0200 | [diff] [blame] | 38 | int decoration_style = 0; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 39 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 40 | rev->abbrev = DEFAULT_ABBREV; |
| 41 | rev->commit_format = CMIT_FMT_DEFAULT; |
Denis Cheng | 94c22a5 | 2008-03-02 17:05:53 +0800 | [diff] [blame] | 42 | if (fmt_pretty) |
Junio C Hamano | 4da45be | 2008-04-07 17:11:34 -0700 | [diff] [blame] | 43 | get_commit_format(fmt_pretty, rev); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 44 | rev->verbose_header = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 45 | DIFF_OPT_SET(&rev->diffopt, RECURSIVE); |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 46 | rev->show_root_diff = default_show_root; |
Alex Riesen | d54276f | 2007-07-04 12:37:27 +0200 | [diff] [blame] | 47 | rev->subject_prefix = fmt_patch_subject_prefix; |
Jeff King | 5ec11af | 2008-12-07 21:54:17 -0500 | [diff] [blame] | 48 | DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV); |
Heikki Orsila | dd0ffd5 | 2008-05-22 18:24:07 +0300 | [diff] [blame] | 49 | |
| 50 | if (default_date_mode) |
| 51 | rev->date_mode = parse_date_format(default_date_mode); |
| 52 | |
Jonathan Nieder | 99caeed | 2009-11-09 09:05:01 -0600 | [diff] [blame] | 53 | /* |
| 54 | * Check for -h before setup_revisions(), or "git log -h" will |
| 55 | * fail when run without a git directory. |
| 56 | */ |
| 57 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 58 | usage(builtin_log_usage); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 59 | argc = setup_revisions(argc, argv, rev, "HEAD"); |
Heikki Orsila | dd0ffd5 | 2008-05-22 18:24:07 +0300 | [diff] [blame] | 60 | |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 61 | if (!rev->show_notes_given && !rev->pretty_given) |
| 62 | rev->show_notes = 1; |
| 63 | |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 64 | if (rev->diffopt.pickaxe || rev->diffopt.filter) |
| 65 | rev->always_show_header = 0; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 66 | if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) { |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 67 | rev->always_show_header = 0; |
| 68 | if (rev->diffopt.nr_paths != 1) |
| 69 | usage("git logs can only follow renames on one pathname at a time"); |
| 70 | } |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 71 | for (i = 1; i < argc; i++) { |
| 72 | const char *arg = argv[i]; |
Jeff King | 6668833 | 2007-05-16 07:15:07 -0400 | [diff] [blame] | 73 | if (!strcmp(arg, "--decorate")) { |
Lars Hjemli | 33e7018 | 2009-08-15 16:23:12 +0200 | [diff] [blame] | 74 | decoration_style = DECORATE_SHORT_REFS; |
| 75 | } else if (!prefixcmp(arg, "--decorate=")) { |
| 76 | const char *v = skip_prefix(arg, "--decorate="); |
| 77 | if (!strcmp(v, "full")) |
| 78 | decoration_style = DECORATE_FULL_REFS; |
| 79 | else if (!strcmp(v, "short")) |
| 80 | decoration_style = DECORATE_SHORT_REFS; |
| 81 | else |
| 82 | die("invalid --decorate option: %s", arg); |
Linus Torvalds | 0f3a290 | 2008-10-27 12:51:59 -0700 | [diff] [blame] | 83 | } else if (!strcmp(arg, "--source")) { |
| 84 | rev->show_source = 1; |
Matthieu Moy | 1c370ea | 2009-08-06 12:47:21 +0200 | [diff] [blame] | 85 | } else if (!strcmp(arg, "-h")) { |
| 86 | usage(builtin_log_usage); |
Linus Torvalds | ca135e7 | 2007-04-16 16:05:10 -0700 | [diff] [blame] | 87 | } else |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 88 | die("unrecognized argument: %s", arg); |
| 89 | } |
Lars Hjemli | 33e7018 | 2009-08-15 16:23:12 +0200 | [diff] [blame] | 90 | if (decoration_style) { |
| 91 | rev->show_decorations = 1; |
| 92 | load_ref_decorations(decoration_style); |
| 93 | } |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 94 | } |
| 95 | |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 96 | /* |
| 97 | * This gives a rough estimate for how many commits we |
| 98 | * will print out in the list. |
| 99 | */ |
| 100 | static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) |
| 101 | { |
| 102 | int n = 0; |
| 103 | |
| 104 | while (list) { |
| 105 | struct commit *commit = list->item; |
| 106 | unsigned int flags = commit->object.flags; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 107 | list = list->next; |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 108 | if (!(flags & (TREESAME | UNINTERESTING))) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 109 | n++; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 110 | } |
| 111 | return n; |
| 112 | } |
| 113 | |
| 114 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) |
| 115 | { |
| 116 | if (rev->shown_one) { |
| 117 | rev->shown_one = 0; |
| 118 | if (rev->commit_format != CMIT_FMT_ONELINE) |
| 119 | putchar(rev->diffopt.line_termination); |
| 120 | } |
| 121 | printf("Final output: %d %s\n", nr, stage); |
| 122 | } |
| 123 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 124 | static struct itimerval early_output_timer; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 125 | |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 126 | static void log_show_early(struct rev_info *revs, struct commit_list *list) |
| 127 | { |
| 128 | int i = revs->early_output; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 129 | int show_header = 1; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 130 | |
| 131 | sort_in_topological_order(&list, revs->lifo); |
| 132 | while (list && i) { |
| 133 | struct commit *commit = list->item; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 134 | switch (simplify_commit(revs, commit)) { |
| 135 | case commit_show: |
| 136 | if (show_header) { |
| 137 | int n = estimate_commit_count(revs, list); |
| 138 | show_early_header(revs, "incomplete", n); |
| 139 | show_header = 0; |
| 140 | } |
| 141 | log_tree_commit(revs, commit); |
| 142 | i--; |
| 143 | break; |
| 144 | case commit_ignore: |
| 145 | break; |
| 146 | case commit_error: |
| 147 | return; |
| 148 | } |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 149 | list = list->next; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 150 | } |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 151 | |
| 152 | /* Did we already get enough commits for the early output? */ |
| 153 | if (!i) |
| 154 | return; |
| 155 | |
| 156 | /* |
| 157 | * ..if no, then repeat it twice a second until we |
| 158 | * do. |
| 159 | * |
| 160 | * NOTE! We don't use "it_interval", because if the |
| 161 | * reader isn't listening, we want our output to be |
| 162 | * throttled by the writing, and not have the timer |
| 163 | * trigger every second even if we're blocked on a |
| 164 | * reader! |
| 165 | */ |
| 166 | early_output_timer.it_value.tv_sec = 0; |
| 167 | early_output_timer.it_value.tv_usec = 500000; |
| 168 | setitimer(ITIMER_REAL, &early_output_timer, NULL); |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void early_output(int signal) |
| 172 | { |
| 173 | show_early_output = log_show_early; |
| 174 | } |
| 175 | |
| 176 | static void setup_early_output(struct rev_info *rev) |
| 177 | { |
| 178 | struct sigaction sa; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * Set up the signal handler, minimally intrusively: |
| 182 | * we only set a single volatile integer word (not |
| 183 | * using sigatomic_t - trying to avoid unnecessary |
| 184 | * system dependencies and headers), and using |
| 185 | * SA_RESTART. |
| 186 | */ |
| 187 | memset(&sa, 0, sizeof(sa)); |
| 188 | sa.sa_handler = early_output; |
| 189 | sigemptyset(&sa.sa_mask); |
| 190 | sa.sa_flags = SA_RESTART; |
| 191 | sigaction(SIGALRM, &sa, NULL); |
| 192 | |
| 193 | /* |
| 194 | * If we can get the whole output in less than a |
| 195 | * tenth of a second, don't even bother doing the |
| 196 | * early-output thing.. |
| 197 | * |
| 198 | * This is a one-time-only trigger. |
| 199 | */ |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 200 | early_output_timer.it_value.tv_sec = 0; |
| 201 | early_output_timer.it_value.tv_usec = 100000; |
| 202 | setitimer(ITIMER_REAL, &early_output_timer, NULL); |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void finish_early_output(struct rev_info *rev) |
| 206 | { |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 207 | int n = estimate_commit_count(rev, rev->commits); |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 208 | signal(SIGALRM, SIG_IGN); |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 209 | show_early_header(rev, "done", n); |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 212 | static int cmd_log_walk(struct rev_info *rev) |
| 213 | { |
| 214 | struct commit *commit; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 215 | |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 216 | if (rev->early_output) |
| 217 | setup_early_output(rev); |
| 218 | |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 219 | if (prepare_revision_walk(rev)) |
| 220 | die("revision walk setup failed"); |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 221 | |
| 222 | if (rev->early_output) |
| 223 | finish_early_output(rev); |
| 224 | |
Peter Valdemar Mørch | 036d17f | 2008-08-11 08:46:24 +0200 | [diff] [blame] | 225 | /* |
Peter Valdemar Mørch | 84102a3 | 2008-08-11 08:46:25 +0200 | [diff] [blame] | 226 | * For --check and --exit-code, the exit code is based on CHECK_FAILED |
| 227 | * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to |
| 228 | * retain that state information if replacing rev->diffopt in this loop |
Peter Valdemar Mørch | 036d17f | 2008-08-11 08:46:24 +0200 | [diff] [blame] | 229 | */ |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 230 | while ((commit = get_revision(rev)) != NULL) { |
| 231 | log_tree_commit(rev, commit); |
Johannes Schindelin | a6c7306 | 2007-01-20 22:28:16 +0100 | [diff] [blame] | 232 | if (!rev->reflog_info) { |
| 233 | /* we allow cycles in reflog ancestry */ |
| 234 | free(commit->buffer); |
| 235 | commit->buffer = NULL; |
| 236 | } |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 237 | free_commit_list(commit->parents); |
| 238 | commit->parents = NULL; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 239 | } |
Peter Valdemar Mørch | 036d17f | 2008-08-11 08:46:24 +0200 | [diff] [blame] | 240 | if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && |
| 241 | DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) { |
| 242 | return 02; |
| 243 | } |
Peter Valdemar Mørch | 84102a3 | 2008-08-11 08:46:25 +0200 | [diff] [blame] | 244 | return diff_result_code(&rev->diffopt, 0); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 247 | static int git_log_config(const char *var, const char *value, void *cb) |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 248 | { |
Denis Cheng | 94c22a5 | 2008-03-02 17:05:53 +0800 | [diff] [blame] | 249 | if (!strcmp(var, "format.pretty")) |
| 250 | return git_config_string(&fmt_pretty, var, value); |
Brian Hetro | 70cff3a | 2008-07-05 01:24:41 -0400 | [diff] [blame] | 251 | if (!strcmp(var, "format.subjectprefix")) |
| 252 | return git_config_string(&fmt_patch_subject_prefix, var, value); |
Heikki Orsila | dd0ffd5 | 2008-05-22 18:24:07 +0300 | [diff] [blame] | 253 | if (!strcmp(var, "log.date")) |
| 254 | return git_config_string(&default_date_mode, var, value); |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 255 | if (!strcmp(var, "log.showroot")) { |
| 256 | default_show_root = git_config_bool(var, value); |
| 257 | return 0; |
| 258 | } |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 259 | return git_diff_ui_config(var, value, cb); |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 262 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 263 | { |
| 264 | struct rev_info rev; |
| 265 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 266 | git_config(git_log_config, NULL); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 267 | |
| 268 | if (diff_use_color_default == -1) |
| 269 | diff_use_color_default = git_use_color_default; |
| 270 | |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 271 | init_revisions(&rev, prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 272 | rev.diff = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 273 | rev.simplify_history = 0; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 274 | cmd_log_init(argc, argv, prefix, &rev); |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 275 | if (!rev.diffopt.output_format) |
| 276 | rev.diffopt.output_format = DIFF_FORMAT_RAW; |
| 277 | return cmd_log_walk(&rev); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 280 | static void show_tagger(char *buf, int len, struct rev_info *rev) |
| 281 | { |
Johannes Schindelin | ea718e6 | 2009-01-02 19:08:43 +0100 | [diff] [blame] | 282 | struct strbuf out = STRBUF_INIT; |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 283 | |
Johannes Schindelin | ea718e6 | 2009-01-02 19:08:43 +0100 | [diff] [blame] | 284 | pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode, |
| 285 | git_log_output_encoding ? |
| 286 | git_log_output_encoding: git_commit_encoding); |
Jeff King | ca4ca9e | 2009-07-17 19:18:34 -0400 | [diff] [blame] | 287 | printf("%s", out.buf); |
Johannes Schindelin | ea718e6 | 2009-01-02 19:08:43 +0100 | [diff] [blame] | 288 | strbuf_release(&out); |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int show_object(const unsigned char *sha1, int show_tag_object, |
| 292 | struct rev_info *rev) |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 293 | { |
| 294 | unsigned long size; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 295 | enum object_type type; |
| 296 | char *buf = read_sha1_file(sha1, &type, &size); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 297 | int offset = 0; |
| 298 | |
| 299 | if (!buf) |
| 300 | return error("Could not read object %s", sha1_to_hex(sha1)); |
| 301 | |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 302 | if (show_tag_object) |
| 303 | while (offset < size && buf[offset] != '\n') { |
| 304 | int new_offset = offset + 1; |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 305 | while (new_offset < size && buf[new_offset++] != '\n') |
| 306 | ; /* do nothing */ |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 307 | if (!prefixcmp(buf + offset, "tagger ")) |
| 308 | show_tagger(buf + offset + 7, |
| 309 | new_offset - offset - 7, rev); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 310 | offset = new_offset; |
| 311 | } |
| 312 | |
| 313 | if (offset < size) |
| 314 | fwrite(buf + offset, size - offset, 1, stdout); |
| 315 | free(buf); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int show_tree_object(const unsigned char *sha1, |
| 320 | const char *base, int baselen, |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 321 | const char *pathname, unsigned mode, int stage, void *context) |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 322 | { |
| 323 | printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); |
| 324 | return 0; |
| 325 | } |
| 326 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 327 | int cmd_show(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 328 | { |
| 329 | struct rev_info rev; |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 330 | struct object_array_entry *objects; |
| 331 | int i, count, ret = 0; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 332 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 333 | git_config(git_log_config, NULL); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 334 | |
| 335 | if (diff_use_color_default == -1) |
| 336 | diff_use_color_default = git_use_color_default; |
| 337 | |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 338 | init_revisions(&rev, prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 339 | rev.diff = 1; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 340 | rev.combine_merges = 1; |
| 341 | rev.dense_combined_merges = 1; |
| 342 | rev.always_show_header = 1; |
| 343 | rev.ignore_merges = 0; |
| 344 | rev.no_walk = 1; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 345 | cmd_log_init(argc, argv, prefix, &rev); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 346 | |
| 347 | count = rev.pending.nr; |
| 348 | objects = rev.pending.objects; |
| 349 | for (i = 0; i < count && !ret; i++) { |
| 350 | struct object *o = objects[i].item; |
| 351 | const char *name = objects[i].name; |
| 352 | switch (o->type) { |
| 353 | case OBJ_BLOB: |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 354 | ret = show_object(o->sha1, 0, NULL); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 355 | break; |
| 356 | case OBJ_TAG: { |
| 357 | struct tag *t = (struct tag *)o; |
| 358 | |
Jeff King | ae03ee6 | 2009-07-18 06:14:37 -0400 | [diff] [blame] | 359 | if (rev.shown_one) |
| 360 | putchar('\n'); |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 361 | printf("%stag %s%s\n", |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 362 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 363 | t->tag, |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 364 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
Johannes Schindelin | 56122ed | 2007-12-18 18:01:33 +0000 | [diff] [blame] | 365 | ret = show_object(o->sha1, 1, &rev); |
Jeff King | ae03ee6 | 2009-07-18 06:14:37 -0400 | [diff] [blame] | 366 | rev.shown_one = 1; |
Junio C Hamano | d2dadfe | 2008-12-15 00:36:56 -0800 | [diff] [blame] | 367 | if (ret) |
| 368 | break; |
| 369 | o = parse_object(t->tagged->sha1); |
| 370 | if (!o) |
| 371 | ret = error("Could not read object %s", |
| 372 | sha1_to_hex(t->tagged->sha1)); |
| 373 | objects[i].item = o; |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 374 | i--; |
| 375 | break; |
| 376 | } |
| 377 | case OBJ_TREE: |
Jeff King | ae03ee6 | 2009-07-18 06:14:37 -0400 | [diff] [blame] | 378 | if (rev.shown_one) |
| 379 | putchar('\n'); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 380 | printf("%stree %s%s\n\n", |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 381 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 382 | name, |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 383 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 384 | read_tree_recursive((struct tree *)o, "", 0, 0, NULL, |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 385 | show_tree_object, NULL); |
Jeff King | ae03ee6 | 2009-07-18 06:14:37 -0400 | [diff] [blame] | 386 | rev.shown_one = 1; |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 387 | break; |
| 388 | case OBJ_COMMIT: |
| 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); |
| 393 | break; |
| 394 | default: |
| 395 | ret = error("Unknown type: %d", o->type); |
| 396 | } |
| 397 | } |
| 398 | free(objects); |
| 399 | return ret; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 402 | /* |
| 403 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" |
| 404 | */ |
| 405 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) |
| 406 | { |
| 407 | struct rev_info rev; |
| 408 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 409 | git_config(git_log_config, NULL); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 410 | |
| 411 | if (diff_use_color_default == -1) |
| 412 | diff_use_color_default = git_use_color_default; |
| 413 | |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 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); |
| 419 | |
| 420 | /* |
| 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. |
| 424 | */ |
| 425 | rev.commit_format = CMIT_FMT_ONELINE; |
Junio C Hamano | 4da45be | 2008-04-07 17:11:34 -0700 | [diff] [blame] | 426 | rev.use_terminator = 1; |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 427 | rev.always_show_header = 1; |
| 428 | |
| 429 | /* |
| 430 | * We get called through "git reflog", so unlike the other log |
| 431 | * routines, we need to set up our pager manually.. |
| 432 | */ |
| 433 | setup_pager(); |
| 434 | |
| 435 | return cmd_log_walk(&rev); |
| 436 | } |
| 437 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 438 | int cmd_log(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 439 | { |
| 440 | struct rev_info rev; |
| 441 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 442 | git_config(git_log_config, NULL); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 443 | |
| 444 | if (diff_use_color_default == -1) |
| 445 | diff_use_color_default = git_use_color_default; |
| 446 | |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 447 | init_revisions(&rev, prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 448 | rev.always_show_header = 1; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 449 | cmd_log_init(argc, argv, prefix, &rev); |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 450 | return cmd_log_walk(&rev); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 451 | } |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 452 | |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 453 | /* format-patch */ |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 454 | |
Junio C Hamano | 917a8f8 | 2007-01-17 15:03:39 -0800 | [diff] [blame] | 455 | static const char *fmt_patch_suffix = ".patch"; |
Brian Gernhardt | 49604a4 | 2007-11-03 23:38:24 -0400 | [diff] [blame] | 456 | static int numbered = 0; |
Brian Gernhardt | a567fdc | 2008-10-02 16:55:39 -0400 | [diff] [blame] | 457 | static int auto_number = 1; |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 458 | |
Jeremy White | 0db5260 | 2009-02-12 09:51:55 -0600 | [diff] [blame] | 459 | static char *default_attach = NULL; |
| 460 | |
Daniel Barkalow | 3ee79d9 | 2008-02-19 02:40:33 -0500 | [diff] [blame] | 461 | static char **extra_hdr; |
| 462 | static int extra_hdr_nr; |
| 463 | static int extra_hdr_alloc; |
| 464 | |
| 465 | static char **extra_to; |
| 466 | static int extra_to_nr; |
| 467 | static int extra_to_alloc; |
| 468 | |
| 469 | static char **extra_cc; |
| 470 | static int extra_cc_nr; |
| 471 | static int extra_cc_alloc; |
| 472 | |
| 473 | static void add_header(const char *value) |
| 474 | { |
| 475 | int len = strlen(value); |
Jim Meyering | c8c4450 | 2008-08-19 20:42:04 +0200 | [diff] [blame] | 476 | while (len && value[len - 1] == '\n') |
Daniel Barkalow | 3ee79d9 | 2008-02-19 02:40:33 -0500 | [diff] [blame] | 477 | len--; |
| 478 | if (!strncasecmp(value, "to: ", 4)) { |
| 479 | ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc); |
| 480 | extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4); |
| 481 | return; |
| 482 | } |
| 483 | if (!strncasecmp(value, "cc: ", 4)) { |
| 484 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); |
| 485 | extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4); |
| 486 | return; |
| 487 | } |
| 488 | ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc); |
| 489 | extra_hdr[extra_hdr_nr++] = xstrndup(value, len); |
| 490 | } |
| 491 | |
Thomas Rast | 30984ed | 2009-02-19 22:26:33 +0100 | [diff] [blame] | 492 | #define THREAD_SHALLOW 1 |
| 493 | #define THREAD_DEEP 2 |
| 494 | static int thread = 0; |
Heiko Voigt | 1d1876e | 2009-04-01 19:51:54 +0200 | [diff] [blame] | 495 | static int do_signoff = 0; |
Thomas Rast | 30984ed | 2009-02-19 22:26:33 +0100 | [diff] [blame] | 496 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 497 | static int git_format_config(const char *var, const char *value, void *cb) |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 498 | { |
| 499 | if (!strcmp(var, "format.headers")) { |
Junio C Hamano | d7fb91c | 2007-01-17 11:13:02 -0800 | [diff] [blame] | 500 | if (!value) |
| 501 | die("format.headers without value"); |
Daniel Barkalow | 3ee79d9 | 2008-02-19 02:40:33 -0500 | [diff] [blame] | 502 | add_header(value); |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 503 | return 0; |
| 504 | } |
Brian Hetro | 70cff3a | 2008-07-05 01:24:41 -0400 | [diff] [blame] | 505 | if (!strcmp(var, "format.suffix")) |
| 506 | return git_config_string(&fmt_patch_suffix, var, value); |
Miklos Vajna | fe8928e | 2008-04-26 23:19:06 +0200 | [diff] [blame] | 507 | if (!strcmp(var, "format.cc")) { |
| 508 | if (!value) |
| 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); |
| 512 | return 0; |
| 513 | } |
Andy Parkins | a159ca0 | 2006-12-13 09:13:28 +0000 | [diff] [blame] | 514 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
Ryan Anderson | f3aafa4 | 2006-07-09 02:28:21 -0400 | [diff] [blame] | 515 | return 0; |
| 516 | } |
Brian Gernhardt | 49604a4 | 2007-11-03 23:38:24 -0400 | [diff] [blame] | 517 | if (!strcmp(var, "format.numbered")) { |
Junio C Hamano | 90f5c18 | 2008-02-11 13:09:16 -0800 | [diff] [blame] | 518 | if (value && !strcasecmp(value, "auto")) { |
Brian Gernhardt | 49604a4 | 2007-11-03 23:38:24 -0400 | [diff] [blame] | 519 | auto_number = 1; |
| 520 | return 0; |
| 521 | } |
Brian Gernhardt | 49604a4 | 2007-11-03 23:38:24 -0400 | [diff] [blame] | 522 | numbered = git_config_bool(var, value); |
Brian Gernhardt | a567fdc | 2008-10-02 16:55:39 -0400 | [diff] [blame] | 523 | auto_number = auto_number && numbered; |
Brian Gernhardt | 49604a4 | 2007-11-03 23:38:24 -0400 | [diff] [blame] | 524 | return 0; |
| 525 | } |
Jeremy White | 0db5260 | 2009-02-12 09:51:55 -0600 | [diff] [blame] | 526 | if (!strcmp(var, "format.attach")) { |
| 527 | if (value && *value) |
| 528 | default_attach = xstrdup(value); |
| 529 | else |
| 530 | default_attach = xstrdup(git_version_string); |
| 531 | return 0; |
| 532 | } |
Thomas Rast | 30984ed | 2009-02-19 22:26:33 +0100 | [diff] [blame] | 533 | if (!strcmp(var, "format.thread")) { |
| 534 | if (value && !strcasecmp(value, "deep")) { |
| 535 | thread = THREAD_DEEP; |
| 536 | return 0; |
| 537 | } |
| 538 | if (value && !strcasecmp(value, "shallow")) { |
| 539 | thread = THREAD_SHALLOW; |
| 540 | return 0; |
| 541 | } |
| 542 | thread = git_config_bool(var, value) && THREAD_SHALLOW; |
| 543 | return 0; |
| 544 | } |
Heiko Voigt | 1d1876e | 2009-04-01 19:51:54 +0200 | [diff] [blame] | 545 | if (!strcmp(var, "format.signoff")) { |
| 546 | do_signoff = git_config_bool(var, value); |
| 547 | return 0; |
| 548 | } |
Adam Roben | dbd2144 | 2007-07-01 17:48:59 -0700 | [diff] [blame] | 549 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 550 | return git_log_config(var, value, cb); |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 553 | static FILE *realstdout = NULL; |
| 554 | static const char *output_directory = NULL; |
Junio C Hamano | 9800a75 | 2009-01-12 15:18:02 -0800 | [diff] [blame] | 555 | static int outdir_offset; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 556 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 557 | static int reopen_stdout(struct commit *commit, struct rev_info *rev) |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 558 | { |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 559 | struct strbuf filename = STRBUF_INIT; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 560 | int suffix_len = strlen(fmt_patch_suffix) + 1; |
| 561 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 562 | if (output_directory) { |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 563 | strbuf_addstr(&filename, output_directory); |
| 564 | if (filename.len >= |
| 565 | PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 566 | return error("name of output directory is too long"); |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 567 | if (filename.buf[filename.len - 1] != '/') |
| 568 | strbuf_addch(&filename, '/'); |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 569 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 570 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 571 | get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename); |
Jon Loeliger | e6ff0f4 | 2007-06-05 15:06:53 -0500 | [diff] [blame] | 572 | |
Junio C Hamano | 90b1994 | 2009-05-23 01:15:35 -0700 | [diff] [blame] | 573 | if (!DIFF_OPT_TST(&rev->diffopt, QUICK)) |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 574 | fprintf(realstdout, "%s\n", filename.buf + outdir_offset); |
Nate Case | ec2956d | 2009-03-18 12:00:45 -0500 | [diff] [blame] | 575 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 576 | if (freopen(filename.buf, "w", stdout) == NULL) |
| 577 | return error("Cannot open patch file %s", filename.buf); |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 578 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 579 | strbuf_release(&filename); |
Jon Loeliger | e6ff0f4 | 2007-06-05 15:06:53 -0500 | [diff] [blame] | 580 | return 0; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 583 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix) |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 584 | { |
| 585 | struct rev_info check_rev; |
| 586 | struct commit *commit; |
| 587 | struct object *o1, *o2; |
| 588 | unsigned flags1, flags2; |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 589 | |
| 590 | if (rev->pending.nr != 2) |
| 591 | die("Need exactly one range."); |
| 592 | |
| 593 | o1 = rev->pending.objects[0].item; |
| 594 | flags1 = o1->flags; |
| 595 | o2 = rev->pending.objects[1].item; |
| 596 | flags2 = o2->flags; |
| 597 | |
| 598 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) |
| 599 | die("Not a range."); |
| 600 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 601 | init_patch_ids(ids); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 602 | |
| 603 | /* given a range a..b get all patch ids for b..a */ |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 604 | init_revisions(&check_rev, prefix); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 605 | o1->flags ^= UNINTERESTING; |
| 606 | o2->flags ^= UNINTERESTING; |
| 607 | add_pending_object(&check_rev, o1, "o1"); |
| 608 | add_pending_object(&check_rev, o2, "o2"); |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 609 | if (prepare_revision_walk(&check_rev)) |
| 610 | die("revision walk setup failed"); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 611 | |
| 612 | while ((commit = get_revision(&check_rev)) != NULL) { |
| 613 | /* ignore merges */ |
| 614 | if (commit->parents && commit->parents->next) |
| 615 | continue; |
| 616 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 617 | add_commit_patch_id(commit, ids); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* reset for next revision walk */ |
Johannes Schindelin | 81db094 | 2006-06-27 22:38:04 +0200 | [diff] [blame] | 621 | clear_commit_marks((struct commit *)o1, |
| 622 | SEEN | UNINTERESTING | SHOWN | ADDED); |
| 623 | clear_commit_marks((struct commit *)o2, |
| 624 | SEEN | UNINTERESTING | SHOWN | ADDED); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 625 | o1->flags = flags1; |
| 626 | o2->flags = flags2; |
| 627 | } |
| 628 | |
Daniel Barkalow | e1a3734 | 2008-02-18 22:56:06 -0500 | [diff] [blame] | 629 | static void gen_message_id(struct rev_info *info, char *base) |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 630 | { |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 631 | const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 632 | const char *email_start = strrchr(committer, '<'); |
| 633 | const char *email_end = strrchr(committer, '>'); |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 634 | struct strbuf buf = STRBUF_INIT; |
Daniel Barkalow | e1a3734 | 2008-02-18 22:56:06 -0500 | [diff] [blame] | 635 | if (!email_start || !email_end || email_start > email_end - 1) |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 636 | die("Could not extract email from committer identity."); |
Daniel Barkalow | e1a3734 | 2008-02-18 22:56:06 -0500 | [diff] [blame] | 637 | strbuf_addf(&buf, "%s.%lu.git.%.*s", base, |
| 638 | (unsigned long) time(NULL), |
| 639 | (int)(email_end - email_start - 1), email_start + 1); |
| 640 | info->message_id = strbuf_detach(&buf, NULL); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 643 | static void make_cover_letter(struct rev_info *rev, int use_stdout, |
| 644 | int numbered, int numbered_files, |
| 645 | struct commit *origin, |
| 646 | int nr, struct commit **list, struct commit *head) |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 647 | { |
| 648 | const char *committer; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 649 | const char *subject_start = NULL; |
| 650 | const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; |
| 651 | const char *msg; |
| 652 | const char *extra_headers = rev->extra_headers; |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 653 | struct shortlog log; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 654 | struct strbuf sb = STRBUF_INIT; |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 655 | int i; |
Brandon Casey | 330db18 | 2009-05-18 18:44:39 -0500 | [diff] [blame] | 656 | const char *encoding = "UTF-8"; |
Daniel Barkalow | 39fe578 | 2008-02-28 12:14:13 -0500 | [diff] [blame] | 657 | struct diff_options opts; |
Junio C Hamano | 267123b | 2008-03-15 00:09:20 -0700 | [diff] [blame] | 658 | int need_8bit_cte = 0; |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 659 | struct commit *commit = NULL; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 660 | |
| 661 | if (rev->commit_format != CMIT_FMT_EMAIL) |
| 662 | die("Cover letter needs email format"); |
| 663 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 664 | committer = git_committer_info(0); |
Stephen Boyd | 6df514a | 2009-03-22 19:14:02 -0700 | [diff] [blame] | 665 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 666 | if (!numbered_files) { |
| 667 | /* |
| 668 | * We fake a commit for the cover letter so we get the filename |
| 669 | * desired. |
| 670 | */ |
| 671 | commit = xcalloc(1, sizeof(*commit)); |
| 672 | commit->buffer = xmalloc(400); |
| 673 | snprintf(commit->buffer, 400, |
| 674 | "tree 0000000000000000000000000000000000000000\n" |
| 675 | "parent %s\n" |
| 676 | "author %s\n" |
| 677 | "committer %s\n\n" |
| 678 | "cover letter\n", |
Stephen Boyd | 108dab2 | 2009-03-22 19:14:05 -0700 | [diff] [blame] | 679 | sha1_to_hex(head->object.sha1), committer, committer); |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | if (!use_stdout && reopen_stdout(commit, rev)) |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 683 | return; |
| 684 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 685 | if (commit) { |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 686 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 687 | free(commit->buffer); |
| 688 | free(commit); |
| 689 | } |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 690 | |
Stephen Boyd | 108dab2 | 2009-03-22 19:14:05 -0700 | [diff] [blame] | 691 | log_write_email_headers(rev, head, &subject_start, &extra_headers, |
Junio C Hamano | 267123b | 2008-03-15 00:09:20 -0700 | [diff] [blame] | 692 | &need_8bit_cte); |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 693 | |
Johannes Schindelin | 0a7f448 | 2009-08-10 18:22:22 +0200 | [diff] [blame] | 694 | for (i = 0; !need_8bit_cte && i < nr; i++) |
| 695 | if (has_non_ascii(list[i]->buffer)) |
| 696 | need_8bit_cte = 1; |
| 697 | |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 698 | msg = body; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 699 | pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822, |
| 700 | encoding); |
| 701 | pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers, |
Junio C Hamano | 267123b | 2008-03-15 00:09:20 -0700 | [diff] [blame] | 702 | encoding, need_8bit_cte); |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 703 | pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0); |
| 704 | printf("%s\n", sb.buf); |
| 705 | |
| 706 | strbuf_release(&sb); |
| 707 | |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 708 | shortlog_init(&log); |
Johannes Schindelin | 859c4fb | 2008-03-02 15:53:39 +0000 | [diff] [blame] | 709 | log.wrap_lines = 1; |
| 710 | log.wrap = 72; |
| 711 | log.in1 = 2; |
| 712 | log.in2 = 4; |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 713 | for (i = 0; i < nr; i++) |
| 714 | shortlog_add_commit(&log, list[i]); |
| 715 | |
| 716 | shortlog_output(&log); |
| 717 | |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 718 | /* |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 719 | * We can only do diffstat with a unique reference point |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 720 | */ |
| 721 | if (!origin) |
| 722 | return; |
| 723 | |
Johannes Schindelin | 5d02294 | 2008-03-02 15:53:04 +0000 | [diff] [blame] | 724 | memcpy(&opts, &rev->diffopt, sizeof(opts)); |
| 725 | opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 726 | |
Daniel Barkalow | 39fe578 | 2008-02-28 12:14:13 -0500 | [diff] [blame] | 727 | diff_setup_done(&opts); |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 728 | |
Daniel Barkalow | 39fe578 | 2008-02-28 12:14:13 -0500 | [diff] [blame] | 729 | diff_tree_sha1(origin->tree->object.sha1, |
| 730 | head->tree->object.sha1, |
| 731 | "", &opts); |
| 732 | diffcore_std(&opts); |
| 733 | diff_flush(&opts); |
| 734 | |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 735 | printf("\n"); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 736 | } |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 737 | |
Junio C Hamano | 8419d2e | 2007-09-13 22:30:45 -0700 | [diff] [blame] | 738 | static const char *clean_message_id(const char *msg_id) |
| 739 | { |
| 740 | char ch; |
| 741 | const char *a, *z, *m; |
Junio C Hamano | 8419d2e | 2007-09-13 22:30:45 -0700 | [diff] [blame] | 742 | |
| 743 | m = msg_id; |
| 744 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) |
| 745 | m++; |
| 746 | a = m; |
| 747 | z = NULL; |
| 748 | while ((ch = *m)) { |
| 749 | if (!isspace(ch) && (ch != '>')) |
| 750 | z = m; |
| 751 | m++; |
| 752 | } |
| 753 | if (!z) |
| 754 | die("insane in-reply-to: %s", msg_id); |
| 755 | if (++z == m) |
| 756 | return a; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 757 | return xmemdupz(a, z - a); |
Junio C Hamano | 8419d2e | 2007-09-13 22:30:45 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Junio C Hamano | 9800a75 | 2009-01-12 15:18:02 -0800 | [diff] [blame] | 760 | static const char *set_outdir(const char *prefix, const char *output_directory) |
| 761 | { |
| 762 | if (output_directory && is_absolute_path(output_directory)) |
| 763 | return output_directory; |
| 764 | |
| 765 | if (!prefix || !*prefix) { |
| 766 | if (output_directory) |
| 767 | return output_directory; |
| 768 | /* The user did not explicitly ask for "./" */ |
| 769 | outdir_offset = 2; |
| 770 | return "./"; |
| 771 | } |
| 772 | |
| 773 | outdir_offset = strlen(prefix); |
| 774 | if (!output_directory) |
| 775 | return prefix; |
| 776 | |
| 777 | return xstrdup(prefix_filename(prefix, outdir_offset, |
| 778 | output_directory)); |
| 779 | } |
| 780 | |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 781 | static const char * const builtin_format_patch_usage[] = { |
| 782 | "git format-patch [options] [<since> | <revision range>]", |
| 783 | NULL |
| 784 | }; |
| 785 | |
| 786 | static int keep_subject = 0; |
| 787 | |
| 788 | static int keep_callback(const struct option *opt, const char *arg, int unset) |
| 789 | { |
| 790 | ((struct rev_info *)opt->value)->total = -1; |
| 791 | keep_subject = 1; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static int subject_prefix = 0; |
| 796 | |
| 797 | static int subject_prefix_callback(const struct option *opt, const char *arg, |
| 798 | int unset) |
| 799 | { |
| 800 | subject_prefix = 1; |
| 801 | ((struct rev_info *)opt->value)->subject_prefix = arg; |
| 802 | return 0; |
| 803 | } |
| 804 | |
Junio C Hamano | 1af4731 | 2009-05-31 16:17:31 -0700 | [diff] [blame] | 805 | static int numbered_cmdline_opt = 0; |
| 806 | |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 807 | static int numbered_callback(const struct option *opt, const char *arg, |
| 808 | int unset) |
| 809 | { |
Junio C Hamano | 1af4731 | 2009-05-31 16:17:31 -0700 | [diff] [blame] | 810 | *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1; |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 811 | if (unset) |
| 812 | auto_number = 0; |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static int no_numbered_callback(const struct option *opt, const char *arg, |
| 817 | int unset) |
| 818 | { |
| 819 | return numbered_callback(opt, arg, 1); |
| 820 | } |
| 821 | |
| 822 | static int output_directory_callback(const struct option *opt, const char *arg, |
| 823 | int unset) |
| 824 | { |
| 825 | const char **dir = (const char **)opt->value; |
| 826 | if (*dir) |
| 827 | die("Two output directories?"); |
| 828 | *dir = arg; |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | static int thread_callback(const struct option *opt, const char *arg, int unset) |
| 833 | { |
| 834 | int *thread = (int *)opt->value; |
| 835 | if (unset) |
| 836 | *thread = 0; |
| 837 | else if (!arg || !strcmp(arg, "shallow")) |
| 838 | *thread = THREAD_SHALLOW; |
| 839 | else if (!strcmp(arg, "deep")) |
| 840 | *thread = THREAD_DEEP; |
| 841 | else |
| 842 | return 1; |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int attach_callback(const struct option *opt, const char *arg, int unset) |
| 847 | { |
| 848 | struct rev_info *rev = (struct rev_info *)opt->value; |
| 849 | if (unset) |
| 850 | rev->mime_boundary = NULL; |
| 851 | else if (arg) |
| 852 | rev->mime_boundary = arg; |
| 853 | else |
| 854 | rev->mime_boundary = git_version_string; |
| 855 | rev->no_inline = unset ? 0 : 1; |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static int inline_callback(const struct option *opt, const char *arg, int unset) |
| 860 | { |
| 861 | struct rev_info *rev = (struct rev_info *)opt->value; |
| 862 | if (unset) |
| 863 | rev->mime_boundary = NULL; |
| 864 | else if (arg) |
| 865 | rev->mime_boundary = arg; |
| 866 | else |
| 867 | rev->mime_boundary = git_version_string; |
| 868 | rev->no_inline = 0; |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | static int header_callback(const struct option *opt, const char *arg, int unset) |
| 873 | { |
| 874 | add_header(arg); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int cc_callback(const struct option *opt, const char *arg, int unset) |
| 879 | { |
| 880 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); |
| 881 | extra_cc[extra_cc_nr++] = xstrdup(arg); |
| 882 | return 0; |
| 883 | } |
| 884 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 885 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 886 | { |
| 887 | struct commit *commit; |
| 888 | struct commit **list = NULL; |
| 889 | struct rev_info rev; |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 890 | int nr = 0, total, i; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 891 | int use_stdout = 0; |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 892 | int start_number = -1; |
Jon Loeliger | e6ff0f4 | 2007-06-05 15:06:53 -0500 | [diff] [blame] | 893 | int numbered_files = 0; /* _just_ numbers */ |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 894 | int ignore_if_in_upstream = 0; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 895 | int cover_letter = 0; |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 896 | int boundary_count = 0; |
Caio Marcelo de Oliveira Filho | 37c22a4 | 2008-05-09 19:55:43 -0300 | [diff] [blame] | 897 | int no_binary_diff = 0; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 898 | struct commit *origin = NULL, *head = NULL; |
Junio C Hamano | 76af073 | 2006-07-14 22:47:53 -0700 | [diff] [blame] | 899 | const char *in_reply_to = NULL; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 900 | struct patch_ids ids; |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 901 | char *add_signoff = NULL; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 902 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 1d46f2e | 2009-11-04 02:19:40 -0500 | [diff] [blame] | 903 | int use_patch_format = 0; |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 904 | const struct option builtin_format_patch_options[] = { |
| 905 | { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL, |
| 906 | "use [PATCH n/m] even with a single patch", |
| 907 | PARSE_OPT_NOARG, numbered_callback }, |
| 908 | { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL, |
| 909 | "use [PATCH] even with multiple patches", |
| 910 | PARSE_OPT_NOARG, no_numbered_callback }, |
| 911 | OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"), |
| 912 | OPT_BOOLEAN(0, "stdout", &use_stdout, |
| 913 | "print patches to standard out"), |
| 914 | OPT_BOOLEAN(0, "cover-letter", &cover_letter, |
| 915 | "generate a cover letter"), |
| 916 | OPT_BOOLEAN(0, "numbered-files", &numbered_files, |
| 917 | "use simple number sequence for output file names"), |
| 918 | OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx", |
| 919 | "use <sfx> instead of '.patch'"), |
| 920 | OPT_INTEGER(0, "start-number", &start_number, |
| 921 | "start numbering patches at <n> instead of 1"), |
| 922 | { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix", |
| 923 | "Use [<prefix>] instead of [PATCH]", |
| 924 | PARSE_OPT_NONEG, subject_prefix_callback }, |
| 925 | { OPTION_CALLBACK, 'o', "output-directory", &output_directory, |
| 926 | "dir", "store resulting files in <dir>", |
| 927 | PARSE_OPT_NONEG, output_directory_callback }, |
| 928 | { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL, |
| 929 | "don't strip/add [PATCH]", |
| 930 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback }, |
| 931 | OPT_BOOLEAN(0, "no-binary", &no_binary_diff, |
| 932 | "don't output binary diffs"), |
| 933 | OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream, |
| 934 | "don't include a patch matching a commit upstream"), |
Björn Gustavsson | 2cfa833 | 2009-11-07 10:58:55 +0100 | [diff] [blame] | 935 | { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL, |
| 936 | "show patch format instead of default (patch + stat)", |
| 937 | PARSE_OPT_NONEG | PARSE_OPT_NOARG }, |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 938 | OPT_GROUP("Messaging"), |
| 939 | { OPTION_CALLBACK, 0, "add-header", NULL, "header", |
| 940 | "add email header", PARSE_OPT_NONEG, |
| 941 | header_callback }, |
| 942 | { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header", |
| 943 | PARSE_OPT_NONEG, cc_callback }, |
| 944 | OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id", |
| 945 | "make first mail a reply to <message-id>"), |
| 946 | { OPTION_CALLBACK, 0, "attach", &rev, "boundary", |
| 947 | "attach the patch", PARSE_OPT_OPTARG, |
| 948 | attach_callback }, |
| 949 | { OPTION_CALLBACK, 0, "inline", &rev, "boundary", |
| 950 | "inline the patch", |
| 951 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
| 952 | inline_callback }, |
| 953 | { OPTION_CALLBACK, 0, "thread", &thread, "style", |
| 954 | "enable message threading, styles: shallow, deep", |
| 955 | PARSE_OPT_OPTARG, thread_callback }, |
| 956 | OPT_END() |
| 957 | }; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 958 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 959 | git_config(git_format_config, NULL); |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 960 | init_revisions(&rev, prefix); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 961 | rev.commit_format = CMIT_FMT_EMAIL; |
| 962 | rev.verbose_header = 1; |
| 963 | rev.diff = 1; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 964 | rev.combine_merges = 0; |
| 965 | rev.ignore_merges = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 966 | DIFF_OPT_SET(&rev.diffopt, RECURSIVE); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 967 | |
Adam Roben | dbd2144 | 2007-07-01 17:48:59 -0700 | [diff] [blame] | 968 | rev.subject_prefix = fmt_patch_subject_prefix; |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 969 | |
Jeremy White | 0db5260 | 2009-02-12 09:51:55 -0600 | [diff] [blame] | 970 | if (default_attach) { |
| 971 | rev.mime_boundary = default_attach; |
| 972 | rev.no_inline = 1; |
| 973 | } |
| 974 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 975 | /* |
| 976 | * Parse the arguments before setup_revisions(), or something |
Frank Lichtenheld | 2dc189a | 2007-05-14 16:44:51 +0200 | [diff] [blame] | 977 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 978 | * possibly a valid SHA1. |
| 979 | */ |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 980 | argc = parse_options(argc, argv, prefix, builtin_format_patch_options, |
Stephen Boyd | fff02ee | 2009-05-16 02:24:46 -0700 | [diff] [blame] | 981 | builtin_format_patch_usage, |
Felipe Contreras | 382da40 | 2009-11-26 21:11:59 +0200 | [diff] [blame] | 982 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | |
| 983 | PARSE_OPT_KEEP_DASHDASH); |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 984 | |
Heiko Voigt | 1d1876e | 2009-04-01 19:51:54 +0200 | [diff] [blame] | 985 | if (do_signoff) { |
| 986 | const char *committer; |
| 987 | const char *endpos; |
| 988 | committer = git_committer_info(IDENT_ERROR_ON_NO_NAME); |
| 989 | endpos = strchr(committer, '>'); |
| 990 | if (!endpos) |
| 991 | die("bogus committer info %s", committer); |
| 992 | add_signoff = xmemdupz(committer, endpos - committer + 1); |
| 993 | } |
| 994 | |
Daniel Barkalow | 3ee79d9 | 2008-02-19 02:40:33 -0500 | [diff] [blame] | 995 | for (i = 0; i < extra_hdr_nr; i++) { |
| 996 | strbuf_addstr(&buf, extra_hdr[i]); |
| 997 | strbuf_addch(&buf, '\n'); |
| 998 | } |
| 999 | |
| 1000 | if (extra_to_nr) |
| 1001 | strbuf_addstr(&buf, "To: "); |
| 1002 | for (i = 0; i < extra_to_nr; i++) { |
| 1003 | if (i) |
| 1004 | strbuf_addstr(&buf, " "); |
| 1005 | strbuf_addstr(&buf, extra_to[i]); |
| 1006 | if (i + 1 < extra_to_nr) |
| 1007 | strbuf_addch(&buf, ','); |
| 1008 | strbuf_addch(&buf, '\n'); |
| 1009 | } |
| 1010 | |
| 1011 | if (extra_cc_nr) |
| 1012 | strbuf_addstr(&buf, "Cc: "); |
| 1013 | for (i = 0; i < extra_cc_nr; i++) { |
| 1014 | if (i) |
| 1015 | strbuf_addstr(&buf, " "); |
| 1016 | strbuf_addstr(&buf, extra_cc[i]); |
| 1017 | if (i + 1 < extra_cc_nr) |
| 1018 | strbuf_addch(&buf, ','); |
| 1019 | strbuf_addch(&buf, '\n'); |
| 1020 | } |
| 1021 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1022 | rev.extra_headers = strbuf_detach(&buf, NULL); |
Daniel Barkalow | 3ee79d9 | 2008-02-19 02:40:33 -0500 | [diff] [blame] | 1023 | |
Junio C Hamano | add5c8a | 2006-05-26 11:30:49 -0700 | [diff] [blame] | 1024 | if (start_number < 0) |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 1025 | start_number = 1; |
Jim Meyering | ca6b91d | 2009-05-09 10:12:01 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* |
| 1028 | * If numbered is set solely due to format.numbered in config, |
| 1029 | * and it would conflict with --keep-subject (-k) from the |
| 1030 | * command line, reset "numbered". |
| 1031 | */ |
| 1032 | if (numbered && keep_subject && !numbered_cmdline_opt) |
| 1033 | numbered = 0; |
| 1034 | |
Junio C Hamano | 63b398a | 2006-05-28 09:23:29 -0700 | [diff] [blame] | 1035 | if (numbered && keep_subject) |
Johannes Schindelin | 8ac80a5 | 2006-05-05 04:31:29 +0200 | [diff] [blame] | 1036 | die ("-n and -k are mutually exclusive."); |
Robin H. Johnson | 2d9e4a4 | 2007-04-11 16:58:07 -0700 | [diff] [blame] | 1037 | if (keep_subject && subject_prefix) |
| 1038 | die ("--subject-prefix and -k are mutually exclusive."); |
Johannes Schindelin | 8ac80a5 | 2006-05-05 04:31:29 +0200 | [diff] [blame] | 1039 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 1040 | argc = setup_revisions(argc, argv, &rev, "HEAD"); |
| 1041 | if (argc > 1) |
| 1042 | die ("unrecognized argument: %s", argv[1]); |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 1043 | |
Björn Gustavsson | 02bc5b0 | 2009-11-07 10:51:56 +0100 | [diff] [blame] | 1044 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME) |
| 1045 | die("--name-only does not make sense"); |
| 1046 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS) |
| 1047 | die("--name-status does not make sense"); |
| 1048 | if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF) |
| 1049 | die("--check does not make sense"); |
| 1050 | |
| 1051 | if (!use_patch_format && |
| 1052 | (!rev.diffopt.output_format || |
| 1053 | rev.diffopt.output_format == DIFF_FORMAT_PATCH)) |
| 1054 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY; |
| 1055 | |
| 1056 | /* Always generate a patch */ |
| 1057 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
Timo Hirvonen | c9b5ef9 | 2006-06-24 20:24:14 +0300 | [diff] [blame] | 1058 | |
Caio Marcelo de Oliveira Filho | 37c22a4 | 2008-05-09 19:55:43 -0300 | [diff] [blame] | 1059 | if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff) |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 1060 | DIFF_OPT_SET(&rev.diffopt, BINARY); |
Junio C Hamano | e47f306 | 2007-01-17 14:32:52 -0800 | [diff] [blame] | 1061 | |
Junio C Hamano | 9800a75 | 2009-01-12 15:18:02 -0800 | [diff] [blame] | 1062 | if (!use_stdout) |
| 1063 | output_directory = set_outdir(prefix, output_directory); |
Matthias Lederhofer | 77e565d | 2006-09-28 21:55:35 +0200 | [diff] [blame] | 1064 | |
Junio C Hamano | efd0201 | 2006-06-06 08:46:23 -0700 | [diff] [blame] | 1065 | if (output_directory) { |
| 1066 | if (use_stdout) |
| 1067 | die("standard output, or directory, which one?"); |
| 1068 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 1069 | die_errno("Could not create directory '%s'", |
| 1070 | output_directory); |
Junio C Hamano | efd0201 | 2006-06-06 08:46:23 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1073 | if (rev.pending.nr == 1) { |
Junio C Hamano | 8a1d076 | 2007-08-28 00:38:48 -0700 | [diff] [blame] | 1074 | if (rev.max_count < 0 && !rev.show_root_diff) { |
| 1075 | /* |
| 1076 | * This is traditional behaviour of "git format-patch |
| 1077 | * origin" that prepares what the origin side still |
| 1078 | * does not have. |
| 1079 | */ |
Junio C Hamano | 7c49628 | 2007-01-17 13:35:13 -0800 | [diff] [blame] | 1080 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
Junio C Hamano | 3384a2d | 2007-12-11 10:09:04 -0800 | [diff] [blame] | 1081 | add_head_to_pending(&rev); |
Junio C Hamano | 7c49628 | 2007-01-17 13:35:13 -0800 | [diff] [blame] | 1082 | } |
Junio C Hamano | 8a1d076 | 2007-08-28 00:38:48 -0700 | [diff] [blame] | 1083 | /* |
| 1084 | * Otherwise, it is "format-patch -22 HEAD", and/or |
| 1085 | * "format-patch --root HEAD". The user wants |
| 1086 | * get_revision() to do the usual traversal. |
Junio C Hamano | 7c49628 | 2007-01-17 13:35:13 -0800 | [diff] [blame] | 1087 | */ |
Johannes Schindelin | e686eb9 | 2006-05-06 22:56:38 +0200 | [diff] [blame] | 1088 | } |
Junio C Hamano | 68c2ec7 | 2009-01-10 12:41:33 -0800 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * We cannot move this anywhere earlier because we do want to |
Junio C Hamano | 9517e6b | 2010-02-03 21:23:18 -0800 | [diff] [blame] | 1092 | * know if --root was given explicitly from the command line. |
Junio C Hamano | 68c2ec7 | 2009-01-10 12:41:33 -0800 | [diff] [blame] | 1093 | */ |
| 1094 | rev.show_root_diff = 1; |
| 1095 | |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1096 | if (cover_letter) { |
| 1097 | /* remember the range */ |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1098 | int i; |
| 1099 | for (i = 0; i < rev.pending.nr; i++) { |
| 1100 | struct object *o = rev.pending.objects[i].item; |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 1101 | if (!(o->flags & UNINTERESTING)) |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1102 | head = (struct commit *)o; |
| 1103 | } |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1104 | /* We can't generate a cover letter without any patches */ |
| 1105 | if (!head) |
| 1106 | return 0; |
| 1107 | } |
Johannes Schindelin | e686eb9 | 2006-05-06 22:56:38 +0200 | [diff] [blame] | 1108 | |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 1109 | if (ignore_if_in_upstream) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1110 | get_patch_ids(&rev, &ids, prefix); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 1111 | |
Johannes Schindelin | 81f3a18 | 2006-05-05 03:33:05 +0200 | [diff] [blame] | 1112 | if (!use_stdout) |
Jim Meyering | f578825 | 2007-06-27 16:28:53 +0200 | [diff] [blame] | 1113 | realstdout = xfdopen(xdup(1), "w"); |
Johannes Schindelin | 81f3a18 | 2006-05-05 03:33:05 +0200 | [diff] [blame] | 1114 | |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 1115 | if (prepare_revision_walk(&rev)) |
| 1116 | die("revision walk setup failed"); |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 1117 | rev.boundary = 1; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1118 | while ((commit = get_revision(&rev)) != NULL) { |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 1119 | if (commit->object.flags & BOUNDARY) { |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 1120 | boundary_count++; |
| 1121 | origin = (boundary_count == 1) ? commit : NULL; |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 1125 | /* ignore merges */ |
| 1126 | if (commit->parents && commit->parents->next) |
| 1127 | continue; |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 1128 | |
| 1129 | if (ignore_if_in_upstream && |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1130 | has_commit_patch_id(commit, &ids)) |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 1131 | continue; |
| 1132 | |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1133 | nr++; |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 1134 | list = xrealloc(list, nr * sizeof(list[0])); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1135 | list[nr - 1] = commit; |
| 1136 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 1137 | total = nr; |
Brian Gernhardt | 49604a4 | 2007-11-03 23:38:24 -0400 | [diff] [blame] | 1138 | if (!keep_subject && auto_number && total > 1) |
| 1139 | numbered = 1; |
Johannes Schindelin | 596524b | 2006-05-05 04:30:52 +0200 | [diff] [blame] | 1140 | if (numbered) |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 1141 | rev.total = total + start_number - 1; |
Thomas Rast | b079c50 | 2009-02-19 22:26:31 +0100 | [diff] [blame] | 1142 | if (in_reply_to || thread || cover_letter) |
| 1143 | rev.ref_message_ids = xcalloc(1, sizeof(struct string_list)); |
| 1144 | if (in_reply_to) { |
| 1145 | const char *msgid = clean_message_id(in_reply_to); |
| 1146 | string_list_append(msgid, rev.ref_message_ids); |
| 1147 | } |
Stephen Boyd | 108dab2 | 2009-03-22 19:14:05 -0700 | [diff] [blame] | 1148 | rev.numbered_files = numbered_files; |
| 1149 | rev.patch_suffix = fmt_patch_suffix; |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1150 | if (cover_letter) { |
| 1151 | if (thread) |
| 1152 | gen_message_id(&rev, "cover"); |
| 1153 | make_cover_letter(&rev, use_stdout, numbered, numbered_files, |
Daniel Barkalow | 2bda2cf | 2008-02-25 18:24:17 -0500 | [diff] [blame] | 1154 | origin, nr, list, head); |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1155 | total++; |
| 1156 | start_number--; |
| 1157 | } |
| 1158 | rev.add_signoff = add_signoff; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1159 | while (0 <= --nr) { |
| 1160 | int shown; |
| 1161 | commit = list[nr]; |
Junio C Hamano | add5c8a | 2006-05-26 11:30:49 -0700 | [diff] [blame] | 1162 | rev.nr = total - nr + (start_number - 1); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 1163 | /* Make the second and subsequent mails replies to the first */ |
Josh Triplett | cc35de8 | 2006-07-14 17:49:04 -0700 | [diff] [blame] | 1164 | if (thread) { |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1165 | /* Have we already had a message ID? */ |
Daniel Barkalow | e1a3734 | 2008-02-18 22:56:06 -0500 | [diff] [blame] | 1166 | if (rev.message_id) { |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1167 | /* |
Thomas Rast | 30984ed | 2009-02-19 22:26:33 +0100 | [diff] [blame] | 1168 | * For deep threading: make every mail |
| 1169 | * a reply to the previous one, no |
| 1170 | * matter what other options are set. |
| 1171 | * |
| 1172 | * For shallow threading: |
| 1173 | * |
Thomas Rast | 2175c10 | 2009-02-19 22:26:32 +0100 | [diff] [blame] | 1174 | * Without --cover-letter and |
| 1175 | * --in-reply-to, make every mail a |
| 1176 | * reply to the one before. |
| 1177 | * |
| 1178 | * With --in-reply-to but no |
| 1179 | * --cover-letter, make every mail a |
| 1180 | * reply to the <reply-to>. |
| 1181 | * |
| 1182 | * With --cover-letter, make every |
| 1183 | * mail but the cover letter a reply |
| 1184 | * to the cover letter. The cover |
| 1185 | * letter is a reply to the |
| 1186 | * --in-reply-to, if specified. |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1187 | */ |
Thomas Rast | 30984ed | 2009-02-19 22:26:33 +0100 | [diff] [blame] | 1188 | if (thread == THREAD_SHALLOW |
| 1189 | && rev.ref_message_ids->nr > 0 |
Thomas Rast | 2175c10 | 2009-02-19 22:26:32 +0100 | [diff] [blame] | 1190 | && (!cover_letter || rev.nr > 1)) |
Daniel Barkalow | e1a3734 | 2008-02-18 22:56:06 -0500 | [diff] [blame] | 1191 | free(rev.message_id); |
| 1192 | else |
Thomas Rast | b079c50 | 2009-02-19 22:26:31 +0100 | [diff] [blame] | 1193 | string_list_append(rev.message_id, |
| 1194 | rev.ref_message_ids); |
Josh Triplett | cc35de8 | 2006-07-14 17:49:04 -0700 | [diff] [blame] | 1195 | } |
Daniel Barkalow | e1a3734 | 2008-02-18 22:56:06 -0500 | [diff] [blame] | 1196 | gen_message_id(&rev, sha1_to_hex(commit->object.sha1)); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 1197 | } |
Stephen Boyd | 6df514a | 2009-03-22 19:14:02 -0700 | [diff] [blame] | 1198 | |
Stephen Boyd | cd2ef59 | 2009-03-22 19:14:03 -0700 | [diff] [blame] | 1199 | if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit, |
| 1200 | &rev)) |
Daniel Barkalow | a5a27c7 | 2008-02-18 22:56:13 -0500 | [diff] [blame] | 1201 | die("Failed to create output files"); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1202 | shown = log_tree_commit(&rev, commit); |
| 1203 | free(commit->buffer); |
| 1204 | commit->buffer = NULL; |
Junio C Hamano | add5c8a | 2006-05-26 11:30:49 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* We put one extra blank line between formatted |
| 1207 | * patches and this flag is used by log-tree code |
| 1208 | * to see if it needs to emit a LF before showing |
| 1209 | * the log; when using one file per patch, we do |
| 1210 | * not want the extra blank line. |
| 1211 | */ |
| 1212 | if (!use_stdout) |
| 1213 | rev.shown_one = 0; |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 1214 | if (shown) { |
| 1215 | if (rev.mime_boundary) |
| 1216 | printf("\n--%s%s--\n\n\n", |
| 1217 | mime_boundary_leader, |
| 1218 | rev.mime_boundary); |
| 1219 | else |
| 1220 | printf("-- \n%s\n\n", git_version_string); |
| 1221 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 1222 | if (!use_stdout) |
| 1223 | fclose(stdout); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1224 | } |
| 1225 | free(list); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1226 | if (ignore_if_in_upstream) |
| 1227 | free_patch_ids(&ids); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 1228 | return 0; |
| 1229 | } |
| 1230 | |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1231 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
| 1232 | { |
| 1233 | unsigned char sha1[20]; |
| 1234 | if (get_sha1(arg, sha1) == 0) { |
| 1235 | struct commit *commit = lookup_commit_reference(sha1); |
| 1236 | if (commit) { |
| 1237 | commit->object.flags |= flags; |
| 1238 | add_pending_object(revs, &commit->object, arg); |
| 1239 | return 0; |
| 1240 | } |
| 1241 | } |
| 1242 | return -1; |
| 1243 | } |
| 1244 | |
| 1245 | static const char cherry_usage[] = |
Markus Heidelberg | 3bc52d7 | 2009-01-01 22:56:29 +0100 | [diff] [blame] | 1246 | "git cherry [-v] [<upstream> [<head> [<limit>]]]"; |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1247 | int cmd_cherry(int argc, const char **argv, const char *prefix) |
| 1248 | { |
| 1249 | struct rev_info revs; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1250 | struct patch_ids ids; |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1251 | struct commit *commit; |
| 1252 | struct commit_list *list = NULL; |
Markus Heidelberg | f296802 | 2008-12-29 18:45:20 +0100 | [diff] [blame] | 1253 | struct branch *current_branch; |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1254 | const char *upstream; |
| 1255 | const char *head = "HEAD"; |
| 1256 | const char *limit = NULL; |
| 1257 | int verbose = 0; |
| 1258 | |
| 1259 | if (argc > 1 && !strcmp(argv[1], "-v")) { |
| 1260 | verbose = 1; |
| 1261 | argc--; |
| 1262 | argv++; |
| 1263 | } |
| 1264 | |
Jonathan Nieder | fef3427 | 2009-11-09 09:04:43 -0600 | [diff] [blame] | 1265 | if (argc > 1 && !strcmp(argv[1], "-h")) |
| 1266 | usage(cherry_usage); |
| 1267 | |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1268 | switch (argc) { |
| 1269 | case 4: |
| 1270 | limit = argv[3]; |
| 1271 | /* FALLTHROUGH */ |
| 1272 | case 3: |
| 1273 | head = argv[2]; |
| 1274 | /* FALLTHROUGH */ |
| 1275 | case 2: |
| 1276 | upstream = argv[1]; |
| 1277 | break; |
| 1278 | default: |
Markus Heidelberg | f296802 | 2008-12-29 18:45:20 +0100 | [diff] [blame] | 1279 | current_branch = branch_get(NULL); |
| 1280 | if (!current_branch || !current_branch->merge |
| 1281 | || !current_branch->merge[0] |
| 1282 | || !current_branch->merge[0]->dst) { |
| 1283 | fprintf(stderr, "Could not find a tracked" |
| 1284 | " remote branch, please" |
| 1285 | " specify <upstream> manually.\n"); |
| 1286 | usage(cherry_usage); |
| 1287 | } |
| 1288 | |
| 1289 | upstream = current_branch->merge[0]->dst; |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | init_revisions(&revs, prefix); |
| 1293 | revs.diff = 1; |
| 1294 | revs.combine_merges = 0; |
| 1295 | revs.ignore_merges = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 1296 | DIFF_OPT_SET(&revs.diffopt, RECURSIVE); |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1297 | |
| 1298 | if (add_pending_commit(head, &revs, 0)) |
| 1299 | die("Unknown commit %s", head); |
| 1300 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) |
| 1301 | die("Unknown commit %s", upstream); |
| 1302 | |
| 1303 | /* Don't say anything if head and upstream are the same. */ |
| 1304 | if (revs.pending.nr == 2) { |
| 1305 | struct object_array_entry *o = revs.pending.objects; |
| 1306 | if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1310 | get_patch_ids(&revs, &ids, prefix); |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1311 | |
| 1312 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) |
| 1313 | die("Unknown commit %s", limit); |
| 1314 | |
| 1315 | /* reverse the list of commits */ |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 1316 | if (prepare_revision_walk(&revs)) |
| 1317 | die("revision walk setup failed"); |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1318 | while ((commit = get_revision(&revs)) != NULL) { |
| 1319 | /* ignore merges */ |
| 1320 | if (commit->parents && commit->parents->next) |
| 1321 | continue; |
| 1322 | |
| 1323 | commit_list_insert(commit, &list); |
| 1324 | } |
| 1325 | |
| 1326 | while (list) { |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1327 | char sign = '+'; |
| 1328 | |
| 1329 | commit = list->item; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1330 | if (has_commit_patch_id(commit, &ids)) |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1331 | sign = '-'; |
| 1332 | |
| 1333 | if (verbose) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1334 | struct strbuf buf = STRBUF_INIT; |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 1335 | struct pretty_print_context ctx = {0}; |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 1336 | pretty_print_commit(CMIT_FMT_ONELINE, commit, |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 1337 | &buf, &ctx); |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1338 | printf("%c %s %s\n", sign, |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 1339 | sha1_to_hex(commit->object.sha1), buf.buf); |
| 1340 | strbuf_release(&buf); |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1341 | } |
| 1342 | else { |
| 1343 | printf("%c %s\n", sign, |
| 1344 | sha1_to_hex(commit->object.sha1)); |
| 1345 | } |
| 1346 | |
| 1347 | list = list->next; |
| 1348 | } |
| 1349 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 1350 | free_patch_ids(&ids); |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 1351 | return 0; |
| 1352 | } |