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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 28, 2015 at 18:44 UTC e34f80278e920e53b69016c7cecb24e4621e4564
3 files changed +38 -1
Documentation/git-merge-file.txt
+2 -1
@@ -41,7 +41,8 @@ lines from `<other-file>`, or lines from both respectively. The length of the
41 conflict markers can be given with the `--marker-size` option.
42
43 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.
46
47 'git merge-file' is designed to be a minimal clone of RCS 'merge'; that is, it
48 implements all of RCS 'merge''s functionality which is needed by
builtin/merge-file.c
+3
@@ -102,5 +102,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
102 free(result.ptr);
103 }
104
105 + if (ret > 127)
106 + ret = 127;
107 +
108 return ret;
109 }
t/t7600-merge.sh
+33
@@ -692,4 +692,37 @@ test_expect_success GPG 'merge --no-edit tag should skip editor' '
692 test_cmp actual expect
693 '
694
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 +
728 test_done