Skip to content

Commit

Permalink
root: make "upstream" the defaultRemote preference
Browse files Browse the repository at this point in the history
When a user fork a project from nothing the "forkToUpstream" approach is
used, where two remotes are set in the resulting git directory: "origin" and
"upstream", with obvious meanings. However, when the user fork a project
directly from a git directory, where the "origin" remote is already set,
then a new remote with user's username is set for the fork.

The current code for defaultRemote has "origin" as the dominant remote,
however, this remote has two different meanings depending on how the fork
was created: in the first approach, mentioned earlier, "origin" points to
the fork, while in the second it points to the actual upstream project. With
that, users get quite confused why `lab mr list` sometimes lists the MR
against upstream project and other times it lists MRs from the fork.

From what I've seen/used and read from the code, defaultRemote should always
point to the upstream project whenever possible. This patch does exact it.
The change is pretty simple: turn "upstream" remote as the prefereble one,
but the result is far more consistent to the user experience.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Mar 17, 2021
1 parent a897827 commit 514d2ce
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,24 @@ func guessDefaultRemote() string {

guess := ""

_, err := gitconfig.Local("remote.upstream.url")
// defaultRemote should try to always point to the upstream project.
// Since "origin" may have two different meanings depending on how the
// user forked the project, thus make "upstream" as the most significant
// remote.
// In forkFromOrigin approach, "origin" remote is the one pointing to
// the upstream project.
_, err := gitconfig.Local("remote.origin.url")
if err == nil {
guess = "upstream"
guess = "origin"
}
_, err = gitconfig.Local("remote.origin.url")
// In forkToUpstream approach, "upstream" remote is the one pointing to
// the upstream project
_, err = gitconfig.Local("remote.upstream.url")
if err == nil {
guess = "origin"
guess = "upstream"
}

// But it's still possible the user used a custom name
if guess == "" {
// use the remote tracked by the default branch if set
if remote, err := gitconfig.Local("branch.main.remote"); err == nil {
Expand Down

0 comments on commit 514d2ce

Please sign in to comment.