Skip to content

Commit ac13e7c

Browse files
committed
Merge branch 'jk/merge-one-file-working-tree'
* jk/merge-one-file-working-tree: merge-one-file: fix broken merges with alternate work trees add tests for merge-index / merge-one-file
2 parents 22dbeee + 6aaeca9 commit ac13e7c

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

git-merge-one-file.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ LONG_USAGE="Usage: git merge-one-file $USAGE
2222
2323
Blob ids and modes should be empty for missing files."
2424

25+
SUBDIRECTORY_OK=Yes
26+
. git-sh-setup
27+
cd_to_toplevel
28+
require_work_tree
29+
2530
if ! test "$#" -eq 7
2631
then
2732
echo "$LONG_USAGE"
@@ -132,7 +137,7 @@ case "${1:-.}${2:-.}${3:-.}" in
132137

133138
# Create the working tree file, using "our tree" version from the
134139
# index, and then store the result of the merge.
135-
git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4"
140+
git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" || exit 1
136141
rm -f -- "$orig" "$src1" "$src2"
137142

138143
if [ "$6" != "$7" ]; then

t/t6060-merge-index.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/sh
2+
3+
test_description='basic git merge-index / git-merge-one-file tests'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'setup diverging branches' '
7+
for i in 1 2 3 4 5 6 7 8 9 10; do
8+
echo $i
9+
done >file &&
10+
git add file &&
11+
git commit -m base &&
12+
git tag base &&
13+
sed s/2/two/ <file >tmp &&
14+
mv tmp file &&
15+
git commit -a -m two &&
16+
git tag two &&
17+
git checkout -b other HEAD^ &&
18+
sed s/10/ten/ <file >tmp &&
19+
mv tmp file &&
20+
git commit -a -m ten &&
21+
git tag ten
22+
'
23+
24+
cat >expect-merged <<'EOF'
25+
1
26+
two
27+
3
28+
4
29+
5
30+
6
31+
7
32+
8
33+
9
34+
ten
35+
EOF
36+
37+
test_expect_success 'read-tree does not resolve content merge' '
38+
git read-tree -i -m base ten two &&
39+
echo file >expect &&
40+
git diff-files --name-only --diff-filter=U >unmerged &&
41+
test_cmp expect unmerged
42+
'
43+
44+
test_expect_success 'git merge-index git-merge-one-file resolves' '
45+
git merge-index git-merge-one-file -a &&
46+
git diff-files --name-only --diff-filter=U >unmerged &&
47+
>expect &&
48+
test_cmp expect unmerged &&
49+
test_cmp expect-merged file &&
50+
git cat-file blob :file >file-index &&
51+
test_cmp expect-merged file-index
52+
'
53+
54+
test_expect_success 'setup bare merge' '
55+
git clone --bare . bare.git &&
56+
(cd bare.git &&
57+
GIT_INDEX_FILE=$PWD/merge.index &&
58+
export GIT_INDEX_FILE &&
59+
git read-tree -i -m base ten two
60+
)
61+
'
62+
63+
test_expect_success 'merge-one-file fails without a work tree' '
64+
(cd bare.git &&
65+
GIT_INDEX_FILE=$PWD/merge.index &&
66+
export GIT_INDEX_FILE &&
67+
test_must_fail git merge-index git-merge-one-file -a
68+
)
69+
'
70+
71+
test_expect_success 'merge-one-file respects GIT_WORK_TREE' '
72+
(cd bare.git &&
73+
mkdir work &&
74+
GIT_WORK_TREE=$PWD/work &&
75+
export GIT_WORK_TREE &&
76+
GIT_INDEX_FILE=$PWD/merge.index &&
77+
export GIT_INDEX_FILE &&
78+
git merge-index git-merge-one-file -a &&
79+
git cat-file blob :file >work/file-index
80+
) &&
81+
test_cmp expect-merged bare.git/work/file &&
82+
test_cmp expect-merged bare.git/work/file-index
83+
'
84+
85+
test_expect_success 'merge-one-file respects core.worktree' '
86+
mkdir subdir &&
87+
git clone . subdir/child &&
88+
(cd subdir &&
89+
GIT_DIR=$PWD/child/.git &&
90+
export GIT_DIR &&
91+
git config core.worktree "$PWD/child" &&
92+
git read-tree -i -m base ten two &&
93+
git merge-index git-merge-one-file -a &&
94+
git cat-file blob :file >file-index
95+
) &&
96+
test_cmp expect-merged subdir/child/file &&
97+
test_cmp expect-merged subdir/file-index
98+
'
99+
100+
test_done

0 commit comments

Comments
 (0)