From: "SZEDER Gábor" <szeder.dev@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, "SZEDER Gábor" <szeder.dev@gmail.com>
Subject: [PATCHv2 20/21] completion: extract repository discovery from __gitdir()
Date: Fri, 3 Feb 2017 03:48:28 +0100 [thread overview]
Message-ID: <20170203024829.8071-21-szeder.dev@gmail.com> (raw)
In-Reply-To: <20170203024829.8071-1-szeder.dev@gmail.com>
To prepare for caching the path to the repository in the following
commit, extract the repository discovering part of __gitdir() into the
__git_find_repo_path() helper function, which stores the found path in
the $__git_repo_path variable instead of printing it. Make __gitdir()
a wrapper around this new function. Declare $__git_repo_path local in
the toplevel completion functions __git_main() and __gitk_main() to
ensure that it never leaks into the environment and influences
subsequent completions (though this isn't necessary right now, as
__gitdir() is still only executed in subshells, but will matter for
the following commit).
Adjust tests checking __gitdir() or any other completion function
calling __gitdir() to perform those checks in a subshell to prevent
$__git_repo_path from leaking into the test environment. Otherwise
leave the tests unchanged to demonstrate that this change doesn't
alter __gitdir()'s behavior.
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
contrib/completion/git-completion.bash | 42 +++++++++++++++++++++-------------
t/t9902-completion.sh | 22 +++++++++++++-----
2 files changed, 42 insertions(+), 22 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 1c7493ff2..7775411cd 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -34,26 +34,35 @@ case "$COMP_WORDBREAKS" in
*) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac
+# Discovers the path to the git repository taking any '--git-dir=<path>' and
+# '-C <path>' options into account and stores it in the $__git_repo_path
+# variable.
+__git_find_repo_path ()
+{
+ if [ -n "${__git_C_args-}" ]; then
+ __git_repo_path="$(git "${__git_C_args[@]}" \
+ ${__git_dir:+--git-dir="$__git_dir"} \
+ rev-parse --absolute-git-dir 2>/dev/null)"
+ elif [ -n "${__git_dir-}" ]; then
+ test -d "$__git_dir" &&
+ __git_repo_path="$__git_dir"
+ elif [ -n "${GIT_DIR-}" ]; then
+ test -d "${GIT_DIR-}" &&
+ __git_repo_path="$GIT_DIR"
+ elif [ -d .git ]; then
+ __git_repo_path=.git
+ else
+ __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
+ fi
+}
+
# __gitdir accepts 0 or 1 arguments (i.e., location)
# returns location of .git repo
__gitdir ()
{
if [ -z "${1-}" ]; then
- if [ -n "${__git_C_args-}" ]; then
- git "${__git_C_args[@]}" \
- ${__git_dir:+--git-dir="$__git_dir"} \
- rev-parse --absolute-git-dir 2>/dev/null
- elif [ -n "${__git_dir-}" ]; then
- test -d "$__git_dir" || return 1
- echo "$__git_dir"
- elif [ -n "${GIT_DIR-}" ]; then
- test -d "${GIT_DIR-}" || return 1
- echo "$GIT_DIR"
- elif [ -d .git ]; then
- echo .git
- else
- git rev-parse --git-dir 2>/dev/null
- fi
+ __git_find_repo_path || return 1
+ echo "$__git_repo_path"
elif [ -d "$1/.git" ]; then
echo "$1/.git"
else
@@ -2783,7 +2792,7 @@ _git_worktree ()
__git_main ()
{
- local i c=1 command __git_dir
+ local i c=1 command __git_dir __git_repo_path
local __git_C_args C_args_count=0
while [ $c -lt $cword ]; do
@@ -2855,6 +2864,7 @@ __gitk_main ()
{
__git_has_doubledash && return
+ local __git_repo_path
local g="$(__gitdir)"
local merge=""
if [ -f "$g/MERGE_HEAD" ]; then
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 984df05b2..1816ed2e0 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -147,19 +147,25 @@ test_expect_success '__gitdir - from command line (through $__git_dir)' '
test_expect_success '__gitdir - repo as argument' '
echo "otherrepo/.git" >expected &&
- __gitdir "otherrepo" >"$actual" &&
+ (
+ __gitdir "otherrepo" >"$actual"
+ ) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - remote as argument' '
echo "remote" >expected &&
- __gitdir "remote" >"$actual" &&
+ (
+ __gitdir "remote" >"$actual"
+ ) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - .git directory in cwd' '
echo ".git" >expected &&
- __gitdir >"$actual" &&
+ (
+ __gitdir >"$actual"
+ ) &&
test_cmp expected "$actual"
'
@@ -450,7 +456,9 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from
git remote add remote_in_config_1 git://remote_1 &&
test_when_finished "git remote remove remote_in_config_2" &&
git remote add remote_in_config_2 git://remote_2 &&
- __git_remotes >actual &&
+ (
+ __git_remotes >actual
+ ) &&
test_cmp expect actual
'
@@ -459,8 +467,10 @@ test_expect_success '__git_is_configured_remote' '
git remote add remote_1 git://remote_1 &&
test_when_finished "git remote remove remote_2" &&
git remote add remote_2 git://remote_2 &&
- verbose __git_is_configured_remote remote_2 &&
- test_must_fail __git_is_configured_remote non-existent
+ (
+ verbose __git_is_configured_remote remote_2 &&
+ test_must_fail __git_is_configured_remote non-existent
+ )
'
test_expect_success 'setup for ref completion' '
--
2.11.0.555.g967c1bcb3
next prev parent reply other threads:[~2017-02-03 2:49 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-03 2:48 [PATCHv2 00/21] completion: various __gitdir()-related improvements SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 01/21] completion: improve __git_refs()'s in-code documentation SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 02/21] completion tests: don't add test cruft to the test repository SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 03/21] completion tests: make the $cur variable local to the test helper functions SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 04/21] completion tests: consolidate getting path of current working directory SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 05/21] completion tests: check __gitdir()'s output in the error cases SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 06/21] completion tests: add tests for the __git_refs() helper function SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 07/21] completion: ensure that the repository path given on the command line exists SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 08/21] completion: fix most spots not respecting 'git --git-dir=<path>' SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 09/21] completion: respect 'git --git-dir=<path>' when listing remote refs SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 10/21] completion: list refs from remote when remote's name matches a directory SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 11/21] completion: don't list 'HEAD' when trying refs completion outside of a repo SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 12/21] completion: list short refs from a remote given as a URL SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 13/21] completion: don't offer commands when 'git --opt' needs an argument SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 14/21] completion: fix completion after 'git -C <path>' SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 15/21] rev-parse: add '--absolute-git-dir' option SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 16/21] completion: respect 'git -C <path>' SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 17/21] completion: don't use __gitdir() for git commands SZEDER Gábor
2017-02-13 19:20 ` [PATCH] completion: restore removed line continuating backslash SZEDER Gábor
2017-02-13 20:45 ` Junio C Hamano
2017-02-15 1:35 ` SZEDER Gábor
2017-02-15 2:41 ` Junio C Hamano
2017-02-15 13:06 ` SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 18/21] completion: consolidate silencing errors from git commands SZEDER Gábor
2017-02-03 2:48 ` [PATCHv2 19/21] completion: don't guard git executions with __gitdir() SZEDER Gábor
2017-02-03 2:48 ` SZEDER Gábor [this message]
2017-02-03 2:48 ` [PATCHv2 21/21] completion: cache the path to the repository SZEDER Gábor
2017-02-06 21:29 ` [PATCHv2 00/21] completion: various __gitdir()-related improvements Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170203024829.8071-21-szeder.dev@gmail.com \
--to=szeder.dev@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.