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" |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 9 | #include "grep.h" |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 10 | #include "reflog-walk.h" |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 11 | #include "patch-ids.h" |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 12 | |
| 13 | static char *path_name(struct name_path *path, const char *name) |
| 14 | { |
| 15 | struct name_path *p; |
| 16 | char *n, *m; |
| 17 | int nlen = strlen(name); |
| 18 | int len = nlen + 1; |
| 19 | |
| 20 | for (p = path; p; p = p->up) { |
| 21 | if (p->elem_len) |
| 22 | len += p->elem_len + 1; |
| 23 | } |
| 24 | n = xmalloc(len); |
| 25 | m = n + len - (nlen + 1); |
| 26 | strcpy(m, name); |
| 27 | for (p = path; p; p = p->up) { |
| 28 | if (p->elem_len) { |
| 29 | m -= p->elem_len + 1; |
| 30 | memcpy(m, p->elem, p->elem_len); |
| 31 | m[p->elem_len] = '/'; |
| 32 | } |
| 33 | } |
| 34 | return n; |
| 35 | } |
| 36 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 37 | void add_object(struct object *obj, |
| 38 | struct object_array *p, |
| 39 | struct name_path *path, |
| 40 | const char *name) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 41 | { |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 42 | add_object_array(obj, path_name(path, name), p); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void mark_blob_uninteresting(struct blob *blob) |
| 46 | { |
| 47 | if (blob->object.flags & UNINTERESTING) |
| 48 | return; |
| 49 | blob->object.flags |= UNINTERESTING; |
| 50 | } |
| 51 | |
| 52 | void mark_tree_uninteresting(struct tree *tree) |
| 53 | { |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 54 | struct tree_desc desc; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 55 | struct name_entry entry; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 56 | struct object *obj = &tree->object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 57 | |
| 58 | if (obj->flags & UNINTERESTING) |
| 59 | return; |
| 60 | obj->flags |= UNINTERESTING; |
| 61 | if (!has_sha1_file(obj->sha1)) |
| 62 | return; |
| 63 | if (parse_tree(tree) < 0) |
| 64 | die("bad tree %s", sha1_to_hex(obj->sha1)); |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 65 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 66 | init_tree_desc(&desc, tree->buffer, tree->size); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 67 | while (tree_entry(&desc, &entry)) { |
| 68 | if (S_ISDIR(entry.mode)) |
| 69 | mark_tree_uninteresting(lookup_tree(entry.sha1)); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 70 | else |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 71 | mark_blob_uninteresting(lookup_blob(entry.sha1)); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 72 | } |
Linus Torvalds | f75e53e | 2006-05-29 12:20:14 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * We don't care about the tree any more |
| 76 | * after it has been marked uninteresting. |
| 77 | */ |
| 78 | free(tree->buffer); |
| 79 | tree->buffer = NULL; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void mark_parents_uninteresting(struct commit *commit) |
| 83 | { |
| 84 | struct commit_list *parents = commit->parents; |
| 85 | |
| 86 | while (parents) { |
| 87 | struct commit *commit = parents->item; |
Matthias Urlichs | d2c4af7 | 2006-03-09 05:04:36 +0100 | [diff] [blame] | 88 | if (!(commit->object.flags & UNINTERESTING)) { |
| 89 | commit->object.flags |= UNINTERESTING; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 90 | |
Matthias Urlichs | d2c4af7 | 2006-03-09 05:04:36 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Normally we haven't parsed the parent |
| 93 | * yet, so we won't have a parent of a parent |
| 94 | * here. However, it may turn out that we've |
| 95 | * reached this commit some other way (where it |
| 96 | * wasn't uninteresting), in which case we need |
| 97 | * to mark its parents recursively too.. |
| 98 | */ |
| 99 | if (commit->parents) |
| 100 | mark_parents_uninteresting(commit); |
| 101 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * A missing commit is ok iff its parent is marked |
| 105 | * uninteresting. |
| 106 | * |
| 107 | * We just mark such a thing parsed, so that when |
| 108 | * it is popped next time around, we won't be trying |
| 109 | * to parse it and get an error. |
| 110 | */ |
| 111 | if (!has_sha1_file(commit->object.sha1)) |
| 112 | commit->object.parsed = 1; |
| 113 | parents = parents->next; |
| 114 | } |
| 115 | } |
| 116 | |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 117 | static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode) |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 118 | { |
Linus Torvalds | aa27e46 | 2007-02-27 16:22:52 -0800 | [diff] [blame] | 119 | if (revs->no_walk && (obj->flags & UNINTERESTING)) |
| 120 | die("object ranges do not make sense when not walking revisions"); |
Johannes Schindelin | 7b69b87 | 2007-07-24 00:39:50 +0100 | [diff] [blame] | 121 | if (revs->reflog_info && obj->type == OBJ_COMMIT && |
| 122 | add_reflog_for_walk(revs->reflog_info, |
| 123 | (struct commit *)obj, name)) |
| 124 | return; |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 125 | add_object_array_with_mode(obj, name, &revs->pending, mode); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 128 | void add_pending_object(struct rev_info *revs, struct object *obj, const char *name) |
| 129 | { |
| 130 | add_pending_object_with_mode(revs, obj, name, S_IFINVALID); |
| 131 | } |
| 132 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 133 | static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 134 | { |
| 135 | struct object *object; |
| 136 | |
| 137 | object = parse_object(sha1); |
| 138 | if (!object) |
| 139 | die("bad object %s", name); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 140 | object->flags |= flags; |
| 141 | return object; |
| 142 | } |
| 143 | |
| 144 | static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name) |
| 145 | { |
| 146 | unsigned long flags = object->flags; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Tag object? Look what it points to.. |
| 150 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 151 | while (object->type == OBJ_TAG) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 152 | struct tag *tag = (struct tag *) object; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 153 | if (revs->tag_objects && !(flags & UNINTERESTING)) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 154 | add_pending_object(revs, object, tag->tag); |
| 155 | object = parse_object(tag->tagged->sha1); |
| 156 | if (!object) |
| 157 | die("bad object %s", sha1_to_hex(tag->tagged->sha1)); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Commit object? Just return it, we'll do all the complex |
| 162 | * reachability crud. |
| 163 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 164 | if (object->type == OBJ_COMMIT) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 165 | struct commit *commit = (struct commit *)object; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 166 | if (parse_commit(commit) < 0) |
| 167 | die("unable to parse commit %s", name); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 168 | if (flags & UNINTERESTING) { |
Linus Torvalds | 4262c1b | 2006-04-18 20:31:41 -0700 | [diff] [blame] | 169 | commit->object.flags |= UNINTERESTING; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 170 | mark_parents_uninteresting(commit); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 171 | revs->limited = 1; |
| 172 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 173 | return commit; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Tree object? Either mark it uniniteresting, or add it |
| 178 | * to the list of objects to look at later.. |
| 179 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 180 | if (object->type == OBJ_TREE) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 181 | struct tree *tree = (struct tree *)object; |
| 182 | if (!revs->tree_objects) |
| 183 | return NULL; |
| 184 | if (flags & UNINTERESTING) { |
| 185 | mark_tree_uninteresting(tree); |
| 186 | return NULL; |
| 187 | } |
| 188 | add_pending_object(revs, object, ""); |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Blob object? You know the drill by now.. |
| 194 | */ |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 195 | if (object->type == OBJ_BLOB) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 196 | struct blob *blob = (struct blob *)object; |
| 197 | if (!revs->blob_objects) |
| 198 | return NULL; |
| 199 | if (flags & UNINTERESTING) { |
| 200 | mark_blob_uninteresting(blob); |
| 201 | return NULL; |
| 202 | } |
| 203 | add_pending_object(revs, object, ""); |
| 204 | return NULL; |
| 205 | } |
| 206 | die("%s is unknown object", name); |
| 207 | } |
| 208 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 209 | static int everybody_uninteresting(struct commit_list *orig) |
| 210 | { |
| 211 | struct commit_list *list = orig; |
| 212 | while (list) { |
| 213 | struct commit *commit = list->item; |
| 214 | list = list->next; |
| 215 | if (commit->object.flags & UNINTERESTING) |
| 216 | continue; |
| 217 | return 0; |
| 218 | } |
| 219 | return 1; |
| 220 | } |
| 221 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 222 | /* |
| 223 | * The goal is to get REV_TREE_NEW as the result only if the |
| 224 | * diff consists of all '+' (and no other changes), and |
| 225 | * REV_TREE_DIFFERENT otherwise (of course if the trees are |
| 226 | * the same we want REV_TREE_SAME). That means that once we |
| 227 | * get to REV_TREE_DIFFERENT, we do not have to look any further. |
| 228 | */ |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 229 | static int tree_difference = REV_TREE_SAME; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 230 | |
| 231 | static void file_add_remove(struct diff_options *options, |
| 232 | int addremove, unsigned mode, |
| 233 | const unsigned char *sha1, |
| 234 | const char *base, const char *path) |
| 235 | { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 236 | int diff = REV_TREE_DIFFERENT; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 237 | |
| 238 | /* |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 239 | * Is it an add of a new file? It means that the old tree |
| 240 | * didn't have it at all, so we will turn "REV_TREE_SAME" -> |
| 241 | * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone |
| 242 | * (and if it already was "REV_TREE_NEW", we'll keep it |
| 243 | * "REV_TREE_NEW" of course). |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 244 | */ |
| 245 | if (addremove == '+') { |
| 246 | diff = tree_difference; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 247 | if (diff != REV_TREE_SAME) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 248 | return; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 249 | diff = REV_TREE_NEW; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 250 | } |
| 251 | tree_difference = diff; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 252 | if (tree_difference == REV_TREE_DIFFERENT) |
| 253 | options->has_changes = 1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void file_change(struct diff_options *options, |
| 257 | unsigned old_mode, unsigned new_mode, |
| 258 | const unsigned char *old_sha1, |
| 259 | const unsigned char *new_sha1, |
| 260 | const char *base, const char *path) |
| 261 | { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 262 | tree_difference = REV_TREE_DIFFERENT; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 263 | options->has_changes = 1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 266 | static int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 267 | { |
| 268 | if (!t1) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 269 | return REV_TREE_NEW; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 270 | if (!t2) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 271 | return REV_TREE_DIFFERENT; |
| 272 | tree_difference = REV_TREE_SAME; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 273 | revs->pruning.has_changes = 0; |
Junio C Hamano | c4e05b1 | 2006-04-10 18:14:54 -0700 | [diff] [blame] | 274 | if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 275 | &revs->pruning) < 0) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 276 | return REV_TREE_DIFFERENT; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 277 | return tree_difference; |
| 278 | } |
| 279 | |
Junio C Hamano | 2d93b9f | 2007-06-08 02:24:58 -0700 | [diff] [blame] | 280 | static int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 281 | { |
| 282 | int retval; |
| 283 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 284 | unsigned long size; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 285 | struct tree_desc empty, real; |
| 286 | |
| 287 | if (!t1) |
| 288 | return 0; |
| 289 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 290 | tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 291 | if (!tree) |
| 292 | return 0; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 293 | init_tree_desc(&real, tree, size); |
| 294 | init_tree_desc(&empty, "", 0); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 295 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 296 | tree_difference = REV_TREE_SAME; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 297 | revs->pruning.has_changes = 0; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 298 | retval = diff_tree(&empty, &real, "", &revs->pruning); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 299 | free(tree); |
| 300 | |
Junio C Hamano | 0a4ba7f | 2007-03-14 13:12:18 -0700 | [diff] [blame] | 301 | return retval >= 0 && (tree_difference == REV_TREE_SAME); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit) |
| 305 | { |
| 306 | struct commit_list **pp, *parent; |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 307 | int tree_changed = 0, tree_same = 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 308 | |
| 309 | if (!commit->tree) |
| 310 | return; |
| 311 | |
| 312 | if (!commit->parents) { |
Junio C Hamano | c4e05b1 | 2006-04-10 18:14:54 -0700 | [diff] [blame] | 313 | if (!rev_same_tree_as_empty(revs, commit->tree)) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 314 | commit->object.flags |= TREECHANGE; |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | pp = &commit->parents; |
| 319 | while ((parent = *pp) != NULL) { |
| 320 | struct commit *p = parent->item; |
| 321 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 322 | if (parse_commit(p) < 0) |
| 323 | die("cannot simplify commit %s (because of %s)", |
| 324 | sha1_to_hex(commit->object.sha1), |
| 325 | sha1_to_hex(p->object.sha1)); |
Junio C Hamano | c4e05b1 | 2006-04-10 18:14:54 -0700 | [diff] [blame] | 326 | switch (rev_compare_tree(revs, p->tree, commit->tree)) { |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 327 | case REV_TREE_SAME: |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 328 | tree_same = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 329 | if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) { |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 330 | /* Even if a merge with an uninteresting |
| 331 | * side branch brought the entire change |
| 332 | * we are interested in, we do not want |
| 333 | * to lose the other branches of this |
| 334 | * merge, so we just keep going. |
| 335 | */ |
| 336 | pp = &parent->next; |
| 337 | continue; |
| 338 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 339 | parent->next = NULL; |
| 340 | commit->parents = parent; |
| 341 | return; |
| 342 | |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 343 | case REV_TREE_NEW: |
| 344 | if (revs->remove_empty_trees && |
Junio C Hamano | c4e05b1 | 2006-04-10 18:14:54 -0700 | [diff] [blame] | 345 | rev_same_tree_as_empty(revs, p->tree)) { |
Junio C Hamano | c348f31 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 346 | /* We are adding all the specified |
| 347 | * paths from this parent, so the |
| 348 | * history beyond this parent is not |
| 349 | * interesting. Remove its parents |
| 350 | * (they are grandparents for us). |
| 351 | * IOW, we pretend this parent is a |
| 352 | * "root" commit. |
Junio C Hamano | a41e109 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 353 | */ |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 354 | if (parse_commit(p) < 0) |
| 355 | die("cannot simplify commit %s (invalid %s)", |
| 356 | sha1_to_hex(commit->object.sha1), |
| 357 | sha1_to_hex(p->object.sha1)); |
Junio C Hamano | c348f31 | 2006-03-12 13:39:31 -0800 | [diff] [blame] | 358 | p->parents = NULL; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 359 | } |
| 360 | /* fallthrough */ |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 361 | case REV_TREE_DIFFERENT: |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 362 | tree_changed = 1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 363 | pp = &parent->next; |
| 364 | continue; |
| 365 | } |
| 366 | die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1)); |
| 367 | } |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 368 | if (tree_changed && !tree_same) |
Junio C Hamano | f3219fb | 2006-03-10 21:59:37 -0800 | [diff] [blame] | 369 | commit->object.flags |= TREECHANGE; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 372 | static int add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 373 | { |
| 374 | struct commit_list *parent = commit->parents; |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 375 | unsigned left_flag; |
Junio C Hamano | 0053e90 | 2007-03-13 01:57:22 -0700 | [diff] [blame] | 376 | int add, rest; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 377 | |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 378 | if (commit->object.flags & ADDED) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 379 | return 0; |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 380 | commit->object.flags |= ADDED; |
| 381 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 382 | /* |
| 383 | * If the commit is uninteresting, don't try to |
| 384 | * prune parents - we want the maximal uninteresting |
| 385 | * set. |
| 386 | * |
| 387 | * Normally we haven't parsed the parent |
| 388 | * yet, so we won't have a parent of a parent |
| 389 | * here. However, it may turn out that we've |
| 390 | * reached this commit some other way (where it |
| 391 | * wasn't uninteresting), in which case we need |
| 392 | * to mark its parents recursively too.. |
| 393 | */ |
| 394 | if (commit->object.flags & UNINTERESTING) { |
| 395 | while (parent) { |
| 396 | struct commit *p = parent->item; |
| 397 | parent = parent->next; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 398 | if (parse_commit(p) < 0) |
| 399 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 400 | p->object.flags |= UNINTERESTING; |
| 401 | if (p->parents) |
| 402 | mark_parents_uninteresting(p); |
| 403 | if (p->object.flags & SEEN) |
| 404 | continue; |
| 405 | p->object.flags |= SEEN; |
| 406 | insert_by_date(p, list); |
| 407 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 408 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Ok, the commit wasn't uninteresting. Try to |
| 413 | * simplify the commit history and find the parent |
| 414 | * that has no differences in the path set if one exists. |
| 415 | */ |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 416 | if (revs->prune_fn) |
| 417 | revs->prune_fn(revs, commit); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 418 | |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 419 | if (revs->no_walk) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 420 | return 0; |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 421 | |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 422 | left_flag = (commit->object.flags & SYMMETRIC_LEFT); |
Junio C Hamano | 0053e90 | 2007-03-13 01:57:22 -0700 | [diff] [blame] | 423 | |
| 424 | rest = !revs->first_parent_only; |
| 425 | for (parent = commit->parents, add = 1; parent; add = rest) { |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 426 | struct commit *p = parent->item; |
| 427 | |
| 428 | parent = parent->next; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 429 | if (parse_commit(p) < 0) |
| 430 | return -1; |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 431 | p->object.flags |= left_flag; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 432 | if (p->object.flags & SEEN) |
| 433 | continue; |
| 434 | p->object.flags |= SEEN; |
Junio C Hamano | 0053e90 | 2007-03-13 01:57:22 -0700 | [diff] [blame] | 435 | if (add) |
| 436 | insert_by_date(p, list); |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 437 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 438 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 441 | 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] | 442 | { |
| 443 | struct commit_list *p; |
| 444 | int left_count = 0, right_count = 0; |
| 445 | int left_first; |
| 446 | struct patch_ids ids; |
| 447 | |
| 448 | /* First count the commits on the left and on the right */ |
| 449 | for (p = list; p; p = p->next) { |
| 450 | struct commit *commit = p->item; |
| 451 | unsigned flags = commit->object.flags; |
| 452 | if (flags & BOUNDARY) |
| 453 | ; |
| 454 | else if (flags & SYMMETRIC_LEFT) |
| 455 | left_count++; |
| 456 | else |
| 457 | right_count++; |
| 458 | } |
| 459 | |
| 460 | left_first = left_count < right_count; |
| 461 | init_patch_ids(&ids); |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 462 | if (revs->diffopt.nr_paths) { |
| 463 | ids.diffopts.nr_paths = revs->diffopt.nr_paths; |
| 464 | ids.diffopts.paths = revs->diffopt.paths; |
| 465 | ids.diffopts.pathlens = revs->diffopt.pathlens; |
| 466 | } |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 467 | |
| 468 | /* Compute patch-ids for one side */ |
| 469 | for (p = list; p; p = p->next) { |
| 470 | struct commit *commit = p->item; |
| 471 | unsigned flags = commit->object.flags; |
| 472 | |
| 473 | if (flags & BOUNDARY) |
| 474 | continue; |
| 475 | /* |
| 476 | * If we have fewer left, left_first is set and we omit |
| 477 | * commits on the right branch in this loop. If we have |
| 478 | * fewer right, we skip the left ones. |
| 479 | */ |
| 480 | if (left_first != !!(flags & SYMMETRIC_LEFT)) |
| 481 | continue; |
| 482 | commit->util = add_commit_patch_id(commit, &ids); |
| 483 | } |
| 484 | |
| 485 | /* Check the other side */ |
| 486 | for (p = list; p; p = p->next) { |
| 487 | struct commit *commit = p->item; |
| 488 | struct patch_id *id; |
| 489 | unsigned flags = commit->object.flags; |
| 490 | |
| 491 | if (flags & BOUNDARY) |
| 492 | continue; |
| 493 | /* |
| 494 | * If we have fewer left, left_first is set and we omit |
| 495 | * commits on the left branch in this loop. |
| 496 | */ |
| 497 | if (left_first == !!(flags & SYMMETRIC_LEFT)) |
| 498 | continue; |
| 499 | |
| 500 | /* |
| 501 | * Have we seen the same patch id? |
| 502 | */ |
| 503 | id = has_commit_patch_id(commit, &ids); |
| 504 | if (!id) |
| 505 | continue; |
| 506 | id->seen = 1; |
| 507 | commit->object.flags |= SHOWN; |
| 508 | } |
| 509 | |
| 510 | /* Now check the original side for seen ones */ |
| 511 | for (p = list; p; p = p->next) { |
| 512 | struct commit *commit = p->item; |
| 513 | struct patch_id *ent; |
| 514 | |
| 515 | ent = commit->util; |
| 516 | if (!ent) |
| 517 | continue; |
| 518 | if (ent->seen) |
| 519 | commit->object.flags |= SHOWN; |
| 520 | commit->util = NULL; |
| 521 | } |
| 522 | |
| 523 | free_patch_ids(&ids); |
| 524 | } |
| 525 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 526 | static int limit_list(struct rev_info *revs) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 527 | { |
| 528 | struct commit_list *list = revs->commits; |
| 529 | struct commit_list *newlist = NULL; |
| 530 | struct commit_list **p = &newlist; |
| 531 | |
| 532 | while (list) { |
| 533 | struct commit_list *entry = list; |
| 534 | struct commit *commit = list->item; |
| 535 | struct object *obj = &commit->object; |
| 536 | |
| 537 | list = list->next; |
| 538 | free(entry); |
| 539 | |
| 540 | if (revs->max_age != -1 && (commit->date < revs->max_age)) |
| 541 | obj->flags |= UNINTERESTING; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 542 | if (add_parents_to_list(revs, commit, &list) < 0) |
| 543 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 544 | if (obj->flags & UNINTERESTING) { |
| 545 | mark_parents_uninteresting(commit); |
| 546 | if (everybody_uninteresting(list)) |
| 547 | break; |
| 548 | continue; |
| 549 | } |
| 550 | if (revs->min_age != -1 && (commit->date > revs->min_age)) |
| 551 | continue; |
| 552 | p = &commit_list_insert(commit, p)->next; |
| 553 | } |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 554 | if (revs->cherry_pick) |
Johannes Schindelin | 36d56de | 2007-07-10 14:50:49 +0100 | [diff] [blame] | 555 | cherry_pick_list(newlist, revs); |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 556 | |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 557 | revs->commits = newlist; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 558 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 561 | struct all_refs_cb { |
| 562 | int all_flags; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 563 | int warned_bad_reflog; |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 564 | struct rev_info *all_revs; |
| 565 | const char *name_for_errormsg; |
| 566 | }; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 567 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 568 | 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] | 569 | { |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 570 | struct all_refs_cb *cb = cb_data; |
| 571 | struct object *object = get_reference(cb->all_revs, path, sha1, |
| 572 | cb->all_flags); |
Johannes Schindelin | 3d1efd8 | 2007-02-23 02:56:31 +0100 | [diff] [blame] | 573 | add_pending_object(cb->all_revs, object, path); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static void handle_all(struct rev_info *revs, unsigned flags) |
| 578 | { |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 579 | struct all_refs_cb cb; |
| 580 | cb.all_revs = revs; |
| 581 | cb.all_flags = flags; |
| 582 | for_each_ref(handle_one_ref, &cb); |
| 583 | } |
| 584 | |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 585 | 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] | 586 | { |
| 587 | struct all_refs_cb *cb = cb_data; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 588 | if (!is_null_sha1(sha1)) { |
| 589 | struct object *o = parse_object(sha1); |
| 590 | if (o) { |
| 591 | o->flags |= cb->all_flags; |
| 592 | add_pending_object(cb->all_revs, o, ""); |
| 593 | } |
| 594 | else if (!cb->warned_bad_reflog) { |
Theodore Ts'o | 46efd2d | 2007-03-30 19:07:05 -0400 | [diff] [blame] | 595 | warning("reflog of '%s' references pruned commits", |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 596 | cb->name_for_errormsg); |
| 597 | cb->warned_bad_reflog = 1; |
| 598 | } |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 599 | } |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 600 | } |
| 601 | |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 602 | static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
| 603 | const char *email, unsigned long timestamp, int tz, |
| 604 | const char *message, void *cb_data) |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 605 | { |
| 606 | handle_one_reflog_commit(osha1, cb_data); |
| 607 | handle_one_reflog_commit(nsha1, cb_data); |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
| 612 | { |
| 613 | struct all_refs_cb *cb = cb_data; |
Shawn O. Pearce | 71b03b4 | 2006-12-21 19:49:06 -0500 | [diff] [blame] | 614 | cb->warned_bad_reflog = 0; |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 615 | cb->name_for_errormsg = path; |
| 616 | for_each_reflog_ent(path, handle_one_reflog_ent, cb_data); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static void handle_reflog(struct rev_info *revs, unsigned flags) |
| 621 | { |
| 622 | struct all_refs_cb cb; |
| 623 | cb.all_revs = revs; |
| 624 | cb.all_flags = flags; |
Junio C Hamano | 25b51e2 | 2007-02-12 23:06:54 -0800 | [diff] [blame] | 625 | for_each_reflog(handle_one_reflog, &cb); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 628 | static int add_parents_only(struct rev_info *revs, const char *arg, int flags) |
| 629 | { |
| 630 | unsigned char sha1[20]; |
| 631 | struct object *it; |
| 632 | struct commit *commit; |
| 633 | struct commit_list *parents; |
| 634 | |
| 635 | if (*arg == '^') { |
| 636 | flags ^= UNINTERESTING; |
| 637 | arg++; |
| 638 | } |
| 639 | if (get_sha1(arg, sha1)) |
| 640 | return 0; |
| 641 | while (1) { |
| 642 | it = get_reference(revs, arg, sha1, 0); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 643 | if (it->type != OBJ_TAG) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 644 | break; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 645 | hashcpy(sha1, ((struct tag*)it)->tagged->sha1); |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 646 | } |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 647 | if (it->type != OBJ_COMMIT) |
Junio C Hamano | ea4a19e | 2006-04-30 00:54:29 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | commit = (struct commit *)it; |
| 650 | for (parents = commit->parents; parents; parents = parents->next) { |
| 651 | it = &parents->item->object; |
| 652 | it->flags |= flags; |
| 653 | add_pending_object(revs, it, arg); |
| 654 | } |
| 655 | return 1; |
| 656 | } |
| 657 | |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 658 | void init_revisions(struct rev_info *revs, const char *prefix) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 659 | { |
| 660 | memset(revs, 0, sizeof(*revs)); |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 661 | |
Junio C Hamano | 6b9c58f | 2006-04-15 23:46:36 -0700 | [diff] [blame] | 662 | revs->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 663 | revs->ignore_merges = 1; |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 664 | revs->simplify_history = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 665 | revs->pruning.recursive = 1; |
Junio C Hamano | dd47aa3 | 2007-03-14 13:18:15 -0700 | [diff] [blame] | 666 | revs->pruning.quiet = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 667 | revs->pruning.add_remove = file_add_remove; |
| 668 | revs->pruning.change = file_change; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 669 | revs->lifo = 1; |
| 670 | revs->dense = 1; |
Linus Torvalds | db6296a | 2006-07-28 21:21:48 -0700 | [diff] [blame] | 671 | revs->prefix = prefix; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 672 | revs->max_age = -1; |
| 673 | revs->min_age = -1; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 674 | revs->skip_count = -1; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 675 | revs->max_count = -1; |
| 676 | |
| 677 | revs->prune_fn = NULL; |
| 678 | revs->prune_data = NULL; |
| 679 | |
| 680 | revs->topo_setter = topo_sort_default_setter; |
| 681 | revs->topo_getter = topo_sort_default_getter; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 682 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 683 | revs->commit_format = CMIT_FMT_DEFAULT; |
| 684 | |
| 685 | diff_setup(&revs->diffopt); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Rene Scharfe | 0d2c9d6 | 2006-07-02 01:29:37 +0200 | [diff] [blame] | 688 | static void add_pending_commit_list(struct rev_info *revs, |
| 689 | struct commit_list *commit_list, |
| 690 | unsigned int flags) |
| 691 | { |
| 692 | while (commit_list) { |
| 693 | struct object *object = &commit_list->item->object; |
| 694 | object->flags |= flags; |
| 695 | add_pending_object(revs, object, sha1_to_hex(object->sha1)); |
| 696 | commit_list = commit_list->next; |
| 697 | } |
| 698 | } |
| 699 | |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 700 | static void prepare_show_merge(struct rev_info *revs) |
| 701 | { |
| 702 | struct commit_list *bases; |
| 703 | struct commit *head, *other; |
| 704 | unsigned char sha1[20]; |
| 705 | const char **prune = NULL; |
| 706 | int i, prune_num = 1; /* counting terminating NULL */ |
| 707 | |
| 708 | if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1))) |
| 709 | die("--merge without HEAD?"); |
| 710 | if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1))) |
| 711 | die("--merge without MERGE_HEAD?"); |
| 712 | add_pending_object(revs, &head->object, "HEAD"); |
| 713 | add_pending_object(revs, &other->object, "MERGE_HEAD"); |
| 714 | bases = get_merge_bases(head, other, 1); |
| 715 | while (bases) { |
| 716 | struct commit *it = bases->item; |
| 717 | struct commit_list *n = bases->next; |
| 718 | free(bases); |
| 719 | bases = n; |
| 720 | it->object.flags |= UNINTERESTING; |
| 721 | add_pending_object(revs, &it->object, "(merge-base)"); |
| 722 | } |
| 723 | |
| 724 | if (!active_nr) |
| 725 | read_cache(); |
| 726 | for (i = 0; i < active_nr; i++) { |
| 727 | struct cache_entry *ce = active_cache[i]; |
| 728 | if (!ce_stage(ce)) |
| 729 | continue; |
| 730 | if (ce_path_match(ce, revs->prune_data)) { |
| 731 | prune_num++; |
| 732 | prune = xrealloc(prune, sizeof(*prune) * prune_num); |
| 733 | prune[prune_num-2] = ce->name; |
| 734 | prune[prune_num-1] = NULL; |
| 735 | } |
| 736 | while ((i+1 < active_nr) && |
| 737 | ce_same_name(ce, active_cache[i+1])) |
| 738 | i++; |
| 739 | } |
| 740 | revs->prune_data = prune; |
| 741 | } |
| 742 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 743 | int handle_revision_arg(const char *arg, struct rev_info *revs, |
| 744 | int flags, |
| 745 | int cant_be_filename) |
| 746 | { |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 747 | unsigned mode; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 748 | char *dotdot; |
| 749 | struct object *object; |
| 750 | unsigned char sha1[20]; |
| 751 | int local_flags; |
| 752 | |
| 753 | dotdot = strstr(arg, ".."); |
| 754 | if (dotdot) { |
| 755 | unsigned char from_sha1[20]; |
| 756 | const char *next = dotdot + 2; |
| 757 | const char *this = arg; |
| 758 | int symmetric = *next == '.'; |
| 759 | unsigned int flags_exclude = flags ^ UNINTERESTING; |
| 760 | |
| 761 | *dotdot = 0; |
| 762 | next += symmetric; |
| 763 | |
| 764 | if (!*next) |
| 765 | next = "HEAD"; |
| 766 | if (dotdot == arg) |
| 767 | this = "HEAD"; |
| 768 | if (!get_sha1(this, from_sha1) && |
| 769 | !get_sha1(next, sha1)) { |
| 770 | struct commit *a, *b; |
| 771 | struct commit_list *exclude; |
| 772 | |
| 773 | a = lookup_commit_reference(from_sha1); |
| 774 | b = lookup_commit_reference(sha1); |
| 775 | if (!a || !b) { |
| 776 | die(symmetric ? |
| 777 | "Invalid symmetric difference expression %s...%s" : |
| 778 | "Invalid revision range %s..%s", |
| 779 | arg, next); |
| 780 | } |
| 781 | |
| 782 | if (!cant_be_filename) { |
| 783 | *dotdot = '.'; |
| 784 | verify_non_filename(revs->prefix, arg); |
| 785 | } |
| 786 | |
| 787 | if (symmetric) { |
| 788 | exclude = get_merge_bases(a, b, 1); |
| 789 | add_pending_commit_list(revs, exclude, |
| 790 | flags_exclude); |
| 791 | free_commit_list(exclude); |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 792 | a->object.flags |= flags | SYMMETRIC_LEFT; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 793 | } else |
| 794 | a->object.flags |= flags_exclude; |
| 795 | b->object.flags |= flags; |
| 796 | add_pending_object(revs, &a->object, this); |
| 797 | add_pending_object(revs, &b->object, next); |
| 798 | return 0; |
| 799 | } |
| 800 | *dotdot = '.'; |
| 801 | } |
| 802 | dotdot = strstr(arg, "^@"); |
| 803 | if (dotdot && !dotdot[2]) { |
| 804 | *dotdot = 0; |
| 805 | if (add_parents_only(revs, arg, flags)) |
| 806 | return 0; |
| 807 | *dotdot = '^'; |
| 808 | } |
Junio C Hamano | 62476c8 | 2006-10-31 14:22:34 -0800 | [diff] [blame] | 809 | dotdot = strstr(arg, "^!"); |
| 810 | if (dotdot && !dotdot[2]) { |
| 811 | *dotdot = 0; |
| 812 | if (!add_parents_only(revs, arg, flags ^ UNINTERESTING)) |
| 813 | *dotdot = '^'; |
| 814 | } |
| 815 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 816 | local_flags = 0; |
| 817 | if (*arg == '^') { |
| 818 | local_flags = UNINTERESTING; |
| 819 | arg++; |
| 820 | } |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 821 | if (get_sha1_with_mode(arg, sha1, &mode)) |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 822 | return -1; |
| 823 | if (!cant_be_filename) |
| 824 | verify_non_filename(revs->prefix, arg); |
| 825 | object = get_reference(revs, arg, sha1, flags ^ local_flags); |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 826 | add_pending_object_with_mode(revs, object, arg, mode); |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 830 | static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) |
| 831 | { |
| 832 | if (!revs->grep_filter) { |
| 833 | struct grep_opt *opt = xcalloc(1, sizeof(*opt)); |
| 834 | opt->status_only = 1; |
| 835 | opt->pattern_tail = &(opt->pattern_list); |
| 836 | opt->regflags = REG_NEWLINE; |
| 837 | revs->grep_filter = opt; |
| 838 | } |
| 839 | append_grep_pattern(revs->grep_filter, ptn, |
| 840 | "command line", 0, what); |
| 841 | } |
| 842 | |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 843 | static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern) |
| 844 | { |
| 845 | char *pat; |
Linus Torvalds | a2ed6ae | 2006-09-18 10:07:51 -0700 | [diff] [blame] | 846 | const char *prefix; |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 847 | int patlen, fldlen; |
| 848 | |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 849 | fldlen = strlen(field); |
| 850 | patlen = strlen(pattern); |
Linus Torvalds | a2ed6ae | 2006-09-18 10:07:51 -0700 | [diff] [blame] | 851 | pat = xmalloc(patlen + fldlen + 10); |
| 852 | prefix = ".*"; |
| 853 | if (*pattern == '^') { |
| 854 | prefix = ""; |
| 855 | pattern++; |
| 856 | } |
| 857 | sprintf(pat, "^%s %s%s", field, prefix, pattern); |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 858 | add_grep(revs, pat, GREP_PATTERN_HEAD); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static void add_message_grep(struct rev_info *revs, const char *pattern) |
| 862 | { |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 863 | add_grep(revs, pattern, GREP_PATTERN_BODY); |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Junio C Hamano | 106d710 | 2006-09-06 02:12:09 -0700 | [diff] [blame] | 866 | static void add_ignore_packed(struct rev_info *revs, const char *name) |
| 867 | { |
| 868 | int num = ++revs->num_ignore_packed; |
| 869 | |
| 870 | revs->ignore_packed = xrealloc(revs->ignore_packed, |
| 871 | sizeof(const char **) * (num + 1)); |
| 872 | revs->ignore_packed[num-1] = name; |
| 873 | revs->ignore_packed[num] = NULL; |
| 874 | } |
| 875 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 876 | /* |
| 877 | * Parse revision information, filling in the "rev_info" structure, |
| 878 | * and removing the used arguments from the argument list. |
| 879 | * |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 880 | * Returns the number of arguments left that weren't recognized |
| 881 | * (which are also moved to the head of the argument list) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 882 | */ |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 883 | int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 884 | { |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 885 | int i, flags, seen_dashdash, show_merge; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 886 | const char **unrecognized = argv + 1; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 887 | int left = 1; |
Junio C Hamano | 70d0afb | 2006-09-27 17:55:58 -0700 | [diff] [blame] | 888 | int all_match = 0; |
Petr Baudis | 93d496a | 2007-05-19 02:13:29 +0200 | [diff] [blame] | 889 | int regflags = 0; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 890 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 891 | /* First, search for "--" */ |
| 892 | seen_dashdash = 0; |
| 893 | for (i = 1; i < argc; i++) { |
| 894 | const char *arg = argv[i]; |
| 895 | if (strcmp(arg, "--")) |
| 896 | continue; |
| 897 | argv[i] = NULL; |
| 898 | argc = i; |
Junio C Hamano | a65f200 | 2007-08-30 22:58:26 -0700 | [diff] [blame] | 899 | if (argv[i + 1]) |
| 900 | revs->prune_data = get_pathspec(revs->prefix, argv + i + 1); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 901 | seen_dashdash = 1; |
| 902 | break; |
| 903 | } |
| 904 | |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 905 | flags = show_merge = 0; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 906 | for (i = 1; i < argc; i++) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 907 | const char *arg = argv[i]; |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 908 | if (*arg == '-') { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 909 | int opts; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 910 | if (!prefixcmp(arg, "--max-count=")) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 911 | revs->max_count = atoi(arg + 12); |
| 912 | continue; |
| 913 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 914 | if (!prefixcmp(arg, "--skip=")) { |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 915 | revs->skip_count = atoi(arg + 7); |
| 916 | continue; |
| 917 | } |
Matthias Kestenholz | de5f2bf | 2006-05-03 12:51:40 +0200 | [diff] [blame] | 918 | /* accept -<digit>, like traditional "head" */ |
Junio C Hamano | 64bc6e3 | 2006-03-02 15:24:01 -0800 | [diff] [blame] | 919 | if ((*arg == '-') && isdigit(arg[1])) { |
| 920 | revs->max_count = atoi(arg + 1); |
| 921 | continue; |
| 922 | } |
| 923 | if (!strcmp(arg, "-n")) { |
| 924 | if (argc <= i + 1) |
| 925 | die("-n requires an argument"); |
| 926 | revs->max_count = atoi(argv[++i]); |
| 927 | continue; |
| 928 | } |
Junio C Hamano | 1968d77 | 2007-02-20 01:55:07 -0800 | [diff] [blame] | 929 | if (!prefixcmp(arg, "-n")) { |
Junio C Hamano | 64bc6e3 | 2006-03-02 15:24:01 -0800 | [diff] [blame] | 930 | revs->max_count = atoi(arg + 2); |
| 931 | continue; |
| 932 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 933 | if (!prefixcmp(arg, "--max-age=")) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 934 | revs->max_age = atoi(arg + 10); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 935 | continue; |
| 936 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 937 | if (!prefixcmp(arg, "--since=")) { |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 938 | revs->max_age = approxidate(arg + 8); |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 939 | continue; |
| 940 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 941 | if (!prefixcmp(arg, "--after=")) { |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 942 | revs->max_age = approxidate(arg + 8); |
Junio C Hamano | 5306968 | 2006-04-01 18:38:25 -0800 | [diff] [blame] | 943 | continue; |
| 944 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 945 | if (!prefixcmp(arg, "--min-age=")) { |
Junio C Hamano | 5306968 | 2006-04-01 18:38:25 -0800 | [diff] [blame] | 946 | revs->min_age = atoi(arg + 10); |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 947 | continue; |
| 948 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 949 | if (!prefixcmp(arg, "--before=")) { |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 950 | revs->min_age = approxidate(arg + 9); |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 951 | continue; |
| 952 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 953 | if (!prefixcmp(arg, "--until=")) { |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 954 | revs->min_age = approxidate(arg + 8); |
Junio C Hamano | fd75166 | 2006-03-01 00:12:37 -0800 | [diff] [blame] | 955 | continue; |
| 956 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 957 | if (!strcmp(arg, "--all")) { |
| 958 | handle_all(revs, flags); |
| 959 | continue; |
| 960 | } |
Junio C Hamano | 0053e90 | 2007-03-13 01:57:22 -0700 | [diff] [blame] | 961 | if (!strcmp(arg, "--first-parent")) { |
| 962 | revs->first_parent_only = 1; |
| 963 | continue; |
| 964 | } |
Junio C Hamano | 6304929 | 2006-12-18 17:25:28 -0800 | [diff] [blame] | 965 | if (!strcmp(arg, "--reflog")) { |
| 966 | handle_reflog(revs, flags); |
| 967 | continue; |
| 968 | } |
Johannes Schindelin | 084ae0a | 2007-01-24 15:05:16 +0100 | [diff] [blame] | 969 | if (!strcmp(arg, "-g") || |
| 970 | !strcmp(arg, "--walk-reflogs")) { |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 971 | init_reflog_walk(&revs->reflog_info); |
| 972 | continue; |
| 973 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 974 | if (!strcmp(arg, "--not")) { |
| 975 | flags ^= UNINTERESTING; |
| 976 | continue; |
| 977 | } |
| 978 | if (!strcmp(arg, "--default")) { |
| 979 | if (++i >= argc) |
| 980 | die("bad --default argument"); |
| 981 | def = argv[i]; |
| 982 | continue; |
| 983 | } |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 984 | if (!strcmp(arg, "--merge")) { |
| 985 | show_merge = 1; |
| 986 | continue; |
| 987 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 988 | if (!strcmp(arg, "--topo-order")) { |
| 989 | revs->topo_order = 1; |
| 990 | continue; |
| 991 | } |
| 992 | if (!strcmp(arg, "--date-order")) { |
| 993 | revs->lifo = 0; |
| 994 | revs->topo_order = 1; |
| 995 | continue; |
| 996 | } |
Linus Torvalds | 7b0c996 | 2006-03-30 16:52:42 -0800 | [diff] [blame] | 997 | if (!strcmp(arg, "--parents")) { |
| 998 | revs->parents = 1; |
| 999 | continue; |
| 1000 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1001 | if (!strcmp(arg, "--dense")) { |
| 1002 | revs->dense = 1; |
| 1003 | continue; |
| 1004 | } |
| 1005 | if (!strcmp(arg, "--sparse")) { |
| 1006 | revs->dense = 0; |
| 1007 | continue; |
| 1008 | } |
| 1009 | if (!strcmp(arg, "--remove-empty")) { |
| 1010 | revs->remove_empty_trees = 1; |
| 1011 | continue; |
| 1012 | } |
Junio C Hamano | 5cdeae7 | 2006-03-28 00:04:50 -0800 | [diff] [blame] | 1013 | if (!strcmp(arg, "--no-merges")) { |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1014 | revs->no_merges = 1; |
| 1015 | continue; |
| 1016 | } |
Junio C Hamano | 384e99a | 2006-03-27 23:58:34 -0800 | [diff] [blame] | 1017 | if (!strcmp(arg, "--boundary")) { |
| 1018 | revs->boundary = 1; |
| 1019 | continue; |
| 1020 | } |
Junio C Hamano | 8dce823 | 2006-12-19 02:28:16 -0800 | [diff] [blame] | 1021 | if (!strcmp(arg, "--left-right")) { |
| 1022 | revs->left_right = 1; |
Junio C Hamano | 74bd902 | 2006-12-16 15:31:25 -0800 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 1025 | if (!strcmp(arg, "--cherry-pick")) { |
| 1026 | revs->cherry_pick = 1; |
Johannes Schindelin | 023756f | 2007-09-15 18:39:52 +0100 | [diff] [blame] | 1027 | revs->limited = 1; |
Junio C Hamano | d7a17ca | 2007-04-09 03:40:38 -0700 | [diff] [blame] | 1028 | continue; |
| 1029 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1030 | if (!strcmp(arg, "--objects")) { |
| 1031 | revs->tag_objects = 1; |
| 1032 | revs->tree_objects = 1; |
| 1033 | revs->blob_objects = 1; |
| 1034 | continue; |
| 1035 | } |
| 1036 | if (!strcmp(arg, "--objects-edge")) { |
| 1037 | revs->tag_objects = 1; |
| 1038 | revs->tree_objects = 1; |
| 1039 | revs->blob_objects = 1; |
| 1040 | revs->edge_hint = 1; |
| 1041 | continue; |
| 1042 | } |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 1043 | if (!strcmp(arg, "--unpacked")) { |
| 1044 | revs->unpacked = 1; |
Junio C Hamano | 106d710 | 2006-09-06 02:12:09 -0700 | [diff] [blame] | 1045 | free(revs->ignore_packed); |
| 1046 | revs->ignore_packed = NULL; |
| 1047 | revs->num_ignore_packed = 0; |
| 1048 | continue; |
| 1049 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1050 | if (!prefixcmp(arg, "--unpacked=")) { |
Junio C Hamano | 106d710 | 2006-09-06 02:12:09 -0700 | [diff] [blame] | 1051 | revs->unpacked = 1; |
| 1052 | add_ignore_packed(revs, arg+11); |
Linus Torvalds | d9a8368 | 2006-02-27 08:54:36 -0800 | [diff] [blame] | 1053 | continue; |
| 1054 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1055 | if (!strcmp(arg, "-r")) { |
| 1056 | revs->diff = 1; |
| 1057 | revs->diffopt.recursive = 1; |
| 1058 | continue; |
| 1059 | } |
| 1060 | if (!strcmp(arg, "-t")) { |
| 1061 | revs->diff = 1; |
| 1062 | revs->diffopt.recursive = 1; |
| 1063 | revs->diffopt.tree_in_recursive = 1; |
| 1064 | continue; |
| 1065 | } |
| 1066 | if (!strcmp(arg, "-m")) { |
| 1067 | revs->ignore_merges = 0; |
| 1068 | continue; |
| 1069 | } |
| 1070 | if (!strcmp(arg, "-c")) { |
| 1071 | revs->diff = 1; |
Junio C Hamano | 0fe7c1d | 2006-04-29 01:24:49 -0700 | [diff] [blame] | 1072 | revs->dense_combined_merges = 0; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1073 | revs->combine_merges = 1; |
| 1074 | continue; |
| 1075 | } |
| 1076 | if (!strcmp(arg, "--cc")) { |
| 1077 | revs->diff = 1; |
| 1078 | revs->dense_combined_merges = 1; |
| 1079 | revs->combine_merges = 1; |
| 1080 | continue; |
| 1081 | } |
| 1082 | if (!strcmp(arg, "-v")) { |
| 1083 | revs->verbose_header = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1084 | continue; |
| 1085 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1086 | if (!prefixcmp(arg, "--pretty")) { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1087 | revs->verbose_header = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1088 | revs->commit_format = get_commit_format(arg+8); |
| 1089 | continue; |
| 1090 | } |
| 1091 | if (!strcmp(arg, "--root")) { |
| 1092 | revs->show_root_diff = 1; |
| 1093 | continue; |
| 1094 | } |
| 1095 | if (!strcmp(arg, "--no-commit-id")) { |
| 1096 | revs->no_commit_id = 1; |
| 1097 | continue; |
| 1098 | } |
| 1099 | if (!strcmp(arg, "--always")) { |
| 1100 | revs->always_show_header = 1; |
| 1101 | continue; |
| 1102 | } |
| 1103 | if (!strcmp(arg, "--no-abbrev")) { |
| 1104 | revs->abbrev = 0; |
| 1105 | continue; |
| 1106 | } |
| 1107 | if (!strcmp(arg, "--abbrev")) { |
| 1108 | revs->abbrev = DEFAULT_ABBREV; |
| 1109 | continue; |
| 1110 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1111 | if (!prefixcmp(arg, "--abbrev=")) { |
Linus Torvalds | 508d9e3 | 2006-05-27 12:24:30 -0700 | [diff] [blame] | 1112 | revs->abbrev = strtoul(arg + 9, NULL, 10); |
| 1113 | if (revs->abbrev < MINIMUM_ABBREV) |
| 1114 | revs->abbrev = MINIMUM_ABBREV; |
| 1115 | else if (revs->abbrev > 40) |
| 1116 | revs->abbrev = 40; |
| 1117 | continue; |
| 1118 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1119 | if (!strcmp(arg, "--abbrev-commit")) { |
| 1120 | revs->abbrev_commit = 1; |
| 1121 | continue; |
| 1122 | } |
| 1123 | if (!strcmp(arg, "--full-diff")) { |
| 1124 | revs->diff = 1; |
| 1125 | revs->full_diff = 1; |
| 1126 | continue; |
| 1127 | } |
Linus Torvalds | 9202434 | 2006-06-11 10:57:35 -0700 | [diff] [blame] | 1128 | if (!strcmp(arg, "--full-history")) { |
| 1129 | revs->simplify_history = 0; |
| 1130 | continue; |
| 1131 | } |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 1132 | if (!strcmp(arg, "--relative-date")) { |
Junio C Hamano | a7b02cc | 2007-04-24 23:36:22 -0700 | [diff] [blame] | 1133 | revs->date_mode = DATE_RELATIVE; |
| 1134 | continue; |
| 1135 | } |
| 1136 | if (!strncmp(arg, "--date=", 7)) { |
| 1137 | if (!strcmp(arg + 7, "relative")) |
| 1138 | revs->date_mode = DATE_RELATIVE; |
Junio C Hamano | b727a23 | 2007-07-13 23:03:37 -0700 | [diff] [blame] | 1139 | else if (!strcmp(arg + 7, "iso8601") || |
| 1140 | !strcmp(arg + 7, "iso")) |
| 1141 | revs->date_mode = DATE_ISO8601; |
| 1142 | else if (!strcmp(arg + 7, "rfc2822") || |
| 1143 | !strcmp(arg + 7, "rfc")) |
| 1144 | revs->date_mode = DATE_RFC2822; |
| 1145 | else if (!strcmp(arg + 7, "short")) |
| 1146 | revs->date_mode = DATE_SHORT; |
Junio C Hamano | a7b02cc | 2007-04-24 23:36:22 -0700 | [diff] [blame] | 1147 | else if (!strcmp(arg + 7, "local")) |
| 1148 | revs->date_mode = DATE_LOCAL; |
| 1149 | else if (!strcmp(arg + 7, "default")) |
| 1150 | revs->date_mode = DATE_NORMAL; |
| 1151 | else |
| 1152 | die("unknown date format %s", arg); |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 1153 | continue; |
| 1154 | } |
Marco Costalba | 9fa3465 | 2007-07-20 20:15:13 +0200 | [diff] [blame] | 1155 | if (!strcmp(arg, "--log-size")) { |
| 1156 | revs->show_log_size = 1; |
| 1157 | continue; |
| 1158 | } |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1159 | |
| 1160 | /* |
| 1161 | * Grepping the commit log |
| 1162 | */ |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1163 | if (!prefixcmp(arg, "--author=")) { |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1164 | add_header_grep(revs, "author", arg+9); |
| 1165 | continue; |
| 1166 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1167 | if (!prefixcmp(arg, "--committer=")) { |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1168 | add_header_grep(revs, "committer", arg+12); |
| 1169 | continue; |
| 1170 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1171 | if (!prefixcmp(arg, "--grep=")) { |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1172 | add_message_grep(revs, arg+7); |
| 1173 | continue; |
| 1174 | } |
Junio C Hamano | e5633cb | 2007-07-21 23:18:33 -0700 | [diff] [blame] | 1175 | if (!strcmp(arg, "--extended-regexp") || |
| 1176 | !strcmp(arg, "-E")) { |
Petr Baudis | 93d496a | 2007-05-19 02:13:29 +0200 | [diff] [blame] | 1177 | regflags |= REG_EXTENDED; |
| 1178 | continue; |
| 1179 | } |
Junio C Hamano | e5633cb | 2007-07-21 23:18:33 -0700 | [diff] [blame] | 1180 | if (!strcmp(arg, "--regexp-ignore-case") || |
| 1181 | !strcmp(arg, "-i")) { |
Petr Baudis | 93d496a | 2007-05-19 02:13:29 +0200 | [diff] [blame] | 1182 | regflags |= REG_ICASE; |
| 1183 | continue; |
| 1184 | } |
Junio C Hamano | 70d0afb | 2006-09-27 17:55:58 -0700 | [diff] [blame] | 1185 | if (!strcmp(arg, "--all-match")) { |
| 1186 | all_match = 1; |
| 1187 | continue; |
| 1188 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1189 | if (!prefixcmp(arg, "--encoding=")) { |
Junio C Hamano | 7cbcf4d | 2006-12-30 11:59:08 -0800 | [diff] [blame] | 1190 | arg += 11; |
| 1191 | if (strcmp(arg, "none")) |
James Bowes | 567fb65 | 2007-03-19 17:42:40 -0400 | [diff] [blame] | 1192 | git_log_output_encoding = xstrdup(arg); |
Junio C Hamano | 7cbcf4d | 2006-12-30 11:59:08 -0800 | [diff] [blame] | 1193 | else |
| 1194 | git_log_output_encoding = ""; |
| 1195 | continue; |
| 1196 | } |
Johannes Schindelin | 9c5e66e | 2007-01-20 23:04:02 +0100 | [diff] [blame] | 1197 | if (!strcmp(arg, "--reverse")) { |
| 1198 | revs->reverse ^= 1; |
| 1199 | continue; |
| 1200 | } |
Johannes Schindelin | 8e64006 | 2007-07-24 00:38:40 +0100 | [diff] [blame] | 1201 | if (!strcmp(arg, "--no-walk")) { |
| 1202 | revs->no_walk = 1; |
| 1203 | continue; |
| 1204 | } |
| 1205 | if (!strcmp(arg, "--do-walk")) { |
| 1206 | revs->no_walk = 0; |
| 1207 | continue; |
| 1208 | } |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1209 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1210 | opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i); |
| 1211 | if (opts > 0) { |
Junio C Hamano | 66e41f7 | 2007-06-15 23:48:35 -0700 | [diff] [blame] | 1212 | if (strcmp(argv[i], "-z")) |
| 1213 | revs->diff = 1; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1214 | i += opts - 1; |
| 1215 | continue; |
| 1216 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1217 | *unrecognized++ = arg; |
| 1218 | left++; |
| 1219 | continue; |
| 1220 | } |
Rene Scharfe | 0d2c9d6 | 2006-07-02 01:29:37 +0200 | [diff] [blame] | 1221 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1222 | if (handle_revision_arg(arg, revs, flags, seen_dashdash)) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1223 | int j; |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1224 | if (seen_dashdash || *arg == '^') |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1225 | die("bad revision '%s'", arg); |
| 1226 | |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 1227 | /* If we didn't have a "--": |
| 1228 | * (1) all filenames must exist; |
| 1229 | * (2) all rev-args must not be interpretable |
| 1230 | * as a valid filename. |
| 1231 | * but the latter we have checked in the main loop. |
| 1232 | */ |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 1233 | for (j = i; j < argc; j++) |
| 1234 | verify_filename(revs->prefix, argv[j]); |
| 1235 | |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1236 | revs->prune_data = get_pathspec(revs->prefix, |
| 1237 | argv + i); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1238 | break; |
| 1239 | } |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1240 | } |
Junio C Hamano | 5d6f093 | 2006-09-05 21:28:36 -0700 | [diff] [blame] | 1241 | |
Petr Baudis | 93d496a | 2007-05-19 02:13:29 +0200 | [diff] [blame] | 1242 | if (revs->grep_filter) |
| 1243 | revs->grep_filter->regflags |= regflags; |
| 1244 | |
Junio C Hamano | ae3e5e1 | 2006-07-03 02:59:32 -0700 | [diff] [blame] | 1245 | if (show_merge) |
| 1246 | prepare_show_merge(revs); |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1247 | if (def && !revs->pending.nr) { |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1248 | unsigned char sha1[20]; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1249 | struct object *object; |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 1250 | unsigned mode; |
| 1251 | if (get_sha1_with_mode(def, sha1, &mode)) |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1252 | die("bad default revision '%s'", def); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1253 | object = get_reference(revs, def, sha1, 0); |
Martin Koegler | bb6c2fb | 2007-04-22 18:43:59 +0200 | [diff] [blame] | 1254 | add_pending_object_with_mode(revs, object, def, mode); |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1255 | } |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1256 | |
Junio C Hamano | 9dad9d2 | 2006-10-30 18:58:03 -0800 | [diff] [blame] | 1257 | if (revs->topo_order) |
Junio C Hamano | 5306968 | 2006-04-01 18:38:25 -0800 | [diff] [blame] | 1258 | revs->limited = 1; |
| 1259 | |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1260 | if (revs->prune_data) { |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1261 | diff_tree_setup_paths(revs->prune_data, &revs->pruning); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 1262 | /* Can't prune commits with rename following: the paths change.. */ |
| 1263 | if (!revs->diffopt.follow_renames) |
| 1264 | revs->prune_fn = try_to_simplify_commit; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1265 | if (!revs->full_diff) |
| 1266 | diff_tree_setup_paths(revs->prune_data, &revs->diffopt); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1267 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1268 | if (revs->combine_merges) { |
| 1269 | revs->ignore_merges = 0; |
Timo Hirvonen | 0e677e1 | 2006-06-24 20:25:08 +0300 | [diff] [blame] | 1270 | if (revs->dense_combined_merges && !revs->diffopt.output_format) |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1271 | revs->diffopt.output_format = DIFF_FORMAT_PATCH; |
| 1272 | } |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1273 | revs->diffopt.abbrev = revs->abbrev; |
Junio C Hamano | 72ee96c | 2006-08-09 12:45:27 -0700 | [diff] [blame] | 1274 | if (diff_setup_done(&revs->diffopt) < 0) |
| 1275 | die("diff_setup_done failed"); |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1276 | |
Junio C Hamano | 70d0afb | 2006-09-27 17:55:58 -0700 | [diff] [blame] | 1277 | if (revs->grep_filter) { |
| 1278 | revs->grep_filter->all_match = all_match; |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1279 | compile_grep_patterns(revs->grep_filter); |
Junio C Hamano | 70d0afb | 2006-09-27 17:55:58 -0700 | [diff] [blame] | 1280 | } |
Junio C Hamano | bd95fcd | 2006-09-17 17:23:20 -0700 | [diff] [blame] | 1281 | |
Shawn O. Pearce | d56651c | 2007-08-19 22:33:43 -0400 | [diff] [blame] | 1282 | if (revs->reverse && revs->reflog_info) |
| 1283 | die("cannot combine --reverse with --walk-reflogs"); |
| 1284 | |
Linus Torvalds | ae56354 | 2006-02-25 16:19:46 -0800 | [diff] [blame] | 1285 | return left; |
| 1286 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1287 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1288 | int prepare_revision_walk(struct rev_info *revs) |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1289 | { |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1290 | int nr = revs->pending.nr; |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 1291 | struct object_array_entry *e, *list; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1292 | |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 1293 | e = list = revs->pending.objects; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1294 | revs->pending.nr = 0; |
| 1295 | revs->pending.alloc = 0; |
| 1296 | revs->pending.objects = NULL; |
| 1297 | while (--nr >= 0) { |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 1298 | struct commit *commit = handle_commit(revs, e->item, e->name); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1299 | if (commit) { |
| 1300 | if (!(commit->object.flags & SEEN)) { |
| 1301 | commit->object.flags |= SEEN; |
| 1302 | insert_by_date(commit, &revs->commits); |
| 1303 | } |
| 1304 | } |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 1305 | e++; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1306 | } |
Junio C Hamano | 94d2367 | 2007-01-10 22:36:16 -0800 | [diff] [blame] | 1307 | free(list); |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 1308 | |
Linus Torvalds | ba1d450 | 2006-04-15 12:09:56 -0700 | [diff] [blame] | 1309 | if (revs->no_walk) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1310 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1311 | if (revs->limited) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1312 | if (limit_list(revs) < 0) |
| 1313 | return -1; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1314 | if (revs->topo_order) |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1315 | sort_in_topological_order_fn(&revs->commits, revs->lifo, |
| 1316 | revs->topo_setter, |
| 1317 | revs->topo_getter); |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1318 | return 0; |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1321 | enum rewrite_result { |
| 1322 | rewrite_one_ok, |
| 1323 | rewrite_one_noparents, |
| 1324 | rewrite_one_error, |
| 1325 | }; |
| 1326 | |
| 1327 | 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] | 1328 | { |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1329 | for (;;) { |
| 1330 | struct commit *p = *pp; |
Linus Torvalds | 3381c79 | 2006-04-08 17:05:58 -0700 | [diff] [blame] | 1331 | if (!revs->limited) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1332 | if (add_parents_to_list(revs, p, &revs->commits) < 0) |
| 1333 | return rewrite_one_error; |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 1334 | if (p->parents && p->parents->next) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1335 | return rewrite_one_ok; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1336 | if (p->object.flags & (TREECHANGE | UNINTERESTING)) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1337 | return rewrite_one_ok; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1338 | if (!p->parents) |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1339 | return rewrite_one_noparents; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1340 | *pp = p->parents->item; |
| 1341 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1344 | static void remove_duplicate_parents(struct commit *commit) |
| 1345 | { |
Linus Torvalds | e1abc69 | 2007-07-20 23:11:19 -0700 | [diff] [blame] | 1346 | struct commit_list **pp, *p; |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1347 | |
| 1348 | /* Examine existing parents while marking ones we have seen... */ |
Linus Torvalds | e1abc69 | 2007-07-20 23:11:19 -0700 | [diff] [blame] | 1349 | pp = &commit->parents; |
| 1350 | while ((p = *pp) != NULL) { |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1351 | struct commit *parent = p->item; |
Linus Torvalds | e1abc69 | 2007-07-20 23:11:19 -0700 | [diff] [blame] | 1352 | if (parent->object.flags & TMP_MARK) { |
| 1353 | *pp = p->next; |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1354 | continue; |
Linus Torvalds | e1abc69 | 2007-07-20 23:11:19 -0700 | [diff] [blame] | 1355 | } |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1356 | parent->object.flags |= TMP_MARK; |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1357 | pp = &p->next; |
| 1358 | } |
| 1359 | /* ... and clear the temporary mark */ |
| 1360 | for (p = commit->parents; p; p = p->next) |
| 1361 | p->item->object.flags &= ~TMP_MARK; |
| 1362 | } |
| 1363 | |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1364 | static int rewrite_parents(struct rev_info *revs, struct commit *commit) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1365 | { |
| 1366 | struct commit_list **pp = &commit->parents; |
| 1367 | while (*pp) { |
| 1368 | struct commit_list *parent = *pp; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1369 | switch (rewrite_one(revs, &parent->item)) { |
| 1370 | case rewrite_one_ok: |
| 1371 | break; |
| 1372 | case rewrite_one_noparents: |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1373 | *pp = parent->next; |
| 1374 | continue; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1375 | case rewrite_one_error: |
| 1376 | return -1; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1377 | } |
| 1378 | pp = &parent->next; |
| 1379 | } |
Junio C Hamano | 11d6596 | 2007-07-08 19:03:19 -0700 | [diff] [blame] | 1380 | remove_duplicate_parents(commit); |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1381 | return 0; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1382 | } |
Linus Torvalds | a4a88b2 | 2006-02-28 11:24:00 -0800 | [diff] [blame] | 1383 | |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 1384 | static int commit_match(struct commit *commit, struct rev_info *opt) |
| 1385 | { |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1386 | if (!opt->grep_filter) |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 1387 | return 1; |
Junio C Hamano | 2d10c55 | 2006-09-20 13:21:56 -0700 | [diff] [blame] | 1388 | return grep_buffer(opt->grep_filter, |
| 1389 | NULL, /* we say nothing, not even filename */ |
| 1390 | commit->buffer, strlen(commit->buffer)); |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1393 | static struct commit *get_revision_1(struct rev_info *revs) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1394 | { |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1395 | if (!revs->commits) |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1396 | return NULL; |
| 1397 | |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1398 | do { |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 1399 | struct commit_list *entry = revs->commits; |
| 1400 | struct commit *commit = entry->item; |
Linus Torvalds | ea5ed3a | 2006-03-05 09:53:52 -0800 | [diff] [blame] | 1401 | |
Linus Torvalds | cb11574 | 2006-06-17 18:47:58 -0700 | [diff] [blame] | 1402 | revs->commits = entry->next; |
| 1403 | free(entry); |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1404 | |
Johannes Schindelin | 8860fd4 | 2007-01-11 11:47:48 +0100 | [diff] [blame] | 1405 | if (revs->reflog_info) |
| 1406 | fake_reflog_parent(revs->reflog_info, commit); |
| 1407 | |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1408 | /* |
| 1409 | * 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] | 1410 | * the parents here. We also need to do the date-based limiting |
| 1411 | * that we'd otherwise have done in limit_list(). |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1412 | */ |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 1413 | if (!revs->limited) { |
Jan Harkes | 744f498 | 2006-10-30 20:37:49 -0500 | [diff] [blame] | 1414 | if (revs->max_age != -1 && |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1415 | (commit->date < revs->max_age)) |
| 1416 | continue; |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1417 | if (add_parents_to_list(revs, commit, &revs->commits) < 0) |
| 1418 | return NULL; |
Linus Torvalds | be7db6e | 2006-04-01 16:35:06 -0800 | [diff] [blame] | 1419 | } |
Junio C Hamano | 384e99a | 2006-03-27 23:58:34 -0800 | [diff] [blame] | 1420 | if (commit->object.flags & SHOWN) |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1421 | continue; |
Junio C Hamano | 1b65a5a | 2006-04-16 18:12:49 -0700 | [diff] [blame] | 1422 | |
Jan Harkes | 744f498 | 2006-10-30 20:37:49 -0500 | [diff] [blame] | 1423 | if (revs->unpacked && has_sha1_pack(commit->object.sha1, |
| 1424 | revs->ignore_packed)) |
| 1425 | continue; |
| 1426 | |
Junio C Hamano | 1b65a5a | 2006-04-16 18:12:49 -0700 | [diff] [blame] | 1427 | if (commit->object.flags & UNINTERESTING) |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1428 | continue; |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1429 | if (revs->min_age != -1 && (commit->date > revs->min_age)) |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1430 | continue; |
Junio C Hamano | 384e99a | 2006-03-27 23:58:34 -0800 | [diff] [blame] | 1431 | if (revs->no_merges && |
| 1432 | commit->parents && commit->parents->next) |
Linus Torvalds | 2a0925b | 2006-03-30 17:05:25 -0800 | [diff] [blame] | 1433 | continue; |
Junio C Hamano | 8ecae9b | 2006-09-17 15:43:40 -0700 | [diff] [blame] | 1434 | if (!commit_match(commit, revs)) |
| 1435 | continue; |
Fredrik Kuivinen | 8efdc32 | 2006-03-10 10:21:39 +0100 | [diff] [blame] | 1436 | if (revs->prune_fn && revs->dense) { |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 1437 | /* Commit without changes? */ |
| 1438 | if (!(commit->object.flags & TREECHANGE)) { |
| 1439 | /* drop merges unless we want parenthood */ |
| 1440 | if (!revs->parents) |
| 1441 | continue; |
| 1442 | /* non-merge - always ignore it */ |
Linus Torvalds | 02d3dca | 2006-07-02 10:55:59 -0700 | [diff] [blame] | 1443 | if (!commit->parents || !commit->parents->next) |
Linus Torvalds | 6631c73 | 2006-06-30 20:21:59 -0700 | [diff] [blame] | 1444 | continue; |
| 1445 | } |
Alex Riesen | cc0e6c5 | 2007-05-04 23:54:57 +0200 | [diff] [blame] | 1446 | if (revs->parents && rewrite_parents(revs, commit) < 0) |
| 1447 | return NULL; |
Junio C Hamano | 384e99a | 2006-03-27 23:58:34 -0800 | [diff] [blame] | 1448 | } |
Linus Torvalds | 765ac8e | 2006-02-28 15:07:20 -0800 | [diff] [blame] | 1449 | return commit; |
| 1450 | } while (revs->commits); |
| 1451 | return NULL; |
| 1452 | } |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1453 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1454 | static void gc_boundary(struct object_array *array) |
| 1455 | { |
| 1456 | unsigned nr = array->nr; |
| 1457 | unsigned alloc = array->alloc; |
| 1458 | struct object_array_entry *objects = array->objects; |
| 1459 | |
| 1460 | if (alloc <= nr) { |
| 1461 | unsigned i, j; |
| 1462 | for (i = j = 0; i < nr; i++) { |
| 1463 | if (objects[i].item->flags & SHOWN) |
| 1464 | continue; |
| 1465 | if (i != j) |
| 1466 | objects[j] = objects[i]; |
| 1467 | j++; |
| 1468 | } |
Junio C Hamano | 892ae6b | 2007-03-06 03:00:18 -0800 | [diff] [blame] | 1469 | for (i = j; i < nr; i++) |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1470 | objects[i].item = NULL; |
| 1471 | array->nr = j; |
| 1472 | } |
| 1473 | } |
| 1474 | |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1475 | struct commit *get_revision(struct rev_info *revs) |
| 1476 | { |
| 1477 | struct commit *c = NULL; |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1478 | struct commit_list *l; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1479 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1480 | if (revs->boundary == 2) { |
| 1481 | unsigned i; |
| 1482 | struct object_array *array = &revs->boundary_commits; |
| 1483 | struct object_array_entry *objects = array->objects; |
| 1484 | for (i = 0; i < array->nr; i++) { |
| 1485 | c = (struct commit *)(objects[i].item); |
| 1486 | if (!c) |
| 1487 | continue; |
| 1488 | if (!(c->object.flags & CHILD_SHOWN)) |
| 1489 | continue; |
| 1490 | if (!(c->object.flags & SHOWN)) |
| 1491 | break; |
Johannes Schindelin | 9c5e66e | 2007-01-20 23:04:02 +0100 | [diff] [blame] | 1492 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1493 | if (array->nr <= i) |
Johannes Schindelin | 9c5e66e | 2007-01-20 23:04:02 +0100 | [diff] [blame] | 1494 | return NULL; |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1495 | |
| 1496 | c->object.flags |= SHOWN | BOUNDARY; |
Johannes Schindelin | 9c5e66e | 2007-01-20 23:04:02 +0100 | [diff] [blame] | 1497 | return c; |
| 1498 | } |
| 1499 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1500 | if (revs->reverse) { |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1501 | int limit = -1; |
| 1502 | |
| 1503 | if (0 <= revs->max_count) { |
| 1504 | limit = revs->max_count; |
| 1505 | if (0 < revs->skip_count) |
| 1506 | limit += revs->skip_count; |
| 1507 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1508 | l = NULL; |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1509 | while ((c = get_revision_1(revs))) { |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1510 | commit_list_insert(c, &l); |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1511 | if ((0 < limit) && !--limit) |
| 1512 | break; |
| 1513 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1514 | revs->commits = l; |
| 1515 | revs->reverse = 0; |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1516 | revs->max_count = -1; |
Junio C Hamano | 2b06469 | 2007-03-05 16:10:28 -0800 | [diff] [blame] | 1517 | c = NULL; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1520 | /* |
| 1521 | * Now pick up what they want to give us |
| 1522 | */ |
Junio C Hamano | 8839ac9 | 2007-03-06 03:20:55 -0800 | [diff] [blame] | 1523 | c = get_revision_1(revs); |
| 1524 | if (c) { |
| 1525 | while (0 < revs->skip_count) { |
| 1526 | revs->skip_count--; |
| 1527 | c = get_revision_1(revs); |
| 1528 | if (!c) |
| 1529 | break; |
| 1530 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Check the max_count. |
| 1535 | */ |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1536 | switch (revs->max_count) { |
| 1537 | case -1: |
| 1538 | break; |
| 1539 | case 0: |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1540 | c = NULL; |
| 1541 | break; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1542 | default: |
| 1543 | revs->max_count--; |
| 1544 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1545 | |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1546 | if (c) |
| 1547 | c->object.flags |= SHOWN; |
| 1548 | |
| 1549 | if (!revs->boundary) { |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1550 | return c; |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1551 | } |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1552 | |
| 1553 | if (!c) { |
| 1554 | /* |
| 1555 | * get_revision_1() runs out the commits, and |
| 1556 | * we are done computing the boundaries. |
| 1557 | * switch to boundary commits output mode. |
| 1558 | */ |
| 1559 | revs->boundary = 2; |
| 1560 | return get_revision(revs); |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * boundary commits are the commits that are parents of the |
| 1565 | * ones we got from get_revision_1() but they themselves are |
| 1566 | * not returned from get_revision_1(). Before returning |
| 1567 | * 'c', we need to mark its parents that they could be boundaries. |
| 1568 | */ |
| 1569 | |
| 1570 | for (l = c->parents; l; l = l->next) { |
| 1571 | struct object *p; |
| 1572 | p = &(l->item->object); |
Junio C Hamano | c33d859 | 2007-03-05 18:23:57 -0800 | [diff] [blame] | 1573 | if (p->flags & (CHILD_SHOWN | SHOWN)) |
Junio C Hamano | 86ab490 | 2007-03-05 13:10:06 -0800 | [diff] [blame] | 1574 | continue; |
| 1575 | p->flags |= CHILD_SHOWN; |
| 1576 | gc_boundary(&revs->boundary_commits); |
| 1577 | add_object_array(p, NULL, &revs->boundary_commits); |
| 1578 | } |
| 1579 | |
| 1580 | return c; |
Junio C Hamano | d5db6c9 | 2006-12-19 18:25:32 -0800 | [diff] [blame] | 1581 | } |