Skip to content

Commit 81effe9

Browse files
wetnebgitster
authored andcommitted
merge-ll: expose revision names to custom drivers
Custom merge drivers need access to the names of the revisions they are working on, so that the merge conflict markers they introduce can refer to those revisions. The placeholders '%S', '%X' and '%Y' are introduced to this end. Signed-off-by: Antonin Delpeuch <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 186b115 commit 81effe9

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

Documentation/gitattributes.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,11 +1137,11 @@ The `merge.*.name` variable gives the driver a human-readable
11371137
name.
11381138

11391139
The `merge.*.driver` variable's value is used to construct a
1140-
command to run to merge ancestor's version (`%O`), current
1140+
command to run to common ancestor's version (`%O`), current
11411141
version (`%A`) and the other branches' version (`%B`). These
11421142
three tokens are replaced with the names of temporary files that
11431143
hold the contents of these versions when the command line is
1144-
built. Additionally, %L will be replaced with the conflict marker
1144+
built. Additionally, `%L` will be replaced with the conflict marker
11451145
size (see below).
11461146

11471147
The merge driver is expected to leave the result of the merge in
@@ -1159,8 +1159,9 @@ When left unspecified, the driver itself is used for both
11591159
internal merge and the final merge.
11601160

11611161
The merge driver can learn the pathname in which the merged result
1162-
will be stored via placeholder `%P`.
1163-
1162+
will be stored via placeholder `%P`. The conflict labels to be used
1163+
for the common ancestor, local head and other head can be passed by
1164+
using '%S', '%X' and '%Y` respectively.
11641165

11651166
`conflict-marker-size`
11661167
^^^^^^^^^^^^^^^^^^^^^^

merge-ll.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ static void create_temp(mmfile_t *src, char *path, size_t len)
185185
static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
186186
mmbuffer_t *result,
187187
const char *path,
188-
mmfile_t *orig, const char *orig_name UNUSED,
189-
mmfile_t *src1, const char *name1 UNUSED,
190-
mmfile_t *src2, const char *name2 UNUSED,
188+
mmfile_t *orig, const char *orig_name,
189+
mmfile_t *src1, const char *name1,
190+
mmfile_t *src2, const char *name2,
191191
const struct ll_merge_options *opts,
192192
int marker_size)
193193
{
@@ -222,6 +222,12 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
222222
strbuf_addf(&cmd, "%d", marker_size);
223223
else if (skip_prefix(format, "P", &format))
224224
sq_quote_buf(&cmd, path);
225+
else if (skip_prefix(format, "S", &format))
226+
sq_quote_buf(&cmd, orig_name ? orig_name : "");
227+
else if (skip_prefix(format, "X", &format))
228+
sq_quote_buf(&cmd, name1 ? name1 : "");
229+
else if (skip_prefix(format, "Y", &format))
230+
sq_quote_buf(&cmd, name2 ? name2 : "");
225231
else
226232
strbuf_addch(&cmd, '%');
227233
}
@@ -315,7 +321,12 @@ static int read_merge_config(const char *var, const char *value,
315321
* %B - temporary file name for the other branches' version.
316322
* %L - conflict marker length
317323
* %P - the original path (safely quoted for the shell)
324+
* %S - the revision for the merge base
325+
* %X - the revision for our version
326+
* %Y - the revision for their version
318327
*
328+
* If the file is not named indentically in all versions, then each
329+
* revision is joined with the corresponding path, separated by a colon.
319330
* The external merge driver should write the results in the
320331
* file named by %A, and signal that it has done with zero exit
321332
* status.

t/t6406-merge-attr.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ test_expect_success setup '
4242
#!/bin/sh
4343
4444
orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
45+
orig_name="$6" our_name="$7" their_name="$8"
4546
(
4647
echo "orig is $orig"
4748
echo "ours is $ours"
4849
echo "theirs is $theirs"
4950
echo "path is $path"
51+
echo "orig_name is $orig_name"
52+
echo "our_name is $our_name"
53+
echo "their_name is $their_name"
5054
echo "=== orig ==="
5155
cat "$orig"
5256
echo "=== ours ==="
@@ -121,7 +125,7 @@ test_expect_success 'custom merge backend' '
121125
122126
git reset --hard anchor &&
123127
git config --replace-all \
124-
merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
128+
merge.custom.driver "./custom-merge %O %A %B 0 %P %S %X %Y" &&
125129
git config --replace-all \
126130
merge.custom.name "custom merge driver for testing" &&
127131
@@ -132,7 +136,8 @@ test_expect_success 'custom merge backend' '
132136
o=$(git unpack-file main^:text) &&
133137
a=$(git unpack-file side^:text) &&
134138
b=$(git unpack-file main:text) &&
135-
sh -c "./custom-merge $o $a $b 0 text" &&
139+
base_revid=$(git rev-parse --short main^) &&
140+
sh -c "./custom-merge $o $a $b 0 text $base_revid HEAD main" &&
136141
sed -e 1,3d $a >check-2 &&
137142
cmp check-1 check-2 &&
138143
rm -f $o $a $b
@@ -142,7 +147,7 @@ test_expect_success 'custom merge backend' '
142147
143148
git reset --hard anchor &&
144149
git config --replace-all \
145-
merge.custom.driver "./custom-merge %O %A %B 1 %P" &&
150+
merge.custom.driver "./custom-merge %O %A %B 1 %P %S %X %Y" &&
146151
git config --replace-all \
147152
merge.custom.name "custom merge driver for testing" &&
148153
@@ -159,7 +164,8 @@ test_expect_success 'custom merge backend' '
159164
o=$(git unpack-file main^:text) &&
160165
a=$(git unpack-file anchor:text) &&
161166
b=$(git unpack-file main:text) &&
162-
sh -c "./custom-merge $o $a $b 0 text" &&
167+
base_revid=$(git rev-parse --short main^) &&
168+
sh -c "./custom-merge $o $a $b 0 text $base_revid HEAD main" &&
163169
sed -e 1,3d $a >check-2 &&
164170
cmp check-1 check-2 &&
165171
sed -e 1,3d -e 4q $a >check-3 &&
@@ -173,7 +179,7 @@ test_expect_success !WINDOWS 'custom merge driver that is killed with a signal'
173179
174180
git reset --hard anchor &&
175181
git config --replace-all \
176-
merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
182+
merge.custom.driver "./custom-merge %O %A %B 0 %P %S %X %Y" &&
177183
git config --replace-all \
178184
merge.custom.name "custom merge driver for testing" &&
179185

0 commit comments

Comments
 (0)