Skip to content

Commit e34f802

Browse files
peffgitster
authored andcommitted
merge-file: clamp exit code to maximum 127
Git-merge-file is documented to return one of three exit codes: - zero means the merge was successful - a negative number means an error occurred - a positive number indicates the number of conflicts Unfortunately, this all gets stuffed into an 8-bit return code. Which means that if you have 256 conflicts, this wraps to zero, and the merge appears to succeed (and commits a blob full of conflict-marker cruft!). This patch clamps the return value to a maximum of 127, which we should be able to safely represent everywhere. This also leaves 128-255 for other values. Shells (and some parts of git) will typically represent signal death as 128 plus the signal number. And negative values are typically coerced to an 8-bit unsigned value (so "return -1" ends up as 255). Technically negative returns have the same problem (e.g., "-256" wraps back to 0), but this is not a problem in practice, as the only negative value we use is "-1". Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 282616c commit e34f802

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Documentation/git-merge-file.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ lines from `<other-file>`, or lines from both respectively. The length of the
4141
conflict markers can be given with the `--marker-size` option.
4242

4343
The exit value of this program is negative on error, and the number of
44-
conflicts otherwise. If the merge was clean, the exit value is 0.
44+
conflicts otherwise (truncated to 127 if there are more than that many
45+
conflicts). If the merge was clean, the exit value is 0.
4546

4647
'git merge-file' is designed to be a minimal clone of RCS 'merge'; that is, it
4748
implements all of RCS 'merge''s functionality which is needed by

builtin/merge-file.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
102102
free(result.ptr);
103103
}
104104

105+
if (ret > 127)
106+
ret = 127;
107+
105108
return ret;
106109
}

t/t7600-merge.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,37 @@ test_expect_success GPG 'merge --no-edit tag should skip editor' '
692692
test_cmp actual expect
693693
'
694694

695+
test_expect_success 'set up mod-256 conflict scenario' '
696+
# 256 near-identical stanzas...
697+
for i in $(test_seq 1 256); do
698+
for j in 1 2 3 4 5; do
699+
echo $i-$j
700+
done
701+
done >file &&
702+
git add file &&
703+
git commit -m base &&
704+
705+
# one side changes the first line of each to "master"
706+
sed s/-1/-master/ <file >tmp &&
707+
mv tmp file &&
708+
git commit -am master &&
709+
710+
# and the other to "side"; merging the two will
711+
# yield 256 separate conflicts
712+
git checkout -b side HEAD^ &&
713+
sed s/-1/-side/ <file >tmp &&
714+
mv tmp file &&
715+
git commit -am side
716+
'
717+
718+
test_expect_success 'merge detects mod-256 conflicts (recursive)' '
719+
git reset --hard &&
720+
test_must_fail git merge -s recursive master
721+
'
722+
723+
test_expect_success 'merge detects mod-256 conflicts (resolve)' '
724+
git reset --hard &&
725+
test_must_fail git merge -s resolve master
726+
'
727+
695728
test_done

0 commit comments

Comments
 (0)