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