Teach 'git apply' to look at $GIT_DIR/config
When neither --index nor --cached was used, git-apply did not try calling setup_git_directory(), which means it did not look at configuration files at all. This fixes it to call the setup function but still allow the command to be run in a directory not controlled by git. The bug probably meant that 'git apply', not moving up to the toplevel, did not apply properly formatted diffs from the toplevel when you are inside a subdirectory, even though 'git apply --index' would. As a side effect, this patch fixes it as well. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Feb 17, 2007 at 13:12 UTC
dc7b24364dcbe9ab74d937c92af9999ac1a2db0b
2 files changed
+103
-8
builtin-apply.c
+13
-8
@@ -2595,9 +2595,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2595
int read_stdin = 1;
2596
int inaccurate_eof = 0;
2597
int errs = 0;
2598
+ int is_not_gitdir = 0;
2599
2600
const char *whitespace_option = NULL;
2601
2602
+ prefix = setup_git_directory_gently(&is_not_gitdir);
2603
+ prefix_length = prefix ? strlen(prefix) : 0;
2604
+ if (!is_not_gitdir) {
2605
+ git_config(git_apply_config);
2606
+ if (apply_default_whitespace)
2607
+ parse_whitespace_option(apply_default_whitespace);
2608
+ }
2609
+
2610
for (i = 1; i < argc; i++) {
2611
const char *arg = argv[i];
2612
char *end;
@@ -2648,10 +2657,14 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2657
continue;
2658
}
2659
if (!strcmp(arg, "--index")) {
2660
+ if (is_not_gitdir)
2661
+ die("--index outside a repository");
2662
check_index = 1;
2663
continue;
2664
}
2665
if (!strcmp(arg, "--cached")) {
2666
+ if (is_not_gitdir)
2667
+ die("--cached outside a repository");
2668
check_index = 1;
2669
cached = 1;
2670
continue;
@@ -2700,14 +2713,6 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2713
inaccurate_eof = 1;
2714
continue;
2715
}
2703
-
2704
- if (check_index && prefix_length < 0) {
2705
- prefix = setup_git_directory();
2706
- prefix_length = prefix ? strlen(prefix) : 0;
2707
- git_config(git_apply_config);
2708
- if (!whitespace_option && apply_default_whitespace)
2709
- parse_whitespace_option(apply_default_whitespace);
2710
- }
2716
if (0 < prefix_length)
2717
arg = prefix_filename(prefix, prefix_length, arg);
2718
t/t4119-apply-config.sh
new
+90
@@ -0,0 +1,90 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2007 Junio C Hamano
4
+#
5
+
6
+test_description='git-apply --whitespace=strip and configuration file.
7
+
8
+'
9
+
10
+. ./test-lib.sh
11
+
12
+test_expect_success setup '
13
+ echo A >file1 &&
14
+ cp file1 saved &&
15
+ git add file1 &&
16
+ echo "B " >file1 &&
17
+ git diff >patch.file
18
+'
19
+
20
+test_expect_success 'apply --whitespace=strip' '
21
+
22
+ cp saved file1 &&
23
+ git update-index --refresh &&
24
+
25
+ git apply --whitespace=strip patch.file &&
26
+ if grep " " file1
27
+ then
28
+ echo "Eh?"
29
+ false
30
+ else
31
+ echo Happy
32
+ fi
33
+'
34
+
35
+test_expect_success 'apply --whitespace=strip from config' '
36
+
37
+ cp saved file1 &&
38
+ git update-index --refresh &&
39
+
40
+ git config apply.whitespace strip &&
41
+ git apply patch.file &&
42
+ if grep " " file1
43
+ then
44
+ echo "Eh?"
45
+ false
46
+ else
47
+ echo Happy
48
+ fi
49
+'
50
+
51
+mkdir sub
52
+D=`pwd`
53
+
54
+test_expect_success 'apply --whitespace=strip in subdir' '
55
+
56
+ cd "$D" &&
57
+ git config --unset-all apply.whitespace
58
+ cp saved file1 &&
59
+ git update-index --refresh &&
60
+
61
+ cd sub &&
62
+ git apply --whitespace=strip ../patch.file &&
63
+ if grep " " ../file1
64
+ then
65
+ echo "Eh?"
66
+ false
67
+ else
68
+ echo Happy
69
+ fi
70
+'
71
+
72
+test_expect_success 'apply --whitespace=strip from config in subdir' '
73
+
74
+ cd "$D" &&
75
+ git config apply.whitespace strip &&
76
+ cp saved file1 &&
77
+ git update-index --refresh &&
78
+
79
+ cd sub &&
80
+ git apply ../patch.file &&
81
+ if grep " " file1
82
+ then
83
+ echo "Eh?"
84
+ false
85
+ else
86
+ echo Happy
87
+ fi
88
+'
89
+
90
+test_done