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

[PATCH 1/3] apply: use the right attribute for paths in non-Git patches

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 6, 2014, 22:58 UTC
Message-ID
<1407365885-1628-2-git-send-email-gitster@pobox.com>
In-Reply-To
<1407365885-1628-1-git-send-email-gitster@pobox.com>

We parse each patchfile and find to what path it applies to and then use that name to consult the attribute system to find what whitespace rules to be applied, and also which target file (either in the working tree or in the index) to replay the changes the patch represents.

A non-Git patch is taken as relative to the current working directory, and the prefix_patches() helper function introduced in 56185f49 (git-apply: require -p<n> when working in a subdirectory., 2007-02-19) is used to prepend the prefix to these names found in the patch input.

However, this prepending of the prefix to the pathnames is done after the patch is fully parsed and affects only what target files are patched. Because the attributes are checked against the names found in the patch during the parsing, not against the final path, whitespace checks that are done during parsing end up using attributes for a wrong path.

Because a Git-generated patch records the full path to the target with -p$n prefix (e.g. a/ and b/) and we apply it relative to the top of the working tree, prefix_patches() is a no-op and this problem does not manifest for them.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/apply.c         | 41 +++++++++++++++++++----------------------
 t/t4119-apply-config.sh | 17 +++++++++++++++++
 2 files changed, 36 insertions(+), 22 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 6013e19..4270cde 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1920,6 +1920,23 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 	return used;
 }
 
+static void prefix_one(char **name)
+{
+	char *old_name = *name;
+	if (!old_name)
+		return;
+	*name = xstrdup(prefix_filename(prefix, prefix_length, *name));
+	free(old_name);
+}
+
+static void prefix_patch(struct patch *p)
+{
+	if (!prefix || p->is_toplevel_relative)
+		return;
+	prefix_one(&p->new_name);
+	prefix_one(&p->old_name);
+}
+
 /*
  * Read the patch text in "buffer" that extends for "size" bytes; stop
  * reading after seeing a single patch (i.e. changes to a single file).
@@ -1935,6 +1952,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 	if (offset < 0)
 		return offset;
 
+	prefix_patch(patch);
+
 	patch->ws_rule = whitespace_rule(patch->new_name
 					 ? patch->new_name
 					 : patch->old_name);
@@ -4164,26 +4183,6 @@ static int use_patch(struct patch *p)
 	return !has_include;
 }
 
-
-static void prefix_one(char **name)
-{
-	char *old_name = *name;
-	if (!old_name)
-		return;
-	*name = xstrdup(prefix_filename(prefix, prefix_length, *name));
-	free(old_name);
-}
-
-static void prefix_patches(struct patch *p)
-{
-	if (!prefix || p->is_toplevel_relative)
-		return;
-	for ( ; p; p = p->next) {
-		prefix_one(&p->new_name);
-		prefix_one(&p->old_name);
-	}
-}
-
 #define INACCURATE_EOF	(1<<0)
 #define RECOUNT		(1<<1)
 
@@ -4209,8 +4208,6 @@ static int apply_patch(int fd, const char *filename, int options)
 			break;
 		if (apply_in_reverse)
 			reverse_patches(patch);
-		if (prefix)
-			prefix_patches(patch);
 		if (use_patch(patch)) {
 			patch_stats(patch);
 			*listp = patch;
diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh
index 3d0384d..be325fa 100755
--- a/t/t4119-apply-config.sh
+++ b/t/t4119-apply-config.sh
@@ -159,4 +159,21 @@ test_expect_success 'same but with traditional patch input of depth 2' '
 	check_result sub/file1
 '
 
+test_expect_success 'in subdir with traditional patch input' '
+	cd "$D" &&
+	git config apply.whitespace strip &&
+	cat >.gitattributes <<-EOF &&
+	/* whitespace=blank-at-eol
+	sub/* whitespace=-blank-at-eol
+	EOF
+	rm -f sub/file1 &&
+	cp saved sub/file1 &&
+	git update-index --refresh &&
+
+	cd sub &&
+	git apply ../gpatch.file &&
+	echo "B " >expect &&
+	test_cmp expect file1
+'
+
 test_done
-- 
2.1.0-rc1-209-g4e1b551
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 6 in “Bug v1.9.3: Spurious whitespace warnings when using git apply with the --exclude option”
  1. Peyton RandolphAug 6, 2014
  2. Junio C HamanoAug 6, 2014
  3. 0/3 Two fixes to "git apply"Junio C Hamano, Aug 6, 2014
  4. 1/3 apply: use the right attribute for paths in non-Git patchesJunio C Hamano, Aug 6, 2014
  5. 2/3 apply: hoist use_patch() helper for path exclusion upJunio C Hamano, Aug 6, 2014
  6. 3/3 apply: omit ws check for excluded pathsJunio C Hamano, Aug 6, 2014

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.