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" |
| 8 | #include "commit.h" |
| 9 | #include "diff.h" |
| 10 | #include "revision.h" |
| 11 | #include "log-tree.h" |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 12 | #include "builtin.h" |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 13 | #include "tag.h" |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 14 | #include "reflog-walk.h" |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 15 | |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 16 | static int default_show_root = 1; |
| 17 | |
Johannes Schindelin | e686eb9 | 2006-05-06 22:56:38 +0200 | [diff] [blame] | 18 | /* this is in builtin-diff.c */ |
| 19 | void add_head(struct rev_info *revs); |
| 20 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 21 | 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] | 22 | struct rev_info *rev) |
| 23 | { |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 24 | int i; |
| 25 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 26 | rev->abbrev = DEFAULT_ABBREV; |
| 27 | rev->commit_format = CMIT_FMT_DEFAULT; |
| 28 | rev->verbose_header = 1; |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 29 | rev->show_root_diff = default_show_root; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 30 | argc = setup_revisions(argc, argv, rev, "HEAD"); |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 31 | if (rev->diffopt.pickaxe || rev->diffopt.filter) |
| 32 | rev->always_show_header = 0; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 33 | for (i = 1; i < argc; i++) { |
| 34 | const char *arg = argv[i]; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 35 | if (!prefixcmp(arg, "--encoding=")) { |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 36 | arg += 11; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 37 | if (strcmp(arg, "none")) |
James Bowes | 567fb65 | 2007-03-19 17:42:40 -0400 | [diff] [blame] | 38 | git_log_output_encoding = xstrdup(arg); |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 39 | else |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 40 | git_log_output_encoding = ""; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 41 | } |
| 42 | else |
| 43 | die("unrecognized argument: %s", arg); |
| 44 | } |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int cmd_log_walk(struct rev_info *rev) |
| 48 | { |
| 49 | struct commit *commit; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 50 | |
| 51 | prepare_revision_walk(rev); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 52 | while ((commit = get_revision(rev)) != NULL) { |
| 53 | log_tree_commit(rev, commit); |
Johannes Schindelin | a6c7306 | 2007-01-20 22:28:16 +0100 | [diff] [blame] | 54 | if (!rev->reflog_info) { |
| 55 | /* we allow cycles in reflog ancestry */ |
| 56 | free(commit->buffer); |
| 57 | commit->buffer = NULL; |
| 58 | } |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 59 | free_commit_list(commit->parents); |
| 60 | commit->parents = NULL; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 65 | static int git_log_config(const char *var, const char *value) |
| 66 | { |
| 67 | if (!strcmp(var, "log.showroot")) { |
| 68 | default_show_root = git_config_bool(var, value); |
| 69 | return 0; |
| 70 | } |
| 71 | return git_diff_ui_config(var, value); |
| 72 | } |
| 73 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 74 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 75 | { |
| 76 | struct rev_info rev; |
| 77 | |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 78 | git_config(git_log_config); |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 79 | init_revisions(&rev, prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 80 | rev.diff = 1; |
| 81 | rev.diffopt.recursive = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 82 | rev.simplify_history = 0; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 83 | cmd_log_init(argc, argv, prefix, &rev); |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 84 | if (!rev.diffopt.output_format) |
| 85 | rev.diffopt.output_format = DIFF_FORMAT_RAW; |
| 86 | return cmd_log_walk(&rev); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 89 | static int show_object(const unsigned char *sha1, int suppress_header) |
| 90 | { |
| 91 | unsigned long size; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 92 | enum object_type type; |
| 93 | char *buf = read_sha1_file(sha1, &type, &size); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 94 | int offset = 0; |
| 95 | |
| 96 | if (!buf) |
| 97 | return error("Could not read object %s", sha1_to_hex(sha1)); |
| 98 | |
| 99 | if (suppress_header) |
| 100 | while (offset < size && buf[offset++] != '\n') { |
| 101 | int new_offset = offset; |
| 102 | while (new_offset < size && buf[new_offset++] != '\n') |
| 103 | ; /* do nothing */ |
| 104 | offset = new_offset; |
| 105 | } |
| 106 | |
| 107 | if (offset < size) |
| 108 | fwrite(buf + offset, size - offset, 1, stdout); |
| 109 | free(buf); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int show_tree_object(const unsigned char *sha1, |
| 114 | const char *base, int baselen, |
| 115 | const char *pathname, unsigned mode, int stage) |
| 116 | { |
| 117 | printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); |
| 118 | return 0; |
| 119 | } |
| 120 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 121 | int cmd_show(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 122 | { |
| 123 | struct rev_info rev; |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 124 | struct object_array_entry *objects; |
| 125 | int i, count, ret = 0; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 126 | |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 127 | git_config(git_log_config); |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 128 | init_revisions(&rev, prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 129 | rev.diff = 1; |
| 130 | rev.diffopt.recursive = 1; |
| 131 | rev.combine_merges = 1; |
| 132 | rev.dense_combined_merges = 1; |
| 133 | rev.always_show_header = 1; |
| 134 | rev.ignore_merges = 0; |
| 135 | rev.no_walk = 1; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 136 | cmd_log_init(argc, argv, prefix, &rev); |
Johannes Schindelin | 5d7eeee | 2006-12-14 11:31:05 +0100 | [diff] [blame] | 137 | |
| 138 | count = rev.pending.nr; |
| 139 | objects = rev.pending.objects; |
| 140 | for (i = 0; i < count && !ret; i++) { |
| 141 | struct object *o = objects[i].item; |
| 142 | const char *name = objects[i].name; |
| 143 | switch (o->type) { |
| 144 | case OBJ_BLOB: |
| 145 | ret = show_object(o->sha1, 0); |
| 146 | break; |
| 147 | case OBJ_TAG: { |
| 148 | struct tag *t = (struct tag *)o; |
| 149 | |
| 150 | printf("%stag %s%s\n\n", |
| 151 | diff_get_color(rev.diffopt.color_diff, |
| 152 | DIFF_COMMIT), |
| 153 | t->tag, |
| 154 | diff_get_color(rev.diffopt.color_diff, |
| 155 | DIFF_RESET)); |
| 156 | ret = show_object(o->sha1, 1); |
| 157 | objects[i].item = (struct object *)t->tagged; |
| 158 | i--; |
| 159 | break; |
| 160 | } |
| 161 | case OBJ_TREE: |
| 162 | printf("%stree %s%s\n\n", |
| 163 | diff_get_color(rev.diffopt.color_diff, |
| 164 | DIFF_COMMIT), |
| 165 | name, |
| 166 | diff_get_color(rev.diffopt.color_diff, |
| 167 | DIFF_RESET)); |
| 168 | read_tree_recursive((struct tree *)o, "", 0, 0, NULL, |
| 169 | show_tree_object); |
| 170 | break; |
| 171 | case OBJ_COMMIT: |
| 172 | rev.pending.nr = rev.pending.alloc = 0; |
| 173 | rev.pending.objects = NULL; |
| 174 | add_object_array(o, name, &rev.pending); |
| 175 | ret = cmd_log_walk(&rev); |
| 176 | break; |
| 177 | default: |
| 178 | ret = error("Unknown type: %d", o->type); |
| 179 | } |
| 180 | } |
| 181 | free(objects); |
| 182 | return ret; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 185 | /* |
| 186 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" |
| 187 | */ |
| 188 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) |
| 189 | { |
| 190 | struct rev_info rev; |
| 191 | |
| 192 | git_config(git_log_config); |
| 193 | init_revisions(&rev, prefix); |
| 194 | init_reflog_walk(&rev.reflog_info); |
| 195 | rev.abbrev_commit = 1; |
| 196 | rev.verbose_header = 1; |
| 197 | cmd_log_init(argc, argv, prefix, &rev); |
| 198 | |
| 199 | /* |
| 200 | * This means that we override whatever commit format the user gave |
| 201 | * on the cmd line. Sad, but cmd_log_init() currently doesn't |
| 202 | * allow us to set a different default. |
| 203 | */ |
| 204 | rev.commit_format = CMIT_FMT_ONELINE; |
| 205 | rev.always_show_header = 1; |
| 206 | |
| 207 | /* |
| 208 | * We get called through "git reflog", so unlike the other log |
| 209 | * routines, we need to set up our pager manually.. |
| 210 | */ |
| 211 | setup_pager(); |
| 212 | |
| 213 | return cmd_log_walk(&rev); |
| 214 | } |
| 215 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 216 | int cmd_log(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 217 | { |
| 218 | struct rev_info rev; |
| 219 | |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 220 | git_config(git_log_config); |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 221 | init_revisions(&rev, prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 222 | rev.always_show_header = 1; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 223 | cmd_log_init(argc, argv, prefix, &rev); |
Timo Hirvonen | 9dafea2 | 2006-06-25 15:39:35 +0300 | [diff] [blame] | 224 | return cmd_log_walk(&rev); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 225 | } |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 226 | |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 227 | /* format-patch */ |
| 228 | #define FORMAT_PATCH_NAME_MAX 64 |
| 229 | |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 230 | static int istitlechar(char c) |
| 231 | { |
| 232 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 233 | (c >= '0' && c <= '9') || c == '.' || c == '_'; |
| 234 | } |
| 235 | |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 236 | static char *extra_headers = NULL; |
| 237 | static int extra_headers_size = 0; |
Junio C Hamano | 917a8f8 | 2007-01-17 15:03:39 -0800 | [diff] [blame] | 238 | static const char *fmt_patch_suffix = ".patch"; |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 239 | |
| 240 | static int git_format_config(const char *var, const char *value) |
| 241 | { |
| 242 | if (!strcmp(var, "format.headers")) { |
Junio C Hamano | d7fb91c | 2007-01-17 11:13:02 -0800 | [diff] [blame] | 243 | int len; |
| 244 | |
| 245 | if (!value) |
| 246 | die("format.headers without value"); |
| 247 | len = strlen(value); |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 248 | extra_headers_size += len + 1; |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 249 | extra_headers = xrealloc(extra_headers, extra_headers_size); |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 250 | extra_headers[extra_headers_size - len - 1] = 0; |
| 251 | strcat(extra_headers, value); |
| 252 | return 0; |
| 253 | } |
Junio C Hamano | 03eeaea | 2007-01-17 11:12:03 -0800 | [diff] [blame] | 254 | if (!strcmp(var, "format.suffix")) { |
| 255 | if (!value) |
| 256 | die("format.suffix without value"); |
| 257 | fmt_patch_suffix = xstrdup(value); |
| 258 | return 0; |
| 259 | } |
Andy Parkins | a159ca0 | 2006-12-13 09:13:28 +0000 | [diff] [blame] | 260 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
Ryan Anderson | f3aafa4 | 2006-07-09 02:28:21 -0400 | [diff] [blame] | 261 | return 0; |
| 262 | } |
Peter Baumann | 0f03ca9 | 2006-11-23 10:36:33 +0100 | [diff] [blame] | 263 | return git_log_config(var, value); |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Johannes Schindelin | 81f3a18 | 2006-05-05 03:33:05 +0200 | [diff] [blame] | 267 | static FILE *realstdout = NULL; |
Junio C Hamano | efd0201 | 2006-06-06 08:46:23 -0700 | [diff] [blame] | 268 | static const char *output_directory = NULL; |
Johannes Schindelin | 81f3a18 | 2006-05-05 03:33:05 +0200 | [diff] [blame] | 269 | |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 270 | static int reopen_stdout(struct commit *commit, int nr, int keep_subject) |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 271 | { |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 272 | char filename[PATH_MAX]; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 273 | char *sol; |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 274 | int len = 0; |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 275 | int suffix_len = strlen(fmt_patch_suffix) + 1; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 276 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 277 | if (output_directory) { |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 278 | if (strlen(output_directory) >= |
| 279 | sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len) |
| 280 | return error("name of output directory is too long"); |
| 281 | strlcpy(filename, output_directory, sizeof(filename) - suffix_len); |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 282 | len = strlen(filename); |
| 283 | if (filename[len - 1] != '/') |
| 284 | filename[len++] = '/'; |
| 285 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 286 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 287 | sprintf(filename + len, "%04d", nr); |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 288 | len = strlen(filename); |
| 289 | |
| 290 | sol = strstr(commit->buffer, "\n\n"); |
| 291 | if (sol) { |
| 292 | int j, space = 1; |
| 293 | |
| 294 | sol += 2; |
| 295 | /* strip [PATCH] or [PATCH blabla] */ |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 296 | if (!keep_subject && !prefixcmp(sol, "[PATCH")) { |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 297 | char *eos = strchr(sol + 6, ']'); |
| 298 | if (eos) { |
| 299 | while (isspace(*eos)) |
| 300 | eos++; |
| 301 | sol = eos; |
| 302 | } |
| 303 | } |
| 304 | |
Junio C Hamano | 03eeaea | 2007-01-17 11:12:03 -0800 | [diff] [blame] | 305 | for (j = 0; |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 306 | j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 && |
| 307 | len < sizeof(filename) - suffix_len && |
Junio C Hamano | 03eeaea | 2007-01-17 11:12:03 -0800 | [diff] [blame] | 308 | sol[j] && sol[j] != '\n'; |
| 309 | j++) { |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 310 | if (istitlechar(sol[j])) { |
| 311 | if (space) { |
| 312 | filename[len++] = '-'; |
| 313 | space = 0; |
| 314 | } |
| 315 | filename[len++] = sol[j]; |
| 316 | if (sol[j] == '.') |
| 317 | while (sol[j + 1] == '.') |
| 318 | j++; |
| 319 | } else |
| 320 | space = 1; |
| 321 | } |
| 322 | while (filename[len - 1] == '.' || filename[len - 1] == '-') |
| 323 | len--; |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 324 | filename[len] = 0; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 325 | } |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 326 | if (len + suffix_len >= sizeof(filename)) |
| 327 | return error("Patch pathname too long"); |
Junio C Hamano | 03eeaea | 2007-01-17 11:12:03 -0800 | [diff] [blame] | 328 | strcpy(filename + len, fmt_patch_suffix); |
Johannes Schindelin | 81f3a18 | 2006-05-05 03:33:05 +0200 | [diff] [blame] | 329 | fprintf(realstdout, "%s\n", filename); |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 330 | if (freopen(filename, "w", stdout) == NULL) |
| 331 | return error("Cannot open patch file %s",filename); |
| 332 | return 0; |
| 333 | |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 336 | static int get_patch_id(struct commit *commit, struct diff_options *options, |
| 337 | unsigned char *sha1) |
| 338 | { |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 339 | if (commit->parents) |
| 340 | diff_tree_sha1(commit->parents->item->object.sha1, |
| 341 | commit->object.sha1, "", options); |
| 342 | else |
| 343 | diff_root_tree_sha1(commit->object.sha1, "", options); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 344 | diffcore_std(options); |
| 345 | return diff_flush_patch_id(options, sha1); |
| 346 | } |
| 347 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 348 | static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix) |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 349 | { |
| 350 | struct rev_info check_rev; |
| 351 | struct commit *commit; |
| 352 | struct object *o1, *o2; |
| 353 | unsigned flags1, flags2; |
| 354 | unsigned char sha1[20]; |
| 355 | |
| 356 | if (rev->pending.nr != 2) |
| 357 | die("Need exactly one range."); |
| 358 | |
| 359 | o1 = rev->pending.objects[0].item; |
| 360 | flags1 = o1->flags; |
| 361 | o2 = rev->pending.objects[1].item; |
| 362 | flags2 = o2->flags; |
| 363 | |
| 364 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) |
| 365 | die("Not a range."); |
| 366 | |
| 367 | diff_setup(options); |
| 368 | options->recursive = 1; |
| 369 | if (diff_setup_done(options) < 0) |
| 370 | die("diff_setup_done failed"); |
| 371 | |
| 372 | /* given a range a..b get all patch ids for b..a */ |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 373 | init_revisions(&check_rev, prefix); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 374 | o1->flags ^= UNINTERESTING; |
| 375 | o2->flags ^= UNINTERESTING; |
| 376 | add_pending_object(&check_rev, o1, "o1"); |
| 377 | add_pending_object(&check_rev, o2, "o2"); |
| 378 | prepare_revision_walk(&check_rev); |
| 379 | |
| 380 | while ((commit = get_revision(&check_rev)) != NULL) { |
| 381 | /* ignore merges */ |
| 382 | if (commit->parents && commit->parents->next) |
| 383 | continue; |
| 384 | |
| 385 | if (!get_patch_id(commit, options, sha1)) |
| 386 | created_object(sha1, xcalloc(1, sizeof(struct object))); |
| 387 | } |
| 388 | |
| 389 | /* reset for next revision walk */ |
Johannes Schindelin | 81db094 | 2006-06-27 22:38:04 +0200 | [diff] [blame] | 390 | clear_commit_marks((struct commit *)o1, |
| 391 | SEEN | UNINTERESTING | SHOWN | ADDED); |
| 392 | clear_commit_marks((struct commit *)o2, |
| 393 | SEEN | UNINTERESTING | SHOWN | ADDED); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 394 | o1->flags = flags1; |
| 395 | o2->flags = flags2; |
| 396 | } |
| 397 | |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 398 | static void gen_message_id(char *dest, unsigned int length, char *base) |
| 399 | { |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 400 | const char *committer = git_committer_info(-1); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 401 | const char *email_start = strrchr(committer, '<'); |
| 402 | const char *email_end = strrchr(committer, '>'); |
| 403 | if(!email_start || !email_end || email_start > email_end - 1) |
| 404 | die("Could not extract email from committer identity."); |
Junio C Hamano | 76af073 | 2006-07-14 22:47:53 -0700 | [diff] [blame] | 405 | snprintf(dest, length, "%s.%lu.git.%.*s", base, |
| 406 | (unsigned long) time(NULL), |
| 407 | (int)(email_end - email_start - 1), email_start + 1); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 410 | 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] | 411 | { |
| 412 | struct commit *commit; |
| 413 | struct commit **list = NULL; |
| 414 | struct rev_info rev; |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 415 | int nr = 0, total, i, j; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 416 | int use_stdout = 0; |
Johannes Schindelin | 596524b | 2006-05-05 04:30:52 +0200 | [diff] [blame] | 417 | int numbered = 0; |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 418 | int start_number = -1; |
Johannes Schindelin | 8ac80a5 | 2006-05-05 04:31:29 +0200 | [diff] [blame] | 419 | int keep_subject = 0; |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 420 | int ignore_if_in_upstream = 0; |
Josh Triplett | cc35de8 | 2006-07-14 17:49:04 -0700 | [diff] [blame] | 421 | int thread = 0; |
Junio C Hamano | 76af073 | 2006-07-14 22:47:53 -0700 | [diff] [blame] | 422 | const char *in_reply_to = NULL; |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 423 | struct diff_options patch_id_opts; |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 424 | char *add_signoff = NULL; |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 425 | char message_id[1024]; |
| 426 | char ref_message_id[1024]; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 427 | |
Eric Wong | 97beb81 | 2006-07-07 03:10:45 -0700 | [diff] [blame] | 428 | git_config(git_format_config); |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 429 | init_revisions(&rev, prefix); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 430 | rev.commit_format = CMIT_FMT_EMAIL; |
| 431 | rev.verbose_header = 1; |
| 432 | rev.diff = 1; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 433 | rev.combine_merges = 0; |
| 434 | rev.ignore_merges = 1; |
Junio C Hamano | 27e1b12 | 2006-06-29 00:18:52 -0700 | [diff] [blame] | 435 | rev.diffopt.msg_sep = ""; |
| 436 | rev.diffopt.recursive = 1; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 437 | |
Johannes Schindelin | 20ff068 | 2006-06-02 15:21:17 +0200 | [diff] [blame] | 438 | rev.extra_headers = extra_headers; |
| 439 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 440 | /* |
| 441 | * Parse the arguments before setup_revisions(), or something |
Frank Lichtenheld | 2dc189a | 2007-05-14 16:44:51 +0200 | [diff] [blame] | 442 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 443 | * possibly a valid SHA1. |
| 444 | */ |
| 445 | for (i = 1, j = 1; i < argc; i++) { |
| 446 | if (!strcmp(argv[i], "--stdout")) |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 447 | use_stdout = 1; |
Johannes Schindelin | 596524b | 2006-05-05 04:30:52 +0200 | [diff] [blame] | 448 | else if (!strcmp(argv[i], "-n") || |
| 449 | !strcmp(argv[i], "--numbered")) |
| 450 | numbered = 1; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 451 | else if (!prefixcmp(argv[i], "--start-number=")) |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 452 | start_number = strtol(argv[i] + 15, NULL, 10); |
| 453 | else if (!strcmp(argv[i], "--start-number")) { |
| 454 | i++; |
| 455 | if (i == argc) |
| 456 | die("Need a number for --start-number"); |
| 457 | start_number = strtol(argv[i], NULL, 10); |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 458 | } |
| 459 | else if (!strcmp(argv[i], "-k") || |
Johannes Schindelin | 8ac80a5 | 2006-05-05 04:31:29 +0200 | [diff] [blame] | 460 | !strcmp(argv[i], "--keep-subject")) { |
| 461 | keep_subject = 1; |
| 462 | rev.total = -1; |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 463 | } |
Junio C Hamano | efd0201 | 2006-06-06 08:46:23 -0700 | [diff] [blame] | 464 | else if (!strcmp(argv[i], "--output-directory") || |
| 465 | !strcmp(argv[i], "-o")) { |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 466 | i++; |
Junio C Hamano | efd0201 | 2006-06-06 08:46:23 -0700 | [diff] [blame] | 467 | if (argc <= i) |
| 468 | die("Which directory?"); |
| 469 | if (output_directory) |
| 470 | die("Two output directories?"); |
| 471 | output_directory = argv[i]; |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 472 | } |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 473 | else if (!strcmp(argv[i], "--signoff") || |
| 474 | !strcmp(argv[i], "-s")) { |
Eric W. Biederman | 6c4cca1 | 2006-06-12 13:31:38 -0600 | [diff] [blame] | 475 | const char *committer; |
| 476 | const char *endpos; |
Eric W. Biederman | 6c4cca1 | 2006-06-12 13:31:38 -0600 | [diff] [blame] | 477 | committer = git_committer_info(1); |
| 478 | endpos = strchr(committer, '>'); |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 479 | if (!endpos) |
| 480 | die("bogos committer info %s\n", committer); |
| 481 | add_signoff = xmalloc(endpos - committer + 2); |
| 482 | memcpy(add_signoff, committer, endpos - committer + 1); |
| 483 | add_signoff[endpos - committer + 1] = 0; |
| 484 | } |
Johannes Schindelin | c112f68 | 2007-03-04 00:12:06 +0100 | [diff] [blame] | 485 | else if (!strcmp(argv[i], "--attach")) { |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 486 | rev.mime_boundary = git_version_string; |
Johannes Schindelin | c112f68 | 2007-03-04 00:12:06 +0100 | [diff] [blame] | 487 | rev.no_inline = 1; |
| 488 | } |
| 489 | else if (!prefixcmp(argv[i], "--attach=")) { |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 490 | rev.mime_boundary = argv[i] + 9; |
Johannes Schindelin | c112f68 | 2007-03-04 00:12:06 +0100 | [diff] [blame] | 491 | rev.no_inline = 1; |
| 492 | } |
| 493 | else if (!strcmp(argv[i], "--inline")) { |
| 494 | rev.mime_boundary = git_version_string; |
| 495 | rev.no_inline = 0; |
| 496 | } |
| 497 | else if (!prefixcmp(argv[i], "--inline=")) { |
| 498 | rev.mime_boundary = argv[i] + 9; |
| 499 | rev.no_inline = 0; |
| 500 | } |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 501 | else if (!strcmp(argv[i], "--ignore-if-in-upstream")) |
| 502 | ignore_if_in_upstream = 1; |
Josh Triplett | cc35de8 | 2006-07-14 17:49:04 -0700 | [diff] [blame] | 503 | else if (!strcmp(argv[i], "--thread")) |
| 504 | thread = 1; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 505 | else if (!prefixcmp(argv[i], "--in-reply-to=")) |
Josh Triplett | da56645 | 2006-07-14 17:49:08 -0700 | [diff] [blame] | 506 | in_reply_to = argv[i] + 14; |
| 507 | else if (!strcmp(argv[i], "--in-reply-to")) { |
| 508 | i++; |
| 509 | if (i == argc) |
| 510 | die("Need a Message-Id for --in-reply-to"); |
| 511 | in_reply_to = argv[i]; |
| 512 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 513 | else if (!prefixcmp(argv[i], "--suffix=")) |
Junio C Hamano | 03eeaea | 2007-01-17 11:12:03 -0800 | [diff] [blame] | 514 | fmt_patch_suffix = argv[i] + 9; |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 515 | else |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 516 | argv[j++] = argv[i]; |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 517 | } |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 518 | argc = j; |
| 519 | |
Junio C Hamano | add5c8a | 2006-05-26 11:30:49 -0700 | [diff] [blame] | 520 | if (start_number < 0) |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 521 | start_number = 1; |
Junio C Hamano | 63b398a | 2006-05-28 09:23:29 -0700 | [diff] [blame] | 522 | if (numbered && keep_subject) |
Johannes Schindelin | 8ac80a5 | 2006-05-05 04:31:29 +0200 | [diff] [blame] | 523 | die ("-n and -k are mutually exclusive."); |
| 524 | |
Johannes Schindelin | 2448482 | 2006-05-05 03:33:32 +0200 | [diff] [blame] | 525 | argc = setup_revisions(argc, argv, &rev, "HEAD"); |
| 526 | if (argc > 1) |
| 527 | die ("unrecognized argument: %s", argv[1]); |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 528 | |
Timo Hirvonen | c9b5ef9 | 2006-06-24 20:24:14 +0300 | [diff] [blame] | 529 | if (!rev.diffopt.output_format) |
Junio C Hamano | c261e43 | 2007-01-17 13:51:44 -0800 | [diff] [blame] | 530 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH; |
Timo Hirvonen | c9b5ef9 | 2006-06-24 20:24:14 +0300 | [diff] [blame] | 531 | |
Junio C Hamano | e47f306 | 2007-01-17 14:32:52 -0800 | [diff] [blame] | 532 | if (!rev.diffopt.text) |
| 533 | rev.diffopt.binary = 1; |
| 534 | |
Jeff King | 90f70a9 | 2007-01-22 22:38:28 -0500 | [diff] [blame] | 535 | if (!output_directory && !use_stdout) |
Matthias Lederhofer | 77e565d | 2006-09-28 21:55:35 +0200 | [diff] [blame] | 536 | output_directory = prefix; |
| 537 | |
Junio C Hamano | efd0201 | 2006-06-06 08:46:23 -0700 | [diff] [blame] | 538 | if (output_directory) { |
| 539 | if (use_stdout) |
| 540 | die("standard output, or directory, which one?"); |
| 541 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) |
| 542 | die("Could not create directory %s", |
| 543 | output_directory); |
| 544 | } |
| 545 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 546 | if (rev.pending.nr == 1) { |
Junio C Hamano | 7c49628 | 2007-01-17 13:35:13 -0800 | [diff] [blame] | 547 | if (rev.max_count < 0) { |
| 548 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
| 549 | add_head(&rev); |
| 550 | } |
| 551 | /* Otherwise, it is "format-patch -22 HEAD", and |
| 552 | * get_revision() would return only the specified count. |
| 553 | */ |
Johannes Schindelin | e686eb9 | 2006-05-06 22:56:38 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 556 | if (ignore_if_in_upstream) |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 557 | get_patch_ids(&rev, &patch_id_opts, prefix); |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 558 | |
Johannes Schindelin | 81f3a18 | 2006-05-05 03:33:05 +0200 | [diff] [blame] | 559 | if (!use_stdout) |
| 560 | realstdout = fdopen(dup(1), "w"); |
| 561 | |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 562 | prepare_revision_walk(&rev); |
| 563 | while ((commit = get_revision(&rev)) != NULL) { |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 564 | unsigned char sha1[20]; |
| 565 | |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 566 | /* ignore merges */ |
| 567 | if (commit->parents && commit->parents->next) |
| 568 | continue; |
Johannes Schindelin | 9c6efa3 | 2006-06-25 03:52:01 +0200 | [diff] [blame] | 569 | |
| 570 | if (ignore_if_in_upstream && |
| 571 | !get_patch_id(commit, &patch_id_opts, sha1) && |
| 572 | lookup_object(sha1)) |
| 573 | continue; |
| 574 | |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 575 | nr++; |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 576 | list = xrealloc(list, nr * sizeof(list[0])); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 577 | list[nr - 1] = commit; |
| 578 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 579 | total = nr; |
Johannes Schindelin | 596524b | 2006-05-05 04:30:52 +0200 | [diff] [blame] | 580 | if (numbered) |
Johannes Schindelin | fa0f02d | 2006-05-25 23:55:11 +0200 | [diff] [blame] | 581 | rev.total = total + start_number - 1; |
Junio C Hamano | cf2251b | 2006-05-31 15:11:49 -0700 | [diff] [blame] | 582 | rev.add_signoff = add_signoff; |
Josh Triplett | da56645 | 2006-07-14 17:49:08 -0700 | [diff] [blame] | 583 | rev.ref_message_id = in_reply_to; |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 584 | while (0 <= --nr) { |
| 585 | int shown; |
| 586 | commit = list[nr]; |
Junio C Hamano | add5c8a | 2006-05-26 11:30:49 -0700 | [diff] [blame] | 587 | rev.nr = total - nr + (start_number - 1); |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 588 | /* Make the second and subsequent mails replies to the first */ |
Josh Triplett | cc35de8 | 2006-07-14 17:49:04 -0700 | [diff] [blame] | 589 | if (thread) { |
| 590 | if (nr == (total - 2)) { |
| 591 | strncpy(ref_message_id, message_id, |
| 592 | sizeof(ref_message_id)); |
| 593 | ref_message_id[sizeof(ref_message_id)-1]='\0'; |
| 594 | rev.ref_message_id = ref_message_id; |
| 595 | } |
| 596 | gen_message_id(message_id, sizeof(message_id), |
| 597 | sha1_to_hex(commit->object.sha1)); |
| 598 | rev.message_id = message_id; |
Josh Triplett | d1566f7 | 2006-07-14 17:48:51 -0700 | [diff] [blame] | 599 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 600 | if (!use_stdout) |
Robin Rosenberg | c06d2da | 2007-02-23 23:27:58 +0100 | [diff] [blame] | 601 | if (reopen_stdout(commit, rev.nr, keep_subject)) |
| 602 | die("Failed to create output files"); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 603 | shown = log_tree_commit(&rev, commit); |
| 604 | free(commit->buffer); |
| 605 | commit->buffer = NULL; |
Junio C Hamano | add5c8a | 2006-05-26 11:30:49 -0700 | [diff] [blame] | 606 | |
| 607 | /* We put one extra blank line between formatted |
| 608 | * patches and this flag is used by log-tree code |
| 609 | * to see if it needs to emit a LF before showing |
| 610 | * the log; when using one file per patch, we do |
| 611 | * not want the extra blank line. |
| 612 | */ |
| 613 | if (!use_stdout) |
| 614 | rev.shown_one = 0; |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 615 | if (shown) { |
| 616 | if (rev.mime_boundary) |
| 617 | printf("\n--%s%s--\n\n\n", |
| 618 | mime_boundary_leader, |
| 619 | rev.mime_boundary); |
| 620 | else |
| 621 | printf("-- \n%s\n\n", git_version_string); |
| 622 | } |
Johannes Schindelin | 0377db7 | 2006-05-05 01:16:40 +0200 | [diff] [blame] | 623 | if (!use_stdout) |
| 624 | fclose(stdout); |
Junio C Hamano | 91efcf6 | 2006-04-21 13:19:58 -0700 | [diff] [blame] | 625 | } |
| 626 | free(list); |
| 627 | return 0; |
| 628 | } |
| 629 | |
Rene Scharfe | e827633 | 2006-10-24 01:01:57 +0200 | [diff] [blame] | 630 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
| 631 | { |
| 632 | unsigned char sha1[20]; |
| 633 | if (get_sha1(arg, sha1) == 0) { |
| 634 | struct commit *commit = lookup_commit_reference(sha1); |
| 635 | if (commit) { |
| 636 | commit->object.flags |= flags; |
| 637 | add_pending_object(revs, &commit->object, arg); |
| 638 | return 0; |
| 639 | } |
| 640 | } |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | static const char cherry_usage[] = |
| 645 | "git-cherry [-v] <upstream> [<head>] [<limit>]"; |
| 646 | int cmd_cherry(int argc, const char **argv, const char *prefix) |
| 647 | { |
| 648 | struct rev_info revs; |
| 649 | struct diff_options patch_id_opts; |
| 650 | struct commit *commit; |
| 651 | struct commit_list *list = NULL; |
| 652 | const char *upstream; |
| 653 | const char *head = "HEAD"; |
| 654 | const char *limit = NULL; |
| 655 | int verbose = 0; |
| 656 | |
| 657 | if (argc > 1 && !strcmp(argv[1], "-v")) { |
| 658 | verbose = 1; |
| 659 | argc--; |
| 660 | argv++; |
| 661 | } |
| 662 | |
| 663 | switch (argc) { |
| 664 | case 4: |
| 665 | limit = argv[3]; |
| 666 | /* FALLTHROUGH */ |
| 667 | case 3: |
| 668 | head = argv[2]; |
| 669 | /* FALLTHROUGH */ |
| 670 | case 2: |
| 671 | upstream = argv[1]; |
| 672 | break; |
| 673 | default: |
| 674 | usage(cherry_usage); |
| 675 | } |
| 676 | |
| 677 | init_revisions(&revs, prefix); |
| 678 | revs.diff = 1; |
| 679 | revs.combine_merges = 0; |
| 680 | revs.ignore_merges = 1; |
| 681 | revs.diffopt.recursive = 1; |
| 682 | |
| 683 | if (add_pending_commit(head, &revs, 0)) |
| 684 | die("Unknown commit %s", head); |
| 685 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) |
| 686 | die("Unknown commit %s", upstream); |
| 687 | |
| 688 | /* Don't say anything if head and upstream are the same. */ |
| 689 | if (revs.pending.nr == 2) { |
| 690 | struct object_array_entry *o = revs.pending.objects; |
| 691 | if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | get_patch_ids(&revs, &patch_id_opts, prefix); |
| 696 | |
| 697 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) |
| 698 | die("Unknown commit %s", limit); |
| 699 | |
| 700 | /* reverse the list of commits */ |
| 701 | prepare_revision_walk(&revs); |
| 702 | while ((commit = get_revision(&revs)) != NULL) { |
| 703 | /* ignore merges */ |
| 704 | if (commit->parents && commit->parents->next) |
| 705 | continue; |
| 706 | |
| 707 | commit_list_insert(commit, &list); |
| 708 | } |
| 709 | |
| 710 | while (list) { |
| 711 | unsigned char sha1[20]; |
| 712 | char sign = '+'; |
| 713 | |
| 714 | commit = list->item; |
| 715 | if (!get_patch_id(commit, &patch_id_opts, sha1) && |
| 716 | lookup_object(sha1)) |
| 717 | sign = '-'; |
| 718 | |
| 719 | if (verbose) { |
| 720 | static char buf[16384]; |
| 721 | pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, |
| 722 | buf, sizeof(buf), 0, NULL, NULL, 0); |
| 723 | printf("%c %s %s\n", sign, |
| 724 | sha1_to_hex(commit->object.sha1), buf); |
| 725 | } |
| 726 | else { |
| 727 | printf("%c %s\n", sign, |
| 728 | sha1_to_hex(commit->object.sha1)); |
| 729 | } |
| 730 | |
| 731 | list = list->next; |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |