Skip to content

Commit 183f880

Browse files
committed
Merge branch 'kb/p4merge'
Adjust the order mergetools feeds the files to the p4merge backend to match the p4 convention. * kb/p4merge: merge-one-file: force content conflict for "both sides added" case git-merge-one-file: send "ERROR:" messages to stderr git-merge-one-file: style cleanup merge-one-file: remove stale comment mergetools/p4merge: create a base if none available mergetools/p4merge: swap LOCAL and REMOTE
2 parents 7f95f2d + 4fa5c05 commit 183f880

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

Documentation/git-sh-setup.txt

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ get_author_ident_from_commit::
8282
outputs code for use with eval to set the GIT_AUTHOR_NAME,
8383
GIT_AUTHOR_EMAIL and GIT_AUTHOR_DATE variables for a given commit.
8484

85+
create_virtual_base::
86+
modifies the first file so only lines in common with the
87+
second file remain. If there is insufficient common material,
88+
then the first file is left empty. The result is suitable
89+
as a virtual base input for a 3-way merge.
90+
8591
GIT
8692
---
8793
Part of the linkgit:git[1] suite

git-merge-one-file.sh

+28-33
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ SUBDIRECTORY_OK=Yes
2727
cd_to_toplevel
2828
require_work_tree
2929

30-
if ! test "$#" -eq 7
30+
if test $# != 7
3131
then
3232
echo "$LONG_USAGE"
3333
exit 1
@@ -38,7 +38,8 @@ case "${1:-.}${2:-.}${3:-.}" in
3838
# Deleted in both or deleted in one and unchanged in the other
3939
#
4040
"$1.." | "$1.$1" | "$1$1.")
41-
if [ "$2" ]; then
41+
if test -n "$2"
42+
then
4243
echo "Removing $4"
4344
else
4445
# read-tree checked that index matches HEAD already,
@@ -48,7 +49,8 @@ case "${1:-.}${2:-.}${3:-.}" in
4849
# we do not have it in the index, though.
4950
exec git update-index --remove -- "$4"
5051
fi
51-
if test -f "$4"; then
52+
if test -f "$4"
53+
then
5254
rm -f -- "$4" &&
5355
rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
5456
fi &&
@@ -67,7 +69,7 @@ case "${1:-.}${2:-.}${3:-.}" in
6769
echo "Adding $4"
6870
if test -f "$4"
6971
then
70-
echo "ERROR: untracked $4 is overwritten by the merge."
72+
echo "ERROR: untracked $4 is overwritten by the merge." >&2
7173
exit 1
7274
fi
7375
git update-index --add --cacheinfo "$7" "$3" "$4" &&
@@ -78,9 +80,10 @@ case "${1:-.}${2:-.}${3:-.}" in
7880
# Added in both, identically (check for same permissions).
7981
#
8082
".$3$2")
81-
if [ "$6" != "$7" ]; then
82-
echo "ERROR: File $4 added identically in both branches,"
83-
echo "ERROR: but permissions conflict $6->$7."
83+
if test "$6" != "$7"
84+
then
85+
echo "ERROR: File $4 added identically in both branches," >&2
86+
echo "ERROR: but permissions conflict $6->$7." >&2
8487
exit 1
8588
fi
8689
echo "Adding $4"
@@ -95,71 +98,63 @@ case "${1:-.}${2:-.}${3:-.}" in
9598

9699
case ",$6,$7," in
97100
*,120000,*)
98-
echo "ERROR: $4: Not merging symbolic link changes."
101+
echo "ERROR: $4: Not merging symbolic link changes." >&2
99102
exit 1
100103
;;
101104
*,160000,*)
102-
echo "ERROR: $4: Not merging conflicting submodule changes."
105+
echo "ERROR: $4: Not merging conflicting submodule changes." >&2
103106
exit 1
104107
;;
105108
esac
106109

107-
src2=`git-unpack-file $3`
110+
src1=$(git-unpack-file $2)
111+
src2=$(git-unpack-file $3)
108112
case "$1" in
109113
'')
110114
echo "Added $4 in both, but differently."
111-
# This extracts OUR file in $orig, and uses git apply to
112-
# remove lines that are unique to ours.
113-
orig=`git-unpack-file $2`
114-
sz0=`wc -c <"$orig"`
115-
@@DIFF@@ -u -La/$orig -Lb/$orig $orig $src2 | git apply --no-add
116-
sz1=`wc -c <"$orig"`
117-
118-
# If we do not have enough common material, it is not
119-
# worth trying two-file merge using common subsections.
120-
expr $sz0 \< $sz1 \* 2 >/dev/null || : >$orig
115+
orig=$(git-unpack-file $2)
116+
create_virtual_base "$orig" "$src2"
121117
;;
122118
*)
123119
echo "Auto-merging $4"
124-
orig=`git-unpack-file $1`
120+
orig=$(git-unpack-file $1)
125121
;;
126122
esac
127123

128-
# Be careful for funny filename such as "-L" in "$4", which
129-
# would confuse "merge" greatly.
130-
src1=`git-unpack-file $2`
131124
git merge-file "$src1" "$orig" "$src2"
132125
ret=$?
133126
msg=
134-
if [ $ret -ne 0 ]; then
127+
if test $ret != 0 || test -z "$1"
128+
then
135129
msg='content conflict'
130+
ret=1
136131
fi
137132

138133
# Create the working tree file, using "our tree" version from the
139134
# index, and then store the result of the merge.
140135
git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" || exit 1
141136
rm -f -- "$orig" "$src1" "$src2"
142137

143-
if [ "$6" != "$7" ]; then
144-
if [ -n "$msg" ]; then
138+
if test "$6" != "$7"
139+
then
140+
if test -n "$msg"
141+
then
145142
msg="$msg, "
146143
fi
147144
msg="${msg}permissions conflict: $5->$6,$7"
148145
ret=1
149146
fi
150-
if [ "$1" = '' ]; then
151-
ret=1
152-
fi
153147

154-
if [ $ret -ne 0 ]; then
155-
echo "ERROR: $msg in $4"
148+
if test $ret != 0
149+
then
150+
echo "ERROR: $msg in $4" >&2
156151
exit 1
157152
fi
158153
exec git update-index -- "$4"
159154
;;
160155

161156
*)
162-
echo "ERROR: $4: Not handling case $1 -> $2 -> $3"
157+
echo "ERROR: $4: Not handling case $1 -> $2 -> $3" >&2
163158
;;
164159
esac
165160
exit 1

git-sh-setup.sh

+12
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ clear_local_git_env() {
249249
unset $(git rev-parse --local-env-vars)
250250
}
251251
252+
# Generate a virtual base file for a two-file merge. Uses git apply to
253+
# remove lines from $1 that are not in $2, leaving only common lines.
254+
create_virtual_base() {
255+
sz0=$(wc -c <"$1")
256+
@@DIFF@@ -u -La/"$1" -Lb/"$1" "$1" "$2" | git apply --no-add
257+
sz1=$(wc -c <"$1")
258+
259+
# If we do not have enough common material, it is not
260+
# worth trying two-file merge using common subsections.
261+
expr $sz0 \< $sz1 \* 2 >/dev/null || : >"$1"
262+
}
263+
252264
253265
# Platform specific tweaks to work around some commands
254266
case $(uname -s) in

mergetools/p4merge

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ diff_cmd () {
2121

2222
merge_cmd () {
2323
touch "$BACKUP"
24-
$base_present || >"$BASE"
25-
"$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
24+
if ! $base_present
25+
then
26+
cp -- "$LOCAL" "$BASE"
27+
create_virtual_base "$BASE" "$REMOTE"
28+
fi
29+
"$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"
2630
check_unchanged
2731
}
2832

0 commit comments

Comments
 (0)