Fix remote autoselection when not on a branch

We currently have code that determines which remote to us automatically.
For example, if there is only one remote, we want to use it, no matter
what it's named, because there's no other logical choice.

However, if we were not on any branch, we'd always end up picking
"origin", even if that remote didn't exist. This appears to have been
because we wanted to avoid a lookup with an empty branch name in the
configuration. Doing so is harmless, though, so let's simply check that
the branch name isn't empty before we execute that condition, and fall
back to the rest of the checks if it is. This ensures that we always
pick the only remote when there's just one.
This commit is contained in:
brian m. carlson 2019-08-06 21:53:13 +00:00
parent ccfbffe537
commit f6f4f6a8e4
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
2 changed files with 32 additions and 6 deletions

@ -230,12 +230,7 @@ func (c *Configuration) Remote() string {
defer c.loading.Unlock()
if c.currentRemote == nil {
if len(ref.Name) == 0 {
c.currentRemote = &defaultRemote
return defaultRemote
}
if remote, ok := c.Git.Get(fmt.Sprintf("branch.%s.remote", ref.Name)); ok {
if remote, ok := c.Git.Get(fmt.Sprintf("branch.%s.remote", ref.Name)); len(ref.Name) != 0 && ok {
// try tracking remote
c.currentRemote = &remote
} else if remotes := c.Remotes(); len(remotes) == 1 {

@ -219,3 +219,34 @@ begin_test "smudge skip download failure"
)
end_test
begin_test "smudge no ref, non-origin"
(
set -e
reponame="$(basename "$0" ".sh")-no-ref-non-origin"
setup_remote_repo "$reponame"
clone_repo "$reponame" "$reponame-1"
git lfs track "*.dat"
echo "smudge a" > a.dat
git add .gitattributes a.dat
git commit -m "add a.dat"
git push origin master
master=$(git rev-parse master)
cd ..
git init "$reponame"
cd "$reponame"
# We intentionally pick a name that is not origin to exercise the remote
# selection code path. Since there is only one remote, we should use it
# regardless of its name
git config remote.random.url "$GITSERVER/$reponame"
git fetch "$GITSERVER/$reponame"
git checkout "$master"
[ "smudge a" = "$(cat a.dat)" ]
)
end_test