Skip to content

Commit

Permalink
fork: Allow forking from remotes other than "origin"
Browse files Browse the repository at this point in the history
The fork command can be used in two ways:

 1. when passed a namespaced project name (e.g. "SomeGroup/AwesomeProject"),
    the project is forked (and optionally cloned)
 2. without arguments, the project name is determined from the "origin"
    remote in the current repository, forked and added as remote

The second mode currently doesn't work if the repository pointed to by
"origin" doesn't use the Namespace/Project structure gitlab expects.

Support that use case by also picking the second mode when the passed
argument corresponds to an existing remote.

Fixes #563
  • Loading branch information
fmuellner committed Jan 21, 2021
1 parent 7c2c165 commit 78cb696
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions cmd/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (

// forkCmd represents the fork command
var forkCmd = &cobra.Command{
Use: "fork [upstream-to-fork]",
Use: "fork [remote|upstream-to-fork]",
Short: "Fork a remote repository on GitLab and add as remote",
Long: ``,
Args: cobra.MaximumNArgs(1),
Expand All @@ -47,15 +47,33 @@ var forkCmd = &cobra.Command{
}
}

remote, project := "", ""
if len(args) == 1 {
forkToUpstream(args[0])
if ok, _ := git.IsRemote(args[0]); ok {
remote = args[0]
} else {
project = args[0]
}
}

if project != "" {
forkToUpstream(project)
return
}
forkFromOrigin()

if remote == "" {
remote = "origin"
}

project, err := git.PathWithNameSpace(remote)
if err != nil {
log.Fatal(err)
}
forkFromOrigin(project)
},
}

func forkFromOrigin() {
func forkFromOrigin(project string) {
// Check for custom target namespace
remote := lab.User()
if targetData.group != "" {
Expand All @@ -82,11 +100,6 @@ func forkFromOrigin() {
return
}

project, err := git.PathWithNameSpace("origin")
if err != nil {
log.Fatal(err)
}

forkRemoteURL, err := lab.Fork(project, forkOpts, useHTTP, waitFork)
if err != nil {
if err.Error() == "not finished" {
Expand Down

0 comments on commit 78cb696

Please sign in to comment.