Skip to content

Commit e9a9d6e

Browse files
committed
Merge branch 'js/apply-root'
* js/apply-root: git-apply --directory: make --root more similar to GNU diff apply --root: thinkofix. Teach "git apply" to prepend a prefix with "--root=<root>"
2 parents f7484db + f556388 commit e9a9d6e

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

Documentation/git-apply.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SYNOPSIS
1414
[--allow-binary-replacement | --binary] [--reject] [-z]
1515
[-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]
1616
[--whitespace=<nowarn|warn|fix|error|error-all>]
17-
[--exclude=PATH] [--verbose] [<patch>...]
17+
[--exclude=PATH] [--directory=<root>] [--verbose] [<patch>...]
1818

1919
DESCRIPTION
2020
-----------
@@ -182,6 +182,14 @@ behavior:
182182
by inspecting the patch (e.g. after editing the patch without
183183
adjusting the hunk headers appropriately).
184184

185+
--directory=<root>::
186+
Prepend <root> to all filenames. If a "-p" argument was passed, too,
187+
it is applied before prepending the new root.
188+
+
189+
For example, a patch that talks about updating `a/git-gui.sh` to `b/git-gui.sh`
190+
can be applied to the file in the working tree `modules/git-gui/git-gui.sh` by
191+
running `git apply --directory=modules/git-gui`.
192+
185193
Configuration
186194
-------------
187195

builtin-apply.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static int whitespace_error;
5858
static int squelch_whitespace_errors = 5;
5959
static int applied_after_fixing_ws;
6060
static const char *patch_input_file;
61+
static const char *root;
62+
static int root_len;
6163

6264
static void parse_whitespace_option(const char *option)
6365
{
@@ -340,6 +342,8 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
340342
*/
341343
strbuf_remove(&name, 0, cp - name.buf);
342344
free(def);
345+
if (root)
346+
strbuf_insert(&name, 0, root, root_len);
343347
return strbuf_detach(&name, NULL);
344348
}
345349
}
@@ -378,6 +382,14 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
378382
free(def);
379383
}
380384

385+
if (root) {
386+
char *ret = xmalloc(root_len + len + 1);
387+
strcpy(ret, root);
388+
memcpy(ret + root_len, start, len);
389+
ret[root_len + len] = '\0';
390+
return ret;
391+
}
392+
381393
return xmemdupz(start, len);
382394
}
383395

@@ -3240,6 +3252,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
32403252
options |= RECOUNT;
32413253
continue;
32423254
}
3255+
if (!prefixcmp(arg, "--directory=")) {
3256+
arg += strlen("--directory=");
3257+
root_len = strlen(arg);
3258+
if (root_len && arg[root_len - 1] != '/') {
3259+
char *new_root;
3260+
root = new_root = xmalloc(root_len + 2);
3261+
strcpy(new_root, arg);
3262+
strcpy(new_root + root_len++, "/");
3263+
} else
3264+
root = arg;
3265+
continue;
3266+
}
32433267
if (0 < prefix_length)
32443268
arg = prefix_filename(prefix, prefix_length, arg);
32453269

t/t4128-apply-root.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
test_description='apply same filename'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
9+
mkdir -p some/sub/dir &&
10+
echo Hello > some/sub/dir/file &&
11+
git add some/sub/dir/file &&
12+
git commit -m initial &&
13+
git tag initial
14+
15+
'
16+
17+
cat > patch << EOF
18+
diff a/bla/blub/dir/file b/bla/blub/dir/file
19+
--- a/bla/blub/dir/file
20+
+++ b/bla/blub/dir/file
21+
@@ -1,1 +1,1 @@
22+
-Hello
23+
+Bello
24+
EOF
25+
26+
test_expect_success 'apply --directory -p (1)' '
27+
28+
git apply --directory=some/sub -p3 --index patch &&
29+
test Bello = $(git show :some/sub/dir/file) &&
30+
test Bello = $(cat some/sub/dir/file)
31+
32+
'
33+
34+
test_expect_success 'apply --directory -p (2) ' '
35+
36+
git reset --hard initial &&
37+
git apply --directory=some/sub/ -p3 --index patch &&
38+
test Bello = $(git show :some/sub/dir/file) &&
39+
test Bello = $(cat some/sub/dir/file)
40+
41+
'
42+
43+
test_done

0 commit comments

Comments
 (0)