@matheusbsbs / matheus-bagliano / commits / 0afa7644f2
Fork 0

Fix overwriting of files when applying contextually independent diffs

Noticed by applying two diffs of different contexts to the same file. The check for existence of a file was wrong: the test assumed it was a directory and reset the errno (twice: directly and by calling lstat). So if an entry existed and was _not_ a directory no attempt was made to rename into it, because the errno (expected by renaming code) was already reset to 0. This resulted in error: fatal: unable to write file file mode 100644 For Linux, removing "errno = 0" is enough, as lstat wont modify errno if it was successful. The behavior should not be depended upon, though, so modify the "if" as well. The test simulates this situation. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Alex Riesen committed Apr 18, 2007 at 23:58 UTC 0afa7644f2f3543a033d327468ab97d7581f9d13
2 files changed +34 -2
builtin-apply.c
+1 -2
@@ -2416,8 +2416,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
2416 * used to be.
2417 */
2418 struct stat st;
2419 - errno = 0;
2420 - if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path))
2419 + if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
2420 errno = EEXIST;
2421 }
2422
t/t4121-apply-diffs.sh new
+33
@@ -0,0 +1,33 @@
1 +#!/bin/sh
2 +
3 +test_description='git-apply for contextually independent diffs'
4 +. ./test-lib.sh
5 +
6 +echo '1
7 +2
8 +3
9 +4
10 +5
11 +6
12 +7
13 +8' >file
14 +
15 +test_expect_success 'setup' \
16 + 'git add file &&
17 + git commit -q -m 1 &&
18 + git checkout -b test &&
19 + mv file file.tmp &&
20 + echo 0 >file &&
21 + cat file.tmp >>file &&
22 + rm file.tmp &&
23 + git commit -a -q -m 2 &&
24 + echo 9 >>file &&
25 + git commit -a -q -m 3 &&
26 + git checkout master'
27 +
28 +test_expect_success \
29 + 'check if contextually independent diffs for the same file apply' \
30 + '( git diff test~2 test~1; git diff test~1 test~0 )| git apply'
31 +
32 +test_done
33 +