Skip to content

Commit ba0254c

Browse files
committed
Merge branch 'tr/maint-merge-file-subdir'
* tr/maint-merge-file-subdir: merge-file: correctly find files when called in subdir prefix_filename(): safely handle the case where pfx_len=0
2 parents e2110c8 + 55846b9 commit ba0254c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

builtin/merge-file.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
2828
xmparam_t xmp = {{0}};
2929
int ret = 0, i = 0, to_stdout = 0;
3030
int quiet = 0;
31+
int prefixlen = 0;
3132
struct option options[] = {
3233
OPT_BOOLEAN('p', "stdout", &to_stdout, "send results to standard output"),
3334
OPT_SET_INT(0, "diff3", &xmp.style, "use a diff3 based merge", XDL_MERGE_DIFF3),
@@ -65,10 +66,14 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
6566
"%s\n", strerror(errno));
6667
}
6768

69+
if (prefix)
70+
prefixlen = strlen(prefix);
71+
6872
for (i = 0; i < 3; i++) {
73+
const char *fname = prefix_filename(prefix, prefixlen, argv[i]);
6974
if (!names[i])
7075
names[i] = argv[i];
71-
if (read_mmfile(mmfs + i, argv[i]))
76+
if (read_mmfile(mmfs + i, fname))
7277
return -1;
7378
if (buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
7479
return error("Cannot merge binary files: %s\n",

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
4646
{
4747
static char path[PATH_MAX];
4848
#ifndef WIN32
49-
if (!pfx || !*pfx || is_absolute_path(arg))
49+
if (!pfx_len || is_absolute_path(arg))
5050
return arg;
5151
memcpy(path, pfx, pfx_len);
5252
strcpy(path + pfx_len, arg);
@@ -55,7 +55,7 @@ const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
5555
/* don't add prefix to absolute paths, but still replace '\' by '/' */
5656
if (is_absolute_path(arg))
5757
pfx_len = 0;
58-
else
58+
else if (pfx_len)
5959
memcpy(path, pfx, pfx_len);
6060
strcpy(path + pfx_len, arg);
6161
for (p = path + pfx_len; *p; p++)

t/t6023-merge-file.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ cp new1.txt test.txt
6464
test_expect_success "merge without conflict" \
6565
"git merge-file test.txt orig.txt new2.txt"
6666

67+
test_expect_success 'works in subdirectory' '
68+
mkdir dir &&
69+
cp new1.txt dir/a.txt &&
70+
cp orig.txt dir/o.txt &&
71+
cp new2.txt dir/b.txt &&
72+
( cd dir && git merge-file a.txt o.txt b.txt )
73+
'
74+
6775
cp new1.txt test.txt
6876
test_expect_success "merge without conflict (--quiet)" \
6977
"git merge-file --quiet test.txt orig.txt new2.txt"

0 commit comments

Comments
 (0)