Skip to content

Commit ceff8e7

Browse files
torvaldsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 433e972 commit ceff8e7

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

revision.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ static int everybody_uninteresting(struct commit_list *orig)
256256

257257
/*
258258
* 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.
263265
*/
264266
static int tree_difference = REV_TREE_SAME;
265267

@@ -268,22 +270,9 @@ static void file_add_remove(struct diff_options *options,
268270
const unsigned char *sha1,
269271
const char *fullpath)
270272
{
271-
int diff = REV_TREE_DIFFERENT;
273+
int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
272274

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;
287276
if (tree_difference == REV_TREE_DIFFERENT)
288277
DIFF_OPT_SET(options, HAS_CHANGES);
289278
}
@@ -305,6 +294,8 @@ static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct
305294

306295
if (!t1)
307296
return REV_TREE_NEW;
297+
if (!t2)
298+
return REV_TREE_OLD;
308299

309300
if (revs->simplify_by_decoration) {
310301
/*
@@ -323,8 +314,7 @@ static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct
323314
if (!revs->prune_data)
324315
return REV_TREE_SAME;
325316
}
326-
if (!t2)
327-
return REV_TREE_DIFFERENT;
317+
328318
tree_difference = REV_TREE_SAME;
329319
DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
330320
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)
429419
p->parents = NULL;
430420
}
431421
/* fallthrough */
422+
case REV_TREE_OLD:
432423
case REV_TREE_DIFFERENT:
433424
tree_changed = 1;
434425
pp = &parent->next;

revision.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ struct rev_info {
118118
};
119119

120120
#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 */
123124

124125
/* revision.c */
125126
void read_revisions_from_stdin(struct rev_info *revs);

0 commit comments

Comments
 (0)