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" |
Thomas Rast | 12da1d1 | 2013-03-28 17:47:32 +0100 | [diff] [blame] | 16 | #include "line-log.h" |
Antoine Pelisse | d72fbe8 | 2013-01-05 22:26:45 +0100 | [diff] [blame] | 17 | #include "mailmap.h" |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 18 | #include "commit-slab.h" |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 19 | #include "dir.h" |
Jeff King | 4fe1021 | 2014-10-16 20:44:23 -0400 | [diff] [blame] | 20 | #include "cache-tree.h" |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 21 | |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 22 | volatile show_early_output_fn_t show_early_output; |
| 23 | |
Linus Torvalds | cf2ab91 | 2009-04-10 18:15:26 -0700 | [diff] [blame] | 24 | char *path_name(const struct name_path *path, const char *name) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 25 | { |
Linus Torvalds | cf2ab91 | 2009-04-10 18:15:26 -0700 | [diff] [blame] | 26 | const struct name_path *p; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 27 | char *n, *m; |
| 28 | int nlen = strlen(name); |
| 29 | int len = nlen + 1; |
| 30 | |
| 31 | for (p = path; p; p = p->up) { |
| 32 | if (p->elem_len) |
| 33 | len += p->elem_len + 1; |
| 34 | } |
| 35 | n = xmalloc(len); |
| 36 | m = n + len - (nlen + 1); |
| 37 | strcpy(m, name); |
| 38 | for (p = path; p; p = p->up) { |
| 39 | if (p->elem_len) { |
| 40 | m -= p->elem_len + 1; |
| 41 | memcpy(m, p->elem, p->elem_len); |
| 42 | m[p->elem_len] = '/'; |
| 43 | } |
| 44 | } |
| 45 | return n; |
| 46 | } |
| 47 | |
Junio C Hamano | beba25a | 2011-08-17 14:30:35 -0700 | [diff] [blame] | 48 | static int show_path_component_truncated(FILE *out, const char *name, int len) |
| 49 | { |
| 50 | int cnt; |
| 51 | for (cnt = 0; cnt < len; cnt++) { |
| 52 | int ch = name[cnt]; |
| 53 | if (!ch || ch == '\n') |
| 54 | return -1; |
| 55 | fputc(ch, out); |
| 56 | } |
| 57 | return len; |
| 58 | } |
| 59 | |
| 60 | static int show_path_truncated(FILE *out, const struct name_path *path) |
| 61 | { |
| 62 | int emitted, ours; |
| 63 | |
| 64 | if (!path) |
| 65 | return 0; |
| 66 | emitted = show_path_truncated(out, path->up); |
| 67 | if (emitted < 0) |
| 68 | return emitted; |
| 69 | if (emitted) |
| 70 | fputc('/', out); |
| 71 | ours = show_path_component_truncated(out, path->elem, path->elem_len); |
| 72 | if (ours < 0) |
| 73 | return ours; |
| 74 | return ours || emitted; |
| 75 | } |
| 76 | |
Michael Haggerty | ff5f5f2 | 2013-05-25 11:08:07 +0200 | [diff] [blame] | 77 | void show_object_with_name(FILE *out, struct object *obj, |
| 78 | const struct name_path *path, const char *component) |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 79 | { |
Junio C Hamano | beba25a | 2011-08-17 14:30:35 -0700 | [diff] [blame] | 80 | struct name_path leaf; |
| 81 | leaf.up = (struct name_path *)path; |
| 82 | leaf.elem = component; |
| 83 | leaf.elem_len = strlen(component); |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 84 | |
Junio C Hamano | beba25a | 2011-08-17 14:30:35 -0700 | [diff] [blame] | 85 | fprintf(out, "%s ", sha1_to_hex(obj->sha1)); |
| 86 | show_path_truncated(out, &leaf); |
| 87 | fputc('\n', out); |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 90 | static void mark_blob_uninteresting(struct blob *blob) |
| 91 | { |
Martin Koegler | c1ee901 | 2008-02-18 21:47:54 +0100 | [diff] [blame] | 92 | if (!blob) |
| 93 | return; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 94 | if (blob->object.flags & UNINTERESTING) |
| 95 | return; |
| 96 | blob->object.flags |= UNINTERESTING; |
| 97 | } |
| 98 | |
Junio C Hamano | 2ac5e44 | 2014-01-15 15:38:01 -0800 | [diff] [blame] | 99 | static void mark_tree_contents_uninteresting(struct tree *tree) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 100 | { |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 101 | struct tree_desc desc; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 102 | struct name_entry entry; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 103 | struct object *obj = &tree->object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 104 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 105 | if (!has_sha1_file(obj->sha1)) |
| 106 | return; |
| 107 | if (parse_tree(tree) < 0) |
| 108 | die("bad tree %s", sha1_to_hex(obj->sha1)); |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 109 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 110 | init_tree_desc(&desc, tree->buffer, tree->size); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 111 | while (tree_entry(&desc, &entry)) { |
Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 +0000 | [diff] [blame] | 112 | switch (object_type(entry.mode)) { |
| 113 | case OBJ_TREE: |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 114 | mark_tree_uninteresting(lookup_tree(entry.sha1)); |
Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 +0000 | [diff] [blame] | 115 | break; |
| 116 | case OBJ_BLOB: |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 117 | mark_blob_uninteresting(lookup_blob(entry.sha1)); |
Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 +0000 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | /* Subproject commit - not in this repository */ |
| 121 | break; |
| 122 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 123 | } |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * We don't care about the tree any more |
| 127 | * after it has been marked uninteresting. |
| 128 | */ |
Jeff King | 6e454b9 | 2013-06-05 18:37:39 -0400 | [diff] [blame] | 129 | free_tree_buffer(tree); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Junio C Hamano | 2ac5e44 | 2014-01-15 15:38:01 -0800 | [diff] [blame] | 132 | void mark_tree_uninteresting(struct tree *tree) |
| 133 | { |
| 134 | struct object *obj = &tree->object; |
| 135 | |
| 136 | if (!tree) |
| 137 | return; |
| 138 | if (obj->flags & UNINTERESTING) |
| 139 | return; |
| 140 | obj->flags |= UNINTERESTING; |
| 141 | mark_tree_contents_uninteresting(tree); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void mark_parents_uninteresting(struct commit *commit) |
| 145 | { |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 146 | struct commit_list *parents = NULL, *l; |
| 147 | |
| 148 | for (l = commit->parents; l; l = l->next) |
| 149 | commit_list_insert(l->item, &parents); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 150 | |
| 151 | while (parents) { |
| 152 | struct commit *commit = parents->item; |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 153 | l = parents; |
| 154 | parents = parents->next; |
| 155 | free(l); |
| 156 | |
| 157 | while (commit) { |
| 158 | /* |
| 159 | * A missing commit is ok iff its parent is marked |
| 160 | * uninteresting. |
| 161 | * |
| 162 | * We just mark such a thing parsed, so that when |
| 163 | * it is popped next time around, we won't be trying |
| 164 | * to parse it and get an error. |
| 165 | */ |
| 166 | if (!has_sha1_file(commit->object.sha1)) |
| 167 | commit->object.parsed = 1; |
| 168 | |
| 169 | if (commit->object.flags & UNINTERESTING) |
| 170 | break; |
| 171 | |
Matthias Urlichs | d2c4af7 | 2006-03-09 05:04:36 +0100 | [diff] [blame] | 172 | commit->object.flags |= UNINTERESTING; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 173 | |
Matthias Urlichs | d2c4af7 | 2006-03-09 05:04:36 +0100 | [diff] [blame] | 174 | /* |
| 175 | * Normally we haven't parsed the parent |
| 176 | * yet, so we won't have a parent of a parent |
| 177 | * here. However, it may turn out that we've |
| 178 | * reached this commit some other way (where it |
| 179 | * wasn't uninteresting), in which case we need |
| 180 | * to mark its parents recursively too.. |
| 181 | */ |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 182 | if (!commit->parents) |
| 183 | break; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 184 | |
Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 19:19:53 +0700 | [diff] [blame] | 185 | for (l = commit->parents->next; l; l = l->next) |
| 186 | commit_list_insert(l->item, &parents); |
| 187 | commit = commit->parents->item; |
| 188 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 192 | static void add_pending_object_with_path(struct rev_info *revs, |
Michael Haggerty | ff5f5f2 | 2013-05-25 11:08:07 +0200 | [diff] [blame] | 193 | struct object *obj, |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 194 | const char *name, unsigned mode, |
| 195 | const char *path) |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 196 | { |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 197 | if (!obj) |
| 198 | return; |
Linus Torvalds | aa27e46 | 2007-02-27 16:22:52 -0800 | [diff] [blame] | 199 | if (revs->no_walk && (obj->flags & UNINTERESTING)) |
Linus Torvalds | f222abd | 2009-07-13 14:41:12 -0700 | [diff] [blame] | 200 | revs->no_walk = 0; |
Junio C Hamano | 105e473 | 2010-01-26 13:48:28 -0800 | [diff] [blame] | 201 | if (revs->reflog_info && obj->type == OBJ_COMMIT) { |
| 202 | struct strbuf buf = STRBUF_INIT; |
Felipe Contreras | cf99a76 | 2013-09-02 01:34:29 -0500 | [diff] [blame] | 203 | int len = interpret_branch_name(name, 0, &buf); |
Junio C Hamano | 105e473 | 2010-01-26 13:48:28 -0800 | [diff] [blame] | 204 | int st; |
| 205 | |
| 206 | if (0 < len && name[len] && buf.len) |
| 207 | strbuf_addstr(&buf, name + len); |
| 208 | st = add_reflog_for_walk(revs->reflog_info, |
| 209 | (struct commit *)obj, |
| 210 | buf.buf[0] ? buf.buf: name); |
| 211 | strbuf_release(&buf); |
| 212 | if (st) |
| 213 | return; |
| 214 | } |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 215 | add_object_array_with_path(obj, name, &revs->pending, mode, path); |
| 216 | } |
| 217 | |
| 218 | static void add_pending_object_with_mode(struct rev_info *revs, |
| 219 | struct object *obj, |
| 220 | const char *name, unsigned mode) |
| 221 | { |
| 222 | add_pending_object_with_path(revs, obj, name, mode, NULL); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Michael Haggerty | ff5f5f2 | 2013-05-25 11:08:07 +0200 | [diff] [blame] | 225 | void add_pending_object(struct rev_info *revs, |
| 226 | struct object *obj, const char *name) |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 227 | { |
| 228 | add_pending_object_with_mode(revs, obj, name, S_IFINVALID); |
| 229 | } |
| 230 | |
Junio C Hamano | 3384a2d | 2007-12-11 10:09:04 -0800 | [diff] [blame] | 231 | void add_head_to_pending(struct rev_info *revs) |
| 232 | { |
| 233 | unsigned char sha1[20]; |
| 234 | struct object *obj; |
| 235 | if (get_sha1("HEAD", sha1)) |
| 236 | return; |
| 237 | obj = parse_object(sha1); |
| 238 | if (!obj) |
| 239 | return; |
| 240 | add_pending_object(revs, obj, "HEAD"); |
| 241 | } |
| 242 | |
Michael Haggerty | ff5f5f2 | 2013-05-25 11:08:07 +0200 | [diff] [blame] | 243 | static struct object *get_reference(struct rev_info *revs, const char *name, |
| 244 | const unsigned char *sha1, |
| 245 | unsigned int flags) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 246 | { |
| 247 | struct object *object; |
| 248 | |
| 249 | object = parse_object(sha1); |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 250 | if (!object) { |
| 251 | if (revs->ignore_missing) |
| 252 | return object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 253 | die("bad object %s", name); |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 254 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 255 | object->flags |= flags; |
| 256 | return object; |
| 257 | } |
| 258 | |
René Scharfe | 26c3177 | 2011-10-01 17:43:52 +0200 | [diff] [blame] | 259 | void add_pending_sha1(struct rev_info *revs, const char *name, |
| 260 | const unsigned char *sha1, unsigned int flags) |
| 261 | { |
| 262 | struct object *object = get_reference(revs, name, sha1, flags); |
| 263 | add_pending_object(revs, object, name); |
| 264 | } |
| 265 | |
Michael Haggerty | ff5f5f2 | 2013-05-25 11:08:07 +0200 | [diff] [blame] | 266 | static struct commit *handle_commit(struct rev_info *revs, |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 267 | struct object_array_entry *entry) |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 268 | { |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 269 | struct object *object = entry->item; |
| 270 | const char *name = entry->name; |
| 271 | const char *path = entry->path; |
| 272 | unsigned int mode = entry->mode; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 273 | unsigned long flags = object->flags; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * Tag object? Look what it points to.. |
| 277 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 278 | while (object->type == OBJ_TAG) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 279 | struct tag *tag = (struct tag *) object; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 280 | if (revs->tag_objects && !(flags & UNINTERESTING)) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 281 | add_pending_object(revs, object, tag->tag); |
Martin Koegler | 9684afd | 2008-02-18 21:48:01 +0100 | [diff] [blame] | 282 | if (!tag->tagged) |
| 283 | die("bad tag"); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 284 | object = parse_object(tag->tagged->sha1); |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 285 | if (!object) { |
| 286 | if (flags & UNINTERESTING) |
| 287 | return NULL; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 288 | die("bad object %s", sha1_to_hex(tag->tagged->sha1)); |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 289 | } |
Junio C Hamano | a743528 | 2014-01-15 12:26:13 -0800 | [diff] [blame] | 290 | object->flags |= flags; |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 291 | /* |
| 292 | * We'll handle the tagged object by looping or dropping |
| 293 | * through to the non-tag handlers below. Do not |
| 294 | * propagate data from the tag's pending entry. |
| 295 | */ |
| 296 | name = ""; |
| 297 | path = NULL; |
| 298 | mode = 0; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Commit object? Just return it, we'll do all the complex |
| 303 | * reachability crud. |
| 304 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 305 | if (object->type == OBJ_COMMIT) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 306 | struct commit *commit = (struct commit *)object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 307 | if (parse_commit(commit) < 0) |
| 308 | die("unable to parse commit %s", name); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 309 | if (flags & UNINTERESTING) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 310 | mark_parents_uninteresting(commit); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 311 | revs->limited = 1; |
| 312 | } |
Linus Torvalds | 0f3a290 | 2008-10-27 12:51:59 -0700 | [diff] [blame] | 313 | if (revs->show_source && !commit->util) |
Jeff King | 1da1e07 | 2014-10-15 18:35:12 -0400 | [diff] [blame] | 314 | commit->util = xstrdup(name); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 315 | return commit; |
| 316 | } |
| 317 | |
| 318 | /* |
Mike Ralphson | 3ea3c21 | 2009-04-17 19:13:30 +0100 | [diff] [blame] | 319 | * Tree object? Either mark it uninteresting, or add it |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 320 | * to the list of objects to look at later.. |
| 321 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 322 | if (object->type == OBJ_TREE) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 323 | struct tree *tree = (struct tree *)object; |
| 324 | if (!revs->tree_objects) |
| 325 | return NULL; |
| 326 | if (flags & UNINTERESTING) { |
Junio C Hamano | 2ac5e44 | 2014-01-15 15:38:01 -0800 | [diff] [blame] | 327 | mark_tree_contents_uninteresting(tree); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 330 | add_pending_object_with_path(revs, object, name, mode, path); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Blob object? You know the drill by now.. |
| 336 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 337 | if (object->type == OBJ_BLOB) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 338 | if (!revs->blob_objects) |
| 339 | return NULL; |
Junio C Hamano | a743528 | 2014-01-15 12:26:13 -0800 | [diff] [blame] | 340 | if (flags & UNINTERESTING) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 341 | return NULL; |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 342 | add_pending_object_with_path(revs, object, name, mode, path); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | die("%s is unknown object", name); |
| 346 | } |
| 347 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 348 | static int everybody_uninteresting(struct commit_list *orig) |
| 349 | { |
| 350 | struct commit_list *list = orig; |
| 351 | while (list) { |
| 352 | struct commit *commit = list->item; |
| 353 | list = list->next; |
| 354 | if (commit->object.flags & UNINTERESTING) |
| 355 | continue; |
| 356 | return 0; |
| 357 | } |
| 358 | return 1; |
| 359 | } |
| 360 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 361 | /* |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 362 | * A definition of "relevant" commit that we can use to simplify limited graphs |
| 363 | * by eliminating side branches. |
| 364 | * |
| 365 | * A "relevant" commit is one that is !UNINTERESTING (ie we are including it |
| 366 | * in our list), or that is a specified BOTTOM commit. Then after computing |
| 367 | * a limited list, during processing we can generally ignore boundary merges |
| 368 | * coming from outside the graph, (ie from irrelevant parents), and treat |
| 369 | * those merges as if they were single-parent. TREESAME is defined to consider |
| 370 | * only relevant parents, if any. If we are TREESAME to our on-graph parents, |
| 371 | * we don't care if we were !TREESAME to non-graph parents. |
| 372 | * |
| 373 | * Treating bottom commits as relevant ensures that a limited graph's |
| 374 | * connection to the actual bottom commit is not viewed as a side branch, but |
| 375 | * treated as part of the graph. For example: |
| 376 | * |
| 377 | * ....Z...A---X---o---o---B |
| 378 | * . / |
| 379 | * W---Y |
| 380 | * |
| 381 | * When computing "A..B", the A-X connection is at least as important as |
| 382 | * Y-X, despite A being flagged UNINTERESTING. |
| 383 | * |
| 384 | * And when computing --ancestry-path "A..B", the A-X connection is more |
| 385 | * important than Y-X, despite both A and Y being flagged UNINTERESTING. |
| 386 | */ |
| 387 | static inline int relevant_commit(struct commit *commit) |
| 388 | { |
| 389 | return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Return a single relevant commit from a parent list. If we are a TREESAME |
| 394 | * commit, and this selects one of our parents, then we can safely simplify to |
| 395 | * that parent. |
| 396 | */ |
| 397 | static struct commit *one_relevant_parent(const struct rev_info *revs, |
| 398 | struct commit_list *orig) |
| 399 | { |
| 400 | struct commit_list *list = orig; |
| 401 | struct commit *relevant = NULL; |
| 402 | |
| 403 | if (!orig) |
| 404 | return NULL; |
| 405 | |
| 406 | /* |
| 407 | * For 1-parent commits, or if first-parent-only, then return that |
| 408 | * first parent (even if not "relevant" by the above definition). |
| 409 | * TREESAME will have been set purely on that parent. |
| 410 | */ |
| 411 | if (revs->first_parent_only || !orig->next) |
| 412 | return orig->item; |
| 413 | |
| 414 | /* |
| 415 | * For multi-parent commits, identify a sole relevant parent, if any. |
| 416 | * If we have only one relevant parent, then TREESAME will be set purely |
| 417 | * with regard to that parent, and we can simplify accordingly. |
| 418 | * |
| 419 | * If we have more than one relevant parent, or no relevant parents |
| 420 | * (and multiple irrelevant ones), then we can't select a parent here |
| 421 | * and return NULL. |
| 422 | */ |
| 423 | while (list) { |
| 424 | struct commit *commit = list->item; |
| 425 | list = list->next; |
| 426 | if (relevant_commit(commit)) { |
| 427 | if (relevant) |
| 428 | return NULL; |
| 429 | relevant = commit; |
| 430 | } |
| 431 | } |
| 432 | return relevant; |
| 433 | } |
| 434 | |
| 435 | /* |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 436 | * 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] | 437 | * diff consists of all '+' (and no other changes), REV_TREE_OLD |
| 438 | * if the whole diff is removal of old data, and otherwise |
| 439 | * REV_TREE_DIFFERENT (of course if the trees are the same we |
| 440 | * want REV_TREE_SAME). |
| 441 | * That means that once we get to REV_TREE_DIFFERENT, we do not |
| 442 | * have to look any further. |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 443 | */ |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 444 | static int tree_difference = REV_TREE_SAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 445 | |
| 446 | static void file_add_remove(struct diff_options *options, |
| 447 | int addremove, unsigned mode, |
| 448 | const unsigned char *sha1, |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 449 | int sha1_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 450 | const char *fullpath, unsigned dirty_submodule) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 451 | { |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 452 | int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 453 | |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 454 | tree_difference |= diff; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 455 | if (tree_difference == REV_TREE_DIFFERENT) |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 456 | DIFF_OPT_SET(options, HAS_CHANGES); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void file_change(struct diff_options *options, |
| 460 | unsigned old_mode, unsigned new_mode, |
| 461 | const unsigned char *old_sha1, |
| 462 | const unsigned char *new_sha1, |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 463 | int old_sha1_valid, int new_sha1_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 464 | const char *fullpath, |
| 465 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 466 | { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 467 | tree_difference = REV_TREE_DIFFERENT; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 468 | DIFF_OPT_SET(options, HAS_CHANGES); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Michael Haggerty | ff5f5f2 | 2013-05-25 11:08:07 +0200 | [diff] [blame] | 471 | static int rev_compare_tree(struct rev_info *revs, |
| 472 | struct commit *parent, struct commit *commit) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 473 | { |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 474 | struct tree *t1 = parent->tree; |
| 475 | struct tree *t2 = commit->tree; |
| 476 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 477 | if (!t1) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 478 | return REV_TREE_NEW; |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 479 | if (!t2) |
| 480 | return REV_TREE_OLD; |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 481 | |
| 482 | if (revs->simplify_by_decoration) { |
| 483 | /* |
| 484 | * If we are simplifying by decoration, then the commit |
| 485 | * is worth showing if it has a tag pointing at it. |
| 486 | */ |
Jeff King | 2608c24 | 2014-08-26 06:23:54 -0400 | [diff] [blame] | 487 | if (get_name_decoration(&commit->object)) |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 488 | return REV_TREE_DIFFERENT; |
| 489 | /* |
| 490 | * A commit that is not pointed by a tag is uninteresting |
| 491 | * if we are not limited by path. This means that you will |
| 492 | * see the usual "commits that touch the paths" plus any |
| 493 | * tagged commit by specifying both --simplify-by-decoration |
| 494 | * and pathspec. |
| 495 | */ |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 496 | if (!revs->prune_data.nr) |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 497 | return REV_TREE_SAME; |
| 498 | } |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 499 | |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 500 | tree_difference = REV_TREE_SAME; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 501 | DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES); |
Junio C Hamano | c4e05b1 | 2006-04-10 18:14:54 -0700 | [diff] [blame] | 502 | if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 503 | &revs->pruning) < 0) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 504 | return REV_TREE_DIFFERENT; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 505 | return tree_difference; |
| 506 | } |
| 507 | |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 508 | 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] | 509 | { |
| 510 | int retval; |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 511 | struct tree *t1 = commit->tree; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 512 | |
| 513 | if (!t1) |
| 514 | return 0; |
| 515 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 516 | tree_difference = REV_TREE_SAME; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 517 | DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES); |
Kirill Smelkov | 6275c91 | 2014-02-05 20:57:12 +0400 | [diff] [blame] | 518 | retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 519 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 520 | return retval >= 0 && (tree_difference == REV_TREE_SAME); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 523 | struct treesame_state { |
| 524 | unsigned int nparents; |
| 525 | unsigned char treesame[FLEX_ARRAY]; |
| 526 | }; |
| 527 | |
| 528 | static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit) |
| 529 | { |
| 530 | unsigned n = commit_list_count(commit->parents); |
| 531 | struct treesame_state *st = xcalloc(1, sizeof(*st) + n); |
| 532 | st->nparents = n; |
| 533 | add_decoration(&revs->treesame, &commit->object, st); |
| 534 | return st; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Must be called immediately after removing the nth_parent from a commit's |
| 539 | * parent list, if we are maintaining the per-parent treesame[] decoration. |
| 540 | * This does not recalculate the master TREESAME flag - update_treesame() |
| 541 | * should be called to update it after a sequence of treesame[] modifications |
| 542 | * that may have affected it. |
| 543 | */ |
| 544 | static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent) |
| 545 | { |
| 546 | struct treesame_state *st; |
| 547 | int old_same; |
| 548 | |
| 549 | if (!commit->parents) { |
| 550 | /* |
| 551 | * Have just removed the only parent from a non-merge. |
| 552 | * Different handling, as we lack decoration. |
| 553 | */ |
| 554 | if (nth_parent != 0) |
| 555 | die("compact_treesame %u", nth_parent); |
| 556 | old_same = !!(commit->object.flags & TREESAME); |
| 557 | if (rev_same_tree_as_empty(revs, commit)) |
| 558 | commit->object.flags |= TREESAME; |
| 559 | else |
| 560 | commit->object.flags &= ~TREESAME; |
| 561 | return old_same; |
| 562 | } |
| 563 | |
| 564 | st = lookup_decoration(&revs->treesame, &commit->object); |
| 565 | if (!st || nth_parent >= st->nparents) |
| 566 | die("compact_treesame %u", nth_parent); |
| 567 | |
| 568 | old_same = st->treesame[nth_parent]; |
| 569 | memmove(st->treesame + nth_parent, |
| 570 | st->treesame + nth_parent + 1, |
| 571 | st->nparents - nth_parent - 1); |
| 572 | |
| 573 | /* |
| 574 | * If we've just become a non-merge commit, update TREESAME |
| 575 | * immediately, and remove the no-longer-needed decoration. |
| 576 | * If still a merge, defer update until update_treesame(). |
| 577 | */ |
| 578 | if (--st->nparents == 1) { |
| 579 | if (commit->parents->next) |
| 580 | die("compact_treesame parents mismatch"); |
| 581 | if (st->treesame[0] && revs->dense) |
| 582 | commit->object.flags |= TREESAME; |
| 583 | else |
| 584 | commit->object.flags &= ~TREESAME; |
| 585 | free(add_decoration(&revs->treesame, &commit->object, NULL)); |
| 586 | } |
| 587 | |
| 588 | return old_same; |
| 589 | } |
| 590 | |
| 591 | static unsigned update_treesame(struct rev_info *revs, struct commit *commit) |
| 592 | { |
| 593 | if (commit->parents && commit->parents->next) { |
| 594 | unsigned n; |
| 595 | struct treesame_state *st; |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 596 | struct commit_list *p; |
| 597 | unsigned relevant_parents; |
| 598 | unsigned relevant_change, irrelevant_change; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 599 | |
| 600 | st = lookup_decoration(&revs->treesame, &commit->object); |
| 601 | if (!st) |
| 602 | die("update_treesame %s", sha1_to_hex(commit->object.sha1)); |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 603 | relevant_parents = 0; |
| 604 | relevant_change = irrelevant_change = 0; |
| 605 | for (p = commit->parents, n = 0; p; n++, p = p->next) { |
| 606 | if (relevant_commit(p->item)) { |
| 607 | relevant_change |= !st->treesame[n]; |
| 608 | relevant_parents++; |
| 609 | } else |
| 610 | irrelevant_change |= !st->treesame[n]; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 611 | } |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 612 | if (relevant_parents ? relevant_change : irrelevant_change) |
| 613 | commit->object.flags &= ~TREESAME; |
| 614 | else |
| 615 | commit->object.flags |= TREESAME; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | return commit->object.flags & TREESAME; |
| 619 | } |
| 620 | |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 621 | static inline int limiting_can_increase_treesame(const struct rev_info *revs) |
| 622 | { |
| 623 | /* |
| 624 | * TREESAME is irrelevant unless prune && dense; |
| 625 | * if simplify_history is set, we can't have a mixture of TREESAME and |
| 626 | * !TREESAME INTERESTING parents (and we don't have treesame[] |
| 627 | * decoration anyway); |
| 628 | * if first_parent_only is set, then the TREESAME flag is locked |
| 629 | * against the first parent (and again we lack treesame[] decoration). |
| 630 | */ |
| 631 | return revs->prune && revs->dense && |
| 632 | !revs->simplify_history && |
| 633 | !revs->first_parent_only; |
| 634 | } |
| 635 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 636 | static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit) |
| 637 | { |
| 638 | struct commit_list **pp, *parent; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 639 | struct treesame_state *ts = NULL; |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 640 | int relevant_change = 0, irrelevant_change = 0; |
| 641 | int relevant_parents, nth_parent; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 642 | |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 643 | /* |
| 644 | * If we don't do pruning, everything is interesting |
| 645 | */ |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 646 | if (!revs->prune) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 647 | return; |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 648 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 649 | if (!commit->tree) |
| 650 | return; |
| 651 | |
| 652 | if (!commit->parents) { |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 653 | if (rev_same_tree_as_empty(revs, commit)) |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 654 | commit->object.flags |= TREESAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 658 | /* |
| 659 | * Normal non-merge commit? If we don't want to make the |
| 660 | * history dense, we consider it always to be a change.. |
| 661 | */ |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 662 | if (!revs->dense && !commit->parents->next) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 663 | return; |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 664 | |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 665 | for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 666 | (parent = *pp) != NULL; |
| 667 | pp = &parent->next, nth_parent++) { |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 668 | struct commit *p = parent->item; |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 669 | if (relevant_commit(p)) |
| 670 | relevant_parents++; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 671 | |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 672 | if (nth_parent == 1) { |
| 673 | /* |
| 674 | * This our second loop iteration - so we now know |
| 675 | * we're dealing with a merge. |
| 676 | * |
| 677 | * Do not compare with later parents when we care only about |
| 678 | * the first parent chain, in order to avoid derailing the |
| 679 | * traversal to follow a side branch that brought everything |
| 680 | * in the path we are limited to by the pathspec. |
| 681 | */ |
| 682 | if (revs->first_parent_only) |
| 683 | break; |
| 684 | /* |
| 685 | * If this will remain a potentially-simplifiable |
| 686 | * merge, remember per-parent treesame if needed. |
| 687 | * Initialise the array with the comparison from our |
| 688 | * first iteration. |
| 689 | */ |
| 690 | if (revs->treesame.name && |
| 691 | !revs->simplify_history && |
| 692 | !(commit->object.flags & UNINTERESTING)) { |
| 693 | ts = initialise_treesame(revs, commit); |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 694 | if (!(irrelevant_change || relevant_change)) |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 695 | ts->treesame[0] = 1; |
| 696 | } |
| 697 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 698 | if (parse_commit(p) < 0) |
| 699 | die("cannot simplify commit %s (because of %s)", |
| 700 | sha1_to_hex(commit->object.sha1), |
| 701 | sha1_to_hex(p->object.sha1)); |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 702 | switch (rev_compare_tree(revs, p, commit)) { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 703 | case REV_TREE_SAME: |
Kevin Bracey | 141efdb | 2013-05-16 18:32:41 +0300 | [diff] [blame] | 704 | if (!revs->simplify_history || !relevant_commit(p)) { |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 705 | /* Even if a merge with an uninteresting |
| 706 | * side branch brought the entire change |
| 707 | * we are interested in, we do not want |
| 708 | * to lose the other branches of this |
| 709 | * merge, so we just keep going. |
| 710 | */ |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 711 | if (ts) |
| 712 | ts->treesame[nth_parent] = 1; |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 713 | continue; |
| 714 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 715 | parent->next = NULL; |
| 716 | commit->parents = parent; |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 717 | commit->object.flags |= TREESAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 718 | return; |
| 719 | |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 720 | case REV_TREE_NEW: |
| 721 | if (revs->remove_empty_trees && |
Linus Torvalds | 3a5e860 | 2008-11-03 10:45:41 -0800 | [diff] [blame] | 722 | rev_same_tree_as_empty(revs, p)) { |
Junio C Hamano | c348f31 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 723 | /* We are adding all the specified |
| 724 | * paths from this parent, so the |
| 725 | * history beyond this parent is not |
| 726 | * interesting. Remove its parents |
| 727 | * (they are grandparents for us). |
| 728 | * IOW, we pretend this parent is a |
| 729 | * "root" commit. |
Junio C Hamano | a41e109 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 730 | */ |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 731 | if (parse_commit(p) < 0) |
| 732 | die("cannot simplify commit %s (invalid %s)", |
| 733 | sha1_to_hex(commit->object.sha1), |
| 734 | sha1_to_hex(p->object.sha1)); |
Junio C Hamano | c348f31 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 735 | p->parents = NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 736 | } |
| 737 | /* fallthrough */ |
Linus Torvalds | ceff8e7 | 2009-06-02 18:34:01 -0700 | [diff] [blame] | 738 | case REV_TREE_OLD: |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 739 | case REV_TREE_DIFFERENT: |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 740 | if (relevant_commit(p)) |
| 741 | relevant_change = 1; |
| 742 | else |
| 743 | irrelevant_change = 1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 744 | continue; |
| 745 | } |
| 746 | die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1)); |
| 747 | } |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * TREESAME is straightforward for single-parent commits. For merge |
| 751 | * commits, it is most useful to define it so that "irrelevant" |
| 752 | * parents cannot make us !TREESAME - if we have any relevant |
| 753 | * parents, then we only consider TREESAMEness with respect to them, |
| 754 | * allowing irrelevant merges from uninteresting branches to be |
| 755 | * simplified away. Only if we have only irrelevant parents do we |
| 756 | * base TREESAME on them. Note that this logic is replicated in |
| 757 | * update_treesame, which should be kept in sync. |
| 758 | */ |
| 759 | if (relevant_parents ? !relevant_change : !irrelevant_change) |
| 760 | commit->object.flags |= TREESAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 763 | 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] | 764 | struct commit_list *cached_base, struct commit_list **cache) |
| 765 | { |
| 766 | struct commit_list *new_entry; |
| 767 | |
| 768 | if (cached_base && p->date < cached_base->item->date) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 769 | new_entry = commit_list_insert_by_date(p, &cached_base->next); |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 770 | else |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 771 | new_entry = commit_list_insert_by_date(p, head); |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 772 | |
| 773 | if (cache && (!*cache || p->date < (*cache)->item->date)) |
| 774 | *cache = new_entry; |
| 775 | } |
| 776 | |
| 777 | static int add_parents_to_list(struct rev_info *revs, struct commit *commit, |
| 778 | struct commit_list **list, struct commit_list **cache_ptr) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 779 | { |
| 780 | struct commit_list *parent = commit->parents; |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 781 | unsigned left_flag; |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 782 | struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 783 | |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 784 | if (commit->object.flags & ADDED) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 785 | return 0; |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 786 | commit->object.flags |= ADDED; |
| 787 | |
Vicent Marti | a330de3 | 2013-10-24 14:01:41 -0400 | [diff] [blame] | 788 | if (revs->include_check && |
| 789 | !revs->include_check(commit, revs->include_check_data)) |
| 790 | return 0; |
| 791 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 792 | /* |
| 793 | * If the commit is uninteresting, don't try to |
| 794 | * prune parents - we want the maximal uninteresting |
| 795 | * set. |
| 796 | * |
| 797 | * Normally we haven't parsed the parent |
| 798 | * yet, so we won't have a parent of a parent |
| 799 | * here. However, it may turn out that we've |
| 800 | * reached this commit some other way (where it |
| 801 | * wasn't uninteresting), in which case we need |
| 802 | * to mark its parents recursively too.. |
| 803 | */ |
| 804 | if (commit->object.flags & UNINTERESTING) { |
| 805 | while (parent) { |
| 806 | struct commit *p = parent->item; |
| 807 | parent = parent->next; |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 808 | if (p) |
| 809 | p->object.flags |= UNINTERESTING; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 810 | if (parse_commit(p) < 0) |
Junio C Hamano | aeeae1b | 2009-01-27 23:19:30 -0800 | [diff] [blame] | 811 | continue; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 812 | if (p->parents) |
| 813 | mark_parents_uninteresting(p); |
| 814 | if (p->object.flags & SEEN) |
| 815 | continue; |
| 816 | p->object.flags |= SEEN; |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 817 | commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 818 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 819 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Ok, the commit wasn't uninteresting. Try to |
| 824 | * simplify the commit history and find the parent |
| 825 | * that has no differences in the path set if one exists. |
| 826 | */ |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 827 | try_to_simplify_commit(revs, commit); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 828 | |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 829 | if (revs->no_walk) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 830 | return 0; |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 831 | |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 832 | left_flag = (commit->object.flags & SYMMETRIC_LEFT); |
Junio C Hamano | 0053e90 | 2007-03-13 01:57:22 -0700 | [diff] [blame] | 833 | |
Stephen R. van den Berg | d9c292e | 2008-04-27 19:32:46 +0200 | [diff] [blame] | 834 | for (parent = commit->parents; parent; parent = parent->next) { |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 835 | struct commit *p = parent->item; |
| 836 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 837 | if (parse_commit(p) < 0) |
| 838 | return -1; |
Linus Torvalds | 0f3a290 | 2008-10-27 12:51:59 -0700 | [diff] [blame] | 839 | if (revs->show_source && !p->util) |
| 840 | p->util = commit->util; |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 841 | p->object.flags |= left_flag; |
Lars Hjemli | ad1012e | 2008-05-12 17:12:36 +0200 | [diff] [blame] | 842 | if (!(p->object.flags & SEEN)) { |
| 843 | p->object.flags |= SEEN; |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 844 | commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr); |
Lars Hjemli | ad1012e | 2008-05-12 17:12:36 +0200 | [diff] [blame] | 845 | } |
Junio C Hamano | 60d30b0 | 2008-07-31 22:17:13 -0700 | [diff] [blame] | 846 | if (revs->first_parent_only) |
Stephen R. van den Berg | d9c292e | 2008-04-27 19:32:46 +0200 | [diff] [blame] | 847 | break; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 848 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 849 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 852 | 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] | 853 | { |
| 854 | struct commit_list *p; |
| 855 | int left_count = 0, right_count = 0; |
| 856 | int left_first; |
| 857 | struct patch_ids ids; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 858 | unsigned cherry_flag; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 859 | |
| 860 | /* First count the commits on the left and on the right */ |
| 861 | for (p = list; p; p = p->next) { |
| 862 | struct commit *commit = p->item; |
| 863 | unsigned flags = commit->object.flags; |
| 864 | if (flags & BOUNDARY) |
| 865 | ; |
| 866 | else if (flags & SYMMETRIC_LEFT) |
| 867 | left_count++; |
| 868 | else |
| 869 | right_count++; |
| 870 | } |
| 871 | |
Thomas Rast | 36c0797 | 2010-02-20 12:42:04 +0100 | [diff] [blame] | 872 | if (!left_count || !right_count) |
| 873 | return; |
| 874 | |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 875 | left_first = left_count < right_count; |
| 876 | init_patch_ids(&ids); |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 877 | ids.diffopts.pathspec = revs->diffopt.pathspec; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 878 | |
| 879 | /* Compute patch-ids for one side */ |
| 880 | for (p = list; p; p = p->next) { |
| 881 | struct commit *commit = p->item; |
| 882 | unsigned flags = commit->object.flags; |
| 883 | |
| 884 | if (flags & BOUNDARY) |
| 885 | continue; |
| 886 | /* |
| 887 | * If we have fewer left, left_first is set and we omit |
| 888 | * commits on the right branch in this loop. If we have |
| 889 | * fewer right, we skip the left ones. |
| 890 | */ |
| 891 | if (left_first != !!(flags & SYMMETRIC_LEFT)) |
| 892 | continue; |
| 893 | commit->util = add_commit_patch_id(commit, &ids); |
| 894 | } |
| 895 | |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 896 | /* either cherry_mark or cherry_pick are true */ |
| 897 | cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN; |
| 898 | |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 899 | /* Check the other side */ |
| 900 | for (p = list; p; p = p->next) { |
| 901 | struct commit *commit = p->item; |
| 902 | struct patch_id *id; |
| 903 | unsigned flags = commit->object.flags; |
| 904 | |
| 905 | if (flags & BOUNDARY) |
| 906 | continue; |
| 907 | /* |
| 908 | * If we have fewer left, left_first is set and we omit |
| 909 | * commits on the left branch in this loop. |
| 910 | */ |
| 911 | if (left_first == !!(flags & SYMMETRIC_LEFT)) |
| 912 | continue; |
| 913 | |
| 914 | /* |
| 915 | * Have we seen the same patch id? |
| 916 | */ |
| 917 | id = has_commit_patch_id(commit, &ids); |
| 918 | if (!id) |
| 919 | continue; |
| 920 | id->seen = 1; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 921 | commit->object.flags |= cherry_flag; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* Now check the original side for seen ones */ |
| 925 | for (p = list; p; p = p->next) { |
| 926 | struct commit *commit = p->item; |
| 927 | struct patch_id *ent; |
| 928 | |
| 929 | ent = commit->util; |
| 930 | if (!ent) |
| 931 | continue; |
| 932 | if (ent->seen) |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 933 | commit->object.flags |= cherry_flag; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 934 | commit->util = NULL; |
| 935 | } |
| 936 | |
| 937 | free_patch_ids(&ids); |
| 938 | } |
| 939 | |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 940 | /* How many extra uninteresting commits we want to see.. */ |
| 941 | #define SLOP 5 |
| 942 | |
| 943 | 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] | 944 | { |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 945 | /* |
| 946 | * No source list at all? We're definitely done.. |
| 947 | */ |
| 948 | if (!src) |
| 949 | return 0; |
| 950 | |
| 951 | /* |
| 952 | * Does the destination list contain entries with a date |
| 953 | * before the source list? Definitely _not_ done. |
| 954 | */ |
Kacper Kornet | c19d1b4 | 2013-03-22 19:38:19 +0100 | [diff] [blame] | 955 | if (date <= src->item->date) |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 956 | return SLOP; |
| 957 | |
| 958 | /* |
| 959 | * Does the source list still have interesting commits in |
| 960 | * it? Definitely not done.. |
| 961 | */ |
| 962 | if (!everybody_uninteresting(src)) |
| 963 | return SLOP; |
| 964 | |
| 965 | /* Ok, we're closing in.. */ |
| 966 | return slop-1; |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 969 | /* |
| 970 | * "rev-list --ancestry-path A..B" computes commits that are ancestors |
| 971 | * of B but not ancestors of A but further limits the result to those |
| 972 | * that are descendants of A. This takes the list of bottom commits and |
| 973 | * the result of "A..B" without --ancestry-path, and limits the latter |
| 974 | * further to the ones that can reach one of the commits in "bottom". |
| 975 | */ |
| 976 | static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list) |
| 977 | { |
| 978 | struct commit_list *p; |
| 979 | struct commit_list *rlist = NULL; |
| 980 | int made_progress; |
| 981 | |
| 982 | /* |
| 983 | * Reverse the list so that it will be likely that we would |
| 984 | * process parents before children. |
| 985 | */ |
| 986 | for (p = list; p; p = p->next) |
| 987 | commit_list_insert(p->item, &rlist); |
| 988 | |
| 989 | for (p = bottom; p; p = p->next) |
| 990 | p->item->object.flags |= TMP_MARK; |
| 991 | |
| 992 | /* |
| 993 | * Mark the ones that can reach bottom commits in "list", |
| 994 | * in a bottom-up fashion. |
| 995 | */ |
| 996 | do { |
| 997 | made_progress = 0; |
| 998 | for (p = rlist; p; p = p->next) { |
| 999 | struct commit *c = p->item; |
| 1000 | struct commit_list *parents; |
| 1001 | if (c->object.flags & (TMP_MARK | UNINTERESTING)) |
| 1002 | continue; |
| 1003 | for (parents = c->parents; |
| 1004 | parents; |
| 1005 | parents = parents->next) { |
| 1006 | if (!(parents->item->object.flags & TMP_MARK)) |
| 1007 | continue; |
| 1008 | c->object.flags |= TMP_MARK; |
| 1009 | made_progress = 1; |
| 1010 | break; |
| 1011 | } |
| 1012 | } |
| 1013 | } while (made_progress); |
| 1014 | |
| 1015 | /* |
| 1016 | * NEEDSWORK: decide if we want to remove parents that are |
| 1017 | * not marked with TMP_MARK from commit->parents for commits |
| 1018 | * in the resulting list. We may not want to do that, though. |
| 1019 | */ |
| 1020 | |
| 1021 | /* |
| 1022 | * The ones that are not marked with TMP_MARK are uninteresting |
| 1023 | */ |
| 1024 | for (p = list; p; p = p->next) { |
| 1025 | struct commit *c = p->item; |
| 1026 | if (c->object.flags & TMP_MARK) |
| 1027 | continue; |
| 1028 | c->object.flags |= UNINTERESTING; |
| 1029 | } |
| 1030 | |
| 1031 | /* We are done with the TMP_MARK */ |
| 1032 | for (p = list; p; p = p->next) |
| 1033 | p->item->object.flags &= ~TMP_MARK; |
| 1034 | for (p = bottom; p; p = p->next) |
| 1035 | p->item->object.flags &= ~TMP_MARK; |
| 1036 | free_commit_list(rlist); |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Before walking the history, keep the set of "negative" refs the |
| 1041 | * caller has asked to exclude. |
| 1042 | * |
| 1043 | * This is used to compute "rev-list --ancestry-path A..B", as we need |
| 1044 | * to filter the result of "A..B" further to the ones that can actually |
| 1045 | * reach A. |
| 1046 | */ |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1047 | static struct commit_list *collect_bottom_commits(struct commit_list *list) |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1048 | { |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1049 | struct commit_list *elem, *bottom = NULL; |
| 1050 | for (elem = list; elem; elem = elem->next) |
| 1051 | if (elem->item->object.flags & BOTTOM) |
| 1052 | commit_list_insert(elem->item, &bottom); |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1053 | return bottom; |
| 1054 | } |
| 1055 | |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1056 | /* Assumes either left_only or right_only is set */ |
| 1057 | static void limit_left_right(struct commit_list *list, struct rev_info *revs) |
| 1058 | { |
| 1059 | struct commit_list *p; |
| 1060 | |
| 1061 | for (p = list; p; p = p->next) { |
| 1062 | struct commit *commit = p->item; |
| 1063 | |
| 1064 | if (revs->right_only) { |
| 1065 | if (commit->object.flags & SYMMETRIC_LEFT) |
| 1066 | commit->object.flags |= SHOWN; |
| 1067 | } else /* revs->left_only is set */ |
| 1068 | if (!(commit->object.flags & SYMMETRIC_LEFT)) |
| 1069 | commit->object.flags |= SHOWN; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1073 | static int limit_list(struct rev_info *revs) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1074 | { |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 1075 | int slop = SLOP; |
| 1076 | unsigned long date = ~0ul; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1077 | struct commit_list *list = revs->commits; |
| 1078 | struct commit_list *newlist = NULL; |
| 1079 | struct commit_list **p = &newlist; |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1080 | struct commit_list *bottom = NULL; |
| 1081 | |
| 1082 | if (revs->ancestry_path) { |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1083 | bottom = collect_bottom_commits(list); |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1084 | if (!bottom) |
Johan Herland | 97b03c3 | 2010-06-04 01:17:36 +0200 | [diff] [blame] | 1085 | die("--ancestry-path given but there are no bottom commits"); |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1086 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1087 | |
| 1088 | while (list) { |
| 1089 | struct commit_list *entry = list; |
| 1090 | struct commit *commit = list->item; |
| 1091 | struct object *obj = &commit->object; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 1092 | show_early_output_fn_t show; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1093 | |
| 1094 | list = list->next; |
| 1095 | free(entry); |
| 1096 | |
| 1097 | if (revs->max_age != -1 && (commit->date < revs->max_age)) |
| 1098 | obj->flags |= UNINTERESTING; |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 1099 | if (add_parents_to_list(revs, commit, &list, NULL) < 0) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1100 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1101 | if (obj->flags & UNINTERESTING) { |
| 1102 | mark_parents_uninteresting(commit); |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 1103 | if (revs->show_all) |
| 1104 | p = &commit_list_insert(commit, p)->next; |
| 1105 | slop = still_interesting(list, date, slop); |
| 1106 | if (slop) |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 1107 | continue; |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 1108 | /* If showing all, add the whole pending list to the end */ |
| 1109 | if (revs->show_all) |
| 1110 | *p = list; |
| 1111 | break; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1112 | } |
| 1113 | if (revs->min_age != -1 && (commit->date > revs->min_age)) |
| 1114 | continue; |
Linus Torvalds | 7d00419 | 2008-03-17 18:56:33 -0700 | [diff] [blame] | 1115 | date = commit->date; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1116 | p = &commit_list_insert(commit, p)->next; |
Linus Torvalds | cdcefbc | 2007-11-03 11:11:10 -0700 | [diff] [blame] | 1117 | |
| 1118 | show = show_early_output; |
| 1119 | if (!show) |
| 1120 | continue; |
| 1121 | |
| 1122 | show(revs, newlist); |
| 1123 | show_early_output = NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1124 | } |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 1125 | if (revs->cherry_pick || revs->cherry_mark) |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 1126 | cherry_pick_list(newlist, revs); |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 1127 | |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1128 | if (revs->left_only || revs->right_only) |
| 1129 | limit_left_right(newlist, revs); |
| 1130 | |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1131 | if (bottom) { |
| 1132 | limit_to_ancestry(bottom, newlist); |
| 1133 | free_commit_list(bottom); |
| 1134 | } |
| 1135 | |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 1136 | /* |
| 1137 | * Check if any commits have become TREESAME by some of their parents |
| 1138 | * becoming UNINTERESTING. |
| 1139 | */ |
| 1140 | if (limiting_can_increase_treesame(revs)) |
| 1141 | for (list = newlist; list; list = list->next) { |
| 1142 | struct commit *c = list->item; |
| 1143 | if (c->object.flags & (UNINTERESTING | TREESAME)) |
| 1144 | continue; |
| 1145 | update_treesame(revs, c); |
| 1146 | } |
| 1147 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1148 | revs->commits = newlist; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1149 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Michael Haggerty | df835d3 | 2013-05-25 11:08:02 +0200 | [diff] [blame] | 1152 | /* |
| 1153 | * Add an entry to refs->cmdline with the specified information. |
| 1154 | * *name is copied. |
| 1155 | */ |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1156 | static void add_rev_cmdline(struct rev_info *revs, |
| 1157 | struct object *item, |
| 1158 | const char *name, |
| 1159 | int whence, |
| 1160 | unsigned flags) |
| 1161 | { |
| 1162 | struct rev_cmdline_info *info = &revs->cmdline; |
| 1163 | int nr = info->nr; |
| 1164 | |
| 1165 | ALLOC_GROW(info->rev, nr + 1, info->alloc); |
| 1166 | info->rev[nr].item = item; |
Michael Haggerty | df835d3 | 2013-05-25 11:08:02 +0200 | [diff] [blame] | 1167 | info->rev[nr].name = xstrdup(name); |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1168 | info->rev[nr].whence = whence; |
| 1169 | info->rev[nr].flags = flags; |
| 1170 | info->nr++; |
| 1171 | } |
| 1172 | |
Kevin Bracey | a765499 | 2013-05-13 18:00:47 +0300 | [diff] [blame] | 1173 | static void add_rev_cmdline_list(struct rev_info *revs, |
| 1174 | struct commit_list *commit_list, |
| 1175 | int whence, |
| 1176 | unsigned flags) |
| 1177 | { |
| 1178 | while (commit_list) { |
| 1179 | struct object *object = &commit_list->item->object; |
| 1180 | add_rev_cmdline(revs, object, sha1_to_hex(object->sha1), |
| 1181 | whence, flags); |
| 1182 | commit_list = commit_list->next; |
| 1183 | } |
| 1184 | } |
| 1185 | |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1186 | struct all_refs_cb { |
| 1187 | int all_flags; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1188 | int warned_bad_reflog; |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1189 | struct rev_info *all_revs; |
| 1190 | const char *name_for_errormsg; |
| 1191 | }; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1192 | |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1193 | int ref_excluded(struct string_list *ref_excludes, const char *path) |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1194 | { |
| 1195 | struct string_list_item *item; |
| 1196 | |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1197 | if (!ref_excludes) |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1198 | return 0; |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1199 | for_each_string_list_item(item, ref_excludes) { |
Nguyễn Thái Ngọc Duy | eb07894 | 2014-02-15 09:01:46 +0700 | [diff] [blame] | 1200 | if (!wildmatch(item->string, path, 0, NULL)) |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1201 | return 1; |
| 1202 | } |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 1206 | 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] | 1207 | { |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1208 | struct all_refs_cb *cb = cb_data; |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1209 | struct object *object; |
| 1210 | |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1211 | if (ref_excluded(cb->all_revs->ref_excludes, path)) |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1212 | return 0; |
| 1213 | |
| 1214 | object = get_reference(cb->all_revs, path, sha1, cb->all_flags); |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1215 | 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] | 1216 | add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1217 | return 0; |
| 1218 | } |
| 1219 | |
Ilari Liusvaara | d08bae7 | 2010-01-20 11:48:25 +0200 | [diff] [blame] | 1220 | static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs, |
| 1221 | unsigned flags) |
| 1222 | { |
| 1223 | cb->all_revs = revs; |
| 1224 | cb->all_flags = flags; |
| 1225 | } |
| 1226 | |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1227 | void clear_ref_exclusion(struct string_list **ref_excludes_p) |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1228 | { |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1229 | if (*ref_excludes_p) { |
| 1230 | string_list_clear(*ref_excludes_p, 0); |
| 1231 | free(*ref_excludes_p); |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1232 | } |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1233 | *ref_excludes_p = NULL; |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1236 | void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude) |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1237 | { |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1238 | if (!*ref_excludes_p) { |
| 1239 | *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p)); |
| 1240 | (*ref_excludes_p)->strdup_strings = 1; |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1241 | } |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 1242 | string_list_append(*ref_excludes_p, exclude); |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1245 | static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags, |
| 1246 | int (*for_each)(const char *, each_ref_fn, void *)) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1247 | { |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1248 | struct all_refs_cb cb; |
Ilari Liusvaara | d08bae7 | 2010-01-20 11:48:25 +0200 | [diff] [blame] | 1249 | init_all_refs_cb(&cb, revs, flags); |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 1250 | for_each(submodule, handle_one_ref, &cb); |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1253 | 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] | 1254 | { |
| 1255 | struct all_refs_cb *cb = cb_data; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1256 | if (!is_null_sha1(sha1)) { |
| 1257 | struct object *o = parse_object(sha1); |
| 1258 | if (o) { |
| 1259 | o->flags |= cb->all_flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1260 | /* ??? CMDLINEFLAGS ??? */ |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1261 | add_pending_object(cb->all_revs, o, ""); |
| 1262 | } |
| 1263 | else if (!cb->warned_bad_reflog) { |
Theodore Ts'o | 46efd2d | 2007-03-30 19:07:05 -0400 | [diff] [blame] | 1264 | warning("reflog of '%s' references pruned commits", |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1265 | cb->name_for_errormsg); |
| 1266 | cb->warned_bad_reflog = 1; |
| 1267 | } |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1268 | } |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1269 | } |
| 1270 | |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 1271 | static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
| 1272 | const char *email, unsigned long timestamp, int tz, |
| 1273 | const char *message, void *cb_data) |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1274 | { |
| 1275 | handle_one_reflog_commit(osha1, cb_data); |
| 1276 | handle_one_reflog_commit(nsha1, cb_data); |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
| 1281 | { |
| 1282 | struct all_refs_cb *cb = cb_data; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 1283 | cb->warned_bad_reflog = 0; |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1284 | cb->name_for_errormsg = path; |
| 1285 | for_each_reflog_ent(path, handle_one_reflog_ent, cb_data); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Jeff King | 718ccc9 | 2014-10-15 18:38:31 -0400 | [diff] [blame] | 1289 | void add_reflogs_to_pending(struct rev_info *revs, unsigned flags) |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 1290 | { |
| 1291 | struct all_refs_cb cb; |
| 1292 | cb.all_revs = revs; |
| 1293 | cb.all_flags = flags; |
Junio C Hamano | 25b51e2 | 2007-02-12 23:06:54 -0800 | [diff] [blame] | 1294 | for_each_reflog(handle_one_reflog, &cb); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1295 | } |
| 1296 | |
Jeff King | 4fe1021 | 2014-10-16 20:44:23 -0400 | [diff] [blame] | 1297 | static void add_cache_tree(struct cache_tree *it, struct rev_info *revs, |
| 1298 | struct strbuf *path) |
| 1299 | { |
| 1300 | size_t baselen = path->len; |
| 1301 | int i; |
| 1302 | |
| 1303 | if (it->entry_count >= 0) { |
| 1304 | struct tree *tree = lookup_tree(it->sha1); |
| 1305 | add_pending_object_with_path(revs, &tree->object, "", |
| 1306 | 040000, path->buf); |
| 1307 | } |
| 1308 | |
| 1309 | for (i = 0; i < it->subtree_nr; i++) { |
| 1310 | struct cache_tree_sub *sub = it->down[i]; |
| 1311 | strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name); |
| 1312 | add_cache_tree(sub->cache_tree, revs, path); |
| 1313 | strbuf_setlen(path, baselen); |
| 1314 | } |
| 1315 | |
| 1316 | } |
| 1317 | |
| 1318 | void add_index_objects_to_pending(struct rev_info *revs, unsigned flags) |
| 1319 | { |
| 1320 | int i; |
| 1321 | |
| 1322 | read_cache(); |
| 1323 | for (i = 0; i < active_nr; i++) { |
| 1324 | struct cache_entry *ce = active_cache[i]; |
| 1325 | struct blob *blob; |
| 1326 | |
| 1327 | if (S_ISGITLINK(ce->ce_mode)) |
| 1328 | continue; |
| 1329 | |
| 1330 | blob = lookup_blob(ce->sha1); |
| 1331 | if (!blob) |
| 1332 | die("unable to add index blob to traversal"); |
| 1333 | add_pending_object_with_path(revs, &blob->object, "", |
| 1334 | ce->ce_mode, ce->name); |
| 1335 | } |
| 1336 | |
| 1337 | if (active_cache_tree) { |
| 1338 | struct strbuf path = STRBUF_INIT; |
| 1339 | add_cache_tree(active_cache_tree, revs, &path); |
| 1340 | strbuf_release(&path); |
| 1341 | } |
| 1342 | } |
| 1343 | |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1344 | 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] | 1345 | { |
| 1346 | unsigned char sha1[20]; |
| 1347 | struct object *it; |
| 1348 | struct commit *commit; |
| 1349 | struct commit_list *parents; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1350 | const char *arg = arg_; |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (*arg == '^') { |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1353 | flags ^= UNINTERESTING | BOTTOM; |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1354 | arg++; |
| 1355 | } |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1356 | if (get_sha1_committish(arg, sha1)) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1357 | return 0; |
| 1358 | while (1) { |
| 1359 | it = get_reference(revs, arg, sha1, 0); |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 1360 | if (!it && revs->ignore_missing) |
| 1361 | return 0; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1362 | if (it->type != OBJ_TAG) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1363 | break; |
Martin Koegler | 9684afd | 2008-02-18 21:48:01 +0100 | [diff] [blame] | 1364 | if (!((struct tag*)it)->tagged) |
| 1365 | return 0; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1366 | hashcpy(sha1, ((struct tag*)it)->tagged->sha1); |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1367 | } |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1368 | if (it->type != OBJ_COMMIT) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1369 | return 0; |
| 1370 | commit = (struct commit *)it; |
| 1371 | for (parents = commit->parents; parents; parents = parents->next) { |
| 1372 | it = &parents->item->object; |
| 1373 | it->flags |= flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1374 | add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags); |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 1375 | add_pending_object(revs, it, arg); |
| 1376 | } |
| 1377 | return 1; |
| 1378 | } |
| 1379 | |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 1380 | void init_revisions(struct rev_info *revs, const char *prefix) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1381 | { |
| 1382 | memset(revs, 0, sizeof(*revs)); |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 1383 | |
Junio C Hamano | 6b9c58f | 2006-04-15 23:46:36 -0700 | [diff] [blame] | 1384 | revs->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 1385 | revs->ignore_merges = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 1386 | revs->simplify_history = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 1387 | DIFF_OPT_SET(&revs->pruning, RECURSIVE); |
Junio C Hamano | 90b1994 | 2009-05-23 01:15:35 -0700 | [diff] [blame] | 1388 | DIFF_OPT_SET(&revs->pruning, QUICK); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1389 | revs->pruning.add_remove = file_add_remove; |
| 1390 | revs->pruning.change = file_change; |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 1391 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1392 | revs->dense = 1; |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 1393 | revs->prefix = prefix; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1394 | revs->max_age = -1; |
| 1395 | revs->min_age = -1; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1396 | revs->skip_count = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1397 | revs->max_count = -1; |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1398 | revs->max_parents = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1399 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1400 | revs->commit_format = CMIT_FMT_DEFAULT; |
| 1401 | |
Junio C Hamano | 918d4e1 | 2012-10-09 16:40:03 -0700 | [diff] [blame] | 1402 | init_grep_defaults(); |
| 1403 | grep_init(&revs->grep_filter, prefix); |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1404 | revs->grep_filter.status_only = 1; |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1405 | revs->grep_filter.regflags = REG_NEWLINE; |
| 1406 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1407 | diff_setup(&revs->diffopt); |
Junio C Hamano | c0cb4a0 | 2008-02-13 00:34:39 -0800 | [diff] [blame] | 1408 | if (prefix && !revs->diffopt.prefix) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 1409 | revs->diffopt.prefix = prefix; |
| 1410 | revs->diffopt.prefix_length = strlen(prefix); |
| 1411 | } |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1412 | |
| 1413 | revs->notes_opt.use_default_notes = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Rene Scharfe | 0d2c9d6 | 2006-07-02 01:29:37 +0200 | [diff] [blame] | 1416 | static void add_pending_commit_list(struct rev_info *revs, |
| 1417 | struct commit_list *commit_list, |
| 1418 | unsigned int flags) |
| 1419 | { |
| 1420 | while (commit_list) { |
| 1421 | struct object *object = &commit_list->item->object; |
| 1422 | object->flags |= flags; |
| 1423 | add_pending_object(revs, object, sha1_to_hex(object->sha1)); |
| 1424 | commit_list = commit_list->next; |
| 1425 | } |
| 1426 | } |
| 1427 | |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1428 | static void prepare_show_merge(struct rev_info *revs) |
| 1429 | { |
| 1430 | struct commit_list *bases; |
| 1431 | struct commit *head, *other; |
| 1432 | unsigned char sha1[20]; |
| 1433 | const char **prune = NULL; |
| 1434 | int i, prune_num = 1; /* counting terminating NULL */ |
| 1435 | |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1436 | if (get_sha1("HEAD", sha1)) |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1437 | die("--merge without HEAD?"); |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1438 | head = lookup_commit_or_die(sha1, "HEAD"); |
| 1439 | if (get_sha1("MERGE_HEAD", sha1)) |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1440 | die("--merge without MERGE_HEAD?"); |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1441 | other = lookup_commit_or_die(sha1, "MERGE_HEAD"); |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1442 | add_pending_object(revs, &head->object, "HEAD"); |
| 1443 | add_pending_object(revs, &other->object, "MERGE_HEAD"); |
| 1444 | bases = get_merge_bases(head, other, 1); |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1445 | add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM); |
| 1446 | add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM); |
Junio C Hamano | e82447b | 2008-02-26 23:18:38 -0800 | [diff] [blame] | 1447 | free_commit_list(bases); |
| 1448 | head->object.flags |= SYMMETRIC_LEFT; |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1449 | |
| 1450 | if (!active_nr) |
| 1451 | read_cache(); |
| 1452 | for (i = 0; i < active_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1453 | const struct cache_entry *ce = active_cache[i]; |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1454 | if (!ce_stage(ce)) |
| 1455 | continue; |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 1456 | if (ce_path_match(ce, &revs->prune_data, NULL)) { |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1457 | prune_num++; |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 1458 | REALLOC_ARRAY(prune, prune_num); |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1459 | prune[prune_num-2] = ce->name; |
| 1460 | prune[prune_num-1] = NULL; |
| 1461 | } |
| 1462 | while ((i+1 < active_nr) && |
| 1463 | ce_same_name(ce, active_cache[i+1])) |
| 1464 | i++; |
| 1465 | } |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 1466 | free_pathspec(&revs->prune_data); |
Nguyễn Thái Ngọc Duy | 4a2d5ae | 2013-10-26 09:09:20 +0700 | [diff] [blame] | 1467 | parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, |
Junio C Hamano | c6f1b92 | 2013-11-18 14:31:29 -0800 | [diff] [blame] | 1468 | PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune); |
Junio C Hamano | e82447b | 2008-02-26 23:18:38 -0800 | [diff] [blame] | 1469 | revs->limited = 1; |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1472 | 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] | 1473 | { |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 1474 | struct object_context oc; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1475 | char *dotdot; |
| 1476 | struct object *object; |
| 1477 | unsigned char sha1[20]; |
| 1478 | int local_flags; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1479 | const char *arg = arg_; |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 1480 | int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME; |
Junio C Hamano | d5f6b1d | 2012-07-02 12:43:05 -0700 | [diff] [blame] | 1481 | unsigned get_sha1_flags = 0; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1482 | |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1483 | flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM; |
| 1484 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1485 | dotdot = strstr(arg, ".."); |
| 1486 | if (dotdot) { |
| 1487 | unsigned char from_sha1[20]; |
| 1488 | const char *next = dotdot + 2; |
| 1489 | const char *this = arg; |
| 1490 | int symmetric = *next == '.'; |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1491 | unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM); |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 1492 | static const char head_by_default[] = "HEAD"; |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1493 | unsigned int a_flags; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1494 | |
| 1495 | *dotdot = 0; |
| 1496 | next += symmetric; |
| 1497 | |
| 1498 | if (!*next) |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 1499 | next = head_by_default; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1500 | if (dotdot == arg) |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 1501 | this = head_by_default; |
| 1502 | if (this == head_by_default && next == head_by_default && |
| 1503 | !symmetric) { |
| 1504 | /* |
| 1505 | * Just ".."? That is not a range but the |
| 1506 | * pathspec for the parent directory. |
| 1507 | */ |
| 1508 | if (!cant_be_filename) { |
| 1509 | *dotdot = '.'; |
| 1510 | return -1; |
| 1511 | } |
| 1512 | } |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1513 | if (!get_sha1_committish(this, from_sha1) && |
| 1514 | !get_sha1_committish(next, sha1)) { |
Junio C Hamano | 895c5ba | 2013-09-19 14:20:34 -0700 | [diff] [blame] | 1515 | struct object *a_obj, *b_obj; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1516 | |
| 1517 | if (!cant_be_filename) { |
| 1518 | *dotdot = '.'; |
| 1519 | verify_non_filename(revs->prefix, arg); |
| 1520 | } |
| 1521 | |
Junio C Hamano | 895c5ba | 2013-09-19 14:20:34 -0700 | [diff] [blame] | 1522 | a_obj = parse_object(from_sha1); |
| 1523 | b_obj = parse_object(sha1); |
| 1524 | if (!a_obj || !b_obj) { |
| 1525 | missing: |
| 1526 | if (revs->ignore_missing) |
| 1527 | return 0; |
| 1528 | die(symmetric |
| 1529 | ? "Invalid symmetric difference expression %s" |
| 1530 | : "Invalid revision range %s", arg); |
| 1531 | } |
| 1532 | |
| 1533 | if (!symmetric) { |
| 1534 | /* just A..B */ |
| 1535 | a_flags = flags_exclude; |
| 1536 | } else { |
| 1537 | /* A...B -- find merge bases between the two */ |
| 1538 | struct commit *a, *b; |
| 1539 | struct commit_list *exclude; |
| 1540 | |
| 1541 | a = (a_obj->type == OBJ_COMMIT |
| 1542 | ? (struct commit *)a_obj |
| 1543 | : lookup_commit_reference(a_obj->sha1)); |
| 1544 | b = (b_obj->type == OBJ_COMMIT |
| 1545 | ? (struct commit *)b_obj |
| 1546 | : lookup_commit_reference(b_obj->sha1)); |
| 1547 | if (!a || !b) |
| 1548 | goto missing; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1549 | exclude = get_merge_bases(a, b, 1); |
Kevin Bracey | a765499 | 2013-05-13 18:00:47 +0300 | [diff] [blame] | 1550 | add_rev_cmdline_list(revs, exclude, |
| 1551 | REV_CMD_MERGE_BASE, |
| 1552 | flags_exclude); |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1553 | add_pending_commit_list(revs, exclude, |
| 1554 | flags_exclude); |
| 1555 | free_commit_list(exclude); |
Junio C Hamano | 895c5ba | 2013-09-19 14:20:34 -0700 | [diff] [blame] | 1556 | |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1557 | a_flags = flags | SYMMETRIC_LEFT; |
Junio C Hamano | 895c5ba | 2013-09-19 14:20:34 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | a_obj->flags |= a_flags; |
| 1561 | b_obj->flags |= flags; |
| 1562 | add_rev_cmdline(revs, a_obj, this, |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1563 | REV_CMD_LEFT, a_flags); |
Junio C Hamano | 895c5ba | 2013-09-19 14:20:34 -0700 | [diff] [blame] | 1564 | add_rev_cmdline(revs, b_obj, next, |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1565 | REV_CMD_RIGHT, flags); |
Junio C Hamano | 895c5ba | 2013-09-19 14:20:34 -0700 | [diff] [blame] | 1566 | add_pending_object(revs, a_obj, this); |
| 1567 | add_pending_object(revs, b_obj, next); |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1568 | return 0; |
| 1569 | } |
| 1570 | *dotdot = '.'; |
| 1571 | } |
| 1572 | dotdot = strstr(arg, "^@"); |
| 1573 | if (dotdot && !dotdot[2]) { |
| 1574 | *dotdot = 0; |
| 1575 | if (add_parents_only(revs, arg, flags)) |
| 1576 | return 0; |
| 1577 | *dotdot = '^'; |
| 1578 | } |
Junio C Hamano | 62476c8 | 2006-10-31 14:22:34 -0800 | [diff] [blame] | 1579 | dotdot = strstr(arg, "^!"); |
| 1580 | if (dotdot && !dotdot[2]) { |
| 1581 | *dotdot = 0; |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1582 | if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM))) |
Junio C Hamano | 62476c8 | 2006-10-31 14:22:34 -0800 | [diff] [blame] | 1583 | *dotdot = '^'; |
| 1584 | } |
| 1585 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1586 | local_flags = 0; |
| 1587 | if (*arg == '^') { |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 1588 | local_flags = UNINTERESTING | BOTTOM; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1589 | arg++; |
| 1590 | } |
Junio C Hamano | d5f6b1d | 2012-07-02 12:43:05 -0700 | [diff] [blame] | 1591 | |
| 1592 | if (revarg_opt & REVARG_COMMITTISH) |
| 1593 | get_sha1_flags = GET_SHA1_COMMITTISH; |
| 1594 | |
| 1595 | if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc)) |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 1596 | return revs->ignore_missing ? 0 : -1; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1597 | if (!cant_be_filename) |
| 1598 | verify_non_filename(revs->prefix, arg); |
| 1599 | object = get_reference(revs, arg, sha1, flags ^ local_flags); |
Junio C Hamano | 281eee4 | 2011-08-25 17:35:39 -0700 | [diff] [blame] | 1600 | 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] | 1601 | add_pending_object_with_mode(revs, object, arg, oc.mode); |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1605 | struct cmdline_pathspec { |
| 1606 | int alloc; |
| 1607 | int nr; |
| 1608 | const char **path; |
| 1609 | }; |
| 1610 | |
| 1611 | static void append_prune_data(struct cmdline_pathspec *prune, const char **av) |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1612 | { |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1613 | while (*av) { |
Felipe Contreras | 9e57ac5 | 2013-10-31 03:25:43 -0600 | [diff] [blame] | 1614 | ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc); |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1615 | prune->path[prune->nr++] = *(av++); |
| 1616 | } |
| 1617 | } |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1618 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1619 | static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, |
| 1620 | struct cmdline_pathspec *prune) |
| 1621 | { |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1622 | while (strbuf_getwholeline(sb, stdin, '\n') != EOF) { |
| 1623 | int len = sb->len; |
| 1624 | if (len && sb->buf[len - 1] == '\n') |
| 1625 | sb->buf[--len] = '\0'; |
Felipe Contreras | 9e57ac5 | 2013-10-31 03:25:43 -0600 | [diff] [blame] | 1626 | ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc); |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1627 | prune->path[prune->nr++] = xstrdup(sb->buf); |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1628 | } |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 1631 | static void read_revisions_from_stdin(struct rev_info *revs, |
| 1632 | struct cmdline_pathspec *prune) |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1633 | { |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1634 | struct strbuf sb; |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1635 | int seen_dashdash = 0; |
Jeff King | 4c30d50 | 2014-03-12 16:06:17 -0400 | [diff] [blame] | 1636 | int save_warning; |
| 1637 | |
| 1638 | save_warning = warn_on_object_refname_ambiguity; |
| 1639 | warn_on_object_refname_ambiguity = 0; |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1640 | |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1641 | strbuf_init(&sb, 1000); |
| 1642 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { |
| 1643 | int len = sb.len; |
| 1644 | if (len && sb.buf[len - 1] == '\n') |
| 1645 | sb.buf[--len] = '\0'; |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1646 | if (!len) |
| 1647 | break; |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1648 | if (sb.buf[0] == '-') { |
| 1649 | if (len == 2 && sb.buf[1] == '-') { |
| 1650 | seen_dashdash = 1; |
| 1651 | break; |
| 1652 | } |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1653 | die("options not supported in --stdin mode"); |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1654 | } |
Michael Haggerty | 31faeb2 | 2013-05-25 11:08:14 +0200 | [diff] [blame] | 1655 | if (handle_revision_arg(sb.buf, revs, 0, |
Thomas Rast | 70d26c6 | 2013-04-16 11:57:45 +0200 | [diff] [blame] | 1656 | REVARG_CANNOT_BE_FILENAME)) |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1657 | die("bad revision '%s'", sb.buf); |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1658 | } |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 1659 | if (seen_dashdash) |
| 1660 | read_pathspec_from_stdin(revs, &sb, prune); |
Jeff King | 4c30d50 | 2014-03-12 16:06:17 -0400 | [diff] [blame] | 1661 | |
Junio C Hamano | 63d564b | 2009-11-20 02:00:40 -0800 | [diff] [blame] | 1662 | strbuf_release(&sb); |
Jeff King | 4c30d50 | 2014-03-12 16:06:17 -0400 | [diff] [blame] | 1663 | warn_on_object_refname_ambiguity = save_warning; |
Adam Brewster | 1fc561d | 2008-07-05 17:26:39 -0400 | [diff] [blame] | 1664 | } |
| 1665 | |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1666 | static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) |
| 1667 | { |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 1668 | append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what); |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 1671 | 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] | 1672 | { |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 1673 | append_header_grep_pattern(&revs->grep_filter, field, pattern); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | static void add_message_grep(struct rev_info *revs, const char *pattern) |
| 1677 | { |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1678 | add_grep(revs, pattern, GREP_PATTERN_BODY); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
Pierre Habouzit | 6b61ec0 | 2008-07-09 23:38:34 +0200 | [diff] [blame] | 1681 | static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv, |
| 1682 | int *unkc, const char **unkv) |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1683 | { |
| 1684 | const char *arg = argv[0]; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1685 | const char *optarg; |
| 1686 | int argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1687 | |
| 1688 | /* pseudo revision arguments */ |
| 1689 | if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") || |
| 1690 | !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") || |
| 1691 | !strcmp(arg, "--reflog") || !strcmp(arg, "--not") || |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 1692 | !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") || |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1693 | !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") || |
Jeff King | 4fe1021 | 2014-10-16 20:44:23 -0400 | [diff] [blame] | 1694 | !strcmp(arg, "--indexed-objects") || |
Junio C Hamano | 07768e0 | 2014-06-09 11:30:12 -0700 | [diff] [blame] | 1695 | starts_with(arg, "--exclude=") || |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1696 | starts_with(arg, "--branches=") || starts_with(arg, "--tags=") || |
| 1697 | starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk=")) |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1698 | { |
| 1699 | unkv[(*unkc)++] = arg; |
Pierre Habouzit | 0fe8c13 | 2008-07-31 12:22:23 +0200 | [diff] [blame] | 1700 | return 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1703 | if ((argcount = parse_long_opt("max-count", argv, &optarg))) { |
| 1704 | revs->max_count = atoi(optarg); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1705 | revs->no_walk = 0; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1706 | return argcount; |
| 1707 | } else if ((argcount = parse_long_opt("skip", argv, &optarg))) { |
| 1708 | revs->skip_count = atoi(optarg); |
| 1709 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1710 | } else if ((*arg == '-') && isdigit(arg[1])) { |
Junio C Hamano | e3fa568 | 2014-06-06 15:33:25 -0700 | [diff] [blame] | 1711 | /* accept -<digit>, like traditional "head" */ |
| 1712 | if (strtol_i(arg + 1, 10, &revs->max_count) < 0 || |
| 1713 | revs->max_count < 0) |
| 1714 | die("'%s': not a non-negative integer", arg + 1); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1715 | revs->no_walk = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1716 | } else if (!strcmp(arg, "-n")) { |
| 1717 | if (argc <= 1) |
| 1718 | return error("-n requires an argument"); |
| 1719 | revs->max_count = atoi(argv[1]); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1720 | revs->no_walk = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1721 | return 2; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1722 | } else if (starts_with(arg, "-n")) { |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1723 | revs->max_count = atoi(arg + 2); |
Jonathan Nieder | 5853cae | 2010-06-01 03:35:49 -0500 | [diff] [blame] | 1724 | revs->no_walk = 0; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1725 | } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) { |
| 1726 | revs->max_age = atoi(optarg); |
| 1727 | return argcount; |
| 1728 | } else if ((argcount = parse_long_opt("since", argv, &optarg))) { |
| 1729 | revs->max_age = approxidate(optarg); |
| 1730 | return argcount; |
| 1731 | } else if ((argcount = parse_long_opt("after", argv, &optarg))) { |
| 1732 | revs->max_age = approxidate(optarg); |
| 1733 | return argcount; |
| 1734 | } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) { |
| 1735 | revs->min_age = atoi(optarg); |
| 1736 | return argcount; |
| 1737 | } else if ((argcount = parse_long_opt("before", argv, &optarg))) { |
| 1738 | revs->min_age = approxidate(optarg); |
| 1739 | return argcount; |
| 1740 | } else if ((argcount = parse_long_opt("until", argv, &optarg))) { |
| 1741 | revs->min_age = approxidate(optarg); |
| 1742 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1743 | } else if (!strcmp(arg, "--first-parent")) { |
| 1744 | revs->first_parent_only = 1; |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1745 | } else if (!strcmp(arg, "--ancestry-path")) { |
| 1746 | revs->ancestry_path = 1; |
Johan Herland | cb7529e | 2010-06-04 01:17:37 +0200 | [diff] [blame] | 1747 | revs->simplify_history = 0; |
Junio C Hamano | ebdc94f | 2010-04-20 13:48:39 -0700 | [diff] [blame] | 1748 | revs->limited = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1749 | } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) { |
| 1750 | init_reflog_walk(&revs->reflog_info); |
| 1751 | } else if (!strcmp(arg, "--default")) { |
| 1752 | if (argc <= 1) |
| 1753 | return error("bad --default argument"); |
| 1754 | revs->def = argv[1]; |
| 1755 | return 2; |
| 1756 | } else if (!strcmp(arg, "--merge")) { |
| 1757 | revs->show_merge = 1; |
| 1758 | } else if (!strcmp(arg, "--topo-order")) { |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 1759 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1760 | revs->topo_order = 1; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1761 | } else if (!strcmp(arg, "--simplify-merges")) { |
| 1762 | revs->simplify_merges = 1; |
Junio C Hamano | a52f007 | 2012-06-08 14:47:08 -0700 | [diff] [blame] | 1763 | revs->topo_order = 1; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 1764 | revs->rewrite_parents = 1; |
| 1765 | revs->simplify_history = 0; |
| 1766 | revs->limited = 1; |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 1767 | } else if (!strcmp(arg, "--simplify-by-decoration")) { |
| 1768 | revs->simplify_merges = 1; |
Junio C Hamano | a52f007 | 2012-06-08 14:47:08 -0700 | [diff] [blame] | 1769 | revs->topo_order = 1; |
Linus Torvalds | 78892e3 | 2008-11-03 11:25:46 -0800 | [diff] [blame] | 1770 | revs->rewrite_parents = 1; |
| 1771 | revs->simplify_history = 0; |
| 1772 | revs->simplify_by_decoration = 1; |
| 1773 | revs->limited = 1; |
| 1774 | revs->prune = 1; |
Lars Hjemli | 33e7018 | 2009-08-15 16:23:12 +0200 | [diff] [blame] | 1775 | load_ref_decorations(DECORATE_SHORT_REFS); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1776 | } else if (!strcmp(arg, "--date-order")) { |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 1777 | revs->sort_order = REV_SORT_BY_COMMIT_DATE; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1778 | revs->topo_order = 1; |
Junio C Hamano | 81c6b38 | 2013-06-07 10:35:54 -0700 | [diff] [blame] | 1779 | } else if (!strcmp(arg, "--author-date-order")) { |
| 1780 | revs->sort_order = REV_SORT_BY_AUTHOR_DATE; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1781 | revs->topo_order = 1; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1782 | } else if (starts_with(arg, "--early-output")) { |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1783 | int count = 100; |
| 1784 | switch (arg[14]) { |
| 1785 | case '=': |
| 1786 | count = atoi(arg+15); |
| 1787 | /* Fallthrough */ |
| 1788 | case 0: |
| 1789 | revs->topo_order = 1; |
| 1790 | revs->early_output = count; |
| 1791 | } |
| 1792 | } else if (!strcmp(arg, "--parents")) { |
| 1793 | revs->rewrite_parents = 1; |
| 1794 | revs->print_parents = 1; |
| 1795 | } else if (!strcmp(arg, "--dense")) { |
| 1796 | revs->dense = 1; |
| 1797 | } else if (!strcmp(arg, "--sparse")) { |
| 1798 | revs->dense = 0; |
| 1799 | } else if (!strcmp(arg, "--show-all")) { |
| 1800 | revs->show_all = 1; |
| 1801 | } else if (!strcmp(arg, "--remove-empty")) { |
| 1802 | revs->remove_empty_trees = 1; |
Linus Torvalds | b8e8db2 | 2009-06-29 10:28:25 -0700 | [diff] [blame] | 1803 | } else if (!strcmp(arg, "--merges")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1804 | revs->min_parents = 2; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1805 | } else if (!strcmp(arg, "--no-merges")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1806 | revs->max_parents = 1; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1807 | } else if (starts_with(arg, "--min-parents=")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1808 | revs->min_parents = atoi(arg+14); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1809 | } else if (starts_with(arg, "--no-min-parents")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1810 | revs->min_parents = 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1811 | } else if (starts_with(arg, "--max-parents=")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1812 | revs->max_parents = atoi(arg+14); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1813 | } else if (starts_with(arg, "--no-max-parents")) { |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1814 | revs->max_parents = -1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1815 | } else if (!strcmp(arg, "--boundary")) { |
| 1816 | revs->boundary = 1; |
| 1817 | } else if (!strcmp(arg, "--left-right")) { |
| 1818 | revs->left_right = 1; |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1819 | } else if (!strcmp(arg, "--left-only")) { |
Junio C Hamano | 24852d9 | 2011-02-21 16:58:37 -0800 | [diff] [blame] | 1820 | if (revs->right_only) |
Michael J Gruber | 94f605e | 2011-03-07 13:31:42 +0100 | [diff] [blame] | 1821 | die("--left-only is incompatible with --right-only" |
| 1822 | " or --cherry"); |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1823 | revs->left_only = 1; |
| 1824 | } else if (!strcmp(arg, "--right-only")) { |
Junio C Hamano | 24852d9 | 2011-02-21 16:58:37 -0800 | [diff] [blame] | 1825 | if (revs->left_only) |
| 1826 | die("--right-only is incompatible with --left-only"); |
Michael J Gruber | 60adf7d | 2011-02-21 17:09:11 +0100 | [diff] [blame] | 1827 | revs->right_only = 1; |
Michael J Gruber | 94f605e | 2011-03-07 13:31:42 +0100 | [diff] [blame] | 1828 | } else if (!strcmp(arg, "--cherry")) { |
| 1829 | if (revs->left_only) |
| 1830 | die("--cherry is incompatible with --left-only"); |
| 1831 | revs->cherry_mark = 1; |
| 1832 | revs->right_only = 1; |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 1833 | revs->max_parents = 1; |
Michael J Gruber | 94f605e | 2011-03-07 13:31:42 +0100 | [diff] [blame] | 1834 | revs->limited = 1; |
Thomas Rast | f69c501 | 2010-06-10 13:47:23 +0200 | [diff] [blame] | 1835 | } else if (!strcmp(arg, "--count")) { |
| 1836 | revs->count = 1; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 1837 | } else if (!strcmp(arg, "--cherry-mark")) { |
| 1838 | if (revs->cherry_pick) |
| 1839 | die("--cherry-mark is incompatible with --cherry-pick"); |
| 1840 | revs->cherry_mark = 1; |
| 1841 | revs->limited = 1; /* needs limit_list() */ |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1842 | } else if (!strcmp(arg, "--cherry-pick")) { |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 1843 | if (revs->cherry_mark) |
| 1844 | die("--cherry-pick is incompatible with --cherry-mark"); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1845 | revs->cherry_pick = 1; |
| 1846 | revs->limited = 1; |
| 1847 | } else if (!strcmp(arg, "--objects")) { |
| 1848 | revs->tag_objects = 1; |
| 1849 | revs->tree_objects = 1; |
| 1850 | revs->blob_objects = 1; |
| 1851 | } else if (!strcmp(arg, "--objects-edge")) { |
| 1852 | revs->tag_objects = 1; |
| 1853 | revs->tree_objects = 1; |
| 1854 | revs->blob_objects = 1; |
| 1855 | revs->edge_hint = 1; |
Junio C Hamano | 5a48d24 | 2011-09-01 15:43:34 -0700 | [diff] [blame] | 1856 | } else if (!strcmp(arg, "--verify-objects")) { |
| 1857 | revs->tag_objects = 1; |
| 1858 | revs->tree_objects = 1; |
| 1859 | revs->blob_objects = 1; |
| 1860 | revs->verify_objects = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1861 | } else if (!strcmp(arg, "--unpacked")) { |
| 1862 | revs->unpacked = 1; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1863 | } else if (starts_with(arg, "--unpacked=")) { |
Junio C Hamano | 03a9683 | 2009-02-28 00:00:21 -0800 | [diff] [blame] | 1864 | die("--unpacked=<packfile> no longer supported."); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1865 | } else if (!strcmp(arg, "-r")) { |
| 1866 | revs->diff = 1; |
| 1867 | DIFF_OPT_SET(&revs->diffopt, RECURSIVE); |
| 1868 | } else if (!strcmp(arg, "-t")) { |
| 1869 | revs->diff = 1; |
| 1870 | DIFF_OPT_SET(&revs->diffopt, RECURSIVE); |
| 1871 | DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE); |
| 1872 | } else if (!strcmp(arg, "-m")) { |
| 1873 | revs->ignore_merges = 0; |
| 1874 | } else if (!strcmp(arg, "-c")) { |
| 1875 | revs->diff = 1; |
| 1876 | revs->dense_combined_merges = 0; |
| 1877 | revs->combine_merges = 1; |
| 1878 | } else if (!strcmp(arg, "--cc")) { |
| 1879 | revs->diff = 1; |
| 1880 | revs->dense_combined_merges = 1; |
| 1881 | revs->combine_merges = 1; |
| 1882 | } else if (!strcmp(arg, "-v")) { |
| 1883 | revs->verbose_header = 1; |
| 1884 | } else if (!strcmp(arg, "--pretty")) { |
| 1885 | revs->verbose_header = 1; |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1886 | revs->pretty_given = 1; |
Jeff King | ae18165 | 2014-07-29 13:53:40 -0400 | [diff] [blame] | 1887 | get_commit_format(NULL, revs); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1888 | } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) { |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1889 | /* |
| 1890 | * Detached form ("--pretty X" as opposed to "--pretty=X") |
| 1891 | * not allowed, since the argument is optional. |
| 1892 | */ |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1893 | revs->verbose_header = 1; |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1894 | revs->pretty_given = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1895 | get_commit_format(arg+9, revs); |
Jeff King | 7249e91 | 2011-03-29 16:57:47 -0400 | [diff] [blame] | 1896 | } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) { |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1897 | revs->show_notes = 1; |
| 1898 | revs->show_notes_given = 1; |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1899 | revs->notes_opt.use_default_notes = 1; |
Junio C Hamano | 0c37f1f | 2011-10-18 15:53:23 -0700 | [diff] [blame] | 1900 | } else if (!strcmp(arg, "--show-signature")) { |
| 1901 | revs->show_signature = 1; |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 1902 | } else if (!strcmp(arg, "--show-linear-break") || |
| 1903 | starts_with(arg, "--show-linear-break=")) { |
| 1904 | if (starts_with(arg, "--show-linear-break=")) |
| 1905 | revs->break_bar = xstrdup(arg + 20); |
| 1906 | else |
| 1907 | revs->break_bar = " .........."; |
| 1908 | revs->track_linear = 1; |
| 1909 | revs->track_first_time = 1; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1910 | } else if (starts_with(arg, "--show-notes=") || |
| 1911 | starts_with(arg, "--notes=")) { |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 1912 | struct strbuf buf = STRBUF_INIT; |
| 1913 | revs->show_notes = 1; |
| 1914 | revs->show_notes_given = 1; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1915 | if (starts_with(arg, "--show-notes")) { |
Jeff King | 7249e91 | 2011-03-29 16:57:47 -0400 | [diff] [blame] | 1916 | if (revs->notes_opt.use_default_notes < 0) |
| 1917 | revs->notes_opt.use_default_notes = 1; |
| 1918 | strbuf_addstr(&buf, arg+13); |
| 1919 | } |
| 1920 | else |
| 1921 | strbuf_addstr(&buf, arg+8); |
Jeff King | c063f0a | 2011-03-29 16:56:04 -0400 | [diff] [blame] | 1922 | expand_notes_ref(&buf); |
Jeff King | 304cc11 | 2011-03-29 16:56:53 -0400 | [diff] [blame] | 1923 | string_list_append(&revs->notes_opt.extra_notes_refs, |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 1924 | strbuf_detach(&buf, NULL)); |
Junio C Hamano | 66b2ed0 | 2010-01-20 13:59:36 -0800 | [diff] [blame] | 1925 | } else if (!strcmp(arg, "--no-notes")) { |
| 1926 | revs->show_notes = 0; |
| 1927 | revs->show_notes_given = 1; |
Jeff King | 92e0d42 | 2011-03-29 16:59:42 -0400 | [diff] [blame] | 1928 | revs->notes_opt.use_default_notes = -1; |
| 1929 | /* we have been strdup'ing ourselves, so trick |
| 1930 | * string_list into free()ing strings */ |
| 1931 | revs->notes_opt.extra_notes_refs.strdup_strings = 1; |
| 1932 | string_list_clear(&revs->notes_opt.extra_notes_refs, 0); |
| 1933 | revs->notes_opt.extra_notes_refs.strdup_strings = 0; |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 1934 | } else if (!strcmp(arg, "--standard-notes")) { |
| 1935 | revs->show_notes_given = 1; |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1936 | revs->notes_opt.use_default_notes = 1; |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 1937 | } else if (!strcmp(arg, "--no-standard-notes")) { |
Jeff King | 3a03cf6 | 2011-03-29 16:57:27 -0400 | [diff] [blame] | 1938 | revs->notes_opt.use_default_notes = 0; |
Nanako Shiraishi | de84acc | 2009-02-24 18:59:16 +0900 | [diff] [blame] | 1939 | } else if (!strcmp(arg, "--oneline")) { |
| 1940 | revs->verbose_header = 1; |
| 1941 | get_commit_format("oneline", revs); |
Junio C Hamano | 7dccadf | 2010-01-21 14:57:41 -0800 | [diff] [blame] | 1942 | revs->pretty_given = 1; |
Nanako Shiraishi | de84acc | 2009-02-24 18:59:16 +0900 | [diff] [blame] | 1943 | revs->abbrev_commit = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1944 | } else if (!strcmp(arg, "--graph")) { |
| 1945 | revs->topo_order = 1; |
| 1946 | revs->rewrite_parents = 1; |
| 1947 | revs->graph = graph_init(revs); |
| 1948 | } else if (!strcmp(arg, "--root")) { |
| 1949 | revs->show_root_diff = 1; |
| 1950 | } else if (!strcmp(arg, "--no-commit-id")) { |
| 1951 | revs->no_commit_id = 1; |
| 1952 | } else if (!strcmp(arg, "--always")) { |
| 1953 | revs->always_show_header = 1; |
| 1954 | } else if (!strcmp(arg, "--no-abbrev")) { |
| 1955 | revs->abbrev = 0; |
| 1956 | } else if (!strcmp(arg, "--abbrev")) { |
| 1957 | revs->abbrev = DEFAULT_ABBREV; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1958 | } else if (starts_with(arg, "--abbrev=")) { |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1959 | revs->abbrev = strtoul(arg + 9, NULL, 10); |
| 1960 | if (revs->abbrev < MINIMUM_ABBREV) |
| 1961 | revs->abbrev = MINIMUM_ABBREV; |
| 1962 | else if (revs->abbrev > 40) |
| 1963 | revs->abbrev = 40; |
| 1964 | } else if (!strcmp(arg, "--abbrev-commit")) { |
| 1965 | revs->abbrev_commit = 1; |
Jay Soffian | 0c47695 | 2011-05-18 13:56:04 -0400 | [diff] [blame] | 1966 | revs->abbrev_commit_given = 1; |
| 1967 | } else if (!strcmp(arg, "--no-abbrev-commit")) { |
| 1968 | revs->abbrev_commit = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1969 | } else if (!strcmp(arg, "--full-diff")) { |
| 1970 | revs->diff = 1; |
| 1971 | revs->full_diff = 1; |
| 1972 | } else if (!strcmp(arg, "--full-history")) { |
| 1973 | revs->simplify_history = 0; |
| 1974 | } else if (!strcmp(arg, "--relative-date")) { |
| 1975 | revs->date_mode = DATE_RELATIVE; |
Jeff King | f4ea32f | 2009-09-24 04:28:15 -0400 | [diff] [blame] | 1976 | revs->date_mode_explicit = 1; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1977 | } else if ((argcount = parse_long_opt("date", argv, &optarg))) { |
| 1978 | revs->date_mode = parse_date_format(optarg); |
Jeff King | f4ea32f | 2009-09-24 04:28:15 -0400 | [diff] [blame] | 1979 | revs->date_mode_explicit = 1; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1980 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 1981 | } else if (!strcmp(arg, "--log-size")) { |
| 1982 | revs->show_log_size = 1; |
| 1983 | } |
| 1984 | /* |
| 1985 | * Grepping the commit log |
| 1986 | */ |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1987 | else if ((argcount = parse_long_opt("author", argv, &optarg))) { |
| 1988 | add_header_grep(revs, GREP_HEADER_AUTHOR, optarg); |
| 1989 | return argcount; |
| 1990 | } else if ((argcount = parse_long_opt("committer", argv, &optarg))) { |
| 1991 | add_header_grep(revs, GREP_HEADER_COMMITTER, optarg); |
| 1992 | return argcount; |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 1993 | } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) { |
| 1994 | add_header_grep(revs, GREP_HEADER_REFLOG, optarg); |
| 1995 | return argcount; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 1996 | } else if ((argcount = parse_long_opt("grep", argv, &optarg))) { |
| 1997 | add_message_grep(revs, optarg); |
| 1998 | return argcount; |
Junio C Hamano | 17bf35a | 2012-09-13 14:21:44 -0700 | [diff] [blame] | 1999 | } else if (!strcmp(arg, "--grep-debug")) { |
| 2000 | revs->grep_filter.debug = 1; |
Junio C Hamano | 727b6fc | 2012-10-03 15:01:34 -0700 | [diff] [blame] | 2001 | } else if (!strcmp(arg, "--basic-regexp")) { |
| 2002 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2003 | } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) { |
Junio C Hamano | 34a4ae5 | 2012-10-03 14:50:51 -0700 | [diff] [blame] | 2004 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2005 | } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) { |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 2006 | revs->grep_filter.regflags |= REG_ICASE; |
Junio C Hamano | accccde | 2012-02-21 01:02:46 -0800 | [diff] [blame] | 2007 | DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2008 | } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) { |
Junio C Hamano | 34a4ae5 | 2012-10-03 14:50:51 -0700 | [diff] [blame] | 2009 | 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] | 2010 | } else if (!strcmp(arg, "--perl-regexp")) { |
| 2011 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2012 | } else if (!strcmp(arg, "--all-match")) { |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 2013 | revs->grep_filter.all_match = 1; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 2014 | } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) { |
| 2015 | if (strcmp(optarg, "none")) |
| 2016 | git_log_output_encoding = xstrdup(optarg); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2017 | else |
| 2018 | git_log_output_encoding = ""; |
Matthieu Moy | 7d7b86f | 2010-08-05 10:22:55 +0200 | [diff] [blame] | 2019 | return argcount; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2020 | } else if (!strcmp(arg, "--reverse")) { |
| 2021 | revs->reverse ^= 1; |
| 2022 | } else if (!strcmp(arg, "--children")) { |
| 2023 | revs->children.name = "children"; |
| 2024 | revs->limited = 1; |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 2025 | } else if (!strcmp(arg, "--ignore-missing")) { |
| 2026 | revs->ignore_missing = 1; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2027 | } else { |
| 2028 | int opts = diff_opt_parse(&revs->diffopt, argv, argc); |
| 2029 | if (!opts) |
| 2030 | unkv[(*unkc)++] = arg; |
| 2031 | return opts; |
| 2032 | } |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 2033 | if (revs->graph && revs->track_linear) |
| 2034 | die("--show-linear-break and --graph are incompatible"); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2035 | |
| 2036 | return 1; |
| 2037 | } |
| 2038 | |
Pierre Habouzit | 6b61ec0 | 2008-07-09 23:38:34 +0200 | [diff] [blame] | 2039 | void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx, |
| 2040 | const struct option *options, |
| 2041 | const char * const usagestr[]) |
| 2042 | { |
| 2043 | int n = handle_revision_opt(revs, ctx->argc, ctx->argv, |
| 2044 | &ctx->cpidx, ctx->out); |
| 2045 | if (n <= 0) { |
| 2046 | error("unknown option `%s'", ctx->argv[0]); |
| 2047 | usage_with_options(usagestr, options); |
| 2048 | } |
| 2049 | ctx->argv += n; |
| 2050 | ctx->argc -= n; |
| 2051 | } |
| 2052 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 2053 | 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] | 2054 | { |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 2055 | 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] | 2056 | } |
| 2057 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 2058 | 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] | 2059 | { |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 2060 | 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] | 2061 | } |
| 2062 | |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2063 | static int handle_revision_pseudo_opt(const char *submodule, |
| 2064 | struct rev_info *revs, |
| 2065 | int argc, const char **argv, int *flags) |
| 2066 | { |
| 2067 | const char *arg = argv[0]; |
| 2068 | const char *optarg; |
| 2069 | int argcount; |
| 2070 | |
Jonathan Nieder | 0fc63ec | 2011-04-21 05:48:24 -0500 | [diff] [blame] | 2071 | /* |
| 2072 | * NOTE! |
| 2073 | * |
| 2074 | * Commands like "git shortlog" will not accept the options below |
| 2075 | * unless parse_revision_opt queues them (as opposed to erroring |
| 2076 | * out). |
| 2077 | * |
| 2078 | * When implementing your new pseudo-option, remember to |
| 2079 | * register it in the list at the top of handle_revision_opt. |
| 2080 | */ |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2081 | if (!strcmp(arg, "--all")) { |
| 2082 | handle_refs(submodule, revs, *flags, for_each_ref_submodule); |
| 2083 | handle_refs(submodule, revs, *flags, head_ref_submodule); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2084 | clear_ref_exclusion(&revs->ref_excludes); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2085 | } else if (!strcmp(arg, "--branches")) { |
| 2086 | handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2087 | clear_ref_exclusion(&revs->ref_excludes); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2088 | } else if (!strcmp(arg, "--bisect")) { |
| 2089 | handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref); |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 2090 | handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2091 | revs->bisect = 1; |
| 2092 | } else if (!strcmp(arg, "--tags")) { |
| 2093 | handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2094 | clear_ref_exclusion(&revs->ref_excludes); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2095 | } else if (!strcmp(arg, "--remotes")) { |
| 2096 | handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2097 | clear_ref_exclusion(&revs->ref_excludes); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2098 | } else if ((argcount = parse_long_opt("glob", argv, &optarg))) { |
| 2099 | struct all_refs_cb cb; |
| 2100 | init_all_refs_cb(&cb, revs, *flags); |
| 2101 | for_each_glob_ref(handle_one_ref, optarg, &cb); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2102 | clear_ref_exclusion(&revs->ref_excludes); |
Junio C Hamano | e7b432c | 2013-08-30 16:37:55 -0700 | [diff] [blame] | 2103 | return argcount; |
| 2104 | } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) { |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2105 | add_ref_exclusion(&revs->ref_excludes, optarg); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2106 | return argcount; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2107 | } else if (starts_with(arg, "--branches=")) { |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2108 | struct all_refs_cb cb; |
| 2109 | init_all_refs_cb(&cb, revs, *flags); |
| 2110 | for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2111 | clear_ref_exclusion(&revs->ref_excludes); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2112 | } else if (starts_with(arg, "--tags=")) { |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2113 | struct all_refs_cb cb; |
| 2114 | init_all_refs_cb(&cb, revs, *flags); |
| 2115 | for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2116 | clear_ref_exclusion(&revs->ref_excludes); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2117 | } else if (starts_with(arg, "--remotes=")) { |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2118 | struct all_refs_cb cb; |
| 2119 | init_all_refs_cb(&cb, revs, *flags); |
| 2120 | for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb); |
Junio C Hamano | ff32d34 | 2013-11-01 12:02:45 -0700 | [diff] [blame] | 2121 | clear_ref_exclusion(&revs->ref_excludes); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2122 | } else if (!strcmp(arg, "--reflog")) { |
Jeff King | 718ccc9 | 2014-10-15 18:38:31 -0400 | [diff] [blame] | 2123 | add_reflogs_to_pending(revs, *flags); |
Jeff King | 4fe1021 | 2014-10-16 20:44:23 -0400 | [diff] [blame] | 2124 | } else if (!strcmp(arg, "--indexed-objects")) { |
| 2125 | add_index_objects_to_pending(revs, *flags); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2126 | } else if (!strcmp(arg, "--not")) { |
Kevin Bracey | 7f34a46 | 2013-05-16 18:32:38 +0300 | [diff] [blame] | 2127 | *flags ^= UNINTERESTING | BOTTOM; |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2128 | } else if (!strcmp(arg, "--no-walk")) { |
Martin von Zweigbergk | ca92e59 | 2012-08-28 23:15:54 -0700 | [diff] [blame] | 2129 | revs->no_walk = REVISION_WALK_NO_WALK_SORTED; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2130 | } else if (starts_with(arg, "--no-walk=")) { |
Martin von Zweigbergk | ca92e59 | 2012-08-28 23:15:54 -0700 | [diff] [blame] | 2131 | /* |
| 2132 | * Detached form ("--no-walk X" as opposed to "--no-walk=X") |
| 2133 | * not allowed, since the argument is optional. |
| 2134 | */ |
| 2135 | if (!strcmp(arg + 10, "sorted")) |
| 2136 | revs->no_walk = REVISION_WALK_NO_WALK_SORTED; |
| 2137 | else if (!strcmp(arg + 10, "unsorted")) |
| 2138 | revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; |
| 2139 | else |
| 2140 | return error("invalid argument to --no-walk"); |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2141 | } else if (!strcmp(arg, "--do-walk")) { |
| 2142 | revs->no_walk = 0; |
| 2143 | } else { |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | return 1; |
| 2148 | } |
| 2149 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2150 | /* |
| 2151 | * Parse revision information, filling in the "rev_info" structure, |
| 2152 | * and removing the used arguments from the argument list. |
| 2153 | * |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2154 | * Returns the number of arguments left that weren't recognized |
| 2155 | * (which are also moved to the head of the argument list) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2156 | */ |
Junio C Hamano | 32962c9 | 2010-03-08 22:58:09 -0800 | [diff] [blame] | 2157 | 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] | 2158 | { |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 2159 | 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] | 2160 | struct cmdline_pathspec prune_data; |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 2161 | const char *submodule = NULL; |
| 2162 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 2163 | memset(&prune_data, 0, sizeof(prune_data)); |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 2164 | if (opt) |
| 2165 | submodule = opt->submodule; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2166 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2167 | /* First, search for "--" */ |
Clemens Buchacher | 6d5b93f | 2012-04-14 21:04:48 +0200 | [diff] [blame] | 2168 | if (opt && opt->assume_dashdash) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2169 | seen_dashdash = 1; |
Clemens Buchacher | 6d5b93f | 2012-04-14 21:04:48 +0200 | [diff] [blame] | 2170 | } else { |
| 2171 | seen_dashdash = 0; |
| 2172 | for (i = 1; i < argc; i++) { |
| 2173 | const char *arg = argv[i]; |
| 2174 | if (strcmp(arg, "--")) |
| 2175 | continue; |
| 2176 | argv[i] = NULL; |
| 2177 | argc = i; |
| 2178 | if (argv[i + 1]) |
| 2179 | append_prune_data(&prune_data, argv + i + 1); |
| 2180 | seen_dashdash = 1; |
| 2181 | break; |
| 2182 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2183 | } |
| 2184 | |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2185 | /* Second, deal with arguments and options */ |
| 2186 | flags = 0; |
Junio C Hamano | d5f6b1d | 2012-07-02 12:43:05 -0700 | [diff] [blame] | 2187 | revarg_opt = opt ? opt->revarg_opt : 0; |
| 2188 | if (seen_dashdash) |
| 2189 | revarg_opt |= REVARG_CANNOT_BE_FILENAME; |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 2190 | read_from_stdin = 0; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2191 | for (left = i = 1; i < argc; i++) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2192 | const char *arg = argv[i]; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2193 | if (*arg == '-') { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2194 | int opts; |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2195 | |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2196 | opts = handle_revision_pseudo_opt(submodule, |
| 2197 | revs, argc - i, argv + i, |
| 2198 | &flags); |
| 2199 | if (opts > 0) { |
| 2200 | i += opts - 1; |
Uwe Kleine-König | a5aa930 | 2008-02-28 08:24:25 +0100 | [diff] [blame] | 2201 | continue; |
| 2202 | } |
Jonathan Nieder | f6aca0d | 2011-04-21 05:45:07 -0500 | [diff] [blame] | 2203 | |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 2204 | if (!strcmp(arg, "--stdin")) { |
| 2205 | if (revs->disable_stdin) { |
| 2206 | argv[left++] = arg; |
| 2207 | continue; |
| 2208 | } |
| 2209 | if (read_from_stdin++) |
| 2210 | die("--stdin given twice?"); |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 2211 | read_revisions_from_stdin(revs, &prune_data); |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 2212 | continue; |
| 2213 | } |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 2214 | |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2215 | opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2216 | if (opts > 0) { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2217 | i += opts - 1; |
| 2218 | continue; |
| 2219 | } |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2220 | if (opts < 0) |
| 2221 | exit(128); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2222 | continue; |
| 2223 | } |
Rene Scharfe | 0d2c9d6 | 2006-07-02 01:29:37 +0200 | [diff] [blame] | 2224 | |
Junio C Hamano | 8e676e8 | 2012-07-02 12:33:52 -0700 | [diff] [blame] | 2225 | |
| 2226 | if (handle_revision_arg(arg, revs, flags, revarg_opt)) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2227 | int j; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 2228 | if (seen_dashdash || *arg == '^') |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2229 | die("bad revision '%s'", arg); |
| 2230 | |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 2231 | /* If we didn't have a "--": |
| 2232 | * (1) all filenames must exist; |
| 2233 | * (2) all rev-args must not be interpretable |
| 2234 | * as a valid filename. |
| 2235 | * but the latter we have checked in the main loop. |
| 2236 | */ |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 2237 | for (j = i; j < argc; j++) |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 2238 | verify_filename(revs->prefix, argv[j], j == i); |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 2239 | |
Junio C Hamano | 60da8b1 | 2009-11-20 02:50:21 -0800 | [diff] [blame] | 2240 | append_prune_data(&prune_data, argv + i); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2241 | break; |
| 2242 | } |
Dave Olszewski | 8fcaca3 | 2010-03-13 14:47:05 -0800 | [diff] [blame] | 2243 | else |
| 2244 | got_rev_arg = 1; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2245 | } |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 2246 | |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 2247 | if (prune_data.nr) { |
Junio C Hamano | 93e7d67 | 2011-05-11 15:23:25 -0700 | [diff] [blame] | 2248 | /* |
| 2249 | * If we need to introduce the magic "a lone ':' means no |
| 2250 | * pathspec whatsoever", here is the place to do so. |
| 2251 | * |
| 2252 | * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) { |
| 2253 | * prune_data.nr = 0; |
| 2254 | * prune_data.alloc = 0; |
| 2255 | * free(prune_data.path); |
| 2256 | * prune_data.path = NULL; |
| 2257 | * } else { |
| 2258 | * terminate prune_data.alloc with NULL and |
| 2259 | * call init_pathspec() to set revs->prune_data here. |
| 2260 | * } |
| 2261 | */ |
Felipe Contreras | 9e57ac5 | 2013-10-31 03:25:43 -0600 | [diff] [blame] | 2262 | ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc); |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 2263 | prune_data.path[prune_data.nr++] = NULL; |
Nguyễn Thái Ngọc Duy | 0fdc2ae | 2013-07-14 15:35:31 +0700 | [diff] [blame] | 2264 | parse_pathspec(&revs->prune_data, 0, 0, |
| 2265 | revs->prefix, prune_data.path); |
Junio C Hamano | 4da5af3 | 2011-05-11 14:01:19 -0700 | [diff] [blame] | 2266 | } |
Junio C Hamano | 5486ef0 | 2009-11-20 02:33:28 -0800 | [diff] [blame] | 2267 | |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2268 | if (revs->def == NULL) |
Junio C Hamano | 32962c9 | 2010-03-08 22:58:09 -0800 | [diff] [blame] | 2269 | revs->def = opt ? opt->def : NULL; |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 2270 | if (opt && opt->tweak) |
| 2271 | opt->tweak(revs, opt); |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2272 | if (revs->show_merge) |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 2273 | prepare_show_merge(revs); |
Dave Olszewski | 8fcaca3 | 2010-03-13 14:47:05 -0800 | [diff] [blame] | 2274 | if (revs->def && !revs->pending.nr && !got_rev_arg) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2275 | unsigned char sha1[20]; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2276 | struct object *object; |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 2277 | struct object_context oc; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 2278 | if (get_sha1_with_context(revs->def, 0, sha1, &oc)) |
Pierre Habouzit | 02e5422 | 2008-07-08 15:19:33 +0200 | [diff] [blame] | 2279 | die("bad default revision '%s'", revs->def); |
| 2280 | object = get_reference(revs, revs->def, sha1, 0); |
Junio C Hamano | 249c8f4 | 2012-07-02 12:56:44 -0700 | [diff] [blame] | 2281 | add_pending_object_with_mode(revs, object, revs->def, oc.mode); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2282 | } |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 2283 | |
Linus Torvalds | b7bb760 | 2007-09-29 09:50:39 -0700 | [diff] [blame] | 2284 | /* Did the user ask for any diff output? Run the diff! */ |
| 2285 | if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) |
| 2286 | revs->diff = 1; |
| 2287 | |
Arjen Laarhoven | 0faf2da | 2007-12-25 12:06:47 +0100 | [diff] [blame] | 2288 | /* Pickaxe, diff-filter and rename following need diffs */ |
| 2289 | if (revs->diffopt.pickaxe || |
| 2290 | revs->diffopt.filter || |
| 2291 | DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES)) |
Linus Torvalds | b7bb760 | 2007-09-29 09:50:39 -0700 | [diff] [blame] | 2292 | revs->diff = 1; |
| 2293 | |
Junio C Hamano | 9dad9d2 | 2006-10-30 18:58:03 -0800 | [diff] [blame] | 2294 | if (revs->topo_order) |
Junio C Hamano | 5306968 | 2006-04-01 18:38:25 -0800 | [diff] [blame] | 2295 | revs->limited = 1; |
| 2296 | |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 2297 | if (revs->prune_data.nr) { |
Nguyễn Thái Ngọc Duy | bd1928d | 2013-07-14 15:35:58 +0700 | [diff] [blame] | 2298 | copy_pathspec(&revs->pruning.pathspec, &revs->prune_data); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 2299 | /* Can't prune commits with rename following: the paths change.. */ |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2300 | if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES)) |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 2301 | revs->prune = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2302 | if (!revs->full_diff) |
Nguyễn Thái Ngọc Duy | bd1928d | 2013-07-14 15:35:58 +0700 | [diff] [blame] | 2303 | copy_pathspec(&revs->diffopt.pathspec, |
| 2304 | &revs->prune_data); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 2305 | } |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 2306 | if (revs->combine_merges) |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2307 | revs->ignore_merges = 0; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2308 | revs->diffopt.abbrev = revs->abbrev; |
Thomas Rast | 12da1d1 | 2013-03-28 17:47:32 +0100 | [diff] [blame] | 2309 | |
| 2310 | if (revs->line_level_traverse) { |
| 2311 | revs->limited = 1; |
| 2312 | revs->topo_order = 1; |
| 2313 | } |
| 2314 | |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 2315 | diff_setup_done(&revs->diffopt); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 2316 | |
Junio C Hamano | 918d4e1 | 2012-10-09 16:40:03 -0700 | [diff] [blame] | 2317 | grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED, |
| 2318 | &revs->grep_filter); |
Jeff King | 0843acf | 2008-08-25 02:15:05 -0400 | [diff] [blame] | 2319 | compile_grep_patterns(&revs->grep_filter); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 2320 | |
Shawn O. Pearce | d56651c | 2007-08-19 22:33:43 -0400 | [diff] [blame] | 2321 | if (revs->reverse && revs->reflog_info) |
| 2322 | die("cannot combine --reverse with --walk-reflogs"); |
Junio C Hamano | 8bb6588 | 2008-07-08 15:25:44 -0700 | [diff] [blame] | 2323 | if (revs->rewrite_parents && revs->children.name) |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2324 | die("cannot combine --parents and --children"); |
Shawn O. Pearce | d56651c | 2007-08-19 22:33:43 -0400 | [diff] [blame] | 2325 | |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 2326 | /* |
| 2327 | * Limitations on the graph functionality |
| 2328 | */ |
| 2329 | if (revs->reverse && revs->graph) |
| 2330 | die("cannot combine --reverse with --graph"); |
| 2331 | |
| 2332 | if (revs->reflog_info && revs->graph) |
| 2333 | die("cannot combine --walk-reflogs with --graph"); |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2334 | if (!revs->reflog_info && revs->grep_filter.use_reflog_filter) |
| 2335 | die("cannot use --grep-reflog without --walk-reflogs"); |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 2336 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 2337 | return left; |
| 2338 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2339 | |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2340 | static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child) |
| 2341 | { |
| 2342 | struct commit_list *l = xcalloc(1, sizeof(*l)); |
| 2343 | |
| 2344 | l->item = child; |
| 2345 | l->next = add_decoration(&revs->children, &parent->object, l); |
| 2346 | } |
| 2347 | |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2348 | static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit) |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2349 | { |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2350 | struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2351 | struct commit_list **pp, *p; |
| 2352 | int surviving_parents; |
| 2353 | |
| 2354 | /* Examine existing parents while marking ones we have seen... */ |
| 2355 | pp = &commit->parents; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2356 | surviving_parents = 0; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2357 | while ((p = *pp) != NULL) { |
| 2358 | struct commit *parent = p->item; |
| 2359 | if (parent->object.flags & TMP_MARK) { |
| 2360 | *pp = p->next; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2361 | if (ts) |
| 2362 | compact_treesame(revs, commit, surviving_parents); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2363 | continue; |
| 2364 | } |
| 2365 | parent->object.flags |= TMP_MARK; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2366 | surviving_parents++; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2367 | pp = &p->next; |
| 2368 | } |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2369 | /* clear the temporary mark */ |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2370 | for (p = commit->parents; p; p = p->next) { |
| 2371 | p->item->object.flags &= ~TMP_MARK; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2372 | } |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2373 | /* no update_treesame() - removing duplicates can't affect TREESAME */ |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2374 | return surviving_parents; |
| 2375 | } |
| 2376 | |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2377 | struct merge_simplify_state { |
| 2378 | struct commit *simplified; |
| 2379 | }; |
| 2380 | |
| 2381 | static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit) |
| 2382 | { |
| 2383 | struct merge_simplify_state *st; |
| 2384 | |
| 2385 | st = lookup_decoration(&revs->merge_simplification, &commit->object); |
| 2386 | if (!st) { |
| 2387 | st = xcalloc(1, sizeof(*st)); |
| 2388 | add_decoration(&revs->merge_simplification, &commit->object, st); |
| 2389 | } |
| 2390 | return st; |
| 2391 | } |
| 2392 | |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2393 | static int mark_redundant_parents(struct rev_info *revs, struct commit *commit) |
| 2394 | { |
| 2395 | struct commit_list *h = reduce_heads(commit->parents); |
| 2396 | int i = 0, marked = 0; |
| 2397 | struct commit_list *po, *pn; |
| 2398 | |
| 2399 | /* Want these for sanity-checking only */ |
| 2400 | int orig_cnt = commit_list_count(commit->parents); |
| 2401 | int cnt = commit_list_count(h); |
| 2402 | |
| 2403 | /* |
| 2404 | * Not ready to remove items yet, just mark them for now, based |
| 2405 | * on the output of reduce_heads(). reduce_heads outputs the reduced |
| 2406 | * set in its original order, so this isn't too hard. |
| 2407 | */ |
| 2408 | po = commit->parents; |
| 2409 | pn = h; |
| 2410 | while (po) { |
| 2411 | if (pn && po->item == pn->item) { |
| 2412 | pn = pn->next; |
| 2413 | i++; |
| 2414 | } else { |
| 2415 | po->item->object.flags |= TMP_MARK; |
| 2416 | marked++; |
| 2417 | } |
| 2418 | po=po->next; |
| 2419 | } |
| 2420 | |
| 2421 | if (i != cnt || cnt+marked != orig_cnt) |
| 2422 | die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked); |
| 2423 | |
| 2424 | free_commit_list(h); |
| 2425 | |
| 2426 | return marked; |
| 2427 | } |
| 2428 | |
Kevin Bracey | 143f1ea | 2013-05-16 18:32:37 +0300 | [diff] [blame] | 2429 | static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit) |
| 2430 | { |
| 2431 | struct commit_list *p; |
| 2432 | int marked = 0; |
| 2433 | |
| 2434 | for (p = commit->parents; p; p = p->next) { |
| 2435 | struct commit *parent = p->item; |
| 2436 | if (!parent->parents && (parent->object.flags & TREESAME)) { |
| 2437 | parent->object.flags |= TMP_MARK; |
| 2438 | marked++; |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | return marked; |
| 2443 | } |
| 2444 | |
Kevin Bracey | 9c129ea | 2013-05-16 18:32:36 +0300 | [diff] [blame] | 2445 | /* |
| 2446 | * Awkward naming - this means one parent we are TREESAME to. |
| 2447 | * cf mark_treesame_root_parents: root parents that are TREESAME (to an |
| 2448 | * empty tree). Better name suggestions? |
| 2449 | */ |
| 2450 | static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit) |
| 2451 | { |
| 2452 | struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object); |
| 2453 | struct commit *unmarked = NULL, *marked = NULL; |
| 2454 | struct commit_list *p; |
| 2455 | unsigned n; |
| 2456 | |
| 2457 | for (p = commit->parents, n = 0; p; p = p->next, n++) { |
| 2458 | if (ts->treesame[n]) { |
| 2459 | if (p->item->object.flags & TMP_MARK) { |
| 2460 | if (!marked) |
| 2461 | marked = p->item; |
| 2462 | } else { |
| 2463 | if (!unmarked) { |
| 2464 | unmarked = p->item; |
| 2465 | break; |
| 2466 | } |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * If we are TREESAME to a marked-for-deletion parent, but not to any |
| 2473 | * unmarked parents, unmark the first TREESAME parent. This is the |
| 2474 | * parent that the default simplify_history==1 scan would have followed, |
| 2475 | * and it doesn't make sense to omit that path when asking for a |
| 2476 | * simplified full history. Retaining it improves the chances of |
| 2477 | * understanding odd missed merges that took an old version of a file. |
| 2478 | * |
| 2479 | * Example: |
| 2480 | * |
| 2481 | * I--------*X A modified the file, but mainline merge X used |
| 2482 | * \ / "-s ours", so took the version from I. X is |
| 2483 | * `-*A--' TREESAME to I and !TREESAME to A. |
| 2484 | * |
| 2485 | * Default log from X would produce "I". Without this check, |
| 2486 | * --full-history --simplify-merges would produce "I-A-X", showing |
| 2487 | * the merge commit X and that it changed A, but not making clear that |
| 2488 | * it had just taken the I version. With this check, the topology above |
| 2489 | * is retained. |
| 2490 | * |
| 2491 | * Note that it is possible that the simplification chooses a different |
| 2492 | * TREESAME parent from the default, in which case this test doesn't |
| 2493 | * activate, and we _do_ drop the default parent. Example: |
| 2494 | * |
| 2495 | * I------X A modified the file, but it was reverted in B, |
| 2496 | * \ / meaning mainline merge X is TREESAME to both |
| 2497 | * *A-*B parents. |
| 2498 | * |
| 2499 | * Default log would produce "I" by following the first parent; |
| 2500 | * --full-history --simplify-merges will produce "I-A-B". But this is a |
| 2501 | * reasonable result - it presents a logical full history leading from |
| 2502 | * I to X, and X is not an important merge. |
| 2503 | */ |
| 2504 | if (!unmarked && marked) { |
| 2505 | marked->object.flags &= ~TMP_MARK; |
| 2506 | return 1; |
| 2507 | } |
| 2508 | |
| 2509 | return 0; |
| 2510 | } |
| 2511 | |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2512 | static int remove_marked_parents(struct rev_info *revs, struct commit *commit) |
| 2513 | { |
| 2514 | struct commit_list **pp, *p; |
| 2515 | int nth_parent, removed = 0; |
| 2516 | |
| 2517 | pp = &commit->parents; |
| 2518 | nth_parent = 0; |
| 2519 | while ((p = *pp) != NULL) { |
| 2520 | struct commit *parent = p->item; |
| 2521 | if (parent->object.flags & TMP_MARK) { |
| 2522 | parent->object.flags &= ~TMP_MARK; |
| 2523 | *pp = p->next; |
| 2524 | free(p); |
| 2525 | removed++; |
| 2526 | compact_treesame(revs, commit, nth_parent); |
| 2527 | continue; |
| 2528 | } |
| 2529 | pp = &p->next; |
| 2530 | nth_parent++; |
| 2531 | } |
| 2532 | |
| 2533 | /* Removing parents can only increase TREESAMEness */ |
| 2534 | if (removed && !(commit->object.flags & TREESAME)) |
| 2535 | update_treesame(revs, commit); |
| 2536 | |
| 2537 | return nth_parent; |
| 2538 | } |
| 2539 | |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2540 | 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] | 2541 | { |
| 2542 | struct commit_list *p; |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2543 | struct commit *parent; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2544 | struct merge_simplify_state *st, *pst; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2545 | int cnt; |
| 2546 | |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2547 | st = locate_simplify_state(revs, commit); |
| 2548 | |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2549 | /* |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2550 | * Have we handled this one? |
| 2551 | */ |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2552 | if (st->simplified) |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2553 | return tail; |
| 2554 | |
| 2555 | /* |
| 2556 | * An UNINTERESTING commit simplifies to itself, so does a |
| 2557 | * root commit. We do not rewrite parents of such commit |
| 2558 | * anyway. |
| 2559 | */ |
| 2560 | if ((commit->object.flags & UNINTERESTING) || !commit->parents) { |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2561 | st->simplified = commit; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2562 | return tail; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2566 | * Do we know what commit all of our parents that matter |
| 2567 | * should be rewritten to? Otherwise we are not ready to |
| 2568 | * rewrite this one yet. |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2569 | */ |
| 2570 | for (cnt = 0, p = commit->parents; p; p = p->next) { |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2571 | pst = locate_simplify_state(revs, p->item); |
| 2572 | if (!pst->simplified) { |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2573 | tail = &commit_list_insert(p->item, tail)->next; |
| 2574 | cnt++; |
| 2575 | } |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2576 | if (revs->first_parent_only) |
| 2577 | break; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2578 | } |
Junio C Hamano | 53030f8 | 2008-08-18 00:37:34 -0700 | [diff] [blame] | 2579 | if (cnt) { |
| 2580 | tail = &commit_list_insert(commit, tail)->next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2581 | return tail; |
Junio C Hamano | 53030f8 | 2008-08-18 00:37:34 -0700 | [diff] [blame] | 2582 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2583 | |
| 2584 | /* |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2585 | * Rewrite our list of parents. Note that this cannot |
| 2586 | * affect our TREESAME flags in any way - a commit is |
| 2587 | * always TREESAME to its simplification. |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2588 | */ |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2589 | for (p = commit->parents; p; p = p->next) { |
| 2590 | pst = locate_simplify_state(revs, p->item); |
| 2591 | p->item = pst->simplified; |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2592 | if (revs->first_parent_only) |
| 2593 | break; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2594 | } |
Junio C Hamano | 4b7f53d | 2013-01-17 14:23:03 -0800 | [diff] [blame] | 2595 | |
Junio C Hamano | 0290bf1 | 2013-04-08 13:10:27 -0700 | [diff] [blame] | 2596 | if (revs->first_parent_only) |
Junio C Hamano | 6e513ba | 2012-06-08 14:56:03 -0700 | [diff] [blame] | 2597 | cnt = 1; |
Junio C Hamano | 0290bf1 | 2013-04-08 13:10:27 -0700 | [diff] [blame] | 2598 | else |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2599 | cnt = remove_duplicate_parents(revs, commit); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2600 | |
| 2601 | /* |
| 2602 | * It is possible that we are a merge and one side branch |
| 2603 | * does not have any commit that touches the given paths; |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2604 | * in such a case, the immediate parent from that branch |
| 2605 | * will be rewritten to be the merge base. |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2606 | * |
| 2607 | * o----X X: the commit we are looking at; |
| 2608 | * / / o: a commit that touches the paths; |
| 2609 | * ---o----' |
| 2610 | * |
Kevin Bracey | 143f1ea | 2013-05-16 18:32:37 +0300 | [diff] [blame] | 2611 | * Further, a merge of an independent branch that doesn't |
| 2612 | * touch the path will reduce to a treesame root parent: |
| 2613 | * |
| 2614 | * ----o----X X: the commit we are looking at; |
| 2615 | * / o: a commit that touches the paths; |
| 2616 | * r r: a root commit not touching the paths |
| 2617 | * |
| 2618 | * Detect and simplify both cases. |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2619 | */ |
| 2620 | if (1 < cnt) { |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2621 | int marked = mark_redundant_parents(revs, commit); |
Kevin Bracey | 143f1ea | 2013-05-16 18:32:37 +0300 | [diff] [blame] | 2622 | marked += mark_treesame_root_parents(revs, commit); |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2623 | if (marked) |
Kevin Bracey | 9c129ea | 2013-05-16 18:32:36 +0300 | [diff] [blame] | 2624 | marked -= leave_one_treesame_to_parent(revs, commit); |
| 2625 | if (marked) |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2626 | cnt = remove_marked_parents(revs, commit); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | /* |
| 2630 | * A commit simplifies to itself if it is a root, if it is |
| 2631 | * UNINTERESTING, if it touches the given paths, or if it is a |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2632 | * merge and its parents don't simplify to one relevant commit |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2633 | * (the first two cases are already handled at the beginning of |
| 2634 | * this function). |
| 2635 | * |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2636 | * Otherwise, it simplifies to what its sole relevant parent |
| 2637 | * simplifies to. |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2638 | */ |
| 2639 | if (!cnt || |
| 2640 | (commit->object.flags & UNINTERESTING) || |
| 2641 | !(commit->object.flags & TREESAME) || |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2642 | (parent = one_relevant_parent(revs, commit->parents)) == NULL) |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2643 | st->simplified = commit; |
| 2644 | else { |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2645 | pst = locate_simplify_state(revs, parent); |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2646 | st->simplified = pst->simplified; |
| 2647 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2648 | return tail; |
| 2649 | } |
| 2650 | |
| 2651 | static void simplify_merges(struct rev_info *revs) |
| 2652 | { |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2653 | struct commit_list *list, *next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2654 | struct commit_list *yet_to_do, **tail; |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2655 | struct commit *commit; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2656 | |
Junio C Hamano | 5eac739 | 2008-08-14 13:52:36 -0700 | [diff] [blame] | 2657 | if (!revs->prune) |
| 2658 | return; |
Junio C Hamano | 6534703 | 2008-08-03 17:47:16 -0700 | [diff] [blame] | 2659 | |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2660 | /* feed the list reversed */ |
| 2661 | yet_to_do = NULL; |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2662 | for (list = revs->commits; list; list = next) { |
| 2663 | commit = list->item; |
| 2664 | next = list->next; |
| 2665 | /* |
| 2666 | * Do not free(list) here yet; the original list |
| 2667 | * is used later in this function. |
| 2668 | */ |
| 2669 | commit_list_insert(commit, &yet_to_do); |
| 2670 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2671 | while (yet_to_do) { |
| 2672 | list = yet_to_do; |
| 2673 | yet_to_do = NULL; |
| 2674 | tail = &yet_to_do; |
| 2675 | while (list) { |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2676 | commit = list->item; |
| 2677 | next = list->next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2678 | free(list); |
| 2679 | list = next; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2680 | tail = simplify_one(revs, commit, tail); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | /* clean up the result, removing the simplified ones */ |
| 2685 | list = revs->commits; |
| 2686 | revs->commits = NULL; |
| 2687 | tail = &revs->commits; |
| 2688 | while (list) { |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2689 | struct merge_simplify_state *st; |
Junio C Hamano | ab9d75a | 2012-06-08 14:50:22 -0700 | [diff] [blame] | 2690 | |
| 2691 | commit = list->item; |
| 2692 | next = list->next; |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2693 | free(list); |
| 2694 | list = next; |
Junio C Hamano | faf0156 | 2008-08-14 10:59:44 -0700 | [diff] [blame] | 2695 | st = locate_simplify_state(revs, commit); |
| 2696 | if (st->simplified == commit) |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2697 | tail = &commit_list_insert(commit, tail)->next; |
| 2698 | } |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2701 | static void set_children(struct rev_info *revs) |
| 2702 | { |
| 2703 | struct commit_list *l; |
| 2704 | for (l = revs->commits; l; l = l->next) { |
| 2705 | struct commit *commit = l->item; |
| 2706 | struct commit_list *p; |
| 2707 | |
| 2708 | for (p = commit->parents; p; p = p->next) |
| 2709 | add_child(revs, p->item, commit); |
| 2710 | } |
| 2711 | } |
| 2712 | |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 2713 | void reset_revision_walk(void) |
| 2714 | { |
| 2715 | clear_object_flags(SEEN | ADDED | SHOWN); |
| 2716 | } |
| 2717 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2718 | int prepare_revision_walk(struct rev_info *revs) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2719 | { |
Jeff King | 1da1e07 | 2014-10-15 18:35:12 -0400 | [diff] [blame] | 2720 | int i; |
| 2721 | struct object_array old_pending; |
René Scharfe | 2e7da8e | 2012-04-25 22:35:41 +0200 | [diff] [blame] | 2722 | struct commit_list **next = &revs->commits; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2723 | |
Jeff King | 1da1e07 | 2014-10-15 18:35:12 -0400 | [diff] [blame] | 2724 | memcpy(&old_pending, &revs->pending, sizeof(old_pending)); |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 2725 | revs->pending.nr = 0; |
| 2726 | revs->pending.alloc = 0; |
| 2727 | revs->pending.objects = NULL; |
Jeff King | 1da1e07 | 2014-10-15 18:35:12 -0400 | [diff] [blame] | 2728 | for (i = 0; i < old_pending.nr; i++) { |
| 2729 | struct object_array_entry *e = old_pending.objects + i; |
Jeff King | 2073949 | 2014-10-15 18:43:19 -0400 | [diff] [blame] | 2730 | struct commit *commit = handle_commit(revs, e); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2731 | if (commit) { |
| 2732 | if (!(commit->object.flags & SEEN)) { |
| 2733 | commit->object.flags |= SEEN; |
René Scharfe | 2e7da8e | 2012-04-25 22:35:41 +0200 | [diff] [blame] | 2734 | next = commit_list_append(commit, next); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2735 | } |
| 2736 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2737 | } |
René Scharfe | 4a43d37 | 2011-10-01 17:56:08 +0200 | [diff] [blame] | 2738 | if (!revs->leak_pending) |
Jeff King | 1da1e07 | 2014-10-15 18:35:12 -0400 | [diff] [blame] | 2739 | object_array_clear(&old_pending); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 2740 | |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2741 | /* Signal whether we need per-parent treesame decoration */ |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2742 | if (revs->simplify_merges || |
| 2743 | (revs->limited && limiting_can_increase_treesame(revs))) |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2744 | revs->treesame.name = "treesame"; |
| 2745 | |
Martin von Zweigbergk | ca92e59 | 2012-08-28 23:15:54 -0700 | [diff] [blame] | 2746 | if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED) |
| 2747 | commit_list_sort_by_date(&revs->commits); |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 2748 | if (revs->no_walk) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2749 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2750 | if (revs->limited) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2751 | if (limit_list(revs) < 0) |
| 2752 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2753 | if (revs->topo_order) |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 2754 | sort_in_topological_order(&revs->commits, revs->sort_order); |
Thomas Rast | 12da1d1 | 2013-03-28 17:47:32 +0100 | [diff] [blame] | 2755 | if (revs->line_level_traverse) |
| 2756 | line_log_filter(revs); |
Junio C Hamano | 6546b59 | 2008-07-31 01:17:41 -0700 | [diff] [blame] | 2757 | if (revs->simplify_merges) |
| 2758 | simplify_merges(revs); |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2759 | if (revs->children.name) |
| 2760 | set_children(revs); |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2761 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2764 | 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] | 2765 | { |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 2766 | struct commit_list *cache = NULL; |
| 2767 | |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2768 | for (;;) { |
| 2769 | struct commit *p = *pp; |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 2770 | if (!revs->limited) |
Alexander N. Gavrilov | fce87ae | 2008-07-12 22:00:57 +0400 | [diff] [blame] | 2771 | if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2772 | return rewrite_one_error; |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 2773 | if (p->object.flags & UNINTERESTING) |
| 2774 | return rewrite_one_ok; |
| 2775 | if (!(p->object.flags & TREESAME)) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2776 | return rewrite_one_ok; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2777 | if (!p->parents) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2778 | return rewrite_one_noparents; |
Kevin Bracey | 4d82660 | 2013-05-16 18:32:39 +0300 | [diff] [blame] | 2779 | if ((p = one_relevant_parent(revs, p->parents)) == NULL) |
| 2780 | return rewrite_one_ok; |
| 2781 | *pp = p; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2782 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2783 | } |
| 2784 | |
Bo Yang | c7edcae | 2013-03-28 17:47:31 +0100 | [diff] [blame] | 2785 | int rewrite_parents(struct rev_info *revs, struct commit *commit, |
| 2786 | rewrite_parent_fn_t rewrite_parent) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2787 | { |
| 2788 | struct commit_list **pp = &commit->parents; |
| 2789 | while (*pp) { |
| 2790 | struct commit_list *parent = *pp; |
Bo Yang | c7edcae | 2013-03-28 17:47:31 +0100 | [diff] [blame] | 2791 | switch (rewrite_parent(revs, &parent->item)) { |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2792 | case rewrite_one_ok: |
| 2793 | break; |
| 2794 | case rewrite_one_noparents: |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2795 | *pp = parent->next; |
| 2796 | continue; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2797 | case rewrite_one_error: |
| 2798 | return -1; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2799 | } |
| 2800 | pp = &parent->next; |
| 2801 | } |
Kevin Bracey | d0af663 | 2013-05-16 18:32:34 +0300 | [diff] [blame] | 2802 | remove_duplicate_parents(revs, commit); |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 2803 | return 0; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 2804 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 2805 | |
Antoine Pelisse | d72fbe8 | 2013-01-05 22:26:45 +0100 | [diff] [blame] | 2806 | static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap) |
| 2807 | { |
| 2808 | char *person, *endp; |
| 2809 | size_t len, namelen, maillen; |
| 2810 | const char *name; |
| 2811 | const char *mail; |
| 2812 | struct ident_split ident; |
| 2813 | |
| 2814 | person = strstr(buf->buf, what); |
| 2815 | if (!person) |
| 2816 | return 0; |
| 2817 | |
| 2818 | person += strlen(what); |
| 2819 | endp = strchr(person, '\n'); |
| 2820 | if (!endp) |
| 2821 | return 0; |
| 2822 | |
| 2823 | len = endp - person; |
| 2824 | |
| 2825 | if (split_ident_line(&ident, person, len)) |
| 2826 | return 0; |
| 2827 | |
| 2828 | mail = ident.mail_begin; |
| 2829 | maillen = ident.mail_end - ident.mail_begin; |
| 2830 | name = ident.name_begin; |
| 2831 | namelen = ident.name_end - ident.name_begin; |
| 2832 | |
| 2833 | if (map_user(mailmap, &mail, &maillen, &name, &namelen)) { |
| 2834 | struct strbuf namemail = STRBUF_INIT; |
| 2835 | |
| 2836 | strbuf_addf(&namemail, "%.*s <%.*s>", |
| 2837 | (int)namelen, name, (int)maillen, mail); |
| 2838 | |
| 2839 | strbuf_splice(buf, ident.name_begin - buf->buf, |
| 2840 | ident.mail_end - ident.name_begin + 1, |
| 2841 | namemail.buf, namemail.len); |
| 2842 | |
| 2843 | strbuf_release(&namemail); |
| 2844 | |
| 2845 | return 1; |
| 2846 | } |
| 2847 | |
| 2848 | return 0; |
| 2849 | } |
| 2850 | |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 2851 | static int commit_match(struct commit *commit, struct rev_info *opt) |
| 2852 | { |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2853 | int retval; |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2854 | const char *encoding; |
Jeff King | b000c59 | 2014-06-10 17:39:30 -0400 | [diff] [blame] | 2855 | const char *message; |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2856 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2857 | |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 2858 | if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list) |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 2859 | return 1; |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2860 | |
| 2861 | /* Prepend "fake" headers as needed */ |
| 2862 | if (opt->grep_filter.use_reflog_filter) { |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2863 | strbuf_addstr(&buf, "reflog "); |
| 2864 | get_reflog_message(&buf, opt->reflog_info); |
| 2865 | strbuf_addch(&buf, '\n'); |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2866 | } |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2867 | |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2868 | /* |
| 2869 | * We grep in the user's output encoding, under the assumption that it |
| 2870 | * is the encoding they are most likely to write their grep pattern |
| 2871 | * for. In addition, it means we will match the "notes" encoding below, |
| 2872 | * so we will not end up with a buffer that has two different encodings |
| 2873 | * in it. |
| 2874 | */ |
| 2875 | encoding = get_log_output_encoding(); |
Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-19 09:08:40 +1000 | [diff] [blame] | 2876 | message = logmsg_reencode(commit, NULL, encoding); |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2877 | |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2878 | /* Copy the commit to temporary if we are using "fake" headers */ |
| 2879 | if (buf.len) |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2880 | strbuf_addstr(&buf, message); |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 2881 | |
Junio C Hamano | df874fa | 2013-01-08 00:02:49 -0800 | [diff] [blame] | 2882 | if (opt->grep_filter.header_list && opt->mailmap) { |
Antoine Pelisse | d72fbe8 | 2013-01-05 22:26:45 +0100 | [diff] [blame] | 2883 | if (!buf.len) |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2884 | strbuf_addstr(&buf, message); |
Antoine Pelisse | d72fbe8 | 2013-01-05 22:26:45 +0100 | [diff] [blame] | 2885 | |
| 2886 | commit_rewrite_person(&buf, "\nauthor ", opt->mailmap); |
| 2887 | commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap); |
| 2888 | } |
| 2889 | |
Nguyễn Thái Ngọc Duy | 38cfe91 | 2012-09-29 11:41:29 +0700 | [diff] [blame] | 2890 | /* Append "fake" message parts as needed */ |
| 2891 | if (opt->show_notes) { |
| 2892 | if (!buf.len) |
Jeff King | 04deccd | 2013-02-11 15:59:58 -0500 | [diff] [blame] | 2893 | strbuf_addstr(&buf, message); |
| 2894 | format_display_notes(commit->object.sha1, &buf, encoding, 1); |
Nguyễn Thái Ngọc Duy | 38cfe91 | 2012-09-29 11:41:29 +0700 | [diff] [blame] | 2895 | } |
| 2896 | |
Jeff King | b000c59 | 2014-06-10 17:39:30 -0400 | [diff] [blame] | 2897 | /* |
| 2898 | * Find either in the original commit message, or in the temporary. |
| 2899 | * Note that we cast away the constness of "message" here. It is |
| 2900 | * const because it may come from the cached commit buffer. That's OK, |
| 2901 | * because we know that it is modifiable heap memory, and that while |
| 2902 | * grep_buffer may modify it for speed, it will restore any |
| 2903 | * changes before returning. |
| 2904 | */ |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2905 | if (buf.len) |
| 2906 | retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len); |
| 2907 | else |
| 2908 | retval = grep_buffer(&opt->grep_filter, |
Jeff King | b000c59 | 2014-06-10 17:39:30 -0400 | [diff] [blame] | 2909 | (char *)message, strlen(message)); |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2910 | strbuf_release(&buf); |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 2911 | unuse_commit_buffer(commit, message); |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 2912 | return retval; |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 2913 | } |
| 2914 | |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 2915 | static inline int want_ancestry(const struct rev_info *revs) |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2916 | { |
Junio C Hamano | 8bb6588 | 2008-07-08 15:25:44 -0700 | [diff] [blame] | 2917 | return (revs->rewrite_parents || revs->children.name); |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 2920 | 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] | 2921 | { |
| 2922 | if (commit->object.flags & SHOWN) |
| 2923 | return commit_ignore; |
Brandon Casey | 4d6acb7 | 2009-03-19 22:47:54 -0500 | [diff] [blame] | 2924 | if (revs->unpacked && has_sha1_pack(commit->object.sha1)) |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2925 | return commit_ignore; |
Linus Torvalds | 3131b71 | 2008-02-09 14:02:07 -0800 | [diff] [blame] | 2926 | if (revs->show_all) |
| 2927 | return commit_show; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2928 | if (commit->object.flags & UNINTERESTING) |
| 2929 | return commit_ignore; |
| 2930 | if (revs->min_age != -1 && (commit->date > revs->min_age)) |
| 2931 | return commit_ignore; |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 2932 | if (revs->min_parents || (revs->max_parents >= 0)) { |
Kevin Bracey | bf3418b | 2013-05-16 18:32:40 +0300 | [diff] [blame] | 2933 | int n = commit_list_count(commit->parents); |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 2934 | if ((n < revs->min_parents) || |
| 2935 | ((revs->max_parents >= 0) && (n > revs->max_parents))) |
| 2936 | return commit_ignore; |
| 2937 | } |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2938 | if (!commit_match(commit, revs)) |
| 2939 | return commit_ignore; |
Linus Torvalds | 53b2c82 | 2007-11-05 13:22:34 -0800 | [diff] [blame] | 2940 | if (revs->prune && revs->dense) { |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2941 | /* Commit without changes? */ |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 2942 | if (commit->object.flags & TREESAME) { |
Kevin Bracey | bf3418b | 2013-05-16 18:32:40 +0300 | [diff] [blame] | 2943 | int n; |
| 2944 | struct commit_list *p; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2945 | /* drop merges unless we want parenthood */ |
Junio C Hamano | f35f560 | 2008-04-03 02:12:06 -0700 | [diff] [blame] | 2946 | if (!want_ancestry(revs)) |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2947 | return commit_ignore; |
Kevin Bracey | bf3418b | 2013-05-16 18:32:40 +0300 | [diff] [blame] | 2948 | /* |
| 2949 | * If we want ancestry, then need to keep any merges |
| 2950 | * between relevant commits to tie together topology. |
| 2951 | * For consistency with TREESAME and simplification |
| 2952 | * use "relevant" here rather than just INTERESTING, |
| 2953 | * to treat bottom commit(s) as part of the topology. |
| 2954 | */ |
| 2955 | for (n = 0, p = commit->parents; p; p = p->next) |
| 2956 | if (relevant_commit(p->item)) |
| 2957 | if (++n >= 2) |
| 2958 | return commit_show; |
| 2959 | return commit_ignore; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2960 | } |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 2961 | } |
| 2962 | return commit_show; |
| 2963 | } |
| 2964 | |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 2965 | enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit) |
| 2966 | { |
| 2967 | enum commit_action action = get_commit_action(revs, commit); |
| 2968 | |
| 2969 | if (action == commit_show && |
| 2970 | !revs->show_all && |
| 2971 | revs->prune && revs->dense && want_ancestry(revs)) { |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 2972 | /* |
| 2973 | * --full-diff on simplified parents is no good: it |
| 2974 | * will show spurious changes from the commits that |
| 2975 | * were elided. So we save the parents on the side |
| 2976 | * when --full-diff is in effect. |
| 2977 | */ |
| 2978 | if (revs->full_diff) |
| 2979 | save_parents(revs, commit); |
Bo Yang | c7edcae | 2013-03-28 17:47:31 +0100 | [diff] [blame] | 2980 | if (rewrite_parents(revs, commit, rewrite_one) < 0) |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 2981 | return commit_error; |
| 2982 | } |
| 2983 | return action; |
| 2984 | } |
| 2985 | |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 2986 | static void track_linear(struct rev_info *revs, struct commit *commit) |
| 2987 | { |
| 2988 | if (revs->track_first_time) { |
| 2989 | revs->linear = 1; |
| 2990 | revs->track_first_time = 0; |
| 2991 | } else { |
| 2992 | struct commit_list *p; |
| 2993 | for (p = revs->previous_parents; p; p = p->next) |
| 2994 | if (p->item == NULL || /* first commit */ |
| 2995 | !hashcmp(p->item->object.sha1, commit->object.sha1)) |
| 2996 | break; |
| 2997 | revs->linear = p != NULL; |
| 2998 | } |
| 2999 | if (revs->reverse) { |
| 3000 | if (revs->linear) |
| 3001 | commit->object.flags |= TRACK_LINEAR; |
| 3002 | } |
| 3003 | free_commit_list(revs->previous_parents); |
| 3004 | revs->previous_parents = copy_commit_list(commit->parents); |
| 3005 | } |
| 3006 | |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3007 | static struct commit *get_revision_1(struct rev_info *revs) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 3008 | { |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3009 | if (!revs->commits) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 3010 | return NULL; |
| 3011 | |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 3012 | do { |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 3013 | struct commit_list *entry = revs->commits; |
| 3014 | struct commit *commit = entry->item; |
Linus Torvalds | ea5ed3a | 2006-03-05 09:53:52 -0800 | [diff] [blame] | 3015 | |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 3016 | revs->commits = entry->next; |
| 3017 | free(entry); |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 3018 | |
Jeff King | ffa1eea | 2010-11-21 23:42:53 -0500 | [diff] [blame] | 3019 | if (revs->reflog_info) { |
Thomas Rast | 838f9a1 | 2013-08-03 12:36:15 +0200 | [diff] [blame] | 3020 | save_parents(revs, commit); |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 3021 | fake_reflog_parent(revs->reflog_info, commit); |
Jeff King | ffa1eea | 2010-11-21 23:42:53 -0500 | [diff] [blame] | 3022 | commit->object.flags &= ~(ADDED | SEEN | SHOWN); |
| 3023 | } |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 3024 | |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 3025 | /* |
| 3026 | * 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] | 3027 | * the parents here. We also need to do the date-based limiting |
| 3028 | * that we'd otherwise have done in limit_list(). |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 3029 | */ |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 3030 | if (!revs->limited) { |
Jan Harkes | 744f498 | 2006-10-30 20:37:49 -0500 | [diff] [blame] | 3031 | if (revs->max_age != -1 && |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3032 | (commit->date < revs->max_age)) |
| 3033 | continue; |
Vicent Marti | 2db1a43 | 2014-03-28 06:00:43 -0400 | [diff] [blame] | 3034 | if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) { |
| 3035 | if (!revs->ignore_missing_links) |
| 3036 | die("Failed to traverse parents of commit %s", |
| 3037 | sha1_to_hex(commit->object.sha1)); |
| 3038 | } |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 3039 | } |
Junio C Hamano | 1b65a5a | 2006-04-16 18:12:49 -0700 | [diff] [blame] | 3040 | |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 3041 | switch (simplify_commit(revs, commit)) { |
| 3042 | case commit_ignore: |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 3043 | continue; |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 3044 | case commit_error: |
Junio C Hamano | ed62089 | 2009-02-11 01:27:43 -0800 | [diff] [blame] | 3045 | die("Failed to simplify parents of commit %s", |
| 3046 | sha1_to_hex(commit->object.sha1)); |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 3047 | default: |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 3048 | if (revs->track_linear) |
| 3049 | track_linear(revs, commit); |
Linus Torvalds | 252a7c0 | 2007-11-04 12:12:05 -0800 | [diff] [blame] | 3050 | return commit; |
Junio C Hamano | 384e99a | 2006-03-27 23:58:34 -0800 | [diff] [blame] | 3051 | } |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 3052 | } while (revs->commits); |
| 3053 | return NULL; |
| 3054 | } |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3055 | |
Michael Haggerty | be6754c | 2013-05-25 11:08:09 +0200 | [diff] [blame] | 3056 | /* |
| 3057 | * Return true for entries that have not yet been shown. (This is an |
| 3058 | * object_array_each_func_t.) |
| 3059 | */ |
| 3060 | static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused) |
| 3061 | { |
| 3062 | return !(entry->item->flags & SHOWN); |
| 3063 | } |
| 3064 | |
| 3065 | /* |
| 3066 | * If array is on the verge of a realloc, garbage-collect any entries |
| 3067 | * that have already been shown to try to free up some space. |
| 3068 | */ |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3069 | static void gc_boundary(struct object_array *array) |
| 3070 | { |
Michael Haggerty | be6754c | 2013-05-25 11:08:09 +0200 | [diff] [blame] | 3071 | if (array->nr == array->alloc) |
| 3072 | object_array_filter(array, entry_unshown, NULL); |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3073 | } |
| 3074 | |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 3075 | static void create_boundary_commit_list(struct rev_info *revs) |
| 3076 | { |
| 3077 | unsigned i; |
| 3078 | struct commit *c; |
| 3079 | struct object_array *array = &revs->boundary_commits; |
| 3080 | struct object_array_entry *objects = array->objects; |
| 3081 | |
| 3082 | /* |
| 3083 | * If revs->commits is non-NULL at this point, an error occurred in |
| 3084 | * get_revision_1(). Ignore the error and continue printing the |
| 3085 | * boundary commits anyway. (This is what the code has always |
| 3086 | * done.) |
| 3087 | */ |
| 3088 | if (revs->commits) { |
| 3089 | free_commit_list(revs->commits); |
| 3090 | revs->commits = NULL; |
| 3091 | } |
| 3092 | |
| 3093 | /* |
| 3094 | * Put all of the actual boundary commits from revs->boundary_commits |
| 3095 | * into revs->commits |
| 3096 | */ |
| 3097 | for (i = 0; i < array->nr; i++) { |
| 3098 | c = (struct commit *)(objects[i].item); |
| 3099 | if (!c) |
| 3100 | continue; |
| 3101 | if (!(c->object.flags & CHILD_SHOWN)) |
| 3102 | continue; |
| 3103 | if (c->object.flags & (SHOWN | BOUNDARY)) |
| 3104 | continue; |
| 3105 | c->object.flags |= BOUNDARY; |
| 3106 | commit_list_insert(c, &revs->commits); |
| 3107 | } |
| 3108 | |
| 3109 | /* |
| 3110 | * If revs->topo_order is set, sort the boundary commits |
| 3111 | * in topological order |
| 3112 | */ |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 3113 | sort_in_topological_order(&revs->commits, revs->sort_order); |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 3114 | } |
| 3115 | |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 3116 | static struct commit *get_revision_internal(struct rev_info *revs) |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3117 | { |
| 3118 | struct commit *c = NULL; |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3119 | struct commit_list *l; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3120 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3121 | if (revs->boundary == 2) { |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 3122 | /* |
| 3123 | * All of the normal commits have already been returned, |
| 3124 | * and we are now returning boundary commits. |
| 3125 | * create_boundary_commit_list() has populated |
| 3126 | * revs->commits with the remaining commits to return. |
| 3127 | */ |
| 3128 | c = pop_commit(&revs->commits); |
| 3129 | if (c) |
| 3130 | c->object.flags |= SHOWN; |
Johannes Schindelin | 9c5e66e | 2007-01-20 23:04:02 +0100 | [diff] [blame] | 3131 | return c; |
| 3132 | } |
| 3133 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3134 | /* |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 3135 | * If our max_count counter has reached zero, then we are done. We |
| 3136 | * don't simply return NULL because we still might need to show |
| 3137 | * boundary commits. But we want to avoid calling get_revision_1, which |
| 3138 | * might do a considerable amount of work finding the next commit only |
| 3139 | * for us to throw it away. |
| 3140 | * |
| 3141 | * If it is non-zero, then either we don't have a max_count at all |
| 3142 | * (-1), or it is still counting, in which case we decrement. |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3143 | */ |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 3144 | if (revs->max_count) { |
| 3145 | c = get_revision_1(revs); |
| 3146 | if (c) { |
Felipe Contreras | 9e57ac5 | 2013-10-31 03:25:43 -0600 | [diff] [blame] | 3147 | while (revs->skip_count > 0) { |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 3148 | revs->skip_count--; |
| 3149 | c = get_revision_1(revs); |
| 3150 | if (!c) |
| 3151 | break; |
| 3152 | } |
Junio C Hamano | 8839ac9 | 2007-03-06 03:20:55 -0800 | [diff] [blame] | 3153 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3154 | |
Jeff King | b72a190 | 2012-07-13 03:50:23 -0400 | [diff] [blame] | 3155 | if (revs->max_count > 0) |
| 3156 | revs->max_count--; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3157 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3158 | |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 3159 | if (c) |
| 3160 | c->object.flags |= SHOWN; |
| 3161 | |
Felipe Contreras | 9e57ac5 | 2013-10-31 03:25:43 -0600 | [diff] [blame] | 3162 | if (!revs->boundary) |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3163 | return c; |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3164 | |
| 3165 | if (!c) { |
| 3166 | /* |
| 3167 | * get_revision_1() runs out the commits, and |
| 3168 | * we are done computing the boundaries. |
| 3169 | * switch to boundary commits output mode. |
| 3170 | */ |
| 3171 | revs->boundary = 2; |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 3172 | |
| 3173 | /* |
| 3174 | * Update revs->commits to contain the list of |
| 3175 | * boundary commits. |
| 3176 | */ |
| 3177 | create_boundary_commit_list(revs); |
| 3178 | |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 3179 | return get_revision_internal(revs); |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | /* |
| 3183 | * boundary commits are the commits that are parents of the |
| 3184 | * ones we got from get_revision_1() but they themselves are |
| 3185 | * not returned from get_revision_1(). Before returning |
| 3186 | * 'c', we need to mark its parents that they could be boundaries. |
| 3187 | */ |
| 3188 | |
| 3189 | for (l = c->parents; l; l = l->next) { |
| 3190 | struct object *p; |
| 3191 | p = &(l->item->object); |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 3192 | if (p->flags & (CHILD_SHOWN | SHOWN)) |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 3193 | continue; |
| 3194 | p->flags |= CHILD_SHOWN; |
| 3195 | gc_boundary(&revs->boundary_commits); |
| 3196 | add_object_array(p, NULL, &revs->boundary_commits); |
| 3197 | } |
| 3198 | |
| 3199 | return c; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 3200 | } |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 3201 | |
| 3202 | struct commit *get_revision(struct rev_info *revs) |
| 3203 | { |
Thomas Rast | 498bcd3 | 2008-08-29 21:18:38 +0200 | [diff] [blame] | 3204 | struct commit *c; |
| 3205 | struct commit_list *reversed; |
| 3206 | |
| 3207 | if (revs->reverse) { |
| 3208 | reversed = NULL; |
Felipe Contreras | 9e57ac5 | 2013-10-31 03:25:43 -0600 | [diff] [blame] | 3209 | while ((c = get_revision_internal(revs))) |
Thomas Rast | 498bcd3 | 2008-08-29 21:18:38 +0200 | [diff] [blame] | 3210 | commit_list_insert(c, &reversed); |
Thomas Rast | 498bcd3 | 2008-08-29 21:18:38 +0200 | [diff] [blame] | 3211 | revs->commits = reversed; |
| 3212 | revs->reverse = 0; |
| 3213 | revs->reverse_output_stage = 1; |
| 3214 | } |
| 3215 | |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 3216 | if (revs->reverse_output_stage) { |
| 3217 | c = pop_commit(&revs->commits); |
| 3218 | if (revs->track_linear) |
| 3219 | revs->linear = !!(c && c->object.flags & TRACK_LINEAR); |
| 3220 | return c; |
| 3221 | } |
Thomas Rast | 498bcd3 | 2008-08-29 21:18:38 +0200 | [diff] [blame] | 3222 | |
| 3223 | c = get_revision_internal(revs); |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 3224 | if (c && revs->graph) |
| 3225 | graph_update(revs->graph, c); |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 3226 | if (!c) { |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 3227 | free_saved_parents(revs); |
Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 20:23:27 +0700 | [diff] [blame] | 3228 | if (revs->previous_parents) { |
| 3229 | free_commit_list(revs->previous_parents); |
| 3230 | revs->previous_parents = NULL; |
| 3231 | } |
| 3232 | } |
Adam Simpkins | 7fefda5 | 2008-05-04 03:36:54 -0700 | [diff] [blame] | 3233 | return c; |
| 3234 | } |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 3235 | |
| 3236 | char *get_revision_mark(const struct rev_info *revs, const struct commit *commit) |
| 3237 | { |
| 3238 | if (commit->object.flags & BOUNDARY) |
| 3239 | return "-"; |
| 3240 | else if (commit->object.flags & UNINTERESTING) |
| 3241 | return "^"; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 3242 | else if (commit->object.flags & PATCHSAME) |
| 3243 | return "="; |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 3244 | else if (!revs || revs->left_right) { |
| 3245 | if (commit->object.flags & SYMMETRIC_LEFT) |
| 3246 | return "<"; |
| 3247 | else |
| 3248 | return ">"; |
| 3249 | } else if (revs->graph) |
| 3250 | return "*"; |
Michael J Gruber | adbbb31 | 2011-03-07 13:31:40 +0100 | [diff] [blame] | 3251 | else if (revs->cherry_mark) |
| 3252 | return "+"; |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 3253 | return ""; |
| 3254 | } |
Michael J Gruber | b1b4755 | 2011-03-10 15:45:03 +0100 | [diff] [blame] | 3255 | |
| 3256 | void put_revision_mark(const struct rev_info *revs, const struct commit *commit) |
| 3257 | { |
| 3258 | char *mark = get_revision_mark(revs, commit); |
| 3259 | if (!strlen(mark)) |
| 3260 | return; |
| 3261 | fputs(mark, stdout); |
| 3262 | putchar(' '); |
| 3263 | } |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 3264 | |
| 3265 | define_commit_slab(saved_parents, struct commit_list *); |
| 3266 | |
Thomas Rast | 838f9a1 | 2013-08-03 12:36:15 +0200 | [diff] [blame] | 3267 | #define EMPTY_PARENT_LIST ((struct commit_list *)-1) |
| 3268 | |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 3269 | void save_parents(struct rev_info *revs, struct commit *commit) |
| 3270 | { |
| 3271 | struct commit_list **pp; |
| 3272 | |
| 3273 | if (!revs->saved_parents_slab) { |
| 3274 | revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents)); |
| 3275 | init_saved_parents(revs->saved_parents_slab); |
| 3276 | } |
| 3277 | |
| 3278 | pp = saved_parents_at(revs->saved_parents_slab, commit); |
Thomas Rast | 838f9a1 | 2013-08-03 12:36:15 +0200 | [diff] [blame] | 3279 | |
| 3280 | /* |
| 3281 | * When walking with reflogs, we may visit the same commit |
| 3282 | * several times: once for each appearance in the reflog. |
| 3283 | * |
| 3284 | * In this case, save_parents() will be called multiple times. |
| 3285 | * We want to keep only the first set of parents. We need to |
| 3286 | * store a sentinel value for an empty (i.e., NULL) parent |
| 3287 | * list to distinguish it from a not-yet-saved list, however. |
| 3288 | */ |
| 3289 | if (*pp) |
| 3290 | return; |
| 3291 | if (commit->parents) |
| 3292 | *pp = copy_commit_list(commit->parents); |
| 3293 | else |
| 3294 | *pp = EMPTY_PARENT_LIST; |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit) |
| 3298 | { |
Thomas Rast | 838f9a1 | 2013-08-03 12:36:15 +0200 | [diff] [blame] | 3299 | struct commit_list *parents; |
| 3300 | |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 3301 | if (!revs->saved_parents_slab) |
| 3302 | return commit->parents; |
| 3303 | |
Thomas Rast | 838f9a1 | 2013-08-03 12:36:15 +0200 | [diff] [blame] | 3304 | parents = *saved_parents_at(revs->saved_parents_slab, commit); |
| 3305 | if (parents == EMPTY_PARENT_LIST) |
| 3306 | return NULL; |
| 3307 | return parents; |
Thomas Rast | 53d00b3 | 2013-07-31 22:13:20 +0200 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | void free_saved_parents(struct rev_info *revs) |
| 3311 | { |
| 3312 | if (revs->saved_parents_slab) |
| 3313 | clear_saved_parents(revs->saved_parents_slab); |
| 3314 | } |