From 1f5d271f5e8f7b1e2a5b296ff43ca4087eb08244 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Wed, 25 May 2011 20:37:12 -0700 Subject: setup: Provide GIT_PREFIX to built-ins GIT_PREFIX was added in 7cf16a14f5c070f7b14cf28023769450133172ae so that aliases can know the directory from which a !alias was called. Knowing the prefix relative to the root is helpful in other programs so export it to built-ins as well. Helped-by: Michael J Gruber Signed-off-by: David Aguilar Signed-off-by: Junio C Hamano diff --git a/setup.c b/setup.c index 03cd84f..63f5368 100644 --- a/setup.c +++ b/setup.c @@ -602,6 +602,11 @@ const char *setup_git_directory_gently(int *nongit_ok) const char *prefix; prefix = setup_git_directory_gently_1(nongit_ok); + if (prefix) + setenv("GIT_PREFIX", prefix, 1); + else + setenv("GIT_PREFIX", "", 1); + if (startup_info) { startup_info->have_repository = !nongit_ok || !*nongit_ok; startup_info->prefix = prefix; diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh index ddc3921..3c74480 100755 --- a/t/t1020-subdirectory.sh +++ b/t/t1020-subdirectory.sh @@ -139,6 +139,22 @@ test_expect_success 'GIT_PREFIX for !alias' ' test_cmp expect actual ' +test_expect_success 'GIT_PREFIX for built-ins' ' + # Use GIT_EXTERNAL_DIFF to test that the "diff" built-in + # receives the GIT_PREFIX variable. + printf "dir/" >expect && + printf "#!/bin/sh\n" >diff && + printf "printf \"\$GIT_PREFIX\"" >>diff && + chmod +x diff && + ( + cd dir && + printf "change" >two && + env GIT_EXTERNAL_DIFF=./diff git diff >../actual + git checkout -- two + ) && + test_cmp expect actual +' + test_expect_success 'no file/rev ambiguity check inside .git' ' git commit -a -m 1 && ( -- cgit v0.10.2-6-g49f6 From 26b052515d1714918a7da4bfcdaaca37dc4db2e0 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Wed, 25 May 2011 20:37:13 -0700 Subject: git: Remove handling for GIT_PREFIX handle_alias() no longer needs to set GIT_PREFIX since it is defined in setup_git_directory_gently(). Remove the duplicated effort and use run_command_v_opt() since there is no need to setup the environment. Signed-off-by: David Aguilar Signed-off-by: Junio C Hamano diff --git a/git.c b/git.c index ed89951..ef598c3 100644 --- a/git.c +++ b/git.c @@ -179,8 +179,6 @@ static int handle_alias(int *argcp, const char ***argv) if (alias_string[0] == '!') { const char **alias_argv; int argc = *argcp, i; - struct strbuf sb = STRBUF_INIT; - const char *env[2]; commit_pager_choice(); @@ -191,13 +189,7 @@ static int handle_alias(int *argcp, const char ***argv) alias_argv[i] = (*argv)[i]; alias_argv[argc] = NULL; - strbuf_addstr(&sb, "GIT_PREFIX="); - if (subdir) - strbuf_addstr(&sb, subdir); - env[0] = sb.buf; - env[1] = NULL; - ret = run_command_v_opt_cd_env(alias_argv, RUN_USING_SHELL, NULL, env); - strbuf_release(&sb); + ret = run_command_v_opt(alias_argv, RUN_USING_SHELL); if (ret >= 0) /* normal exit */ exit(ret); -- cgit v0.10.2-6-g49f6 From f9ad901fd31991837c9648bbee8ac18b39aa0891 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Wed, 25 May 2011 23:21:01 -0700 Subject: git-mergetool--lib: Make vimdiff retain the current directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using difftool with vimdiff it can be unexpected that the current directory changes to the root of the project. Tell vim to chdir to the value of $GIT_PREFIX to fix this. Care is taken to quote the variable so that vim expands it. This avoids problems when directory names contain spaces. Signed-off-by: David Aguilar Reported-by: Frédéric Heitzmann Signed-off-by: Junio C Hamano diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh index fb3f52b..f5a100a 100644 --- a/git-mergetool--lib.sh +++ b/git-mergetool--lib.sh @@ -86,6 +86,11 @@ get_merge_tool_cmd () { } run_merge_tool () { + # If GIT_PREFIX is empty then we cannot use it in tools + # that expect to be able to chdir() to its value. + GIT_PREFIX=${GIT_PREFIX:-.} + export GIT_PREFIX + merge_tool_path="$(get_merge_tool_path "$1")" || exit base_present="$2" status=0 @@ -188,6 +193,7 @@ run_merge_tool () { check_unchanged else "$merge_tool_path" -R -f -d -c "wincmd l" \ + -c 'cd $GIT_PREFIX' \ "$LOCAL" "$REMOTE" fi ;; @@ -199,6 +205,7 @@ run_merge_tool () { check_unchanged else "$merge_tool_path" -R -f -d -c "wincmd l" \ + -c 'cd $GIT_PREFIX' \ "$LOCAL" "$REMOTE" fi ;; -- cgit v0.10.2-6-g49f6 From c35ec8c901a2391c4652ab1acdff04c9d67b1543 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Thu, 2 Jun 2011 02:26:25 -0700 Subject: t/t7503-pre-commit-hook.sh: Add GIT_PREFIX tests Ensure that the pre-commit hook has access to GIT_PREFIX. Signed-off-by: David Aguilar Signed-off-by: Junio C Hamano diff --git a/t/t7503-pre-commit-hook.sh b/t/t7503-pre-commit-hook.sh index 8528f64..ee7f0cd 100755 --- a/t/t7503-pre-commit-hook.sh +++ b/t/t7503-pre-commit-hook.sh @@ -84,5 +84,38 @@ test_expect_success POSIXPERM '--no-verify with non-executable hook' ' git commit --no-verify -m "more content" ' +chmod +x "$HOOK" + +# a hook that checks $GIT_PREFIX and succeeds inside the +# success/ subdirectory only +cat > "$HOOK" <> file && + git add file && + mkdir success && + ( + cd success && + git commit -m "hook requires GIT_PREFIX = success/" + ) && + rmdir success +' + +test_expect_success 'with failing hook requiring GIT_PREFIX' ' + + echo "more content" >> file && + git add file && + mkdir fail && + ( + cd fail && + test_must_fail git commit -m "hook must fail" + ) && + rmdir fail && + git checkout -- file +' test_done -- cgit v0.10.2-6-g49f6