git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] apply: handle filenames with double slashes better
@ 2009-05-21 12:25 Michal Marek
  2009-05-21 14:56 ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Michal Marek @ 2009-05-21 12:25 UTC (permalink / raw)
  To: git; +Cc: Michal Marek

Collapse double slashes to make patches like this work with --index or
--cached:

git apply --index <<-EOF
	--- a/perl//Git.pm
	+++ b/perl//Git.pm
	@@ -1358,3 +1358,4 @@


	 1; # Famous last words
	+# test
EOF

Signed-off-by: Michal Marek <mmarek@suse.cz>
---
 builtin-apply.c |   26 ++++++++++++++++++++------
 1 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/builtin-apply.c b/builtin-apply.c
index 8a3771e..0c623e4 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -320,6 +320,20 @@ static int name_terminate(const char *name, int namelen, int c, int terminate)
 	return 1;
 }
 
+/* remove double slashes to make --index work with such filenames */
+static char *canon_name(char *name)
+{
+	int i = 0, j = 0;
+
+	while (name[i]) {
+		if ((name[j++] = name[i++]) == '/')
+			while (name[i] == '/')
+				i++;
+	}
+	name[j] = '\0';
+	return name;
+}
+
 static char *find_name(const char *line, char *def, int p_value, int terminate)
 {
 	int len;
@@ -349,7 +363,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
 				free(def);
 				if (root)
 					strbuf_insert(&name, 0, root, root_len);
-				return strbuf_detach(&name, NULL);
+				return canon_name(strbuf_detach(&name, NULL));
 			}
 		}
 		strbuf_release(&name);
@@ -369,10 +383,10 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
 			start = line;
 	}
 	if (!start)
-		return def;
+		return canon_name(def);
 	len = line - start;
 	if (!len)
-		return def;
+		return canon_name(def);
 
 	/*
 	 * Generally we prefer the shorter name, especially
@@ -383,7 +397,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
 	if (def) {
 		int deflen = strlen(def);
 		if (deflen < len && !strncmp(start, def, deflen))
-			return def;
+			return canon_name(def);
 		free(def);
 	}
 
@@ -392,10 +406,10 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
 		strcpy(ret, root);
 		memcpy(ret + root_len, start, len);
 		ret[root_len + len] = '\0';
-		return ret;
+		return canon_name(ret);
 	}
 
-	return xmemdupz(start, len);
+	return canon_name(xmemdupz(start, len));
 }
 
 static int count_slashes(const char *cp)
-- 
1.6.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-05-25  9:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-21 12:25 [PATCH] apply: handle filenames with double slashes better Michal Marek
2009-05-21 14:56 ` Junio C Hamano
2009-05-21 19:12   ` Michal Marek
2009-05-21 19:22     ` Junio C Hamano
2009-05-25  9:11       ` Michal Marek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).