merge-recursive: clarify code in was_tracked()

It can be puzzling to see that was_tracked() asks to get an index entry by name, but does not take a negative return value for an answer. The reason we have to do this is that cache_name_pos() only looks for entries in stage 0, even if nobody asked for any stage in particular. Let's rewrite the logic a little bit, to handle the easy case early: if cache_name_pos() returned a non-negative position, we know it is a match, and we do not even have to compare the name again (cache_name_pos() did that for us already). We can say right away: yes, this file was tracked. Only if there was no exact match do we need to look harder for any matching entry in stage 2. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 26, 2016 at 18:05 UTC f8d83fb66c653fff0541067a0b5a0821d3f548f9
1 file changed +14 -16
merge-recursive.c
+14 -16
@@ -667,23 +667,21 @@ static int was_tracked(const char *path)
667 {
668 int pos = cache_name_pos(path, strlen(path));
669
670 - if (pos < 0)
671 - pos = -1 - pos;
672 - while (pos < active_nr &&
673 - !strcmp(path, active_cache[pos]->name)) {
674 - /*
675 - * If stage #0, it is definitely tracked.
676 - * If it has stage #2 then it was tracked
677 - * before this merge started. All other
678 - * cases the path was not tracked.
679 - */
680 - switch (ce_stage(active_cache[pos])) {
681 - case 0:
682 - case 2:
670 + if (0 <= pos)
671 + /* we have been tracking this path */
672 + return 1;
673 +
674 + /*
675 + * Look for an unmerged entry for the path,
676 + * specifically stage #2, which would indicate
677 + * that "our" side before the merge started
678 + * had the path tracked (and resulted in a conflict).
679 + */
680 + for (pos = -1 - pos;
681 + pos < active_nr && !strcmp(path, active_cache[pos]->name);
682 + pos++)
683 + if (ce_stage(active_cache[pos]) == 2)
684 return 1;
684 - }
685 - pos++;
686 - }
685 return 0;
686 }
687