git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] merge-file: correctly find files when called in subdir

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Oct 17, 2010, 01:30 UTC
Message-ID
<20101017013005.GB26656@burratino>
In-Reply-To
<33ab2f03ed522e1a9be202017b7bbfe35e6d7a99.1287228637.git.trast@student.ethz.ch>
Thomas Rast wrote:
Show 13 quoted lines
> --- a/builtin/merge-file.c
> +++ b/builtin/merge-file.c
> @@ -65,10 +66,18 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
>  				     "%s\n", strerror(errno));
>  	}
>  
> +	if (prefix)
> +		prefixlen = strlen(prefix);
> +
>  	for (i = 0; i < 3; i++) {
> +		const char *name;
> +		if (prefix)
> +			name = prefix_filename(prefix, prefixlen, argv[i]);
Provokes a warning (silly gcc) which can be suppressed with
	int prefixlen = prefixlen;
(or = 0 to fit with the surrounding style).
Show 12 quoted lines
> --- a/t/t6023-merge-file.sh
> +++ b/t/t6023-merge-file.sh
> @@ -64,6 +64,14 @@ cp new1.txt test.txt
>  test_expect_success "merge without conflict" \
>  	"git merge-file test.txt orig.txt new2.txt"
>  
> +test_expect_success 'works in subdirectory' '
> +	mkdir dir &&
> +	cp new1.txt dir/a.txt &&
> +	cp orig.txt dir/o.txt &&
> +	cp new2.txt dir/b.txt &&
> +	( cd dir && git merge-file a.txt o.txt b.txt )
Thanks!  Here's a backport to maint-1.6.1 for kicks.
-- 8< --
From: Thomas Rast <trast@student.ethz.ch>
Subject: merge-file: correctly find files when called in subdir

Since b541248 (merge.conflictstyle: choose between "merge" and "diff3 -m" styles, 2008-08-29), git-merge-file uses setup_directory_gently(), thus cd'ing around to find any possible config files to use.

This broke merge-file when it is called from within a subdirectory of a repository, and the arguments are all relative paths.

Fix by prepending the prefix, as passed down from the main git executable, if there is any.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin-merge-file.c  |   11 ++++++++++-
 t/t6023-merge-file.sh |    8 ++++++++
 2 files changed, 18 insertions(+), 1 deletions(-)
diff --git a/builtin-merge-file.c b/builtin-merge-file.c
index 96edb97..848e3de 100644
--- a/builtin-merge-file.c
+++ b/builtin-merge-file.c
@@ -27,6 +27,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 	mmbuffer_t result = {NULL, 0};
 	xpparam_t xpp = {XDF_NEED_MINIMAL};
 	int ret = 0, i = 0, to_stdout = 0;
+	int prefixlen = 0;
 	int merge_level = XDL_MERGE_ZEALOUS_ALNUM;
 	int merge_style = 0, quiet = 0;
 	int nongit;
@@ -57,10 +58,18 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 				     "%s\n", strerror(errno));
 	}
 
+	if (prefix)
+		prefixlen = strlen(prefix);
+
 	for (i = 0; i < 3; i++) {
+		const char *name;
+		if (prefix)
+			name = prefix_filename(prefix, prefixlen, argv[i]);
+		else
+			name = argv[i];
 		if (!names[i])
 			names[i] = argv[i];
-		if (read_mmfile(mmfs + i, argv[i]))
+		if (read_mmfile(mmfs + i, name))
 			return -1;
 		if (buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
 			return error("Cannot merge binary files: %s\n",
diff --git a/t/t6023-merge-file.sh b/t/t6023-merge-file.sh
index f8942bc..f09430a 100755
--- a/t/t6023-merge-file.sh
+++ b/t/t6023-merge-file.sh
@@ -58,6 +58,14 @@ cp new1.txt test.txt
 test_expect_success "merge without conflict" \
 	"git merge-file test.txt orig.txt new2.txt"
 
+test_expect_success 'works in subdirectory' '
+	mkdir dir &&
+	cp new1.txt dir/a.txt &&
+	cp orig.txt dir/o.txt &&
+	cp new2.txt dir/b.txt &&
+	( cd dir && git merge-file a.txt o.txt b.txt )
+'
+
 cp new1.txt test2.txt
 test_expect_success "merge without conflict (missing LF at EOF)" \
 	"git merge-file test2.txt orig.txt new2.txt"
-- 
1.7.2.3
Previous: Johannes Sixt
Message 10 of 10 in “merge-file: correctly find files when called in subdir”
  1. merge-file: correctly find files when called in subdirThomas Rast, Oct 16, 2010
  2. Bert WesargOct 16, 2010
  3. Jonathan NiederOct 17, 2010
  4. Thomas RastOct 17, 2010
  5. Jonathan NiederOct 17, 2010
  6. 1/2 prefix_filename(): safely handle the case where pfx_len=0Thomas Rast, Oct 17, 2010
  7. 2/2 merge-file: correctly find files when called in subdirThomas Rast, Oct 17, 2010
  8. Jonathan NiederOct 17, 2010
  9. Johannes SixtOct 18, 2010
  10. Jonathan NiederOct 17, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.