mergetool: respect autocrlf by using checkout-index
Previously, git mergetool used cat-file which does not perform git to worktree conversion. This changes mergetool to use git checkout-index instead which means that the temporary files used for mergetool use the correct line endings for the platform. Signed-off-by: Charles Bailey <charles@hashpling.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Charles Bailey committed
Jan 21, 2009 at 22:57 UTC
0ec7b6c26dad51f690cd985d4a5357634123c4b7
2 files changed
+24
-5
git-mergetool.sh
+11
-3
@@ -127,6 +127,14 @@ check_unchanged () {
127
fi
128
}
129
130
+checkout_staged_file () {
131
+ tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
132
+
133
+ if test $? -eq 0 -a -n "$tmpfile" ; then
134
+ mv -- "$tmpfile" "$3"
135
+ fi
136
+}
137
+
138
merge_file () {
139
MERGED="$1"
140
@@ -153,9 +161,9 @@ merge_file () {
161
local_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
162
remote_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
163
156
- base_present && git cat-file blob ":1:$prefix$MERGED" >"$BASE" 2>/dev/null
157
- local_present && git cat-file blob ":2:$prefix$MERGED" >"$LOCAL" 2>/dev/null
158
- remote_present && git cat-file blob ":3:$prefix$MERGED" >"$REMOTE" 2>/dev/null
164
+ base_present && checkout_staged_file 1 "$prefix$MERGED" "$BASE"
165
+ local_present && checkout_staged_file 2 "$prefix$MERGED" "$LOCAL"
166
+ remote_present && checkout_staged_file 3 "$prefix$MERGED" "$REMOTE"
167
168
if test -z "$local_mode" -o -z "$remote_mode"; then
169
echo "Deleted merge conflict for '$MERGED':"
t/t7610-mergetool.sh
+13
-2
@@ -34,13 +34,24 @@ test_expect_success 'custom mergetool' '
34
git config merge.tool mytool &&
35
git config mergetool.mytool.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
36
git config mergetool.mytool.trustExitCode true &&
37
- git checkout branch1 &&
37
+ git checkout branch1 &&
38
test_must_fail git merge master >/dev/null 2>&1 &&
39
( yes "" | git mergetool file1>/dev/null 2>&1 ) &&
40
( yes "" | git mergetool file2>/dev/null 2>&1 ) &&
41
test "$(cat file1)" = "master updated" &&
42
test "$(cat file2)" = "master new" &&
43
- git commit -m "branch1 resolved with mergetool"
43
+ git commit -m "branch1 resolved with mergetool"
44
+'
45
+
46
+test_expect_success 'mergetool crlf' '
47
+ git config core.autocrlf true &&
48
+ git reset --hard HEAD^
49
+ test_must_fail git merge master >/dev/null 2>&1 &&
50
+ ( yes "" | git mergetool file1>/dev/null 2>&1 ) &&
51
+ ( yes "" | git mergetool file2>/dev/null 2>&1 ) &&
52
+ test "$(printf x | cat file1 -)" = "$(printf "master updated\r\nx")" &&
53
+ test "$(printf x | cat file2 -)" = "$(printf "master new\r\nx")" &&
54
+ git commit -m "branch1 resolved with mergetool - autocrlf"
55
'
56
57
test_done