@matheusbsbs / matheus-bagliano / commits / 8efdc326c9
Fork 0

rev-lib: Make it easy to do rename tracking (take 2)

prune_fn in the rev_info structure is called in place of try_to_simplify_commit. This makes it possible to do rename tracking with a custom try_to_simplify_commit-like function. This commit also introduces init_revisions which initialises the rev_info structure with default values. Signed-off-by: Fredrik Kuivinen <freku045@student.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Fredrik Kuivinen committed Mar 10, 2006 at 10:21 UTC 8efdc326c9681e814d79e8da6097c187388dc0c4
3 files changed +75 -47
rev-list.c
+3 -3
@@ -190,7 +190,7 @@ static int count_distance(struct commit_list *entry)
190
191 if (commit->object.flags & (UNINTERESTING | COUNTED))
192 break;
193 - if (!revs.paths || (commit->object.flags & TREECHANGE))
193 + if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
194 nr++;
195 commit->object.flags |= COUNTED;
196 p = commit->parents;
@@ -224,7 +224,7 @@ static struct commit_list *find_bisection(struct commit_list *list)
224 nr = 0;
225 p = list;
226 while (p) {
227 - if (!revs.paths || (p->item->object.flags & TREECHANGE))
227 + if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
228 nr++;
229 p = p->next;
230 }
@@ -234,7 +234,7 @@ static struct commit_list *find_bisection(struct commit_list *list)
234 for (p = list; p; p = p->next) {
235 int distance;
236
237 - if (revs.paths && !(p->item->object.flags & TREECHANGE))
237 + if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
238 continue;
239
240 distance = count_distance(p);
revision.c
+55 -43
@@ -199,31 +199,27 @@ static int everybody_uninteresting(struct commit_list *orig)
199 return 1;
200 }
201
202 -#define TREE_SAME 0
203 -#define TREE_NEW 1
204 -#define TREE_DIFFERENT 2
205 -static int tree_difference = TREE_SAME;
202 +static int tree_difference = REV_TREE_SAME;
203
204 static void file_add_remove(struct diff_options *options,
205 int addremove, unsigned mode,
206 const unsigned char *sha1,
207 const char *base, const char *path)
208 {
212 - int diff = TREE_DIFFERENT;
209 + int diff = REV_TREE_DIFFERENT;
210
211 /*
215 - * Is it an add of a new file? It means that
216 - * the old tree didn't have it at all, so we
217 - * will turn "TREE_SAME" -> "TREE_NEW", but
218 - * leave any "TREE_DIFFERENT" alone (and if
219 - * it already was "TREE_NEW", we'll keep it
220 - * "TREE_NEW" of course).
212 + * Is it an add of a new file? It means that the old tree
213 + * didn't have it at all, so we will turn "REV_TREE_SAME" ->
214 + * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
215 + * (and if it already was "REV_TREE_NEW", we'll keep it
216 + * "REV_TREE_NEW" of course).
217 */
218 if (addremove == '+') {
219 diff = tree_difference;
224 - if (diff != TREE_SAME)
220 + if (diff != REV_TREE_SAME)
221 return;
226 - diff = TREE_NEW;
222 + diff = REV_TREE_NEW;
223 }
224 tree_difference = diff;
225 }
@@ -234,7 +230,7 @@ static void file_change(struct diff_options *options,
230 const unsigned char *new_sha1,
231 const char *base, const char *path)
232 {
237 - tree_difference = TREE_DIFFERENT;
233 + tree_difference = REV_TREE_DIFFERENT;
234 }
235
236 static struct diff_options diff_opt = {
@@ -243,19 +239,19 @@ static struct diff_options diff_opt = {
239 .change = file_change,
240 };
241
246 -static int compare_tree(struct tree *t1, struct tree *t2)
242 +int rev_compare_tree(struct tree *t1, struct tree *t2)
243 {
244 if (!t1)
249 - return TREE_NEW;
245 + return REV_TREE_NEW;
246 if (!t2)
251 - return TREE_DIFFERENT;
252 - tree_difference = TREE_SAME;
247 + return REV_TREE_DIFFERENT;
248 + tree_difference = REV_TREE_SAME;
249 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
254 - return TREE_DIFFERENT;
250 + return REV_TREE_DIFFERENT;
251 return tree_difference;
252 }
253
258 -static int same_tree_as_empty(struct tree *t1)
254 +int rev_same_tree_as_empty(struct tree *t1)
255 {
256 int retval;
257 void *tree;
@@ -288,7 +284,7 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
284 return;
285
286 if (!commit->parents) {
291 - if (!same_tree_as_empty(commit->tree))
287 + if (!rev_same_tree_as_empty(commit->tree))
288 commit->object.flags |= TREECHANGE;
289 return;
290 }
@@ -298,8 +294,8 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
294 struct commit *p = parent->item;
295
296 parse_commit(p);
301 - switch (compare_tree(p->tree, commit->tree)) {
302 - case TREE_SAME:
297 + switch (rev_compare_tree(p->tree, commit->tree)) {
298 + case REV_TREE_SAME:
299 if (p->object.flags & UNINTERESTING) {
300 /* Even if a merge with an uninteresting
301 * side branch brought the entire change
@@ -314,13 +310,14 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
310 commit->parents = parent;
311 return;
312
317 - case TREE_NEW:
318 - if (revs->remove_empty_trees && same_tree_as_empty(p->tree)) {
313 + case REV_TREE_NEW:
314 + if (revs->remove_empty_trees &&
315 + rev_same_tree_as_empty(p->tree)) {
316 *pp = parent->next;
317 continue;
318 }
319 /* fallthrough */
323 - case TREE_DIFFERENT:
320 + case REV_TREE_DIFFERENT:
321 tree_changed = 1;
322 pp = &parent->next;
323 continue;
@@ -368,8 +365,8 @@ static void add_parents_to_list(struct rev_info *revs, struct commit *commit, st
365 * simplify the commit history and find the parent
366 * that has no differences in the path set if one exists.
367 */
371 - if (revs->paths)
372 - try_to_simplify_commit(revs, commit);
368 + if (revs->prune_fn)
369 + revs->prune_fn(revs, commit);
370
371 parent = commit->parents;
372 while (parent) {
@@ -391,9 +388,6 @@ static void limit_list(struct rev_info *revs)
388 struct commit_list *newlist = NULL;
389 struct commit_list **p = &newlist;
390
394 - if (revs->paths)
395 - diff_tree_setup_paths(revs->paths);
396 -
391 while (list) {
392 struct commit_list *entry = list;
393 struct commit *commit = list->item;
@@ -445,6 +439,23 @@ static void handle_all(struct rev_info *revs, unsigned flags)
439 for_each_ref(handle_one_ref);
440 }
441
442 +void init_revisions(struct rev_info *revs)
443 +{
444 + memset(revs, 0, sizeof(*revs));
445 + revs->lifo = 1;
446 + revs->dense = 1;
447 + revs->prefix = setup_git_directory();
448 + revs->max_age = -1;
449 + revs->min_age = -1;
450 + revs->max_count = -1;
451 +
452 + revs->prune_fn = NULL;
453 + revs->prune_data = NULL;
454 +
455 + revs->topo_setter = topo_sort_default_setter;
456 + revs->topo_getter = topo_sort_default_getter;
457 +}
458 +
459 /*
460 * Parse revision information, filling in the "rev_info" structure,
461 * and removing the used arguments from the argument list.
@@ -458,13 +469,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
469 const char **unrecognized = argv + 1;
470 int left = 1;
471
461 - memset(revs, 0, sizeof(*revs));
462 - revs->lifo = 1;
463 - revs->dense = 1;
464 - revs->prefix = setup_git_directory();
465 - revs->max_age = -1;
466 - revs->min_age = -1;
467 - revs->max_count = -1;
472 + init_revisions(revs);
473
474 /* First, search for "--" */
475 seen_dashdash = 0;
@@ -474,7 +479,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
479 continue;
480 argv[i] = NULL;
481 argc = i;
477 - revs->paths = get_pathspec(revs->prefix, argv + i + 1);
482 + revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
483 seen_dashdash = 1;
484 break;
485 }
@@ -638,7 +643,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
643 if (lstat(argv[j], &st) < 0)
644 die("'%s': %s", arg, strerror(errno));
645 }
641 - revs->paths = get_pathspec(revs->prefix, argv + i);
646 + revs->prune_data = get_pathspec(revs->prefix, argv + i);
647 break;
648 }
649 commit = get_commit_reference(revs, arg, sha1, flags ^ local_flags);
@@ -652,8 +657,13 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
657 commit = get_commit_reference(revs, def, sha1, 0);
658 add_one_commit(commit, revs);
659 }
655 - if (revs->paths)
660 +
661 + if (revs->prune_data) {
662 + diff_tree_setup_paths(revs->prune_data);
663 + revs->prune_fn = try_to_simplify_commit;
664 revs->limited = 1;
665 + }
666 +
667 return left;
668 }
669
@@ -663,7 +673,9 @@ void prepare_revision_walk(struct rev_info *revs)
673 if (revs->limited)
674 limit_list(revs);
675 if (revs->topo_order)
666 - sort_in_topological_order(&revs->commits, revs->lifo);
676 + sort_in_topological_order_fn(&revs->commits, revs->lifo,
677 + revs->topo_setter,
678 + revs->topo_getter);
679 }
680
681 static int rewrite_one(struct commit **pp)
@@ -719,7 +731,7 @@ struct commit *get_revision(struct rev_info *revs)
731 return NULL;
732 if (revs->no_merges && commit->parents && commit->parents->next)
733 goto next;
722 - if (revs->paths && revs->dense) {
734 + if (revs->prune_fn && revs->dense) {
735 if (!(commit->object.flags & TREECHANGE))
736 goto next;
737 rewrite_parents(commit);
revision.h
+17 -1
@@ -7,6 +7,10 @@
7 #define SHOWN (1u<<3)
8 #define TMP_MARK (1u<<4) /* for isolated cases; clean after use */
9
10 +struct rev_info;
11 +
12 +typedef void (prune_fn_t)(struct rev_info *revs, struct commit *commit);
13 +
14 struct rev_info {
15 /* Starting list */
16 struct commit_list *commits;
@@ -14,7 +18,8 @@ struct rev_info {
18
19 /* Basic information */
20 const char *prefix;
17 - const char **paths;
21 + void *prune_data;
22 + prune_fn_t *prune_fn;
23
24 /* Traversal flags */
25 unsigned int dense:1,
@@ -33,9 +38,20 @@ struct rev_info {
38 int max_count;
39 unsigned long max_age;
40 unsigned long min_age;
41 +
42 + topo_sort_set_fn_t topo_setter;
43 + topo_sort_get_fn_t topo_getter;
44 };
45
46 +#define REV_TREE_SAME 0
47 +#define REV_TREE_NEW 1
48 +#define REV_TREE_DIFFERENT 2
49 +
50 /* revision.c */
51 +extern int rev_same_tree_as_empty(struct tree *t1);
52 +extern int rev_compare_tree(struct tree *t1, struct tree *t2);
53 +
54 +extern void init_revisions(struct rev_info *revs);
55 extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
56 extern void prepare_revision_walk(struct rev_info *revs);
57 extern struct commit *get_revision(struct rev_info *revs);