git-apply: work from subdirectory.
When applying a patch to index file, we need to know where GIT_DIR is; use setup_git_directory() to find it out. This also allows us to work from a subdirectory if we wanted to. When git-apply is run from a subdirectory, it applies the given patch only to the files under the current directory and below. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 25, 2005 at 23:14 UTC
edf2e37002eeb30a2ccad5db3b3e1fe41cdc7eb0
1 file changed
+18
apply.c
+18
@@ -16,6 +16,9 @@
16
// --numstat does numeric diffstat, and doesn't actually apply
17
// --index-info shows the old and new index info for paths if available.
18
//
19
+static const char *prefix;
20
+static int prefix_length = -1;
21
+
22
static int allow_binary_replacement = 0;
23
static int check_index = 0;
24
static int write_index = 0;
@@ -1706,6 +1709,12 @@ static int use_patch(struct patch *p)
1709
return 0;
1710
x = x->next;
1711
}
1712
+ if (0 < prefix_length) {
1713
+ int pathlen = strlen(pathname);
1714
+ if (pathlen <= prefix_length ||
1715
+ memcmp(prefix, pathname, prefix_length))
1716
+ return 0;
1717
+ }
1718
return 1;
1719
}
1720
@@ -1845,6 +1854,15 @@ int main(int argc, char **argv)
1854
line_termination = 0;
1855
continue;
1856
}
1857
+
1858
+ if (check_index && prefix_length < 0) {
1859
+ prefix = setup_git_directory();
1860
+ prefix_length = prefix ? strlen(prefix) : 0;
1861
+ git_config(git_default_config);
1862
+ }
1863
+ if (0 < prefix_length)
1864
+ arg = prefix_filename(prefix, prefix_length, arg);
1865
+
1866
fd = open(arg, O_RDONLY);
1867
if (fd < 0)
1868
usage(apply_usage);