diff options
author | SZEDER Gábor <szeder@ira.uka.de> | 2016-02-25 23:50:39 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-02-28 16:37:15 -0800 |
commit | f76bf39177b753f6efe06817de827570db3c37ac (patch) | |
tree | af2f22045faeb3a54f2fa400f83053b9d9ba0f98 | |
parent | 113e40737385939666ed04d9b45fcfbb91b5d73f (diff) | |
download | git-f76bf39177b753f6efe06817de827570db3c37ac.tar.gz |
completion: list refs from remote when remote's name matches a directory
If the remote given to __git_refs() happens to match both the name of
a configured remote and the name of a directory in the current working
directory, then that directory is assumed to be a git repository, and
listing refs from that directory will be attempted. This is wrong,
because in such a situation git commands (e.g. 'git fetch|pull|push
<remote>' whom these refs will eventually be passed to) give
precedence to the configured remote. Therefore, __git_refs() should
list refs from the configured remote as well.
Add the helper function __git_is_configured_remote() that checks
whether its argument matches the name of a configured remote. Use
this helper to decide how to handle the remote passed to __git_refs().
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | contrib/completion/git-completion.bash | 28 | ||||
-rwxr-xr-x | t/t9902-completion.sh | 11 |
2 files changed, 33 insertions, 6 deletions
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index adc968acea..b85ab27fb1 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -347,12 +347,18 @@ __git_refs () local format refs if [ -n "$remote" ]; then - if [ -d "$remote/.git" ]; then - dir="$remote/.git" - elif [ -d "$remote" ]; then - dir="$remote" - else + if __git_is_configured_remote "$remote"; then + # configured remote takes precedence over a + # local directory with the same name from_local=n + else + if [ -d "$remote/.git" ]; then + dir="$remote/.git" + elif [ -d "$remote" ]; then + dir="$remote" + else + from_local=n + fi fi fi @@ -434,6 +440,18 @@ __git_remotes () git --git-dir="$d" remote } +# Returns true if $1 matches the name of a configured remote, false otherwise. +__git_is_configured_remote () +{ + local remote + for remote in $(__git_remotes); do + if [ "$remote" = "$1" ]; then + return 0 + fi + done + return 1 +} + __git_list_merge_strategies () { git merge -s help 2>&1 | diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index f9993d2c00..9f8be9ab1f 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -378,6 +378,15 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from test_cmp expect actual ' +test_expect_success '__git_is_configured_remote' ' + test_when_finished "git remote remove remote_1" && + 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 +' + test_expect_success 'setup for ref completion' ' echo content >file1 && echo more >file2 && @@ -519,7 +528,7 @@ test_expect_success '__git_refs - configured remote - full refs - repo given on test_cmp expected "$actual" ' -test_expect_failure '__git_refs - configured remote - remote name matches a directory' ' +test_expect_success '__git_refs - configured remote - remote name matches a directory' ' cat >expected <<-EOF && HEAD branch |