From c4730f35cc6507bc117a08885d88668fe02b1a7d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 1 Jul 2008 00:44:47 +0100 Subject: Teach "git apply" to prepend a prefix with "--root=" With "git apply --root=", all file names in the patch are prepended with . If a "-p" value was given, the paths are stripped _before_ prepending . Wished for by HPA. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-apply.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'builtin-apply.c') diff --git a/builtin-apply.c b/builtin-apply.c index c49788931..bf528966c 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -57,6 +57,8 @@ static int whitespace_error; static int squelch_whitespace_errors = 5; static int applied_after_fixing_ws; static const char *patch_input_file; +static const char *root; +static int root_len; static void parse_whitespace_option(const char *option) { @@ -331,6 +333,8 @@ static char *find_name(const char *line, char *def, int p_value, int terminate) */ strbuf_remove(&name, 0, cp - name.buf); free(def); + if (root) + strbuf_insert(&name, 0, root, root_len); return strbuf_detach(&name, NULL); } } @@ -369,6 +373,14 @@ static char *find_name(const char *line, char *def, int p_value, int terminate) free(def); } + if (root) { + char *ret = xmalloc(root_len + len + 1); + strcpy(ret, root); + memcpy(ret + root_len, start, len); + ret[root_len + len] = '\0'; + return ret; + } + return xmemdupz(start, len); } @@ -3118,6 +3130,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) inaccurate_eof = 1; continue; } + if (!strncmp(arg, "--root=", strlen("--root="))) { + arg += strlen("--root="); + root_len = strlen(arg); + if (root_len && arg[root_len + 1] != '/') { + char *new_root; + root = new_root = xmalloc(root_len + 2); + strcpy(new_root, arg); + strcpy(new_root + root_len++, "/"); + } else + root = arg; + continue; + } if (0 < prefix_length) arg = prefix_filename(prefix, prefix_length, arg); -- cgit v1.2.1 From 8ee4a6c2ec6738cfbc815dc59e44825f2a9b9f15 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 2 Jul 2008 15:28:22 -0700 Subject: apply --root: thinkofix. The end of a string is string[length-1], not string[length+1]. I pointed it out during the review, but I forgot about it when applying the patch. This should fix it. Signed-off-by: Junio C Hamano --- builtin-apply.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-apply.c') diff --git a/builtin-apply.c b/builtin-apply.c index bf528966c..6c3db60b6 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -3130,10 +3130,10 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) inaccurate_eof = 1; continue; } - if (!strncmp(arg, "--root=", strlen("--root="))) { + if (!prefixcmp(arg, "--root=")) { arg += strlen("--root="); root_len = strlen(arg); - if (root_len && arg[root_len + 1] != '/') { + if (root_len && arg[root_len - 1] != '/') { char *new_root; root = new_root = xmalloc(root_len + 2); strcpy(new_root, arg); -- cgit v1.2.1 From f55638874774acc1e13a046353449ebc8734ab08 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 6 Jul 2008 18:36:01 -0700 Subject: git-apply --directory: make --root more similar to GNU diff Applying a patch in the directory that is different from what the patch records is done with --directory option in GNU diff. The --root option we introduced previously does the same, and we can call it the same way to give users more familiar feel. Signed-off-by: Junio C Hamano --- builtin-apply.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-apply.c') diff --git a/builtin-apply.c b/builtin-apply.c index 6c3db60b6..c242bbd83 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -3130,8 +3130,8 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) inaccurate_eof = 1; continue; } - if (!prefixcmp(arg, "--root=")) { - arg += strlen("--root="); + if (!prefixcmp(arg, "--directory=")) { + arg += strlen("--directory="); root_len = strlen(arg); if (root_len && arg[root_len - 1] != '/') { char *new_root; -- cgit v1.2.1