@matheusbsbs / matheus-bagliano / commits / ceff8e7ade
Fork 0

Clean up and simplify rev_compare_tree()

This simplifies the logic of rev_compare_tree() by removing a special case. It does so by turning the special case of finding a diff to be "all new files" into a more generic case of "all new" vs "all removed" vs "mixed changes", so now the code is actually more powerful and more generic, and the added symmetry actually makes it simpler too. This makes no changes to any existing behavior, but apart from the simplification it does make it possible to some day care about whether all changes were just deletions if we want to. Which we may well want to for merge handling. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Jun 2, 2009 at 18:34 UTC ceff8e7adeed51024491deb4933f23db760e5641
2 files changed +15 -23
revision.c
+12 -21
@@ -256,10 +256,12 @@ static int everybody_uninteresting(struct commit_list *orig)
256
257 /*
258 * The goal is to get REV_TREE_NEW as the result only if the
259 - * diff consists of all '+' (and no other changes), and
260 - * REV_TREE_DIFFERENT otherwise (of course if the trees are
261 - * the same we want REV_TREE_SAME). That means that once we
262 - * get to REV_TREE_DIFFERENT, we do not have to look any further.
259 + * diff consists of all '+' (and no other changes), REV_TREE_OLD
260 + * if the whole diff is removal of old data, and otherwise
261 + * REV_TREE_DIFFERENT (of course if the trees are the same we
262 + * want REV_TREE_SAME).
263 + * That means that once we get to REV_TREE_DIFFERENT, we do not
264 + * have to look any further.
265 */
266 static int tree_difference = REV_TREE_SAME;
267
@@ -268,22 +270,9 @@ static void file_add_remove(struct diff_options *options,
270 const unsigned char *sha1,
271 const char *fullpath)
272 {
271 - int diff = REV_TREE_DIFFERENT;
273 + int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
274
273 - /*
274 - * Is it an add of a new file? It means that the old tree
275 - * didn't have it at all, so we will turn "REV_TREE_SAME" ->
276 - * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
277 - * (and if it already was "REV_TREE_NEW", we'll keep it
278 - * "REV_TREE_NEW" of course).
279 - */
280 - if (addremove == '+') {
281 - diff = tree_difference;
282 - if (diff != REV_TREE_SAME)
283 - return;
284 - diff = REV_TREE_NEW;
285 - }
286 - tree_difference = diff;
275 + tree_difference |= diff;
276 if (tree_difference == REV_TREE_DIFFERENT)
277 DIFF_OPT_SET(options, HAS_CHANGES);
278 }
@@ -305,6 +294,8 @@ static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct
294
295 if (!t1)
296 return REV_TREE_NEW;
297 + if (!t2)
298 + return REV_TREE_OLD;
299
300 if (revs->simplify_by_decoration) {
301 /*
@@ -323,8 +314,7 @@ static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct
314 if (!revs->prune_data)
315 return REV_TREE_SAME;
316 }
326 - if (!t2)
327 - return REV_TREE_DIFFERENT;
317 +
318 tree_difference = REV_TREE_SAME;
319 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
320 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
@@ -429,6 +419,7 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
419 p->parents = NULL;
420 }
421 /* fallthrough */
422 + case REV_TREE_OLD:
423 case REV_TREE_DIFFERENT:
424 tree_changed = 1;
425 pp = &parent->next;
revision.h
+3 -2
@@ -118,8 +118,9 @@ struct rev_info {
118 };
119
120 #define REV_TREE_SAME 0
121 -#define REV_TREE_NEW 1
122 -#define REV_TREE_DIFFERENT 2
121 +#define REV_TREE_NEW 1 /* Only new files */
122 +#define REV_TREE_OLD 2 /* Only files removed */
123 +#define REV_TREE_DIFFERENT 3 /* Mixed changes */
124
125 /* revision.c */
126 void read_revisions_from_stdin(struct rev_info *revs);