Skip to content

Commit

Permalink
root: move the remote guessing logic in its own function
Browse files Browse the repository at this point in the history
Cosmetic patch moving the remote guessing logic in its own function for
readability (and enabling future changes to the logic). This patch
shouldn't change lab's behaviour in any way.

Signed-off-by: Antoine Tenart <atenart@kernel.org>
  • Loading branch information
atenart committed Jan 25, 2021
1 parent fe24255 commit e1aa433
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,45 +111,55 @@ var (
forkRemote = ""
)

// Try to guess what should be the default remote.
func guessDefaultRemote() string {
guess := ""

_, err := gitconfig.Local("remote.upstream.url")
if err == nil {
guess = "upstream"
}
_, err = gitconfig.Local("remote.origin.url")
if err == nil {
guess = "origin"
}

if guess == "" {
// use the remote tracked by the default branch if set
if remote, err := gitconfig.Local("branch.main.remote"); err == nil {
guess = remote
} else if remote, err = gitconfig.Local("branch.master.remote"); err == nil {
guess = remote
} else {
// use the first remote added to .git/config file, which, usually, is
// the one from which the repo was clonned
remotesStr, err := git.GetLocalRemotesFromFile()
if err == nil {
remotes := strings.Split(remotesStr, "\n")
// remotes format: remote.<name>.<url|fetch>
remoteName := strings.Split(remotes[0], ".")[1]
guess = remoteName
}
}
}

return guess
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
// Try to gather remote information if running inside a git tree/repo.
// Otherwise, skip it, since the info won't be used at all, also avoiding
// misleading error/warning messages about missing remote.
if git.InsideGitRepo() {
_, err := gitconfig.Local("remote.upstream.url")
if err == nil {
defaultRemote = "upstream"
}
_, err = gitconfig.Local("remote.origin.url")
if err == nil {
defaultRemote = "origin"
}

defaultRemote = guessDefaultRemote()
if defaultRemote == "" {
// use the remote tracked by the default branch if set
if remote, err := gitconfig.Local("branch.main.remote"); err == nil {
defaultRemote = remote
} else if remote, err = gitconfig.Local("branch.master.remote"); err == nil {
defaultRemote = remote
} else {
// use the first remote added to .git/config file, which, usually, is
// the one from which the repo was clonned
remotesStr, err := git.GetLocalRemotesFromFile()
if err == nil {
remotes := strings.Split(remotesStr, "\n")
// remotes format: remote.<name>.<url|fetch>
remoteName := strings.Split(remotes[0], ".")[1]
defaultRemote = remoteName
} else {
log.Println("No default remote found")
}
}
log.Println("No default remote found")
}

// Check if the user fork exists
_, err = gitconfig.Local("remote." + lab.User() + ".url")
_, err := gitconfig.Local("remote." + lab.User() + ".url")
if err == nil {
forkRemote = lab.User()
} else {
Expand Down

0 comments on commit e1aa433

Please sign in to comment.