]> git.ipfire.org Git - thirdparty/git.git/blobdiff - t/t9902-completion.sh
completion: list short refs from a remote given as a URL
[thirdparty/git.git] / t / t9902-completion.sh
index a34e55f874ca06da4cae6ab5d46bc3263c58d521..500505dca1854304c48d13754fd0d05a017a01b1 100755 (executable)
@@ -98,7 +98,7 @@ test_gitcomp ()
 {
        local -a COMPREPLY &&
        sed -e 's/Z$//' >expected &&
-       cur="$1" &&
+       local cur="$1" &&
        shift &&
        __gitcomp "$@" &&
        print_comp &&
@@ -113,7 +113,7 @@ test_gitcomp_nl ()
 {
        local -a COMPREPLY &&
        sed -e 's/Z$//' >expected &&
-       cur="$1" &&
+       local cur="$1" &&
        shift &&
        __gitcomp_nl "$@" &&
        print_comp &&
@@ -124,15 +124,22 @@ invalid_variable_name='${foo.bar}'
 
 actual="$TRASH_DIRECTORY/actual"
 
+if test_have_prereq MINGW
+then
+       ROOT="$(pwd -W)"
+else
+       ROOT="$(pwd)"
+fi
+
 test_expect_success 'setup for __gitdir tests' '
        mkdir -p subdir/subsubdir &&
        git init otherrepo
 '
 
 test_expect_success '__gitdir - from command line (through $__git_dir)' '
-       echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+       echo "$ROOT/otherrepo/.git" >expected &&
        (
-               __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
+               __git_dir="$ROOT/otherrepo/.git" &&
                __gitdir >"$actual"
        ) &&
        test_cmp expected "$actual"
@@ -157,7 +164,7 @@ test_expect_success '__gitdir - .git directory in cwd' '
 '
 
 test_expect_success '__gitdir - .git directory in parent' '
-       echo "$(pwd -P)/.git" >expected &&
+       echo "$ROOT/.git" >expected &&
        (
                cd subdir/subsubdir &&
                __gitdir >"$actual"
@@ -175,7 +182,7 @@ test_expect_success '__gitdir - cwd is a .git directory' '
 '
 
 test_expect_success '__gitdir - parent is a .git directory' '
-       echo "$(pwd -P)/.git" >expected &&
+       echo "$ROOT/.git" >expected &&
        (
                cd .git/refs/heads &&
                __gitdir >"$actual"
@@ -184,9 +191,9 @@ test_expect_success '__gitdir - parent is a .git directory' '
 '
 
 test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
-       echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+       echo "$ROOT/otherrepo/.git" >expected &&
        (
-               GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+               GIT_DIR="$ROOT/otherrepo/.git" &&
                export GIT_DIR &&
                __gitdir >"$actual"
        ) &&
@@ -194,9 +201,9 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
 '
 
 test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
-       echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+       echo "$ROOT/otherrepo/.git" >expected &&
        (
-               GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+               GIT_DIR="$ROOT/otherrepo/.git" &&
                export GIT_DIR &&
                cd subdir &&
                __gitdir >"$actual"
@@ -204,26 +211,26 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
        test_cmp expected "$actual"
 '
 
+test_expect_success '__gitdir - non-existing path in $__git_dir' '
+       (
+               __git_dir="non-existing" &&
+               test_must_fail __gitdir >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
 test_expect_success '__gitdir - non-existing $GIT_DIR' '
        (
-               GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
+               GIT_DIR="$ROOT/non-existing" &&
                export GIT_DIR &&
-               test_must_fail __gitdir
-       )
+               test_must_fail __gitdir >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
 '
 
-function pwd_P_W () {
-       if test_have_prereq MINGW
-       then
-               pwd -W
-       else
-               pwd -P
-       fi
-}
-
 test_expect_success '__gitdir - gitfile in cwd' '
-       echo "$(pwd_P_W)/otherrepo/.git" >expected &&
-       echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
+       echo "$ROOT/otherrepo/.git" >expected &&
+       echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
        test_when_finished "rm -f subdir/.git" &&
        (
                cd subdir &&
@@ -233,8 +240,8 @@ test_expect_success '__gitdir - gitfile in cwd' '
 '
 
 test_expect_success '__gitdir - gitfile in parent' '
-       echo "$(pwd_P_W)/otherrepo/.git" >expected &&
-       echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
+       echo "$ROOT/otherrepo/.git" >expected &&
+       echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
        test_when_finished "rm -f subdir/.git" &&
        (
                cd subdir/subsubdir &&
@@ -244,7 +251,7 @@ test_expect_success '__gitdir - gitfile in parent' '
 '
 
 test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
-       echo "$(pwd -P)/otherrepo/.git" >expected &&
+       echo "$ROOT/otherrepo/.git" >expected &&
        mkdir otherrepo/dir &&
        test_when_finished "rm -rf otherrepo/dir" &&
        ln -s otherrepo/dir link &&
@@ -257,7 +264,8 @@ test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
 '
 
 test_expect_success '__gitdir - not a git repository' '
-       nongit test_must_fail __gitdir
+       nongit test_must_fail __gitdir >"$actual" &&
+       test_must_be_empty "$actual"
 '
 
 test_expect_success '__gitcomp - trailing space - options' '
@@ -365,6 +373,278 @@ 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' '
+       git commit --allow-empty -m initial &&
+       git branch matching-branch &&
+       git tag matching-tag &&
+       (
+               cd otherrepo &&
+               git commit --allow-empty -m initial &&
+               git branch -m master master-in-other &&
+               git branch branch-in-other &&
+               git tag tag-in-other
+       ) &&
+       git remote add other "$ROOT/otherrepo/.git" &&
+       git fetch --no-tags other &&
+       rm -f .git/FETCH_HEAD &&
+       git init thirdrepo
+'
+
+test_expect_success '__git_refs - simple' '
+       cat >expected <<-EOF &&
+       HEAD
+       master
+       matching-branch
+       other/branch-in-other
+       other/master-in-other
+       matching-tag
+       EOF
+       (
+               cur= &&
+               __git_refs >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - full refs' '
+       cat >expected <<-EOF &&
+       refs/heads/master
+       refs/heads/matching-branch
+       EOF
+       (
+               cur=refs/heads/ &&
+               __git_refs >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - repo given on the command line' '
+       cat >expected <<-EOF &&
+       HEAD
+       branch-in-other
+       master-in-other
+       tag-in-other
+       EOF
+       (
+               __git_dir="$ROOT/otherrepo/.git" &&
+               cur= &&
+               __git_refs >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - remote on local file system' '
+       cat >expected <<-EOF &&
+       HEAD
+       branch-in-other
+       master-in-other
+       tag-in-other
+       EOF
+       (
+               cur= &&
+               __git_refs otherrepo >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - remote on local file system - full refs' '
+       cat >expected <<-EOF &&
+       refs/heads/branch-in-other
+       refs/heads/master-in-other
+       refs/tags/tag-in-other
+       EOF
+       (
+               cur=refs/ &&
+               __git_refs otherrepo >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote' '
+       cat >expected <<-EOF &&
+       HEAD
+       branch-in-other
+       master-in-other
+       EOF
+       (
+               cur= &&
+               __git_refs other >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - full refs' '
+       cat >expected <<-EOF &&
+       refs/heads/branch-in-other
+       refs/heads/master-in-other
+       refs/tags/tag-in-other
+       EOF
+       (
+               cur=refs/ &&
+               __git_refs other >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - repo given on the command line' '
+       cat >expected <<-EOF &&
+       HEAD
+       branch-in-other
+       master-in-other
+       EOF
+       (
+               cd thirdrepo &&
+               __git_dir="$ROOT/.git" &&
+               cur= &&
+               __git_refs other >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
+       cat >expected <<-EOF &&
+       refs/heads/branch-in-other
+       refs/heads/master-in-other
+       refs/tags/tag-in-other
+       EOF
+       (
+               cd thirdrepo &&
+               __git_dir="$ROOT/.git" &&
+               cur=refs/ &&
+               __git_refs other >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - remote name matches a directory' '
+       cat >expected <<-EOF &&
+       HEAD
+       branch-in-other
+       master-in-other
+       EOF
+       mkdir other &&
+       test_when_finished "rm -rf other" &&
+       (
+               cur= &&
+               __git_refs other >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - URL remote' '
+       cat >expected <<-EOF &&
+       HEAD
+       branch-in-other
+       master-in-other
+       tag-in-other
+       EOF
+       (
+               cur= &&
+               __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - URL remote - full refs' '
+       cat >expected <<-EOF &&
+       refs/heads/branch-in-other
+       refs/heads/master-in-other
+       refs/tags/tag-in-other
+       EOF
+       (
+               cur=refs/ &&
+               __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - non-existing remote' '
+       (
+               cur= &&
+               __git_refs non-existing >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - non-existing remote - full refs' '
+       (
+               cur=refs/ &&
+               __git_refs non-existing >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - non-existing URL remote' '
+       (
+               cur= &&
+               __git_refs "file://$ROOT/non-existing" >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - non-existing URL remote - full refs' '
+       (
+               cur=refs/ &&
+               __git_refs "file://$ROOT/non-existing" >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - not in a git repository' '
+       (
+               GIT_CEILING_DIRECTORIES="$ROOT" &&
+               export GIT_CEILING_DIRECTORIES &&
+               cd subdir &&
+               cur= &&
+               __git_refs >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
+       cat >expected <<-EOF &&
+       HEAD
+       master
+       matching-branch
+       other/ambiguous
+       other/branch-in-other
+       other/master-in-other
+       remote/ambiguous
+       remote/branch-in-remote
+       matching-tag
+       branch-in-other
+       branch-in-remote
+       master-in-other
+       EOF
+       for remote_ref in refs/remotes/other/ambiguous \
+               refs/remotes/remote/ambiguous \
+               refs/remotes/remote/branch-in-remote
+       do
+               git update-ref $remote_ref master &&
+               test_when_finished "git update-ref -d $remote_ref"
+       done &&
+       (
+               cur= &&
+               __git_refs "" 1 >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+'
+
+test_expect_success 'teardown after ref completion' '
+       git branch -d matching-branch &&
+       git tag -d matching-tag &&
+       git remote remove other
+'
+
 test_expect_success '__git_get_config_variables' '
        cat >expect <<-EOF &&
        name-1
@@ -483,10 +763,10 @@ test_expect_success 'git --help completion' '
        test_completion "git --help core" "core-tutorial "
 '
 
-test_expect_success 'setup for ref completion' '
+test_expect_success 'setup for integration tests' '
        echo content >file1 &&
        echo more >file2 &&
-       git add . &&
+       git add file1 file2 &&
        git commit -m one &&
        git branch mybranch &&
        git tag mytag
@@ -517,7 +797,7 @@ test_expect_success '<ref>: completes paths' '
 
 test_expect_success 'complete tree filename with spaces' '
        echo content >"name with spaces" &&
-       git add . &&
+       git add "name with spaces" &&
        git commit -m spaces &&
        test_completion "git show HEAD:nam" <<-\EOF
        name with spaces Z
@@ -526,7 +806,7 @@ test_expect_success 'complete tree filename with spaces' '
 
 test_expect_success 'complete tree filename with metacharacters' '
        echo content >"name with \${meta}" &&
-       git add . &&
+       git add "name with \${meta}" &&
        git commit -m meta &&
        test_completion "git show HEAD:nam" <<-\EOF
        name with ${meta} Z