Skip to content

Commit

Permalink
fork: allow user to set custom remote name
Browse files Browse the repository at this point in the history
Today, lab only allows the user to setup the <user> or "upstream" remotes in
a fork or clone process. This patch enables the option `--remote-name` (or
`-r`) for setting a custom remote name. It's useful when the user want to
have, in the same folder, different forks of the same project (possibly
for different versions, or one for general testing and another for personal
usage, or even for using with worktrees), which is not a so unusual setup
for big projects.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Mar 2, 2021
1 parent f60cf61 commit d3651ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
19 changes: 9 additions & 10 deletions cmd/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
skipClone = false
waitFork = true
remoteName string
targetData struct {
project string
group string
Expand All @@ -34,6 +35,7 @@ var forkCmd = &cobra.Command{
skipClone, _ = cmd.Flags().GetBool("skip-clone")
noWaitFork, _ := cmd.Flags().GetBool("no-wait")
waitFork = !noWaitFork
remoteName, _ = cmd.Flags().GetString("remote-name")
targetData.project, _ = cmd.Flags().GetString("name")
targetData.group, _ = cmd.Flags().GetString("group")
targetData.path, _ = cmd.Flags().GetString("path")
Expand Down Expand Up @@ -75,17 +77,10 @@ var forkCmd = &cobra.Command{

func forkFromOrigin(project string) {
// Check for custom target namespace
remote := lab.User()
if targetData.group != "" {
remote = targetData.group
}

remote := determineForkRemote(project)
if _, err := gitconfig.Local("remote." + remote + ".url"); err == nil {
log.Fatalf("remote: %s already exists", remote)
}
if _, err := gitconfig.Local("remote.upstream.url"); err == nil {
log.Fatal("remote: upstream already exists")
}

remoteURL, err := gitconfig.Local("remote.origin.url")
if err != nil {
Expand All @@ -109,8 +104,7 @@ func forkFromOrigin(project string) {
}
}

name := determineForkRemote(project)
err = git.RemoteAdd(name, forkRemoteURL, ".")
err = git.RemoteAdd(remote, forkRemoteURL, ".")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -151,6 +145,10 @@ func forkToUpstream(project string) {
}

func determineForkRemote(project string) string {
if remoteName != "" {
return remoteName
}

name := lab.User()
if targetData.group != "" {
name = targetData.group
Expand All @@ -169,6 +167,7 @@ func init() {
forkCmd.Flags().StringP("name", "n", "", "fork project with a different name")
forkCmd.Flags().StringP("group", "g", "", "fork project in a different group (namespace)")
forkCmd.Flags().StringP("path", "p", "", "fork project with a different path")
forkCmd.Flags().StringP("remote-name", "r", "", "use a custom remote name for the fork")
// useHTTP is defined in "util.go"
forkCmd.Flags().BoolVar(&useHTTP, "http", false, "fork using HTTP protocol instead of SSH")
RootCmd.AddCommand(forkCmd)
Expand Down
7 changes: 5 additions & 2 deletions cmd/fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,18 @@ func Test_forkWait(t *testing.T) {
func Test_determineForkRemote(t *testing.T) {
tests := []struct {
desc string
custom string
project string
expected string
}{
{"project is forked from repo", "zaquestion", "lab-testing"},
{"project is user", "lab-testing", "upstream"},
{"project is forked from repo", "", "zaquestion", "lab-testing"},
{"project is user", "", "lab-testing", "upstream"},
{"project is user", "custom-test", "lab-testing", "custom-test"},
}

for _, test := range tests {
test := test
remoteName = test.custom
t.Run(test.desc, func(t *testing.T) {
require.Equal(t, test.expected, determineForkRemote(test.project))
})
Expand Down

0 comments on commit d3651ca

Please sign in to comment.