Skip to content

Commit 78cb696

Browse files
committed
fork: Allow forking from remotes other than "origin"
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
1 parent 7c2c165 commit 78cb696

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

cmd/fork.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525

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

50+
remote, project := "", ""
5051
if len(args) == 1 {
51-
forkToUpstream(args[0])
52+
if ok, _ := git.IsRemote(args[0]); ok {
53+
remote = args[0]
54+
} else {
55+
project = args[0]
56+
}
57+
}
58+
59+
if project != "" {
60+
forkToUpstream(project)
5261
return
5362
}
54-
forkFromOrigin()
63+
64+
if remote == "" {
65+
remote = "origin"
66+
}
67+
68+
project, err := git.PathWithNameSpace(remote)
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
forkFromOrigin(project)
5573
},
5674
}
5775

58-
func forkFromOrigin() {
76+
func forkFromOrigin(project string) {
5977
// Check for custom target namespace
6078
remote := lab.User()
6179
if targetData.group != "" {
@@ -82,11 +100,6 @@ func forkFromOrigin() {
82100
return
83101
}
84102

85-
project, err := git.PathWithNameSpace("origin")
86-
if err != nil {
87-
log.Fatal(err)
88-
}
89-
90103
forkRemoteURL, err := lab.Fork(project, forkOpts, useHTTP, waitFork)
91104
if err != nil {
92105
if err.Error() == "not finished" {

0 commit comments

Comments
 (0)