Skip to content

Commit 4ed5461

Browse files
committed
Merge branch 'da/git-prefix-everywhere' into next
* da/git-prefix-everywhere: t/t7503-pre-commit-hook.sh: Add GIT_PREFIX tests git-mergetool--lib: Make vimdiff retain the current directory git: Remove handling for GIT_PREFIX setup: Provide GIT_PREFIX to built-ins
2 parents 55ac692 + c35ec8c commit 4ed5461

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

git-mergetool--lib.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ get_merge_tool_cmd () {
8686
}
8787

8888
run_merge_tool () {
89+
# If GIT_PREFIX is empty then we cannot use it in tools
90+
# that expect to be able to chdir() to its value.
91+
GIT_PREFIX=${GIT_PREFIX:-.}
92+
export GIT_PREFIX
93+
8994
merge_tool_path="$(get_merge_tool_path "$1")" || exit
9095
base_present="$2"
9196
status=0
@@ -188,6 +193,7 @@ run_merge_tool () {
188193
check_unchanged
189194
else
190195
"$merge_tool_path" -R -f -d -c "wincmd l" \
196+
-c 'cd $GIT_PREFIX' \
191197
"$LOCAL" "$REMOTE"
192198
fi
193199
;;
@@ -199,6 +205,7 @@ run_merge_tool () {
199205
check_unchanged
200206
else
201207
"$merge_tool_path" -R -f -d -c "wincmd l" \
208+
-c 'cd $GIT_PREFIX' \
202209
"$LOCAL" "$REMOTE"
203210
fi
204211
;;

git.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ static int handle_alias(int *argcp, const char ***argv)
183183
if (alias_string[0] == '!') {
184184
const char **alias_argv;
185185
int argc = *argcp, i;
186-
struct strbuf sb = STRBUF_INIT;
187-
const char *env[2];
188186

189187
commit_pager_choice();
190188

@@ -195,13 +193,7 @@ static int handle_alias(int *argcp, const char ***argv)
195193
alias_argv[i] = (*argv)[i];
196194
alias_argv[argc] = NULL;
197195

198-
strbuf_addstr(&sb, "GIT_PREFIX=");
199-
if (subdir)
200-
strbuf_addstr(&sb, subdir);
201-
env[0] = sb.buf;
202-
env[1] = NULL;
203-
ret = run_command_v_opt_cd_env(alias_argv, RUN_USING_SHELL, NULL, env);
204-
strbuf_release(&sb);
196+
ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
205197
if (ret >= 0) /* normal exit */
206198
exit(ret);
207199

setup.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
710710
const char *prefix;
711711

712712
prefix = setup_git_directory_gently_1(nongit_ok);
713+
if (prefix)
714+
setenv("GIT_PREFIX", prefix, 1);
715+
else
716+
setenv("GIT_PREFIX", "", 1);
717+
713718
if (startup_info) {
714719
startup_info->have_repository = !nongit_ok || !*nongit_ok;
715720
startup_info->prefix = prefix;

t/t1020-subdirectory.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ test_expect_success 'GIT_PREFIX for !alias' '
140140
test_cmp expect actual
141141
'
142142

143+
test_expect_success 'GIT_PREFIX for built-ins' '
144+
# Use GIT_EXTERNAL_DIFF to test that the "diff" built-in
145+
# receives the GIT_PREFIX variable.
146+
printf "dir/" >expect &&
147+
printf "#!/bin/sh\n" >diff &&
148+
printf "printf \"\$GIT_PREFIX\"" >>diff &&
149+
chmod +x diff &&
150+
(
151+
cd dir &&
152+
printf "change" >two &&
153+
env GIT_EXTERNAL_DIFF=./diff git diff >../actual
154+
git checkout -- two
155+
) &&
156+
test_cmp expect actual
157+
'
158+
143159
test_expect_success 'no file/rev ambiguity check inside .git' '
144160
git commit -a -m 1 &&
145161
(

t/t7503-pre-commit-hook.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,38 @@ test_expect_success POSIXPERM '--no-verify with non-executable hook' '
8484
git commit --no-verify -m "more content"
8585
8686
'
87+
chmod +x "$HOOK"
88+
89+
# a hook that checks $GIT_PREFIX and succeeds inside the
90+
# success/ subdirectory only
91+
cat > "$HOOK" <<EOF
92+
#!/bin/sh
93+
test \$GIT_PREFIX = success/
94+
EOF
95+
96+
test_expect_success 'with hook requiring GIT_PREFIX' '
97+
98+
echo "more content" >> file &&
99+
git add file &&
100+
mkdir success &&
101+
(
102+
cd success &&
103+
git commit -m "hook requires GIT_PREFIX = success/"
104+
) &&
105+
rmdir success
106+
'
107+
108+
test_expect_success 'with failing hook requiring GIT_PREFIX' '
109+
110+
echo "more content" >> file &&
111+
git add file &&
112+
mkdir fail &&
113+
(
114+
cd fail &&
115+
test_must_fail git commit -m "hook must fail"
116+
) &&
117+
rmdir fail &&
118+
git checkout -- file
119+
'
87120

88121
test_done

0 commit comments

Comments
 (0)