Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "tag.h" |
| 3 | #include "blob.h" |
| 4 | #include "tree.h" |
| 5 | #include "commit.h" |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 6 | #include "diff.h" |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 7 | #include "refs.h" |
| 8 | #include "revision.h" |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 9 | #include "graph.h" |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 10 | #include "grep.h" |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 11 | #include "reflog-walk.h" |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 12 | #include "patch-ids.h" |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 13 | #include "decorate.h" |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 14 | #include "log-tree.h" |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 15 | #include "string-list.h" |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 16 | |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 17 | volatile show_early_output_fn_t show_early_output; |
| 18 | |
Linus Torvalds | cf2ab91 | 2009-04-10 18:15:26 -0700 | [diff] [blame] | 19 | char *path_name(const struct name_path *path, const char *name) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 20 | { |
Linus Torvalds | cf2ab91 | 2009-04-10 18:15:26 -0700 | [diff] [blame] | 21 | const struct name_path *p; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 22 | char *n, *m; |
| 23 | int nlen = strlen(name); |
| 24 | int len = nlen + 1; |
| 25 | |
| 26 | for (p = path; p; p = p->up) { |
| 27 | if (p->elem_len) |
| 28 | len += p->elem_len + 1; |
| 29 | } |
| 30 | n = xmalloc(len); |
| 31 | m = n + len - (nlen + 1); |
| 32 | strcpy(m, name); |
| 33 | for (p = path; p; p = p->up) { |
| 34 | if (p->elem_len) { |
| 35 | m -= p->elem_len + 1; |
| 36 | memcpy(m, p->elem, p->elem_len); |
| 37 | m[p->elem_len] = '/'; |
| 38 | } |
| 39 | } |
| 40 | return n; |
| 41 | } |
| 42 | |
Junio C Hamano | beba25a | 2011-08-17 14:30:35 -0700 | [diff] [blame] | 43 | static int show_path_component_truncated(FILE *out, const char *name, int len) |
| 44 | { |
| 45 | int cnt; |
| 46 | for (cnt = 0; cnt < len; cnt++) { |
| 47 | int ch = name[cnt]; |
| 48 | if (!ch || ch == '\n') |
| 49 | return -1; |
| 50 | fputc(ch, out); |
| 51 | } |
| 52 | return len; |
| 53 | } |
| 54 | |
| 55 | static int show_path_truncated(FILE *out, const struct name_path *path) |
| 56 | { |
| 57 | int emitted, ours; |
| 58 | |
| 59 | if (!path) |
| 60 | return 0; |
| 61 | emitted = show_path_truncated(out, path->up); |
| 62 | if (emitted < 0) |
| 63 | return emitted; |
| 64 | if (emitted) |
| 65 | fputc('/', out); |
| 66 | ours = show_path_component_truncated(out, path->elem, path->elem_len); |
| 67 | if (ours < 0) |
| 68 | return ours; |
| 69 | return ours || emitted; |
| 70 | } |
| 71 | |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 72 | void show_object_with_name(FILE *out, struct object *obj, const struct name_path *path, const char *component) |
| 73 | { |
Junio C Hamano | beba25a | 2011-08-17 14:30:35 -0700 | [diff] [blame] | 74 | struct name_path leaf; |
| 75 | leaf.up = (struct name_path *)path; |
| 76 | leaf.elem = component; |
| 77 | leaf.elem_len = strlen(component); |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 78 | |
Junio C Hamano | beba25a | 2011-08-17 14:30:35 -0700 | [diff] [blame] | 79 | fprintf(out, "%s ", sha1_to_hex(obj->sha1)); |
| 80 | show_path_truncated(out, &leaf); |
| 81 | fputc('\n', out); |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 84 | void add_object(struct object *obj, |
| 85 | struct object_array *p, |
| 86 | struct name_path *path, |
| 87 | const char *name) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 88 | { |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 89 | add_object_array(obj, path_name(path, name), p); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void mark_blob_uninteresting(struct blob *blob) |
| 93 | { |
Martin Koegler | c1ee901 | 2008-02-18 21:47:54 +0100 | [diff] [blame] | 94 | if (!blob) |
| 95 | return; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 96 | if (blob->object.flags & UNINTERESTING) |
| 97 | return; |
| 98 | blob->object.flags |= UNINTERESTING; |
| 99 | } |
| 100 | |
| 101 | void mark_tree_uninteresting(struct tree *tree) |
| 102 | { |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 103 | struct tree_desc desc; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 104 | struct name_entry entry; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 105 | struct object *obj = &tree->object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 106 | |
Martin Koegler | c1ee901 | 2008-02-18 21:47:54 +0100 | [diff] [blame] | 107 | if (!tree) |
| 108 | return; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 109 | if (obj->flags & UNINTERESTING) |
| 110 | return; |
| 111 | obj->flags |= UNINTERESTING; |
| 112 | if (!has_sha1_file(obj->sha1)) |
| 113 | return; |
| 114 | if (parse_tree(tree) < 0) |
| 115 | die("bad tree %s", sha1_to_hex(obj->sha1)); |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 116 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 117 | init_tree_desc(&desc, tree->buffer, tree->size); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 118 | while (tree_entry(&desc, &entry)) { |
Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 +0000 | [diff] [blame] | 119 | switch (object_type(entry.mode)) { |
| 120 | case OBJ_TREE: |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 121 | mark_tree_uninteresting(lookup_tree(entry.sha1)); |
Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 +0000 | [diff] [blame] | 122 | break; |
| 123 | case OBJ_BLOB: |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 124 | mark_blob_uninteresting(lookup_blob(entry.sha1)); |
Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 +0000 | [diff] [blame] | 125 | break; |
| 126 | default: |
| 127 | /* Subproject commit - not in this repository */ |
| 128 | break; |
| 129 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 130 | } |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * We don't care about the tree any more |
| 134 | * after it has been marked uninteresting. |
| 135 | */ |
| 136 | free(tree->buffer); |
| 137 | tree->buffer = NULL; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void mark_parents_uninteresting(struct commit *commit) |
| 141 | { |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 142 | struct commit_list *parents = NULL, *l; |
| 143 | |
| 144 | for (l = commit->parents; l; l = l->next) |
| 145 | commit_list_insert(l->item, &parents); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 146 | |
| 147 | while (parents) { |
| 148 | struct commit *commit = parents->item; |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 149 | l = parents; |
| 150 | parents = parents->next; |
| 151 | free(l); |
| 152 | |
| 153 | while (commit) { |
| 154 | /* |
| 155 | * A missing commit is ok iff its parent is marked |
| 156 | * uninteresting. |
| 157 | * |
| 158 | * We just mark such a thing parsed, so that when |
| 159 | * it is popped next time around, we won't be trying |
| 160 | * to parse it and get an error. |
| 161 | */ |
| 162 | if (!has_sha1_file(commit->object.sha1)) |
| 163 | commit->object.parsed = 1; |
| 164 | |
| 165 | if (commit->object.flags & UNINTERESTING) |
| 166 | break; |
| 167 | |
Matthias Urlichs | d2c4af7 | 2006-03-09 05:04:36 +0100 | [diff] [blame] | 168 | commit->object.flags |= UNINTERESTING; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 169 | |
Matthias Urlichs | d2c4af7 | 2006-03-09 05:04:36 +0100 | [diff] [blame] | 170 | /* |
| 171 | * Normally we haven't parsed the parent |
| 172 | * yet, so we won't have a parent of a parent |
| 173 | * here. However, it may turn out that we've |
| 174 | * reached this commit some other way (where it |
| 175 | * wasn't uninteresting), in which case we need |
| 176 | * to mark its parents recursively too.. |
| 177 | */ |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 178 | if (!commit->parents) |
| 179 | break; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 180 | |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 181 | for (l = commit->parents->next; l; l = l->next) |
| 182 | commit_list_insert(l->item, &parents); |
| 183 | commit = commit->parents->item; |
| 184 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 188 | static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode) |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 189 | { |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 190 | if (!obj) |
| 191 | return; |
Linus Torvalds | aa27e46 | 2007-02-27 16:22:52 -0800 | [diff] [blame] | 192 | if (revs->no_walk && (obj->flags & UNINTERESTING)) |
Linus Torvalds | f222abd | 2009-07-13 14:41:12 -0700 | [diff] [blame] | 193 | revs->no_walk = 0; |
Junio C Hamano | 105e473 | 2010-01-26 13:48:28 -0800 | [diff] [blame] | 194 | if (revs->reflog_info && obj->type == OBJ_COMMIT) { |
| 195 | struct strbuf buf = STRBUF_INIT; |
| 196 | int len = interpret_branch_name(name, &buf); |
| 197 | int st; |
| 198 | |
| 199 | if (0 < len && name[len] && buf.len) |
| 200 | strbuf_addstr(&buf, name + len); |
| 201 | st = add_reflog_for_walk(revs->reflog_info, |
| 202 | (struct commit *)obj, |
| 203 | buf.buf[0] ? buf.buf: name); |
| 204 | strbuf_release(&buf); |
| 205 | if (st) |
| 206 | return; |
| 207 | } |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 208 | add_object_array_with_mode(obj, name, &revs->pending, mode); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 211 | void add_pending_object(struct rev_info *revs, struct object *obj, const char *name) |
| 212 | { |
| 213 | add_pending_object_with_mode(revs, obj, name, S_IFINVALID); |
| 214 | } |
| 215 | |
Junio C Hamano | 3384a2d | 2007-12-11 10:09:04 -0800 | [diff] [blame] | 216 | void add_head_to_pending(struct rev_info *revs) |
| 217 | { |
| 218 | unsigned char sha1[20]; |
| 219 | struct object *obj; |
| 220 | if (get_sha1("HEAD", sha1)) |
| 221 | return; |
| 222 | obj = parse_object(sha1); |
| 223 | if (!obj) |
| 224 | return; |
| 225 | add_pending_object(revs, obj, "HEAD"); |
| 226 | } |
| 227 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 228 | static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 229 | { |
| 230 | struct object *object; |
| 231 | |
| 232 | object = parse_object(sha1); |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 233 | if (!object) { |
| 234 | if (revs->ignore_missing) |
| 235 | return object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 236 | die("bad object %s", name); |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 237 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 238 | object->flags |= flags; |
| 239 | return object; |
| 240 | } |
| 241 | |
René Scharfe | 26c3177 | 2011-10-01 17:43:52 +0200 | [diff] [blame] | 242 | void add_pending_sha1(struct rev_info *revs, const char *name, |
| 243 | const unsigned char *sha1, unsigned int flags) |
| 244 | { |
| 245 | struct object *object = get_reference(revs, name, sha1, flags); |
| 246 | add_pending_object(revs, object, name); |
| 247 | } |
| 248 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 249 | static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name) |
| 250 | { |
| 251 | unsigned long flags = object->flags; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Tag object? Look what it points to.. |
| 255 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 256 | while (object->type == OBJ_TAG) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 257 | struct tag *tag = (struct tag *) object; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 258 | if (revs->tag_objects && !(flags & UNINTERESTING)) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 259 | add_pending_object(revs, object, tag->tag); |
Martin Koegler | 9684afd | 2008-02-18 21:48:01 +0100 | [diff] [blame] | 260 | if (!tag->tagged) |
| 261 | die("bad tag"); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 262 | object = parse_object(tag->tagged->sha1); |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 263 | if (!object) { |
| 264 | if (flags & UNINTERESTING) |
| 265 | return NULL; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 266 | die("bad object %s", sha1_to_hex(tag->tagged->sha1)); |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 267 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Commit object? Just return it, we'll do all the complex |
| 272 | * reachability crud. |
| 273 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 274 | if (object->type == OBJ_COMMIT) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 275 | struct commit *commit = (struct commit *)object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 276 | if (parse_commit(commit) < 0) |
| 277 | die("unable to parse commit %s", name); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 278 | if (flags & UNINTERESTING) { |
Linus Torvalds | 4262c1b | 2006-04-18 20:31:41 -0700 | [diff] [blame] | 279 | commit->object.flags |= UNINTERESTING; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 280 | mark_parents_uninteresting(commit); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 281 | revs->limited = 1; |
| 282 | } |
Linus Torvalds | 0f3a290 | 2008-10-27 12:51:59 -0700 | [diff] [blame] | 283 | if (revs->show_source && !commit->util) |
| 284 | commit->util = (void *) name; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 285 | return commit; |
| 286 | } |
| 287 | |
| 288 | /* |
Mike Ralphson | 3ea3c21 | 2009-04-17 19:13:30 +0100 | [diff] [blame] | 289 | * Tree object? Either mark it uninteresting, or add it |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 290 | * to the list of objects to look at later.. |
| 291 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 292 | if (object->type == OBJ_TREE) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 293 | struct tree *tree = (struct tree *)object; |
| 294 | if (!revs->tree_objects) |
| 295 | return NULL; |
| 296 | if (flags & UNINTERESTING) { |
| 297 | mark_tree_uninteresting(tree); |
| 298 | return NULL; |
| 299 | } |
| 300 | add_pending_object(revs, object, ""); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Blob object? You know the drill by now.. |
| 306 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 307 | if (object->type == OBJ_BLOB) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 308 | struct blob *blob = (struct blob *)object; |
| 309 | if (!revs->blob_objects) |
| 310 | return NULL; |
| 311 | if (flags & UNINTERESTING) { |
| 312 | mark_blob_uninteresting(blob); |
| 313 | return NULL; |
| 314 | } |
| 315 | add_pending_object(revs, object, ""); |
| 316 | return NULL; |
| 317 | } |
| 318 | die("%s is unknown object", name); |
| 319 | } |
| 320 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 321 | static int everybody_uninteresting(struct commit_list *orig) |
| 322 | { |
| 323 | struct commit_list *list = orig; |
| 324 | while (list) { |
| 325 | struct commit *commit = list->item; |
| 326 | list = list->next; |
| 327 | if (commit->object.flags & UNINTERESTING) |
| 328 | continue; |
| 329 | return 0; |
| 330 | } |
| 331 | return 1; |
| 332 | } |
| 333 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 334 | /* |
| 335 | * The goal is to get REV_TREE_NEW as the result only if the |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 336 | * diff consists of all '+' (and no other changes), REV_TREE_OLD |
| 337 | * if the whole diff is removal of old data, and otherwise |
| 338 | * REV_TREE_DIFFERENT (of course if the trees are the same we |
| 339 | * want REV_TREE_SAME). |
| 340 | * That means that once we get to REV_TREE_DIFFERENT, we do not |
| 341 | * have to look any further. |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 342 | */ |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 343 | static int tree_difference = REV_TREE_SAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 344 | |
| 345 | static void file_add_remove(struct diff_options *options, |
| 346 | int addremove, unsigned mode, |
| 347 | const unsigned char *sha1, |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 348 | int sha1_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 349 | const char *fullpath, unsigned dirty_submodule) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 350 | { |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 351 | int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 352 | |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 353 | tree_difference |= diff; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 354 | if (tree_difference == REV_TREE_DIFFERENT) |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 355 | DIFF_OPT_SET(options, HAS_CHANGES); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void file_change(struct diff_options *options, |
| 359 | unsigned old_mode, unsigned new_mode, |
| 360 | const unsigned char *old_sha1, |
| 361 | const unsigned char *new_sha1, |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 362 | int old_sha1_valid, int new_sha1_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 363 | const char *fullpath, |
| 364 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 365 | { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 366 | tree_difference = REV_TREE_DIFFERENT; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 367 | DIFF_OPT_SET(options, HAS_CHANGES); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 370 | static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 371 | { |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 372 | struct tree *t1 = parent->tree; |
| 373 | struct tree *t2 = commit->tree; |
| 374 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 375 | if (!t1) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 376 | return REV_TREE_NEW; |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 377 | if (!t2) |
| 378 | return REV_TREE_OLD; |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 379 | |
| 380 | if (revs->simplify_by_decoration) { |
| 381 | /* |
| 382 | * If we are simplifying by decoration, then the commit |
| 383 | * is worth showing if it has a tag pointing at it. |
| 384 | */ |
| 385 | if (lookup_decoration(&name_decoration, &commit->object)) |
| 386 | return REV_TREE_DIFFERENT; |
| 387 | /* |
| 388 | * A commit that is not pointed by a tag is uninteresting |
| 389 | * if we are not limited by path. This means that you will |
| 390 | * see the usual "commits that touch the paths" plus any |
| 391 | * tagged commit by specifying both --simplify-by-decoration |
| 392 | * and pathspec. |
| 393 | */ |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 394 | if (!revs->prune_data.nr) |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 395 | return REV_TREE_SAME; |
| 396 | } |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 397 | |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 398 | tree_difference = REV_TREE_SAME; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 399 | DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES); |
Junio C Hamano | c4e05b1 | 2006-04-10 18:14:54 -0700 | [diff] [blame] | 400 | if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 401 | &revs->pruning) < 0) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 402 | return REV_TREE_DIFFERENT; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 403 | return tree_difference; |
| 404 | } |
| 405 | |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 406 | static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 407 | { |
| 408 | int retval; |
| 409 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 410 | unsigned long size; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 411 | struct tree_desc empty, real; |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 412 | struct tree *t1 = commit->tree; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 413 | |
| 414 | if (!t1) |
| 415 | return 0; |
| 416 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 417 | tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 418 | if (!tree) |
| 419 | return 0; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 420 | init_tree_desc(&real, tree, size); |
| 421 | init_tree_desc(&empty, "", 0); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 422 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 423 | tree_difference = REV_TREE_SAME; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 424 | DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 425 | retval = diff_tree(&empty, &real, "", &revs->pruning); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 426 | free(tree); |
| 427 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 428 | return retval >= 0 && (tree_difference == REV_TREE_SAME); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit) |
| 432 | { |
| 433 | struct commit_list **pp, *parent; |
Junio C Hamano | 36ed191 | 2012-01-19 11:58:45 -0800 | [diff] [blame] | 434 | int tree_changed = 0, tree_same = 0, nth_parent = 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 435 | |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 436 | /* |
| 437 | * If we don't do pruning, everything is interesting |
| 438 | */ |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 439 | if (!revs->prune) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 440 | return; |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 441 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 442 | if (!commit->tree) |
| 443 | return; |
| 444 | |
| 445 | if (!commit->parents) { |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 446 | if (rev_same_tree_as_empty(revs, commit)) |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 447 | commit->object.flags |= TREESAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 451 | /* |
| 452 | * Normal non-merge commit? If we don't want to make the |
| 453 | * history dense, we consider it always to be a change.. |
| 454 | */ |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 455 | if (!revs->dense && !commit->parents->next) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 456 | return; |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 457 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 458 | pp = &commit->parents; |
| 459 | while ((parent = *pp) != NULL) { |
| 460 | struct commit *p = parent->item; |
| 461 | |
Junio C Hamano | 36ed191 | 2012-01-19 11:58:45 -0800 | [diff] [blame] | 462 | /* |
| 463 | * Do not compare with later parents when we care only about |
| 464 | * the first parent chain, in order to avoid derailing the |
| 465 | * traversal to follow a side branch that brought everything |
| 466 | * in the path we are limited to by the pathspec. |
| 467 | */ |
| 468 | if (revs->first_parent_only && nth_parent++) |
| 469 | break; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 470 | if (parse_commit(p) < 0) |
| 471 | die("cannot simplify commit %s (because of %s)", |
| 472 | sha1_to_hex(commit->object.sha1), |
| 473 | sha1_to_hex(p->object.sha1)); |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 474 | switch (rev_compare_tree(revs, p, commit)) { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 475 | case REV_TREE_SAME: |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 476 | tree_same = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 477 | if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) { |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 478 | /* Even if a merge with an uninteresting |
| 479 | * side branch brought the entire change |
| 480 | * we are interested in, we do not want |
| 481 | * to lose the other branches of this |
| 482 | * merge, so we just keep going. |
| 483 | */ |
| 484 | pp = &parent->next; |
| 485 | continue; |
| 486 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 487 | parent->next = NULL; |
| 488 | commit->parents = parent; |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 489 | commit->object.flags |= TREESAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 490 | return; |
| 491 | |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 492 | case REV_TREE_NEW: |
| 493 | if (revs->remove_empty_trees && |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 494 | rev_same_tree_as_empty(revs, p)) { |
Junio C Hamano | c348f31 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 495 | /* We are adding all the specified |
| 496 | * paths from this parent, so the |
| 497 | * history beyond this parent is not |
| 498 | * interesting. Remove its parents |
| 499 | * (they are grandparents for us). |
| 500 | * IOW, we pretend this parent is a |
| 501 | * "root" commit. |
Junio C Hamano | a41e109 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 502 | */ |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 503 | if (parse_commit(p) < 0) |
| 504 | die("cannot simplify commit %s (invalid %s)", |
| 505 | sha1_to_hex(commit->object.sha1), |
| 506 | sha1_to_hex(p->object.sha1)); |
Junio C Hamano | c348f31 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 507 | p->parents = NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 508 | } |
| 509 | /* fallthrough */ |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 510 | case REV_TREE_OLD: |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 511 | case REV_TREE_DIFFERENT: |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 512 | tree_changed = 1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 513 | pp = &parent->next; |
| 514 | continue; |
| 515 | } |
| 516 | die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1)); |
| 517 | } |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 518 | if (tree_changed && !tree_same) |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 519 | return; |
| 520 | commit->object.flags |= TREESAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 523 | static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head, |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 524 | struct commit_list *cached_base, struct commit_list **cache) |
| 525 | { |
| 526 | struct commit_list *new_entry; |
| 527 | |
| 528 | if (cached_base && p->date < cached_base->item->date) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 529 | new_entry = commit_list_insert_by_date(p, &cached_base->next); |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 530 | else |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 531 | new_entry = commit_list_insert_by_date(p, head); |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 532 | |
| 533 | if (cache && (!*cache || p->date < (*cache)->item->date)) |
| 534 | *cache = new_entry; |
| 535 | } |
| 536 | |
| 537 | static int add_parents_to_list(struct rev_info *revs, struct commit *commit, |
| 538 | struct commit_list **list, struct commit_list **cache_ptr) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 539 | { |
| 540 | struct commit_list *parent = commit->parents; |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 541 | unsigned left_flag; |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 542 | struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 543 | |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 544 | if (commit->object.flags & ADDED) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 545 | return 0; |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 546 | commit->object.flags |= ADDED; |
| 547 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 548 | /* |
| 549 | * If the commit is uninteresting, don't try to |
| 550 | * prune parents - we want the maximal uninteresting |
| 551 | * set. |
| 552 | * |
| 553 | * Normally we haven't parsed the parent |
| 554 | * yet, so we won't have a parent of a parent |
| 555 | * here. However, it may turn out that we've |
| 556 | * reached this commit some other way (where it |
| 557 | * wasn't uninteresting), in which case we need |
| 558 | * to mark its parents recursively too.. |
| 559 | */ |
| 560 | if (commit->object.flags & UNINTERESTING) { |
| 561 | while (parent) { |
| 562 | struct commit *p = parent->item; |
| 563 | parent = parent->next; |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 564 | if (p) |
| 565 | p->object.flags |= UNINTERESTING; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 566 | if (parse_commit(p) < 0) |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 567 | continue; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 568 | if (p->parents) |
| 569 | mark_parents_uninteresting(p); |
| 570 | if (p->object.flags & SEEN) |
| 571 | continue; |
| 572 | p->object.flags |= SEEN; |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 573 | commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 574 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 575 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Ok, the commit wasn't uninteresting. Try to |
| 580 | * simplify the commit history and find the parent |
| 581 | * that has no differences in the path set if one exists. |
| 582 | */ |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 583 | try_to_simplify_commit(revs, commit); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 584 | |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 585 | if (revs->no_walk) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 586 | return 0; |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 587 | |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 588 | left_flag = (commit->object.flags & SYMMETRIC_LEFT); |
Junio C Hamano | 0053e90 | 2007-03-13 01:57:22 -0700 | [diff] [blame] | 589 | |
Stephen R. van den Berg | d9c292e | 2008-04-27 19:32:46 +0200 | [diff] [blame] | 590 | for (parent = commit->parents; parent; parent = parent->next) { |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 591 | struct commit *p = parent->item; |
| 592 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 593 | if (parse_commit(p) < 0) |
| 594 | return -1; |
Linus Torvalds | 0f3a290 | 2008-10-27 12:51:59 -0700 | [diff] [blame] | 595 | if (revs->show_source && !p->util) |
| 596 | p->util = commit->util; |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 597 | p->object.flags |= left_flag; |
Lars Hjemli | ad1012e | 2008-05-12 17:12:36 +0200 | [diff] [blame] | 598 | if (!(p->object.flags & SEEN)) { |
| 599 | p->object.flags |= SEEN; |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 600 | commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr); |
Lars Hjemli | ad1012e | 2008-05-12 17:12:36 +0200 | [diff] [blame] | 601 | } |
Junio C Hamano | 60d30b0 | 2008-07-31 22:17:13 -0700 | [diff] [blame] | 602 | if (revs->first_parent_only) |
Stephen R. van den Berg | d9c292e | 2008-04-27 19:32:46 +0200 | [diff] [blame] | 603 | break; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 604 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 605 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 608 | static void cherry_pick_list(struct commit_list *list, struct rev_info *revs) |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 609 | { |
| 610 | struct commit_list *p; |
| 611 | int left_count = 0, right_count = 0; |
| 612 | int left_first; |
| 613 | struct patch_ids ids; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 614 | unsigned cherry_flag; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 615 | |
| 616 | /* First count the commits on the left and on the right */ |
| 617 | for (p = list; p; p = p->next) { |
| 618 | struct commit *commit = p->item; |
| 619 | unsigned flags = commit->object.flags; |
| 620 | if (flags & BOUNDARY) |
| 621 | ; |
| 622 | else if (flags & SYMMETRIC_LEFT) |
| 623 | left_count++; |
| 624 | else |
| 625 | right_count++; |
| 626 | } |
| 627 | |
Thomas Rast | 36c0797 | 2010-02-20 12:42:04 +0100 | [diff] [blame] | 628 | if (!left_count || !right_count) |
| 629 | return; |
| 630 | |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 631 | left_first = left_count < right_count; |
| 632 | init_patch_ids(&ids); |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 633 | ids.diffopts.pathspec = revs->diffopt.pathspec; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 634 | |
| 635 | /* Compute patch-ids for one side */ |
| 636 | for (p = list; p; p = p->next) { |
| 637 | struct commit *commit = p->item; |
| 638 | unsigned flags = commit->object.flags; |
| 639 | |
| 640 | if (flags & BOUNDARY) |
| 641 | continue; |
| 642 | /* |
| 643 | * If we have fewer left, left_first is set and we omit |
| 644 | * commits on the right branch in this loop. If we have |
| 645 | * fewer right, we skip the left ones. |
| 646 | */ |
| 647 | if (left_first != !!(flags & SYMMETRIC_LEFT)) |
| 648 | continue; |
| 649 | commit->util = add_commit_patch_id(commit, &ids); |
| 650 | } |
| 651 | |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 652 | /* either cherry_mark or cherry_pick are true */ |
| 653 | cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN; |
| 654 | |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 655 | /* Check the other side */ |
| 656 | for (p = list; p; p = p->next) { |
| 657 | struct commit *commit = p->item; |
| 658 | struct patch_id *id; |
| 659 | unsigned flags = commit->object.flags; |
| 660 | |
| 661 | if (flags & BOUNDARY) |
| 662 | continue; |
| 663 | /* |
| 664 | * If we have fewer left, left_first is set and we omit |
| 665 | * commits on the left branch in this loop. |
| 666 | */ |
| 667 | if (left_first == !!(flags & SYMMETRIC_LEFT)) |
| 668 | continue; |
| 669 | |
| 670 | /* |
| 671 | * Have we seen the same patch id? |
| 672 | */ |
| 673 | id = has_commit_patch_id(commit, &ids); |
| 674 | if (!id) |
| 675 | continue; |
| 676 | id->seen = 1; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 677 | commit->object.flags |= cherry_flag; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* Now check the original side for seen ones */ |
| 681 | for (p = list; p; p = p->next) { |
| 682 | struct commit *commit = p->item; |
| 683 | struct patch_id *ent; |
| 684 | |
| 685 | ent = commit->util; |
| 686 | if (!ent) |
| 687 | continue; |
| 688 | if (ent->seen) |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 689 | commit->object.flags |= cherry_flag; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 690 | commit->util = NULL; |
| 691 | } |
| 692 | |
| 693 | free_patch_ids(&ids); |
| 694 | } |
| 695 | |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 696 | /* How many extra uninteresting commits we want to see.. */ |
| 697 | #define SLOP 5 |
| 698 | |
| 699 | static int still_interesting(struct commit_list *src, unsigned long date, int slop) |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 700 | { |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 701 | /* |
| 702 | * No source list at all? We're definitely done.. |
| 703 | */ |
| 704 | if (!src) |
| 705 | return 0; |
| 706 | |
| 707 | /* |
| 708 | * Does the destination list contain entries with a date |
| 709 | * before the source list? Definitely _not_ done. |
| 710 | */ |
| 711 | if (date < src->item->date) |
| 712 | return SLOP; |
| 713 | |
| 714 | /* |
| 715 | * Does the source list still have interesting commits in |
| 716 | * it? Definitely not done.. |
| 717 | */ |
| 718 | if (!everybody_uninteresting(src)) |
| 719 | return SLOP; |
| 720 | |
| 721 | /* Ok, we're closing in.. */ |
| 722 | return slop-1; |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 723 | } |
| 724 | |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 725 | /* |
| 726 | * "rev-list --ancestry-path A..B" computes commits that are ancestors |
| 727 | * of B but not ancestors of A but further limits the result to those |
| 728 | * that are descendants of A. This takes the list of bottom commits and |
| 729 | * the result of "A..B" without --ancestry-path, and limits the latter |
| 730 | * further to the ones that can reach one of the commits in "bottom". |
| 731 | */ |
| 732 | static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list) |
| 733 | { |
| 734 | struct commit_list *p; |
| 735 | struct commit_list *rlist = NULL; |
| 736 | int made_progress; |
| 737 | |
| 738 | /* |
| 739 | * Reverse the list so that it will be likely that we would |
| 740 | * process parents before children. |
| 741 | */ |
| 742 | for (p = list; p; p = p->next) |
| 743 | commit_list_insert(p->item, &rlist); |
| 744 | |
| 745 | for (p = bottom; p; p = p->next) |
| 746 | p->item->object.flags |= TMP_MARK; |
| 747 | |
| 748 | /* |
| 749 | * Mark the ones that can reach bottom commits in "list", |
| 750 | * in a bottom-up fashion. |
| 751 | */ |
| 752 | do { |
| 753 | made_progress = 0; |
| 754 | for (p = rlist; p; p = p->next) { |
| 755 | struct commit *c = p->item; |
| 756 | struct commit_list *parents; |
| 757 | if (c->object.flags & (TMP_MARK | UNINTERESTING)) |
| 758 | continue; |
| 759 | for (parents = c->parents; |
| 760 | parents; |
| 761 | parents = parents->next) { |
| 762 | if (!(parents->item->object.flags & TMP_MARK)) |
| 763 | continue; |
| 764 | c->object.flags |= TMP_MARK; |
| 765 | made_progress = 1; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | } while (made_progress); |
| 770 | |
| 771 | /* |
| 772 | * NEEDSWORK: decide if we want to remove parents that are |
| 773 | * not marked with TMP_MARK from commit->parents for commits |
| 774 | * in the resulting list. We may not want to do that, though. |
| 775 | */ |
| 776 | |
| 777 | /* |
| 778 | * The ones that are not marked with TMP_MARK are uninteresting |
| 779 | */ |
| 780 | for (p = list; p; p = p->next) { |
| 781 | struct commit *c = p->item; |
| 782 | if (c->object.flags & TMP_MARK) |
| 783 | continue; |
| 784 | c->object.flags |= UNINTERESTING; |
| 785 | } |
| 786 | |
| 787 | /* We are done with the TMP_MARK */ |
| 788 | for (p = list; p; p = p->next) |
| 789 | p->item->object.flags &= ~TMP_MARK; |
| 790 | for (p = bottom; p; p = p->next) |
| 791 | p->item->object.flags &= ~TMP_MARK; |
| 792 | free_commit_list(rlist); |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Before walking the history, keep the set of "negative" refs the |
| 797 | * caller has asked to exclude. |
| 798 | * |
| 799 | * This is used to compute "rev-list --ancestry-path A..B", as we need |
| 800 | * to filter the result of "A..B" further to the ones that can actually |
| 801 | * reach A. |
| 802 | */ |
Junio C Hamano | c3502fa | 2011-08-25 17:51:50 -0700 | [diff] [blame] | 803 | static struct commit_list *collect_bottom_commits(struct rev_info *revs) |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 804 | { |
Junio C Hamano | c3502fa | 2011-08-25 17:51:50 -0700 | [diff] [blame] | 805 | struct commit_list *bottom = NULL; |
| 806 | int i; |
| 807 | for (i = 0; i < revs->cmdline.nr; i++) { |
| 808 | struct rev_cmdline_entry *elem = &revs->cmdline.rev[i]; |
| 809 | if ((elem->flags & UNINTERESTING) && |
| 810 | elem->item->type == OBJ_COMMIT) |
| 811 | commit_list_insert((struct commit *)elem->item, &bottom); |
| 812 | } |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 813 | return bottom; |
| 814 | } |
| 815 | |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 816 | /* Assumes either left_only or right_only is set */ |
| 817 | static void limit_left_right(struct commit_list *list, struct rev_info *revs) |
| 818 | { |
| 819 | struct commit_list *p; |
| 820 | |
| 821 | for (p = list; p; p = p->next) { |
| 822 | struct commit *commit = p->item; |
| 823 | |
| 824 | if (revs->right_only) { |
| 825 | if (commit->object.flags & SYMMETRIC_LEFT) |
| 826 | commit->object.flags |= SHOWN; |
| 827 | } else /* revs->left_only is set */ |
| 828 | if (!(commit->object.flags & SYMMETRIC_LEFT)) |
| 829 | commit->object.flags |= SHOWN; |
| 830 | } |
| 831 | } |
| 832 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 833 | static int limit_list(struct rev_info *revs) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 834 | { |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 835 | int slop = SLOP; |
| 836 | unsigned long date = ~0ul; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 837 | struct commit_list *list = revs->commits; |
| 838 | struct commit_list *newlist = NULL; |
| 839 | struct commit_list **p = &newlist; |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 840 | struct commit_list *bottom = NULL; |
| 841 | |
| 842 | if (revs->ancestry_path) { |
Junio C Hamano | c3502fa | 2011-08-25 17:51:50 -0700 | [diff] [blame] | 843 | bottom = collect_bottom_commits(revs); |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 844 | if (!bottom) |
Johan Herland | 97b03c3 | 2010-06-04 01:17:36 +0200 | [diff] [blame] | 845 | die("--ancestry-path given but there are no bottom commits"); |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 846 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 847 | |
| 848 | while (list) { |
| 849 | struct commit_list *entry = list; |
| 850 | struct commit *commit = list->item; |
| 851 | struct object *obj = &commit->object; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 852 | show_early_output_fn_t show; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 853 | |
| 854 | list = list->next; |
| 855 | free(entry); |
| 856 | |
| 857 | if (revs->max_age != -1 && (commit->date < revs->max_age)) |
| 858 | obj->flags |= UNINTERESTING; |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 859 | if (add_parents_to_list(revs, commit, &list, NULL) < 0) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 860 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 861 | if (obj->flags & UNINTERESTING) { |
| 862 | mark_parents_uninteresting(commit); |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 863 | if (revs->show_all) |
| 864 | p = &commit_list_insert(commit, p)->next; |
| 865 | slop = still_interesting(list, date, slop); |
| 866 | if (slop) |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 867 | continue; |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 868 | /* If showing all, add the whole pending list to the end */ |
| 869 | if (revs->show_all) |
| 870 | *p = list; |
| 871 | break; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 872 | } |
| 873 | if (revs->min_age != -1 && (commit->date > revs->min_age)) |
| 874 | continue; |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 875 | date = commit->date; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 876 | p = &commit_list_insert(commit, p)->next; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 877 | |
| 878 | show = show_early_output; |
| 879 | if (!show) |
| 880 | continue; |
| 881 | |
| 882 | show(revs, newlist); |
| 883 | show_early_output = NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 884 | } |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 885 | if (revs->cherry_pick || revs->cherry_mark) |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 886 | cherry_pick_list(newlist, revs); |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 887 | |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 888 | if (revs->left_only || revs->right_only) |
| 889 | limit_left_right(newlist, revs); |
| 890 | |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 891 | if (bottom) { |
| 892 | limit_to_ancestry(bottom, newlist); |
| 893 | free_commit_list(bottom); |
| 894 | } |
| 895 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 896 | revs->commits = newlist; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 897 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 900 | static void add_rev_cmdline(struct rev_info *revs, |
| 901 | struct object *item, |
| 902 | const char *name, |
| 903 | int whence, |
| 904 | unsigned flags) |
| 905 | { |
| 906 | struct rev_cmdline_info *info = &revs->cmdline; |
| 907 | int nr = info->nr; |
| 908 | |
| 909 | ALLOC_GROW(info->rev, nr + 1, info->alloc); |
| 910 | info->rev[nr].item = item; |
| 911 | info->rev[nr].name = name; |
| 912 | info->rev[nr].whence = whence; |
| 913 | info->rev[nr].flags = flags; |
| 914 | info->nr++; |
| 915 | } |
| 916 | |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 917 | struct all_refs_cb { |
| 918 | int all_flags; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 919 | int warned_bad_reflog; |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 920 | struct rev_info *all_revs; |
| 921 | const char *name_for_errormsg; |
| 922 | }; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 923 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 924 | static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 925 | { |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 926 | struct all_refs_cb *cb = cb_data; |
| 927 | struct object *object = get_reference(cb->all_revs, path, sha1, |
| 928 | cb->all_flags); |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 929 | add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags); |
René Scharfe | 26c3177 | 2011-10-01 17:43:52 +0200 | [diff] [blame] | 930 | add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
Ilari Liusvaara | d08bae7 | 2010-01-20 11:48:25 +0200 | [diff] [blame] | 934 | static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs, |
| 935 | unsigned flags) |
| 936 | { |
| 937 | cb->all_revs = revs; |
| 938 | cb->all_flags = flags; |
| 939 | } |
| 940 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 941 | static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags, |
| 942 | int (*for_each)(const char *, each_ref_fn, void *)) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 943 | { |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 944 | struct all_refs_cb cb; |
Ilari Liusvaara | d08bae7 | 2010-01-20 11:48:25 +0200 | [diff] [blame] | 945 | init_all_refs_cb(&cb, revs, flags); |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 946 | for_each(submodule, handle_one_ref, &cb); |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 949 | static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data) |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 950 | { |
| 951 | struct all_refs_cb *cb = cb_data; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 952 | if (!is_null_sha1(sha1)) { |
| 953 | struct object *o = parse_object(sha1); |
| 954 | if (o) { |
| 955 | o->flags |= cb->all_flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 956 | /* ??? CMDLINEFLAGS ??? */ |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 957 | add_pending_object(cb->all_revs, o, ""); |
| 958 | } |
| 959 | else if (!cb->warned_bad_reflog) { |
Theodore Ts'o | 46efd2d | 2007-03-30 19:07:05 -0400 | [diff] [blame] | 960 | warning("reflog of '%s' references pruned commits", |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 961 | cb->name_for_errormsg); |
| 962 | cb->warned_bad_reflog = 1; |
| 963 | } |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 964 | } |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 965 | } |
| 966 | |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 967 | static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
| 968 | const char *email, unsigned long timestamp, int tz, |
| 969 | const char *message, void *cb_data) |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 970 | { |
| 971 | handle_one_reflog_commit(osha1, cb_data); |
| 972 | handle_one_reflog_commit(nsha1, cb_data); |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
| 977 | { |
| 978 | struct all_refs_cb *cb = cb_data; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 979 | cb->warned_bad_reflog = 0; |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 980 | cb->name_for_errormsg = path; |
| 981 | for_each_reflog_ent(path, handle_one_reflog_ent, cb_data); |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static void handle_reflog(struct rev_info *revs, unsigned flags) |
| 986 | { |
| 987 | struct all_refs_cb cb; |
| 988 | cb.all_revs = revs; |
| 989 | cb.all_flags = flags; |
Junio C Hamano | 25b51e2 | 2007-02-12 23:06:54 -0800 | [diff] [blame] | 990 | for_each_reflog(handle_one_reflog, &cb); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 993 | static int add_parents_only(struct rev_info *revs, const char *arg_, int flags) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 994 | { |
| 995 | unsigned char sha1[20]; |
| 996 | struct object *it; |
| 997 | struct commit *commit; |
| 998 | struct commit_list *parents; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 999 | const char *arg = arg_; |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (*arg == '^') { |
| 1002 | flags ^= UNINTERESTING; |
| 1003 | arg++; |
| 1004 | } |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1005 | if (get_sha1_committish(arg, sha1)) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1006 | return 0; |
| 1007 | while (1) { |
| 1008 | it = get_reference(revs, arg, sha1, 0); |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 1009 | if (!it && revs->ignore_missing) |
| 1010 | return 0; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1011 | if (it->type != OBJ_TAG) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1012 | break; |
Martin Koegler | 9684afd | 2008-02-18 21:48:01 +0100 | [diff] [blame] | 1013 | if (!((struct tag*)it)->tagged) |
| 1014 | return 0; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1015 | hashcpy(sha1, ((struct tag*)it)->tagged->sha1); |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1016 | } |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1017 | if (it->type != OBJ_COMMIT) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1018 | return 0; |
| 1019 | commit = (struct commit *)it; |
| 1020 | for (parents = commit->parents; parents; parents = parents->next) { |
| 1021 | it = &parents->item->object; |
| 1022 | it->flags |= flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1023 | add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags); |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1024 | add_pending_object(revs, it, arg); |
| 1025 | } |
| 1026 | return 1; |
| 1027 | } |
| 1028 | |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 1029 | void init_revisions(struct rev_info *revs, const char *prefix) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1030 | { |
| 1031 | memset(revs, 0, sizeof(*revs)); |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 1032 | |
Junio C Hamano | 6b9c58f | 2006-04-15 23:46:36 -0700 | [diff] [blame] | 1033 | revs->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 1034 | revs->ignore_merges = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 1035 | revs->simplify_history = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 1036 | DIFF_OPT_SET(&revs->pruning, RECURSIVE); |
Junio C Hamano | 90b1994 | 2009-05-23 01:15:35 -0700 | [diff] [blame] | 1037 | DIFF_OPT_SET(&revs->pruning, QUICK); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1038 | revs->pruning.add_remove = file_add_remove; |
| 1039 | revs->pruning.change = file_change; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1040 | revs->lifo = 1; |
| 1041 | revs->dense = 1; |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 1042 | revs->prefix = prefix; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1043 | revs->max_age = -1; |
| 1044 | revs->min_age = -1; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1045 | revs->skip_count = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1046 | revs->max_count = -1; |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1047 | revs->max_parents = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1048 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1049 | revs->commit_format = CMIT_FMT_DEFAULT; |
| 1050 | |
Junio C Hamano | 918d4e1 | 2012-10-09 16:40:03 -0700 | [diff] [blame] | 1051 | init_grep_defaults(); |
| 1052 | grep_init(&revs->grep_filter, prefix); |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1053 | revs->grep_filter.status_only = 1; |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1054 | revs->grep_filter.regflags = REG_NEWLINE; |
| 1055 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1056 | diff_setup(&revs->diffopt); |
Junio C Hamano | c0cb4a0 | 2008-02-13 00:34:39 -0800 | [diff] [blame] | 1057 | if (prefix && !revs->diffopt.prefix) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 1058 | revs->diffopt.prefix = prefix; |
| 1059 | revs->diffopt.prefix_length = strlen(prefix); |
| 1060 | } |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1061 | |
| 1062 | revs->notes_opt.use_default_notes = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
Rene Scharfe | 0d2c9d6 | 2006-07-02 01:29:37 +0200 | [diff] [blame] | 1065 | static void add_pending_commit_list(struct rev_info *revs, |
| 1066 | struct commit_list *commit_list, |
| 1067 | unsigned int flags) |
| 1068 | { |
| 1069 | while (commit_list) { |
| 1070 | struct object *object = &commit_list->item->object; |
| 1071 | object->flags |= flags; |
| 1072 | add_pending_object(revs, object, sha1_to_hex(object->sha1)); |
| 1073 | commit_list = commit_list->next; |
| 1074 | } |
| 1075 | } |
| 1076 | |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1077 | static void prepare_show_merge(struct rev_info *revs) |
| 1078 | { |
| 1079 | struct commit_list *bases; |
| 1080 | struct commit *head, *other; |
| 1081 | unsigned char sha1[20]; |
| 1082 | const char **prune = NULL; |
| 1083 | int i, prune_num = 1; /* counting terminating NULL */ |
| 1084 | |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1085 | if (get_sha1("HEAD", sha1)) |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1086 | die("--merge without HEAD?"); |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1087 | head = lookup_commit_or_die(sha1, "HEAD"); |
| 1088 | if (get_sha1("MERGE_HEAD", sha1)) |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1089 | die("--merge without MERGE_HEAD?"); |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1090 | other = lookup_commit_or_die(sha1, "MERGE_HEAD"); |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1091 | add_pending_object(revs, &head->object, "HEAD"); |
| 1092 | add_pending_object(revs, &other->object, "MERGE_HEAD"); |
| 1093 | bases = get_merge_bases(head, other, 1); |
Junio C Hamano | e82447b | 2008-02-26 23:18:38 -0800 | [diff] [blame] | 1094 | add_pending_commit_list(revs, bases, UNINTERESTING); |
| 1095 | free_commit_list(bases); |
| 1096 | head->object.flags |= SYMMETRIC_LEFT; |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1097 | |
| 1098 | if (!active_nr) |
| 1099 | read_cache(); |
| 1100 | for (i = 0; i < active_nr; i++) { |
| 1101 | struct cache_entry *ce = active_cache[i]; |
| 1102 | if (!ce_stage(ce)) |
| 1103 | continue; |
Nguyễn Thái Ngọc Duy | eb9cb55 | 2010-12-17 19:43:07 +0700 | [diff] [blame] | 1104 | if (ce_path_match(ce, &revs->prune_data)) { |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1105 | prune_num++; |
| 1106 | prune = xrealloc(prune, sizeof(*prune) * prune_num); |
| 1107 | prune[prune_num-2] = ce->name; |
| 1108 | prune[prune_num-1] = NULL; |
| 1109 | } |
| 1110 | while ((i+1 < active_nr) && |
| 1111 | ce_same_name(ce, active_cache[i+1])) |
| 1112 | i++; |
| 1113 | } |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 1114 | free_pathspec(&revs->prune_data); |
| 1115 | init_pathspec(&revs->prune_data, prune); |
Junio C Hamano | e82447b | 2008-02-26 23:18:38 -0800 | [diff] [blame] | 1116 | revs->limited = 1; |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1119 | int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt) |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1120 | { |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 1121 | struct object_context oc; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1122 | char *dotdot; |
| 1123 | struct object *object; |
| 1124 | unsigned char sha1[20]; |
| 1125 | int local_flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1126 | const char *arg = arg_; |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1127 | int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME; |
Junio C Hamano | d5f6b1d | 2012-07-02 12:43:05 -0700 | [diff] [blame] | 1128 | unsigned get_sha1_flags = 0; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1129 | |
| 1130 | dotdot = strstr(arg, ".."); |
| 1131 | if (dotdot) { |
| 1132 | unsigned char from_sha1[20]; |
| 1133 | const char *next = dotdot + 2; |
| 1134 | const char *this = arg; |
| 1135 | int symmetric = *next == '.'; |
| 1136 | unsigned int flags_exclude = flags ^ UNINTERESTING; |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 1137 | static const char head_by_default[] = "HEAD"; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1138 | unsigned int a_flags; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1139 | |
| 1140 | *dotdot = 0; |
| 1141 | next += symmetric; |
| 1142 | |
| 1143 | if (!*next) |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 1144 | next = head_by_default; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1145 | if (dotdot == arg) |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 1146 | this = head_by_default; |
| 1147 | if (this == head_by_default && next == head_by_default && |
| 1148 | !symmetric) { |
| 1149 | /* |
| 1150 | * Just ".."? That is not a range but the |
| 1151 | * pathspec for the parent directory. |
| 1152 | */ |
| 1153 | if (!cant_be_filename) { |
| 1154 | *dotdot = '.'; |
| 1155 | return -1; |
| 1156 | } |
| 1157 | } |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1158 | if (!get_sha1_committish(this, from_sha1) && |
| 1159 | !get_sha1_committish(next, sha1)) { |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1160 | struct commit *a, *b; |
| 1161 | struct commit_list *exclude; |
| 1162 | |
| 1163 | a = lookup_commit_reference(from_sha1); |
| 1164 | b = lookup_commit_reference(sha1); |
| 1165 | if (!a || !b) { |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 1166 | if (revs->ignore_missing) |
| 1167 | return 0; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1168 | die(symmetric ? |
| 1169 | "Invalid symmetric difference expression %s...%s" : |
| 1170 | "Invalid revision range %s..%s", |
| 1171 | arg, next); |
| 1172 | } |
| 1173 | |
| 1174 | if (!cant_be_filename) { |
| 1175 | *dotdot = '.'; |
| 1176 | verify_non_filename(revs->prefix, arg); |
| 1177 | } |
| 1178 | |
| 1179 | if (symmetric) { |
| 1180 | exclude = get_merge_bases(a, b, 1); |
| 1181 | add_pending_commit_list(revs, exclude, |
| 1182 | flags_exclude); |
| 1183 | free_commit_list(exclude); |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1184 | a_flags = flags | SYMMETRIC_LEFT; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1185 | } else |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1186 | a_flags = flags_exclude; |
| 1187 | a->object.flags |= a_flags; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1188 | b->object.flags |= flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1189 | add_rev_cmdline(revs, &a->object, this, |
| 1190 | REV_CMD_LEFT, a_flags); |
| 1191 | add_rev_cmdline(revs, &b->object, next, |
| 1192 | REV_CMD_RIGHT, flags); |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1193 | add_pending_object(revs, &a->object, this); |
| 1194 | add_pending_object(revs, &b->object, next); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | *dotdot = '.'; |
| 1198 | } |
| 1199 | dotdot = strstr(arg, "^@"); |
| 1200 | if (dotdot && !dotdot[2]) { |
| 1201 | *dotdot = 0; |
| 1202 | if (add_parents_only(revs, arg, flags)) |
| 1203 | return 0; |
| 1204 | *dotdot = '^'; |
| 1205 | } |
Junio C Hamano | 62476c8 | 2006-10-31 14:22:34 -0800 | [diff] [blame] | 1206 | dotdot = strstr(arg, "^!"); |
| 1207 | if (dotdot && !dotdot[2]) { |
| 1208 | *dotdot = 0; |
| 1209 | if (!add_parents_only(revs, arg, flags ^ UNINTERESTING)) |
| 1210 | *dotdot = '^'; |
| 1211 | } |
| 1212 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1213 | local_flags = 0; |
| 1214 | if (*arg == '^') { |
| 1215 | local_flags = UNINTERESTING; |
| 1216 | arg++; |
| 1217 | } |
Junio C Hamano | d5f6b1d | 2012-07-02 12:43:05 -0700 | [diff] [blame] | 1218 | |
| 1219 | if (revarg_opt & REVARG_COMMITTISH) |
| 1220 | get_sha1_flags = GET_SHA1_COMMITTISH; |
| 1221 | |
| 1222 | if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc)) |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 1223 | return revs->ignore_missing ? 0 : -1; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1224 | if (!cant_be_filename) |
| 1225 | verify_non_filename(revs->prefix, arg); |
| 1226 | object = get_reference(revs, arg, sha1, flags ^ local_flags); |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1227 | add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags); |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 1228 | add_pending_object_with_mode(revs, object, arg, oc.mode); |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1229 | return 0; |
| 1230 | } |
| 1231 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1232 | struct cmdline_pathspec { |
| 1233 | int alloc; |
| 1234 | int nr; |
| 1235 | const char **path; |
| 1236 | }; |
| 1237 | |
| 1238 | static void append_prune_data(struct cmdline_pathspec *prune, const char **av) |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1239 | { |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1240 | while (*av) { |
| 1241 | ALLOC_GROW(prune->path, prune->nr+1, prune->alloc); |
| 1242 | prune->path[prune->nr++] = *(av++); |
| 1243 | } |
| 1244 | } |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1245 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1246 | static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, |
| 1247 | struct cmdline_pathspec *prune) |
| 1248 | { |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1249 | while (strbuf_getwholeline(sb, stdin, '\n') != EOF) { |
| 1250 | int len = sb->len; |
| 1251 | if (len && sb->buf[len - 1] == '\n') |
| 1252 | sb->buf[--len] = '\0'; |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1253 | ALLOC_GROW(prune->path, prune->nr+1, prune->alloc); |
| 1254 | prune->path[prune->nr++] = xstrdup(sb->buf); |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1255 | } |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1258 | static void read_revisions_from_stdin(struct rev_info *revs, |
| 1259 | struct cmdline_pathspec *prune) |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1260 | { |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1261 | struct strbuf sb; |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1262 | int seen_dashdash = 0; |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1263 | |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1264 | strbuf_init(&sb, 1000); |
| 1265 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { |
| 1266 | int len = sb.len; |
| 1267 | if (len && sb.buf[len - 1] == '\n') |
| 1268 | sb.buf[--len] = '\0'; |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1269 | if (!len) |
| 1270 | break; |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1271 | if (sb.buf[0] == '-') { |
| 1272 | if (len == 2 && sb.buf[1] == '-') { |
| 1273 | seen_dashdash = 1; |
| 1274 | break; |
| 1275 | } |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1276 | die("options not supported in --stdin mode"); |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1277 | } |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1278 | if (handle_revision_arg(sb.buf, revs, 0, REVARG_CANNOT_BE_FILENAME)) |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1279 | die("bad revision '%s'", sb.buf); |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1280 | } |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1281 | if (seen_dashdash) |
| 1282 | read_pathspec_from_stdin(revs, &sb, prune); |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1283 | strbuf_release(&sb); |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1286 | static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) |
| 1287 | { |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1288 | append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what); |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 1291 | static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern) |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1292 | { |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 1293 | append_header_grep_pattern(&revs->grep_filter, field, pattern); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | static void add_message_grep(struct rev_info *revs, const char *pattern) |
| 1297 | { |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1298 | add_grep(revs, pattern, GREP_PATTERN_BODY); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Pierre Habouzit | 6b61ec0 | 2008-07-09 23:38:34 +0200 | [diff] [blame] | 1301 | static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv, |
| 1302 | int *unkc, const char **unkv) |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1303 | { |
| 1304 | const char *arg = argv[0]; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1305 | const char *optarg; |
| 1306 | int argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1307 | |
| 1308 | /* pseudo revision arguments */ |
| 1309 | if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") || |
| 1310 | !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") || |
| 1311 | !strcmp(arg, "--reflog") || !strcmp(arg, "--not") || |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 1312 | !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") || |
Jonathan Nieder | 0fc63ec | 2011-04-21 05:48:24 -0500 | [diff] [blame] | 1313 | !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") || |
| 1314 | !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") || |
Martin von Zweigbergk | ca92e59 | 2012-08-28 23:15:54 -0700 | [diff] [blame] | 1315 | !prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk=")) |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1316 | { |
| 1317 | unkv[(*unkc)++] = arg; |
Pierre Habouzit | 0fe8c13 | 2008-07-31 12:22:23 +0200 | [diff] [blame] | 1318 | return 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1321 | if ((argcount = parse_long_opt("max-count", argv, &optarg))) { |
| 1322 | revs->max_count = atoi(optarg); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1323 | revs->no_walk = 0; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1324 | return argcount; |
| 1325 | } else if ((argcount = parse_long_opt("skip", argv, &optarg))) { |
| 1326 | revs->skip_count = atoi(optarg); |
| 1327 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1328 | } else if ((*arg == '-') && isdigit(arg[1])) { |
| 1329 | /* accept -<digit>, like traditional "head" */ |
| 1330 | revs->max_count = atoi(arg + 1); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1331 | revs->no_walk = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1332 | } else if (!strcmp(arg, "-n")) { |
| 1333 | if (argc <= 1) |
| 1334 | return error("-n requires an argument"); |
| 1335 | revs->max_count = atoi(argv[1]); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1336 | revs->no_walk = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1337 | return 2; |
| 1338 | } else if (!prefixcmp(arg, "-n")) { |
| 1339 | revs->max_count = atoi(arg + 2); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1340 | revs->no_walk = 0; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1341 | } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) { |
| 1342 | revs->max_age = atoi(optarg); |
| 1343 | return argcount; |
| 1344 | } else if ((argcount = parse_long_opt("since", argv, &optarg))) { |
| 1345 | revs->max_age = approxidate(optarg); |
| 1346 | return argcount; |
| 1347 | } else if ((argcount = parse_long_opt("after", argv, &optarg))) { |
| 1348 | revs->max_age = approxidate(optarg); |
| 1349 | return argcount; |
| 1350 | } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) { |
| 1351 | revs->min_age = atoi(optarg); |
| 1352 | return argcount; |
| 1353 | } else if ((argcount = parse_long_opt("before", argv, &optarg))) { |
| 1354 | revs->min_age = approxidate(optarg); |
| 1355 | return argcount; |
| 1356 | } else if ((argcount = parse_long_opt("until", argv, &optarg))) { |
| 1357 | revs->min_age = approxidate(optarg); |
| 1358 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1359 | } else if (!strcmp(arg, "--first-parent")) { |
| 1360 | revs->first_parent_only = 1; |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1361 | } else if (!strcmp(arg, "--ancestry-path")) { |
| 1362 | revs->ancestry_path = 1; |
Johan Herland | cb7529e | 2010-06-04 01:17:37 +0200 | [diff] [blame] | 1363 | revs->simplify_history = 0; |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1364 | revs->limited = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1365 | } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) { |
| 1366 | init_reflog_walk(&revs->reflog_info); |
| 1367 | } else if (!strcmp(arg, "--default")) { |
| 1368 | if (argc <= 1) |
| 1369 | return error("bad --default argument"); |
| 1370 | revs->def = argv[1]; |
| 1371 | return 2; |
| 1372 | } else if (!strcmp(arg, "--merge")) { |
| 1373 | revs->show_merge = 1; |
| 1374 | } else if (!strcmp(arg, "--topo-order")) { |
| 1375 | revs->lifo = 1; |
| 1376 | revs->topo_order = 1; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1377 | } else if (!strcmp(arg, "--simplify-merges")) { |
| 1378 | revs->simplify_merges = 1; |
Junio C Hamano | a52f007 | 2012-06-08 14:47:08 -0700 | [diff] [blame] | 1379 | revs->topo_order = 1; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1380 | revs->rewrite_parents = 1; |
| 1381 | revs->simplify_history = 0; |
| 1382 | revs->limited = 1; |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 1383 | } else if (!strcmp(arg, "--simplify-by-decoration")) { |
| 1384 | revs->simplify_merges = 1; |
Junio C Hamano | a52f007 | 2012-06-08 14:47:08 -0700 | [diff] [blame] | 1385 | revs->topo_order = 1; |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 1386 | revs->rewrite_parents = 1; |
| 1387 | revs->simplify_history = 0; |
| 1388 | revs->simplify_by_decoration = 1; |
| 1389 | revs->limited = 1; |
| 1390 | revs->prune = 1; |
Lars Hjemli | 33e7018 | 2009-08-15 16:23:12 +0200 | [diff] [blame] | 1391 | load_ref_decorations(DECORATE_SHORT_REFS); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1392 | } else if (!strcmp(arg, "--date-order")) { |
| 1393 | revs->lifo = 0; |
| 1394 | revs->topo_order = 1; |
| 1395 | } else if (!prefixcmp(arg, "--early-output")) { |
| 1396 | int count = 100; |
| 1397 | switch (arg[14]) { |
| 1398 | case '=': |
| 1399 | count = atoi(arg+15); |
| 1400 | /* Fallthrough */ |
| 1401 | case 0: |
| 1402 | revs->topo_order = 1; |
| 1403 | revs->early_output = count; |
| 1404 | } |
| 1405 | } else if (!strcmp(arg, "--parents")) { |
| 1406 | revs->rewrite_parents = 1; |
| 1407 | revs->print_parents = 1; |
| 1408 | } else if (!strcmp(arg, "--dense")) { |
| 1409 | revs->dense = 1; |
| 1410 | } else if (!strcmp(arg, "--sparse")) { |
| 1411 | revs->dense = 0; |
| 1412 | } else if (!strcmp(arg, "--show-all")) { |
| 1413 | revs->show_all = 1; |
| 1414 | } else if (!strcmp(arg, "--remove-empty")) { |
| 1415 | revs->remove_empty_trees = 1; |
Linus Torvalds | b8e8db2 | 2009-06-29 10:28:25 -0700 | [diff] [blame] | 1416 | } else if (!strcmp(arg, "--merges")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1417 | revs->min_parents = 2; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1418 | } else if (!strcmp(arg, "--no-merges")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1419 | revs->max_parents = 1; |
| 1420 | } else if (!prefixcmp(arg, "--min-parents=")) { |
| 1421 | revs->min_parents = atoi(arg+14); |
| 1422 | } else if (!prefixcmp(arg, "--no-min-parents")) { |
| 1423 | revs->min_parents = 0; |
| 1424 | } else if (!prefixcmp(arg, "--max-parents=")) { |
| 1425 | revs->max_parents = atoi(arg+14); |
| 1426 | } else if (!prefixcmp(arg, "--no-max-parents")) { |
| 1427 | revs->max_parents = -1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1428 | } else if (!strcmp(arg, "--boundary")) { |
| 1429 | revs->boundary = 1; |
| 1430 | } else if (!strcmp(arg, "--left-right")) { |
| 1431 | revs->left_right = 1; |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1432 | } else if (!strcmp(arg, "--left-only")) { |
Junio C Hamano | 24852d9 | 2011-02-21 16:58:37 -0800 | [diff] [blame] | 1433 | if (revs->right_only) |
Michael J Gruber | 94f605e | 2011-03-07 13:31:42 +0100 | [diff] [blame] | 1434 | die("--left-only is incompatible with --right-only" |
| 1435 | " or --cherry"); |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1436 | revs->left_only = 1; |
| 1437 | } else if (!strcmp(arg, "--right-only")) { |
Junio C Hamano | 24852d9 | 2011-02-21 16:58:37 -0800 | [diff] [blame] | 1438 | if (revs->left_only) |
| 1439 | die("--right-only is incompatible with --left-only"); |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1440 | revs->right_only = 1; |
Michael J Gruber | 94f605e | 2011-03-07 13:31:42 +0100 | [diff] [blame] | 1441 | } else if (!strcmp(arg, "--cherry")) { |
| 1442 | if (revs->left_only) |
| 1443 | die("--cherry is incompatible with --left-only"); |
| 1444 | revs->cherry_mark = 1; |
| 1445 | revs->right_only = 1; |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1446 | revs->max_parents = 1; |
Michael J Gruber | 94f605e | 2011-03-07 13:31:42 +0100 | [diff] [blame] | 1447 | revs->limited = 1; |
Thomas Rast | f69c501 | 2010-06-10 13:47:23 +0200 | [diff] [blame] | 1448 | } else if (!strcmp(arg, "--count")) { |
| 1449 | revs->count = 1; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 1450 | } else if (!strcmp(arg, "--cherry-mark")) { |
| 1451 | if (revs->cherry_pick) |
| 1452 | die("--cherry-mark is incompatible with --cherry-pick"); |
| 1453 | revs->cherry_mark = 1; |
| 1454 | revs->limited = 1; /* needs limit_list() */ |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1455 | } else if (!strcmp(arg, "--cherry-pick")) { |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 1456 | if (revs->cherry_mark) |
| 1457 | die("--cherry-pick is incompatible with --cherry-mark"); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1458 | revs->cherry_pick = 1; |
| 1459 | revs->limited = 1; |
| 1460 | } else if (!strcmp(arg, "--objects")) { |
| 1461 | revs->tag_objects = 1; |
| 1462 | revs->tree_objects = 1; |
| 1463 | revs->blob_objects = 1; |
| 1464 | } else if (!strcmp(arg, "--objects-edge")) { |
| 1465 | revs->tag_objects = 1; |
| 1466 | revs->tree_objects = 1; |
| 1467 | revs->blob_objects = 1; |
| 1468 | revs->edge_hint = 1; |
Junio C Hamano | 5a48d24 | 2011-09-01 15:43:34 -0700 | [diff] [blame] | 1469 | } else if (!strcmp(arg, "--verify-objects")) { |
| 1470 | revs->tag_objects = 1; |
| 1471 | revs->tree_objects = 1; |
| 1472 | revs->blob_objects = 1; |
| 1473 | revs->verify_objects = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1474 | } else if (!strcmp(arg, "--unpacked")) { |
| 1475 | revs->unpacked = 1; |
Junio C Hamano | 03a9683 | 2009-02-28 00:00:21 -0800 | [diff] [blame] | 1476 | } else if (!prefixcmp(arg, "--unpacked=")) { |
| 1477 | die("--unpacked=<packfile> no longer supported."); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1478 | } else if (!strcmp(arg, "-r")) { |
| 1479 | revs->diff = 1; |
| 1480 | DIFF_OPT_SET(&revs->diffopt, RECURSIVE); |
| 1481 | } else if (!strcmp(arg, "-t")) { |
| 1482 | revs->diff = 1; |
| 1483 | DIFF_OPT_SET(&revs->diffopt, RECURSIVE); |
| 1484 | DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE); |
| 1485 | } else if (!strcmp(arg, "-m")) { |
| 1486 | revs->ignore_merges = 0; |
| 1487 | } else if (!strcmp(arg, "-c")) { |
| 1488 | revs->diff = 1; |
| 1489 | revs->dense_combined_merges = 0; |
| 1490 | revs->combine_merges = 1; |
| 1491 | } else if (!strcmp(arg, "--cc")) { |
| 1492 | revs->diff = 1; |
| 1493 | revs->dense_combined_merges = 1; |
| 1494 | revs->combine_merges = 1; |
| 1495 | } else if (!strcmp(arg, "-v")) { |
| 1496 | revs->verbose_header = 1; |
| 1497 | } else if (!strcmp(arg, "--pretty")) { |
| 1498 | revs->verbose_header = 1; |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1499 | revs->pretty_given = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1500 | get_commit_format(arg+8, revs); |
Nanako Shiraishi | 3a4c1a5 | 2009-02-24 18:59:14 +0900 | [diff] [blame] | 1501 | } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) { |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1502 | /* |
| 1503 | * Detached form ("--pretty X" as opposed to "--pretty=X") |
| 1504 | * not allowed, since the argument is optional. |
| 1505 | */ |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1506 | revs->verbose_header = 1; |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1507 | revs->pretty_given = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1508 | get_commit_format(arg+9, revs); |
Jeff King | 7249e91 | 2011-03-29 16:57:47 -0400 | [diff] [blame] | 1509 | } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) { |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1510 | revs->show_notes = 1; |
| 1511 | revs->show_notes_given = 1; |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1512 | revs->notes_opt.use_default_notes = 1; |
Junio C Hamano | 0c37f1f | 2011-10-18 15:53:23 -0700 | [diff] [blame] | 1513 | } else if (!strcmp(arg, "--show-signature")) { |
| 1514 | revs->show_signature = 1; |
Jeff King | 7249e91 | 2011-03-29 16:57:47 -0400 | [diff] [blame] | 1515 | } else if (!prefixcmp(arg, "--show-notes=") || |
| 1516 | !prefixcmp(arg, "--notes=")) { |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 1517 | struct strbuf buf = STRBUF_INIT; |
| 1518 | revs->show_notes = 1; |
| 1519 | revs->show_notes_given = 1; |
Jeff King | 7249e91 | 2011-03-29 16:57:47 -0400 | [diff] [blame] | 1520 | if (!prefixcmp(arg, "--show-notes")) { |
| 1521 | if (revs->notes_opt.use_default_notes < 0) |
| 1522 | revs->notes_opt.use_default_notes = 1; |
| 1523 | strbuf_addstr(&buf, arg+13); |
| 1524 | } |
| 1525 | else |
| 1526 | strbuf_addstr(&buf, arg+8); |
Jeff King | c063f0a | 2011-03-29 16:56:04 -0400 | [diff] [blame] | 1527 | expand_notes_ref(&buf); |
Jeff King | 304cc11 | 2011-03-29 16:56:53 -0400 | [diff] [blame] | 1528 | string_list_append(&revs->notes_opt.extra_notes_refs, |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 1529 | strbuf_detach(&buf, NULL)); |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1530 | } else if (!strcmp(arg, "--no-notes")) { |
| 1531 | revs->show_notes = 0; |
| 1532 | revs->show_notes_given = 1; |
Jeff King | 92e0d42 | 2011-03-29 16:59:42 -0400 | [diff] [blame] | 1533 | revs->notes_opt.use_default_notes = -1; |
| 1534 | /* we have been strdup'ing ourselves, so trick |
| 1535 | * string_list into free()ing strings */ |
| 1536 | revs->notes_opt.extra_notes_refs.strdup_strings = 1; |
| 1537 | string_list_clear(&revs->notes_opt.extra_notes_refs, 0); |
| 1538 | revs->notes_opt.extra_notes_refs.strdup_strings = 0; |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 1539 | } else if (!strcmp(arg, "--standard-notes")) { |
| 1540 | revs->show_notes_given = 1; |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1541 | revs->notes_opt.use_default_notes = 1; |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 1542 | } else if (!strcmp(arg, "--no-standard-notes")) { |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1543 | revs->notes_opt.use_default_notes = 0; |
Nanako Shiraishi | de84acc | 2009-02-24 18:59:16 +0900 | [diff] [blame] | 1544 | } else if (!strcmp(arg, "--oneline")) { |
| 1545 | revs->verbose_header = 1; |
| 1546 | get_commit_format("oneline", revs); |
Junio C Hamano | 7dccadf | 2010-01-21 14:57:41 -0800 | [diff] [blame] | 1547 | revs->pretty_given = 1; |
Nanako Shiraishi | de84acc | 2009-02-24 18:59:16 +0900 | [diff] [blame] | 1548 | revs->abbrev_commit = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1549 | } else if (!strcmp(arg, "--graph")) { |
| 1550 | revs->topo_order = 1; |
| 1551 | revs->rewrite_parents = 1; |
| 1552 | revs->graph = graph_init(revs); |
| 1553 | } else if (!strcmp(arg, "--root")) { |
| 1554 | revs->show_root_diff = 1; |
| 1555 | } else if (!strcmp(arg, "--no-commit-id")) { |
| 1556 | revs->no_commit_id = 1; |
| 1557 | } else if (!strcmp(arg, "--always")) { |
| 1558 | revs->always_show_header = 1; |
| 1559 | } else if (!strcmp(arg, "--no-abbrev")) { |
| 1560 | revs->abbrev = 0; |
| 1561 | } else if (!strcmp(arg, "--abbrev")) { |
| 1562 | revs->abbrev = DEFAULT_ABBREV; |
| 1563 | } else if (!prefixcmp(arg, "--abbrev=")) { |
| 1564 | revs->abbrev = strtoul(arg + 9, NULL, 10); |
| 1565 | if (revs->abbrev < MINIMUM_ABBREV) |
| 1566 | revs->abbrev = MINIMUM_ABBREV; |
| 1567 | else if (revs->abbrev > 40) |
| 1568 | revs->abbrev = 40; |
| 1569 | } else if (!strcmp(arg, "--abbrev-commit")) { |
| 1570 | revs->abbrev_commit = 1; |
Jay Soffian | 0c47695 | 2011-05-18 13:56:04 -0400 | [diff] [blame] | 1571 | revs->abbrev_commit_given = 1; |
| 1572 | } else if (!strcmp(arg, "--no-abbrev-commit")) { |
| 1573 | revs->abbrev_commit = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1574 | } else if (!strcmp(arg, "--full-diff")) { |
| 1575 | revs->diff = 1; |
| 1576 | revs->full_diff = 1; |
| 1577 | } else if (!strcmp(arg, "--full-history")) { |
| 1578 | revs->simplify_history = 0; |
| 1579 | } else if (!strcmp(arg, "--relative-date")) { |
| 1580 | revs->date_mode = DATE_RELATIVE; |
Jeff King | f4ea32f | 2009-09-24 04:28:15 -0400 | [diff] [blame] | 1581 | revs->date_mode_explicit = 1; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1582 | } else if ((argcount = parse_long_opt("date", argv, &optarg))) { |
| 1583 | revs->date_mode = parse_date_format(optarg); |
Jeff King | f4ea32f | 2009-09-24 04:28:15 -0400 | [diff] [blame] | 1584 | revs->date_mode_explicit = 1; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1585 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1586 | } else if (!strcmp(arg, "--log-size")) { |
| 1587 | revs->show_log_size = 1; |
| 1588 | } |
| 1589 | /* |
| 1590 | * Grepping the commit log |
| 1591 | */ |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1592 | else if ((argcount = parse_long_opt("author", argv, &optarg))) { |
| 1593 | add_header_grep(revs, GREP_HEADER_AUTHOR, optarg); |
| 1594 | return argcount; |
| 1595 | } else if ((argcount = parse_long_opt("committer", argv, &optarg))) { |
| 1596 | add_header_grep(revs, GREP_HEADER_COMMITTER, optarg); |
| 1597 | return argcount; |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 1598 | } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) { |
| 1599 | add_header_grep(revs, GREP_HEADER_REFLOG, optarg); |
| 1600 | return argcount; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1601 | } else if ((argcount = parse_long_opt("grep", argv, &optarg))) { |
| 1602 | add_message_grep(revs, optarg); |
| 1603 | return argcount; |
Junio C Hamano | 17bf35a | 2012-09-13 14:21:44 -0700 | [diff] [blame] | 1604 | } else if (!strcmp(arg, "--grep-debug")) { |
| 1605 | revs->grep_filter.debug = 1; |
Junio C Hamano | 727b6fc | 2012-10-03 15:01:34 -0700 | [diff] [blame] | 1606 | } else if (!strcmp(arg, "--basic-regexp")) { |
| 1607 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1608 | } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) { |
Junio C Hamano | 34a4ae5 | 2012-10-03 14:50:51 -0700 | [diff] [blame] | 1609 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1610 | } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) { |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1611 | revs->grep_filter.regflags |= REG_ICASE; |
Junio C Hamano | accccde | 2012-02-21 01:02:46 -0800 | [diff] [blame] | 1612 | DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1613 | } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) { |
Junio C Hamano | 34a4ae5 | 2012-10-03 14:50:51 -0700 | [diff] [blame] | 1614 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter); |
Junio C Hamano | 727b6fc | 2012-10-03 15:01:34 -0700 | [diff] [blame] | 1615 | } else if (!strcmp(arg, "--perl-regexp")) { |
| 1616 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1617 | } else if (!strcmp(arg, "--all-match")) { |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1618 | revs->grep_filter.all_match = 1; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1619 | } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) { |
| 1620 | if (strcmp(optarg, "none")) |
| 1621 | git_log_output_encoding = xstrdup(optarg); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1622 | else |
| 1623 | git_log_output_encoding = ""; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1624 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1625 | } else if (!strcmp(arg, "--reverse")) { |
| 1626 | revs->reverse ^= 1; |
| 1627 | } else if (!strcmp(arg, "--children")) { |
| 1628 | revs->children.name = "children"; |
| 1629 | revs->limited = 1; |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 1630 | } else if (!strcmp(arg, "--ignore-missing")) { |
| 1631 | revs->ignore_missing = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1632 | } else { |
| 1633 | int opts = diff_opt_parse(&revs->diffopt, argv, argc); |
| 1634 | if (!opts) |
| 1635 | unkv[(*unkc)++] = arg; |
| 1636 | return opts; |
| 1637 | } |
| 1638 | |
| 1639 | return 1; |
| 1640 | } |
| 1641 | |
Pierre Habouzit | 6b61ec0 | 2008-07-09 23:38:34 +0200 | [diff] [blame] | 1642 | void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx, |
| 1643 | const struct option *options, |
| 1644 | const char * const usagestr[]) |
| 1645 | { |
| 1646 | int n = handle_revision_opt(revs, ctx->argc, ctx->argv, |
| 1647 | &ctx->cpidx, ctx->out); |
| 1648 | if (n <= 0) { |
| 1649 | error("unknown option `%s'", ctx->argv[0]); |
| 1650 | usage_with_options(usagestr, options); |
| 1651 | } |
| 1652 | ctx->argv += n; |
| 1653 | ctx->argc -= n; |
| 1654 | } |
| 1655 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1656 | static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data) |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 1657 | { |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1658 | return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data); |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1661 | static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data) |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 1662 | { |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1663 | return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data); |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 1666 | static int handle_revision_pseudo_opt(const char *submodule, |
| 1667 | struct rev_info *revs, |
| 1668 | int argc, const char **argv, int *flags) |
| 1669 | { |
| 1670 | const char *arg = argv[0]; |
| 1671 | const char *optarg; |
| 1672 | int argcount; |
| 1673 | |
Jonathan Nieder | 0fc63ec | 2011-04-21 05:48:24 -0500 | [diff] [blame] | 1674 | /* |
| 1675 | * NOTE! |
| 1676 | * |
| 1677 | * Commands like "git shortlog" will not accept the options below |
| 1678 | * unless parse_revision_opt queues them (as opposed to erroring |
| 1679 | * out). |
| 1680 | * |
| 1681 | * When implementing your new pseudo-option, remember to |
| 1682 | * register it in the list at the top of handle_revision_opt. |
| 1683 | */ |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 1684 | if (!strcmp(arg, "--all")) { |
| 1685 | handle_refs(submodule, revs, *flags, for_each_ref_submodule); |
| 1686 | handle_refs(submodule, revs, *flags, head_ref_submodule); |
| 1687 | } else if (!strcmp(arg, "--branches")) { |
| 1688 | handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule); |
| 1689 | } else if (!strcmp(arg, "--bisect")) { |
| 1690 | handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref); |
| 1691 | handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref); |
| 1692 | revs->bisect = 1; |
| 1693 | } else if (!strcmp(arg, "--tags")) { |
| 1694 | handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule); |
| 1695 | } else if (!strcmp(arg, "--remotes")) { |
| 1696 | handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule); |
| 1697 | } else if ((argcount = parse_long_opt("glob", argv, &optarg))) { |
| 1698 | struct all_refs_cb cb; |
| 1699 | init_all_refs_cb(&cb, revs, *flags); |
| 1700 | for_each_glob_ref(handle_one_ref, optarg, &cb); |
| 1701 | return argcount; |
| 1702 | } else if (!prefixcmp(arg, "--branches=")) { |
| 1703 | struct all_refs_cb cb; |
| 1704 | init_all_refs_cb(&cb, revs, *flags); |
| 1705 | for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb); |
| 1706 | } else if (!prefixcmp(arg, "--tags=")) { |
| 1707 | struct all_refs_cb cb; |
| 1708 | init_all_refs_cb(&cb, revs, *flags); |
| 1709 | for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb); |
| 1710 | } else if (!prefixcmp(arg, "--remotes=")) { |
| 1711 | struct all_refs_cb cb; |
| 1712 | init_all_refs_cb(&cb, revs, *flags); |
| 1713 | for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb); |
| 1714 | } else if (!strcmp(arg, "--reflog")) { |
| 1715 | handle_reflog(revs, *flags); |
| 1716 | } else if (!strcmp(arg, "--not")) { |
| 1717 | *flags ^= UNINTERESTING; |
| 1718 | } else if (!strcmp(arg, "--no-walk")) { |
Martin von Zweigbergk | ca92e59 | 2012-08-28 23:15:54 -0700 | [diff] [blame] | 1719 | revs->no_walk = REVISION_WALK_NO_WALK_SORTED; |
| 1720 | } else if (!prefixcmp(arg, "--no-walk=")) { |
| 1721 | /* |
| 1722 | * Detached form ("--no-walk X" as opposed to "--no-walk=X") |
| 1723 | * not allowed, since the argument is optional. |
| 1724 | */ |
| 1725 | if (!strcmp(arg + 10, "sorted")) |
| 1726 | revs->no_walk = REVISION_WALK_NO_WALK_SORTED; |
| 1727 | else if (!strcmp(arg + 10, "unsorted")) |
| 1728 | revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; |
| 1729 | else |
| 1730 | return error("invalid argument to --no-walk"); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 1731 | } else if (!strcmp(arg, "--do-walk")) { |
| 1732 | revs->no_walk = 0; |
| 1733 | } else { |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | return 1; |
| 1738 | } |
| 1739 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1740 | /* |
| 1741 | * Parse revision information, filling in the "rev_info" structure, |
| 1742 | * and removing the used arguments from the argument list. |
| 1743 | * |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1744 | * Returns the number of arguments left that weren't recognized |
| 1745 | * (which are also moved to the head of the argument list) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1746 | */ |
Junio C Hamano | 32962c9 | 2010-03-08 22:58:09 -0800 | [diff] [blame] | 1747 | int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1748 | { |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1749 | int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt; |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1750 | struct cmdline_pathspec prune_data; |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1751 | const char *submodule = NULL; |
| 1752 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1753 | memset(&prune_data, 0, sizeof(prune_data)); |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1754 | if (opt) |
| 1755 | submodule = opt->submodule; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1756 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1757 | /* First, search for "--" */ |
Clemens Buchacher | 6d5b93f | 2012-04-14 21:04:48 +0200 | [diff] [blame] | 1758 | if (opt && opt->assume_dashdash) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1759 | seen_dashdash = 1; |
Clemens Buchacher | 6d5b93f | 2012-04-14 21:04:48 +0200 | [diff] [blame] | 1760 | } else { |
| 1761 | seen_dashdash = 0; |
| 1762 | for (i = 1; i < argc; i++) { |
| 1763 | const char *arg = argv[i]; |
| 1764 | if (strcmp(arg, "--")) |
| 1765 | continue; |
| 1766 | argv[i] = NULL; |
| 1767 | argc = i; |
| 1768 | if (argv[i + 1]) |
| 1769 | append_prune_data(&prune_data, argv + i + 1); |
| 1770 | seen_dashdash = 1; |
| 1771 | break; |
| 1772 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1775 | /* Second, deal with arguments and options */ |
| 1776 | flags = 0; |
Junio C Hamano | d5f6b1d | 2012-07-02 12:43:05 -0700 | [diff] [blame] | 1777 | revarg_opt = opt ? opt->revarg_opt : 0; |
| 1778 | if (seen_dashdash) |
| 1779 | revarg_opt |= REVARG_CANNOT_BE_FILENAME; |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 1780 | read_from_stdin = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1781 | for (left = i = 1; i < argc; i++) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1782 | const char *arg = argv[i]; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1783 | if (*arg == '-') { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1784 | int opts; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1785 | |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 1786 | opts = handle_revision_pseudo_opt(submodule, |
| 1787 | revs, argc - i, argv + i, |
| 1788 | &flags); |
| 1789 | if (opts > 0) { |
| 1790 | i += opts - 1; |
Uwe Kleine-König | a5aa930 | 2008-02-28 08:24:25 +0100 | [diff] [blame] | 1791 | continue; |
| 1792 | } |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 1793 | |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 1794 | if (!strcmp(arg, "--stdin")) { |
| 1795 | if (revs->disable_stdin) { |
| 1796 | argv[left++] = arg; |
| 1797 | continue; |
| 1798 | } |
| 1799 | if (read_from_stdin++) |
| 1800 | die("--stdin given twice?"); |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1801 | read_revisions_from_stdin(revs, &prune_data); |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 1802 | continue; |
| 1803 | } |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1804 | |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1805 | opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1806 | if (opts > 0) { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1807 | i += opts - 1; |
| 1808 | continue; |
| 1809 | } |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1810 | if (opts < 0) |
| 1811 | exit(128); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1812 | continue; |
| 1813 | } |
Rene Scharfe | 0d2c9d6 | 2006-07-02 01:29:37 +0200 | [diff] [blame] | 1814 | |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1815 | |
| 1816 | if (handle_revision_arg(arg, revs, flags, revarg_opt)) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1817 | int j; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1818 | if (seen_dashdash || *arg == '^') |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1819 | die("bad revision '%s'", arg); |
| 1820 | |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 1821 | /* If we didn't have a "--": |
| 1822 | * (1) all filenames must exist; |
| 1823 | * (2) all rev-args must not be interpretable |
| 1824 | * as a valid filename. |
| 1825 | * but the latter we have checked in the main loop. |
| 1826 | */ |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 1827 | for (j = i; j < argc; j++) |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 1828 | verify_filename(revs->prefix, argv[j], j == i); |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 1829 | |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1830 | append_prune_data(&prune_data, argv + i); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1831 | break; |
| 1832 | } |
Dave Olszewski | 8fcaca3 | 2010-03-13 14:47:05 -0800 | [diff] [blame] | 1833 | else |
| 1834 | got_rev_arg = 1; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1835 | } |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1836 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1837 | if (prune_data.nr) { |
Junio C Hamano | 93e7d67 | 2011-05-11 15:23:25 -0700 | [diff] [blame] | 1838 | /* |
| 1839 | * If we need to introduce the magic "a lone ':' means no |
| 1840 | * pathspec whatsoever", here is the place to do so. |
| 1841 | * |
| 1842 | * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) { |
| 1843 | * prune_data.nr = 0; |
| 1844 | * prune_data.alloc = 0; |
| 1845 | * free(prune_data.path); |
| 1846 | * prune_data.path = NULL; |
| 1847 | * } else { |
| 1848 | * terminate prune_data.alloc with NULL and |
| 1849 | * call init_pathspec() to set revs->prune_data here. |
| 1850 | * } |
| 1851 | */ |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1852 | ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc); |
| 1853 | prune_data.path[prune_data.nr++] = NULL; |
Junio C Hamano | 8f2d4b1 | 2011-05-11 15:05:01 -0700 | [diff] [blame] | 1854 | init_pathspec(&revs->prune_data, |
| 1855 | get_pathspec(revs->prefix, prune_data.path)); |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1856 | } |
Junio C Hamano | 5486ef0 | 2009-11-20 02:33:28 -0800 | [diff] [blame] | 1857 | |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1858 | if (revs->def == NULL) |
Junio C Hamano | 32962c9 | 2010-03-08 22:58:09 -0800 | [diff] [blame] | 1859 | revs->def = opt ? opt->def : NULL; |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 1860 | if (opt && opt->tweak) |
| 1861 | opt->tweak(revs, opt); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1862 | if (revs->show_merge) |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1863 | prepare_show_merge(revs); |
Dave Olszewski | 8fcaca3 | 2010-03-13 14:47:05 -0800 | [diff] [blame] | 1864 | if (revs->def && !revs->pending.nr && !got_rev_arg) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1865 | unsigned char sha1[20]; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1866 | struct object *object; |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 1867 | struct object_context oc; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1868 | if (get_sha1_with_context(revs->def, 0, sha1, &oc)) |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1869 | die("bad default revision '%s'", revs->def); |
| 1870 | object = get_reference(revs, revs->def, sha1, 0); |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 1871 | add_pending_object_with_mode(revs, object, revs->def, oc.mode); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1872 | } |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1873 | |
Linus Torvalds | b7bb760 | 2007-09-29 09:50:39 -0700 | [diff] [blame] | 1874 | /* Did the user ask for any diff output? Run the diff! */ |
| 1875 | if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) |
| 1876 | revs->diff = 1; |
| 1877 | |
Arjen Laarhoven | 0faf2da | 2007-12-25 12:06:47 +0100 | [diff] [blame] | 1878 | /* Pickaxe, diff-filter and rename following need diffs */ |
| 1879 | if (revs->diffopt.pickaxe || |
| 1880 | revs->diffopt.filter || |
| 1881 | DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES)) |
Linus Torvalds | b7bb760 | 2007-09-29 09:50:39 -0700 | [diff] [blame] | 1882 | revs->diff = 1; |
| 1883 | |
Junio C Hamano | 9dad9d2 | 2006-10-30 18:58:03 -0800 | [diff] [blame] | 1884 | if (revs->topo_order) |
Junio C Hamano | 5306968 | 2006-04-01 18:38:25 -0800 | [diff] [blame] | 1885 | revs->limited = 1; |
| 1886 | |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 1887 | if (revs->prune_data.nr) { |
| 1888 | diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 1889 | /* Can't prune commits with rename following: the paths change.. */ |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 1890 | if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES)) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 1891 | revs->prune = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1892 | if (!revs->full_diff) |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 1893 | diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1894 | } |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 1895 | if (revs->combine_merges) |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1896 | revs->ignore_merges = 0; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1897 | revs->diffopt.abbrev = revs->abbrev; |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 1898 | diff_setup_done(&revs->diffopt); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1899 | |
Junio C Hamano | 918d4e1 | 2012-10-09 16:40:03 -0700 | [diff] [blame] | 1900 | grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED, |
| 1901 | &revs->grep_filter); |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1902 | compile_grep_patterns(&revs->grep_filter); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1903 | |
Shawn O. Pearce | d56651c | 2007-08-19 22:33:43 -0400 | [diff] [blame] | 1904 | if (revs->reverse && revs->reflog_info) |
| 1905 | die("cannot combine --reverse with --walk-reflogs"); |
Junio C Hamano | 8bb6588 | 2008-07-08 15:25:44 -0700 | [diff] [blame] | 1906 | if (revs->rewrite_parents && revs->children.name) |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 1907 | die("cannot combine --parents and --children"); |
Shawn O. Pearce | d56651c | 2007-08-19 22:33:43 -0400 | [diff] [blame] | 1908 | |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 1909 | /* |
| 1910 | * Limitations on the graph functionality |
| 1911 | */ |
| 1912 | if (revs->reverse && revs->graph) |
| 1913 | die("cannot combine --reverse with --graph"); |
| 1914 | |
| 1915 | if (revs->reflog_info && revs->graph) |
| 1916 | die("cannot combine --walk-reflogs with --graph"); |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 1917 | if (!revs->reflog_info && revs->grep_filter.use_reflog_filter) |
| 1918 | die("cannot use --grep-reflog without --walk-reflogs"); |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 1919 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1920 | return left; |
| 1921 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1922 | |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 1923 | static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child) |
| 1924 | { |
| 1925 | struct commit_list *l = xcalloc(1, sizeof(*l)); |
| 1926 | |
| 1927 | l->item = child; |
| 1928 | l->next = add_decoration(&revs->children, &parent->object, l); |
| 1929 | } |
| 1930 | |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1931 | static int remove_duplicate_parents(struct commit *commit) |
| 1932 | { |
| 1933 | struct commit_list **pp, *p; |
| 1934 | int surviving_parents; |
| 1935 | |
| 1936 | /* Examine existing parents while marking ones we have seen... */ |
| 1937 | pp = &commit->parents; |
| 1938 | while ((p = *pp) != NULL) { |
| 1939 | struct commit *parent = p->item; |
| 1940 | if (parent->object.flags & TMP_MARK) { |
| 1941 | *pp = p->next; |
| 1942 | continue; |
| 1943 | } |
| 1944 | parent->object.flags |= TMP_MARK; |
| 1945 | pp = &p->next; |
| 1946 | } |
| 1947 | /* count them while clearing the temporary mark */ |
| 1948 | surviving_parents = 0; |
| 1949 | for (p = commit->parents; p; p = p->next) { |
| 1950 | p->item->object.flags &= ~TMP_MARK; |
| 1951 | surviving_parents++; |
| 1952 | } |
| 1953 | return surviving_parents; |
| 1954 | } |
| 1955 | |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 1956 | struct merge_simplify_state { |
| 1957 | struct commit *simplified; |
| 1958 | }; |
| 1959 | |
| 1960 | static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit) |
| 1961 | { |
| 1962 | struct merge_simplify_state *st; |
| 1963 | |
| 1964 | st = lookup_decoration(&revs->merge_simplification, &commit->object); |
| 1965 | if (!st) { |
| 1966 | st = xcalloc(1, sizeof(*st)); |
| 1967 | add_decoration(&revs->merge_simplification, &commit->object, st); |
| 1968 | } |
| 1969 | return st; |
| 1970 | } |
| 1971 | |
| 1972 | static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail) |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1973 | { |
| 1974 | struct commit_list *p; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 1975 | struct merge_simplify_state *st, *pst; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1976 | int cnt; |
| 1977 | |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 1978 | st = locate_simplify_state(revs, commit); |
| 1979 | |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1980 | /* |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1981 | * Have we handled this one? |
| 1982 | */ |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 1983 | if (st->simplified) |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1984 | return tail; |
| 1985 | |
| 1986 | /* |
| 1987 | * An UNINTERESTING commit simplifies to itself, so does a |
| 1988 | * root commit. We do not rewrite parents of such commit |
| 1989 | * anyway. |
| 1990 | */ |
| 1991 | if ((commit->object.flags & UNINTERESTING) || !commit->parents) { |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 1992 | st->simplified = commit; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1993 | return tail; |
| 1994 | } |
| 1995 | |
| 1996 | /* |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 1997 | * Do we know what commit all of our parents that matter |
| 1998 | * should be rewritten to? Otherwise we are not ready to |
| 1999 | * rewrite this one yet. |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2000 | */ |
| 2001 | for (cnt = 0, p = commit->parents; p; p = p->next) { |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2002 | pst = locate_simplify_state(revs, p->item); |
| 2003 | if (!pst->simplified) { |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2004 | tail = &commit_list_insert(p->item, tail)->next; |
| 2005 | cnt++; |
| 2006 | } |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2007 | if (revs->first_parent_only) |
| 2008 | break; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2009 | } |
Junio C Hamano | 53030f8 | 2008-08-18 00:37:34 -0700 | [diff] [blame] | 2010 | if (cnt) { |
| 2011 | tail = &commit_list_insert(commit, tail)->next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2012 | return tail; |
Junio C Hamano | 53030f8 | 2008-08-18 00:37:34 -0700 | [diff] [blame] | 2013 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2014 | |
| 2015 | /* |
| 2016 | * Rewrite our list of parents. |
| 2017 | */ |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2018 | for (p = commit->parents; p; p = p->next) { |
| 2019 | pst = locate_simplify_state(revs, p->item); |
| 2020 | p->item = pst->simplified; |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2021 | if (revs->first_parent_only) |
| 2022 | break; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2023 | } |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2024 | if (!revs->first_parent_only) |
| 2025 | cnt = remove_duplicate_parents(commit); |
| 2026 | else |
| 2027 | cnt = 1; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2028 | |
| 2029 | /* |
| 2030 | * It is possible that we are a merge and one side branch |
| 2031 | * does not have any commit that touches the given paths; |
| 2032 | * in such a case, the immediate parents will be rewritten |
| 2033 | * to different commits. |
| 2034 | * |
| 2035 | * o----X X: the commit we are looking at; |
| 2036 | * / / o: a commit that touches the paths; |
| 2037 | * ---o----' |
| 2038 | * |
| 2039 | * Further reduce the parents by removing redundant parents. |
| 2040 | */ |
| 2041 | if (1 < cnt) { |
| 2042 | struct commit_list *h = reduce_heads(commit->parents); |
| 2043 | cnt = commit_list_count(h); |
| 2044 | free_commit_list(commit->parents); |
| 2045 | commit->parents = h; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * A commit simplifies to itself if it is a root, if it is |
| 2050 | * UNINTERESTING, if it touches the given paths, or if it is a |
| 2051 | * merge and its parents simplifies to more than one commits |
| 2052 | * (the first two cases are already handled at the beginning of |
| 2053 | * this function). |
| 2054 | * |
| 2055 | * Otherwise, it simplifies to what its sole parent simplifies to. |
| 2056 | */ |
| 2057 | if (!cnt || |
| 2058 | (commit->object.flags & UNINTERESTING) || |
| 2059 | !(commit->object.flags & TREESAME) || |
| 2060 | (1 < cnt)) |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2061 | st->simplified = commit; |
| 2062 | else { |
| 2063 | pst = locate_simplify_state(revs, commit->parents->item); |
| 2064 | st->simplified = pst->simplified; |
| 2065 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2066 | return tail; |
| 2067 | } |
| 2068 | |
| 2069 | static void simplify_merges(struct rev_info *revs) |
| 2070 | { |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2071 | struct commit_list *list, *next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2072 | struct commit_list *yet_to_do, **tail; |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2073 | struct commit *commit; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2074 | |
Junio C Hamano | 5eac739 | 2008-08-14 13:52:36 -0700 | [diff] [blame] | 2075 | if (!revs->prune) |
| 2076 | return; |
Junio C Hamano | 6534703 | 2008-08-03 17:47:16 -0700 | [diff] [blame] | 2077 | |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2078 | /* feed the list reversed */ |
| 2079 | yet_to_do = NULL; |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2080 | for (list = revs->commits; list; list = next) { |
| 2081 | commit = list->item; |
| 2082 | next = list->next; |
| 2083 | /* |
| 2084 | * Do not free(list) here yet; the original list |
| 2085 | * is used later in this function. |
| 2086 | */ |
| 2087 | commit_list_insert(commit, &yet_to_do); |
| 2088 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2089 | while (yet_to_do) { |
| 2090 | list = yet_to_do; |
| 2091 | yet_to_do = NULL; |
| 2092 | tail = &yet_to_do; |
| 2093 | while (list) { |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2094 | commit = list->item; |
| 2095 | next = list->next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2096 | free(list); |
| 2097 | list = next; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2098 | tail = simplify_one(revs, commit, tail); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | /* clean up the result, removing the simplified ones */ |
| 2103 | list = revs->commits; |
| 2104 | revs->commits = NULL; |
| 2105 | tail = &revs->commits; |
| 2106 | while (list) { |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2107 | struct merge_simplify_state *st; |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2108 | |
| 2109 | commit = list->item; |
| 2110 | next = list->next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2111 | free(list); |
| 2112 | list = next; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2113 | st = locate_simplify_state(revs, commit); |
| 2114 | if (st->simplified == commit) |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2115 | tail = &commit_list_insert(commit, tail)->next; |
| 2116 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2119 | static void set_children(struct rev_info *revs) |
| 2120 | { |
| 2121 | struct commit_list *l; |
| 2122 | for (l = revs->commits; l; l = l->next) { |
| 2123 | struct commit *commit = l->item; |
| 2124 | struct commit_list *p; |
| 2125 | |
| 2126 | for (p = commit->parents; p; p = p->next) |
| 2127 | add_child(revs, p->item, commit); |
| 2128 | } |
| 2129 | } |
| 2130 | |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 2131 | void reset_revision_walk(void) |
| 2132 | { |
| 2133 | clear_object_flags(SEEN | ADDED | SHOWN); |
| 2134 | } |
| 2135 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2136 | int prepare_revision_walk(struct rev_info *revs) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2137 | { |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 2138 | int nr = revs->pending.nr; |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 2139 | struct object_array_entry *e, *list; |
René Scharfe | 2e7da8e | 2012-04-25 22:35:41 +0200 | [diff] [blame] | 2140 | struct commit_list **next = &revs->commits; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2141 | |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 2142 | e = list = revs->pending.objects; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 2143 | revs->pending.nr = 0; |
| 2144 | revs->pending.alloc = 0; |
| 2145 | revs->pending.objects = NULL; |
| 2146 | while (--nr >= 0) { |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 2147 | struct commit *commit = handle_commit(revs, e->item, e->name); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2148 | if (commit) { |
| 2149 | if (!(commit->object.flags & SEEN)) { |
| 2150 | commit->object.flags |= SEEN; |
René Scharfe | 2e7da8e | 2012-04-25 22:35:41 +0200 | [diff] [blame] | 2151 | next = commit_list_append(commit, next); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 2154 | e++; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2155 | } |
René Scharfe | 4a43d37 | 2011-10-01 17:56:08 +0200 | [diff] [blame] | 2156 | if (!revs->leak_pending) |
| 2157 | free(list); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2158 | |
Martin von Zweigbergk | ca92e59 | 2012-08-28 23:15:54 -0700 | [diff] [blame] | 2159 | if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED) |
| 2160 | commit_list_sort_by_date(&revs->commits); |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 2161 | if (revs->no_walk) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2162 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2163 | if (revs->limited) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2164 | if (limit_list(revs) < 0) |
| 2165 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2166 | if (revs->topo_order) |
Linus Torvalds | 23c17d4 | 2007-11-02 13:32:58 -0700 | [diff] [blame] | 2167 | sort_in_topological_order(&revs->commits, revs->lifo); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2168 | if (revs->simplify_merges) |
| 2169 | simplify_merges(revs); |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2170 | if (revs->children.name) |
| 2171 | set_children(revs); |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2172 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2175 | enum rewrite_result { |
| 2176 | rewrite_one_ok, |
| 2177 | rewrite_one_noparents, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 2178 | rewrite_one_error |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2179 | }; |
| 2180 | |
| 2181 | static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2182 | { |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 2183 | struct commit_list *cache = NULL; |
| 2184 | |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2185 | for (;;) { |
| 2186 | struct commit *p = *pp; |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 2187 | if (!revs->limited) |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 2188 | if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2189 | return rewrite_one_error; |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 2190 | if (p->parents && p->parents->next) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2191 | return rewrite_one_ok; |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 2192 | if (p->object.flags & UNINTERESTING) |
| 2193 | return rewrite_one_ok; |
| 2194 | if (!(p->object.flags & TREESAME)) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2195 | return rewrite_one_ok; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2196 | if (!p->parents) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2197 | return rewrite_one_noparents; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2198 | *pp = p->parents->item; |
| 2199 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2200 | } |
| 2201 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2202 | static int rewrite_parents(struct rev_info *revs, struct commit *commit) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2203 | { |
| 2204 | struct commit_list **pp = &commit->parents; |
| 2205 | while (*pp) { |
| 2206 | struct commit_list *parent = *pp; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2207 | switch (rewrite_one(revs, &parent->item)) { |
| 2208 | case rewrite_one_ok: |
| 2209 | break; |
| 2210 | case rewrite_one_noparents: |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2211 | *pp = parent->next; |
| 2212 | continue; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2213 | case rewrite_one_error: |
| 2214 | return -1; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2215 | } |
| 2216 | pp = &parent->next; |
| 2217 | } |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 2218 | remove_duplicate_parents(commit); |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2219 | return 0; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2220 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2221 | |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 2222 | static int commit_match(struct commit *commit, struct rev_info *opt) |
| 2223 | { |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2224 | int retval; |
| 2225 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 2226 | if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list) |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 2227 | return 1; |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2228 | |
| 2229 | /* Prepend "fake" headers as needed */ |
| 2230 | if (opt->grep_filter.use_reflog_filter) { |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2231 | strbuf_addstr(&buf, "reflog "); |
| 2232 | get_reflog_message(&buf, opt->reflog_info); |
| 2233 | strbuf_addch(&buf, '\n'); |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2234 | } |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2235 | |
| 2236 | /* Copy the commit to temporary if we are using "fake" headers */ |
| 2237 | if (buf.len) |
| 2238 | strbuf_addstr(&buf, commit->buffer); |
| 2239 | |
Nguyễn Thái Ngọc Duy | 38cfe91 | 2012-09-29 11:41:29 +0700 | [diff] [blame] | 2240 | /* Append "fake" message parts as needed */ |
| 2241 | if (opt->show_notes) { |
| 2242 | if (!buf.len) |
| 2243 | strbuf_addstr(&buf, commit->buffer); |
| 2244 | format_display_notes(commit->object.sha1, &buf, |
Junio C Hamano | 76141e2 | 2012-10-17 21:41:54 -0700 | [diff] [blame] | 2245 | get_log_output_encoding(), 1); |
Nguyễn Thái Ngọc Duy | 38cfe91 | 2012-09-29 11:41:29 +0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2248 | /* Find either in the commit object, or in the temporary */ |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2249 | if (buf.len) |
| 2250 | retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len); |
| 2251 | else |
| 2252 | retval = grep_buffer(&opt->grep_filter, |
| 2253 | commit->buffer, strlen(commit->buffer)); |
| 2254 | strbuf_release(&buf); |
| 2255 | return retval; |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2258 | static inline int want_ancestry(struct rev_info *revs) |
| 2259 | { |
Junio C Hamano | 8bb6588 | 2008-07-08 15:25:44 -0700 | [diff] [blame] | 2260 | return (revs->rewrite_parents || revs->children.name); |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2261 | } |
| 2262 | |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 2263 | enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit) |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2264 | { |
| 2265 | if (commit->object.flags & SHOWN) |
| 2266 | return commit_ignore; |
Brandon Casey | 4d6acb7 | 2009-03-19 22:47:54 -0500 | [diff] [blame] | 2267 | if (revs->unpacked && has_sha1_pack(commit->object.sha1)) |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2268 | return commit_ignore; |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 2269 | if (revs->show_all) |
| 2270 | return commit_show; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2271 | if (commit->object.flags & UNINTERESTING) |
| 2272 | return commit_ignore; |
| 2273 | if (revs->min_age != -1 && (commit->date > revs->min_age)) |
| 2274 | return commit_ignore; |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 2275 | if (revs->min_parents || (revs->max_parents >= 0)) { |
| 2276 | int n = 0; |
| 2277 | struct commit_list *p; |
| 2278 | for (p = commit->parents; p; p = p->next) |
| 2279 | n++; |
| 2280 | if ((n < revs->min_parents) || |
| 2281 | ((revs->max_parents >= 0) && (n > revs->max_parents))) |
| 2282 | return commit_ignore; |
| 2283 | } |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2284 | if (!commit_match(commit, revs)) |
| 2285 | return commit_ignore; |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 2286 | if (revs->prune && revs->dense) { |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2287 | /* Commit without changes? */ |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 2288 | if (commit->object.flags & TREESAME) { |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2289 | /* drop merges unless we want parenthood */ |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2290 | if (!want_ancestry(revs)) |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2291 | return commit_ignore; |
| 2292 | /* non-merge - always ignore it */ |
| 2293 | if (!commit->parents || !commit->parents->next) |
| 2294 | return commit_ignore; |
| 2295 | } |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2296 | } |
| 2297 | return commit_show; |
| 2298 | } |
| 2299 | |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 2300 | enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit) |
| 2301 | { |
| 2302 | enum commit_action action = get_commit_action(revs, commit); |
| 2303 | |
| 2304 | if (action == commit_show && |
| 2305 | !revs->show_all && |
| 2306 | revs->prune && revs->dense && want_ancestry(revs)) { |
| 2307 | if (rewrite_parents(revs, commit) < 0) |
| 2308 | return commit_error; |
| 2309 | } |
| 2310 | return action; |
| 2311 | } |
| 2312 | |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2313 | static struct commit *get_revision_1(struct rev_info *revs) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2314 | { |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2315 | if (!revs->commits) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2316 | return NULL; |
| 2317 | |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2318 | do { |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 2319 | struct commit_list *entry = revs->commits; |
| 2320 | struct commit *commit = entry->item; |
Linus Torvalds | ea5ed3a | 2006-03-05 09:53:52 -0800 | [diff] [blame] | 2321 | |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 2322 | revs->commits = entry->next; |
| 2323 | free(entry); |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 2324 | |
Jeff King | ffa1eea | 2010-11-21 23:42:53 -0500 | [diff] [blame] | 2325 | if (revs->reflog_info) { |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 2326 | fake_reflog_parent(revs->reflog_info, commit); |
Jeff King | ffa1eea | 2010-11-21 23:42:53 -0500 | [diff] [blame] | 2327 | commit->object.flags &= ~(ADDED | SEEN | SHOWN); |
| 2328 | } |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 2329 | |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 2330 | /* |
| 2331 | * If we haven't done the list limiting, we need to look at |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 2332 | * the parents here. We also need to do the date-based limiting |
| 2333 | * that we'd otherwise have done in limit_list(). |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 2334 | */ |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 2335 | if (!revs->limited) { |
Jan Harkes | 744f498 | 2006-10-30 20:37:49 -0500 | [diff] [blame] | 2336 | if (revs->max_age != -1 && |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2337 | (commit->date < revs->max_age)) |
| 2338 | continue; |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 2339 | if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) |
Junio C Hamano | ed62089 | 2009-02-11 01:27:43 -0800 | [diff] [blame] | 2340 | die("Failed to traverse parents of commit %s", |
| 2341 | sha1_to_hex(commit->object.sha1)); |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 2342 | } |
Junio C Hamano | 1b65a5a | 2006-04-16 18:12:49 -0700 | [diff] [blame] | 2343 | |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2344 | switch (simplify_commit(revs, commit)) { |
| 2345 | case commit_ignore: |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 2346 | continue; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2347 | case commit_error: |
Junio C Hamano | ed62089 | 2009-02-11 01:27:43 -0800 | [diff] [blame] | 2348 | die("Failed to simplify parents of commit %s", |
| 2349 | sha1_to_hex(commit->object.sha1)); |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2350 | default: |
| 2351 | return commit; |
Junio C Hamano | 384e99a | 2006-03-27 23:58:34 -0800 | [diff] [blame] | 2352 | } |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2353 | } while (revs->commits); |
| 2354 | return NULL; |
| 2355 | } |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2356 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2357 | static void gc_boundary(struct object_array *array) |
| 2358 | { |
| 2359 | unsigned nr = array->nr; |
| 2360 | unsigned alloc = array->alloc; |
| 2361 | struct object_array_entry *objects = array->objects; |
| 2362 | |
| 2363 | if (alloc <= nr) { |
| 2364 | unsigned i, j; |
| 2365 | for (i = j = 0; i < nr; i++) { |
| 2366 | if (objects[i].item->flags & SHOWN) |
| 2367 | continue; |
| 2368 | if (i != j) |
| 2369 | objects[j] = objects[i]; |
| 2370 | j++; |
| 2371 | } |
Junio C Hamano | 892ae6b | 2007-03-06 03:00:18 -0800 | [diff] [blame] | 2372 | for (i = j; i < nr; i++) |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2373 | objects[i].item = NULL; |
| 2374 | array->nr = j; |
| 2375 | } |
| 2376 | } |
| 2377 | |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 2378 | static void create_boundary_commit_list(struct rev_info *revs) |
| 2379 | { |
| 2380 | unsigned i; |
| 2381 | struct commit *c; |
| 2382 | struct object_array *array = &revs->boundary_commits; |
| 2383 | struct object_array_entry *objects = array->objects; |
| 2384 | |
| 2385 | /* |
| 2386 | * If revs->commits is non-NULL at this point, an error occurred in |
| 2387 | * get_revision_1(). Ignore the error and continue printing the |
| 2388 | * boundary commits anyway. (This is what the code has always |
| 2389 | * done.) |
| 2390 | */ |
| 2391 | if (revs->commits) { |
| 2392 | free_commit_list(revs->commits); |
| 2393 | revs->commits = NULL; |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * Put all of the actual boundary commits from revs->boundary_commits |
| 2398 | * into revs->commits |
| 2399 | */ |
| 2400 | for (i = 0; i < array->nr; i++) { |
| 2401 | c = (struct commit *)(objects[i].item); |
| 2402 | if (!c) |
| 2403 | continue; |
| 2404 | if (!(c->object.flags & CHILD_SHOWN)) |
| 2405 | continue; |
| 2406 | if (c->object.flags & (SHOWN | BOUNDARY)) |
| 2407 | continue; |
| 2408 | c->object.flags |= BOUNDARY; |
| 2409 | commit_list_insert(c, &revs->commits); |
| 2410 | } |
| 2411 | |
| 2412 | /* |
| 2413 | * If revs->topo_order is set, sort the boundary commits |
| 2414 | * in topological order |
| 2415 | */ |
| 2416 | sort_in_topological_order(&revs->commits, revs->lifo); |
| 2417 | } |
| 2418 | |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 2419 | static struct commit *get_revision_internal(struct rev_info *revs) |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2420 | { |
| 2421 | struct commit *c = NULL; |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2422 | struct commit_list *l; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2423 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2424 | if (revs->boundary == 2) { |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 2425 | /* |
| 2426 | * All of the normal commits have already been returned, |
| 2427 | * and we are now returning boundary commits. |
| 2428 | * create_boundary_commit_list() has populated |
| 2429 | * revs->commits with the remaining commits to return. |
| 2430 | */ |
| 2431 | c = pop_commit(&revs->commits); |
| 2432 | if (c) |
| 2433 | c->object.flags |= SHOWN; |
Johannes Schindelin | 9c5e66e | 2007-01-20 23:04:02 +0100 | [diff] [blame] | 2434 | return c; |
| 2435 | } |
| 2436 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2437 | /* |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 2438 | * If our max_count counter has reached zero, then we are done. We |
| 2439 | * don't simply return NULL because we still might need to show |
| 2440 | * boundary commits. But we want to avoid calling get_revision_1, which |
| 2441 | * might do a considerable amount of work finding the next commit only |
| 2442 | * for us to throw it away. |
| 2443 | * |
| 2444 | * If it is non-zero, then either we don't have a max_count at all |
| 2445 | * (-1), or it is still counting, in which case we decrement. |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2446 | */ |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 2447 | if (revs->max_count) { |
| 2448 | c = get_revision_1(revs); |
| 2449 | if (c) { |
| 2450 | while (0 < revs->skip_count) { |
| 2451 | revs->skip_count--; |
| 2452 | c = get_revision_1(revs); |
| 2453 | if (!c) |
| 2454 | break; |
| 2455 | } |
Junio C Hamano | 8839ac9 | 2007-03-06 03:20:55 -0800 | [diff] [blame] | 2456 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2457 | |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 2458 | if (revs->max_count > 0) |
| 2459 | revs->max_count--; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2460 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2461 | |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 2462 | if (c) |
| 2463 | c->object.flags |= SHOWN; |
| 2464 | |
| 2465 | if (!revs->boundary) { |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2466 | return c; |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 2467 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2468 | |
| 2469 | if (!c) { |
| 2470 | /* |
| 2471 | * get_revision_1() runs out the commits, and |
| 2472 | * we are done computing the boundaries. |
| 2473 | * switch to boundary commits output mode. |
| 2474 | */ |
| 2475 | revs->boundary = 2; |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 2476 | |
| 2477 | /* |
| 2478 | * Update revs->commits to contain the list of |
| 2479 | * boundary commits. |
| 2480 | */ |
| 2481 | create_boundary_commit_list(revs); |
| 2482 | |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 2483 | return get_revision_internal(revs); |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | * boundary commits are the commits that are parents of the |
| 2488 | * ones we got from get_revision_1() but they themselves are |
| 2489 | * not returned from get_revision_1(). Before returning |
| 2490 | * 'c', we need to mark its parents that they could be boundaries. |
| 2491 | */ |
| 2492 | |
| 2493 | for (l = c->parents; l; l = l->next) { |
| 2494 | struct object *p; |
| 2495 | p = &(l->item->object); |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 2496 | if (p->flags & (CHILD_SHOWN | SHOWN)) |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 2497 | continue; |
| 2498 | p->flags |= CHILD_SHOWN; |
| 2499 | gc_boundary(&revs->boundary_commits); |
| 2500 | add_object_array(p, NULL, &revs->boundary_commits); |
| 2501 | } |
| 2502 | |
| 2503 | return c; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 2504 | } |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 2505 | |
| 2506 | struct commit *get_revision(struct rev_info *revs) |
| 2507 | { |
Thomas Rast | 498bcd3 | 2008-08-29 21:18:38 +0200 | [diff] [blame] | 2508 | struct commit *c; |
| 2509 | struct commit_list *reversed; |
| 2510 | |
| 2511 | if (revs->reverse) { |
| 2512 | reversed = NULL; |
| 2513 | while ((c = get_revision_internal(revs))) { |
| 2514 | commit_list_insert(c, &reversed); |
| 2515 | } |
| 2516 | revs->commits = reversed; |
| 2517 | revs->reverse = 0; |
| 2518 | revs->reverse_output_stage = 1; |
| 2519 | } |
| 2520 | |
| 2521 | if (revs->reverse_output_stage) |
| 2522 | return pop_commit(&revs->commits); |
| 2523 | |
| 2524 | c = get_revision_internal(revs); |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 2525 | if (c && revs->graph) |
| 2526 | graph_update(revs->graph, c); |
| 2527 | return c; |
| 2528 | } |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 2529 | |
| 2530 | char *get_revision_mark(const struct rev_info *revs, const struct commit *commit) |
| 2531 | { |
| 2532 | if (commit->object.flags & BOUNDARY) |
| 2533 | return "-"; |
| 2534 | else if (commit->object.flags & UNINTERESTING) |
| 2535 | return "^"; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 2536 | else if (commit->object.flags & PATCHSAME) |
| 2537 | return "="; |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 2538 | else if (!revs || revs->left_right) { |
| 2539 | if (commit->object.flags & SYMMETRIC_LEFT) |
| 2540 | return "<"; |
| 2541 | else |
| 2542 | return ">"; |
| 2543 | } else if (revs->graph) |
| 2544 | return "*"; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 2545 | else if (revs->cherry_mark) |
| 2546 | return "+"; |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 2547 | return ""; |
| 2548 | } |
Michael J Gruber | b1b4755 | 2011-03-10 15:45:03 +0100 | [diff] [blame] | 2549 | |
| 2550 | void put_revision_mark(const struct rev_info *revs, const struct commit *commit) |
| 2551 | { |
| 2552 | char *mark = get_revision_mark(revs, commit); |
| 2553 | if (!strlen(mark)) |
| 2554 | return; |
| 2555 | fputs(mark, stdout); |
| 2556 | putchar(' '); |
| 2557 | } |