diff --git a/cmd/rm.go b/cmd/rm.go index a421483..041a790 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -5,7 +5,9 @@ import ( "errors" "fmt" "github.com/barrettj12/jit/common/env" + "github.com/barrettj12/jit/common/git" "github.com/barrettj12/jit/common/path" + "github.com/barrettj12/jit/common/types" "github.com/spf13/cobra" "os" "strings" @@ -27,6 +29,7 @@ func Remove(cmd *cobra.Command, args []string) error { return err } + // TODO: work out which branch is tracking (could be different name) split := strings.SplitN(branch, ":", 2) if len(split) >= 2 { branch = split[1] @@ -45,19 +48,22 @@ func Remove(cmd *cobra.Command, args []string) error { // TODO: need to be able to handle branches with "/" in the name // $ jit rm imerge/3.3 // Delete remote tracking branch barrettj12/imerge? [y/n] - remote, remoteBranch, err := common.PushLoc(branch) - switch err { - case common.ErrUpstreamNotFound: + localBranch := types.LocalBranch(branch) + remoteBranch, err := git.PushTarget(localBranch) + if remoteBranch == types.NoRemote { // no-op fmt.Printf("no remote tracking branch found for branch %q\n", branch) + } + switch err { case nil: - ok, err := confirm(fmt.Sprintf("Delete remote tracking branch %s/%s", remote, remoteBranch)) + ok, err := confirm(fmt.Sprintf("Delete remote tracking branch %q", remoteBranch)) if err != nil { return err } if ok { - err = common.Git("push", "-d", remote, remoteBranch) + // TODO: replace with git.Push + err = common.Git("push", "-d", string(remoteBranch.Remote), remoteBranch.Branch) if err != nil { return err } diff --git a/cmd/what.go b/cmd/what.go index 6c613f1..04a48f2 100644 --- a/cmd/what.go +++ b/cmd/what.go @@ -2,6 +2,8 @@ package cmd import ( "fmt" + "github.com/barrettj12/jit/common/git" + "github.com/barrettj12/jit/common/types" "github.com/spf13/cobra" "os" @@ -21,20 +23,22 @@ func What(cmd *cobra.Command, args []string) error { return err } - remote, remoteBranch, err := common.PushLoc(localBranch) + remoteBranch, err := git.PushTarget(types.LocalBranch(localBranch)) if err != nil { - fmt.Printf("ERROR: %v\n", err) + fmt.Printf("WARNING: couldn't get remote: %v\n", err) // Just default to the local branch name defaultRemote, _ := common.DefaultRemote() - remote = string(defaultRemote) - remoteBranch = localBranch - fmt.Printf("assuming remote branch is %s:%s\n\n", remote, remoteBranch) + remoteBranch = types.RemoteBranch{ + Remote: defaultRemote, + Branch: localBranch, + } + fmt.Printf("assuming remote branch is %q\n\n", remoteBranch) } res := common.Exec(common.ExecArgs{ Cmd: "gh", Args: []string{ - "pr", "view", fmt.Sprintf("%s:%s", remote, remoteBranch), + "pr", "view", fmt.Sprintf("%s:%s", remoteBranch.Remote, remoteBranch.Branch), "--json", "title,state,headRefName,baseRefName,url", "-t", ` {{.title}} {{.state}}: {{.headRefName}} -> {{.baseRefName}} diff --git a/common/common.go b/common/common.go index b66d841..23e9c45 100644 --- a/common/common.go +++ b/common/common.go @@ -9,7 +9,6 @@ import ( "os" "os/exec" "path/filepath" - "strings" ) func Execute(script string, args ...string) error { @@ -82,25 +81,6 @@ func WorktreePath(branch string) (string, error) { return filepath.Join(gitDir.Path(), branch), nil } -var ErrUpstreamNotFound = fmt.Errorf("upstream not found") - -// Returns push location (remote, branch) for the given branch -// TODO: replace this with git.PushTarget -func PushLoc(localBranch string) (remote, remoteBranch string, err error) { - stdout, err := ExecGit(path.CurrentDir, "for-each-ref", "--format='%(push:short)'", - fmt.Sprintf("refs/heads/%s", localBranch)) - if err != nil { - return "", "", err - } - - pushloc := strings.Trim(stdout, "'\n") - if pushloc == "" { - return "", "", ErrUpstreamNotFound - } - split := strings.Split(pushloc, "/") - return split[0], split[1], nil -} - func DefaultRemote() (types.RemoteName, error) { ghUser, err := env.GitHubUser() if err != nil { diff --git a/common/git/branch.go b/common/git/branch.go index 7b54618..62b7154 100644 --- a/common/git/branch.go +++ b/common/git/branch.go @@ -31,8 +31,8 @@ func CreateBranch(name, base types.LocalBranch) error { // A return value of "" means no upstream is set. func PushTarget(branch types.LocalBranch) (types.RemoteBranch, error) { out, err := internalExec(internalExecArgs{ - args: []string{"rev-parse", "--abbrev-ref", - fmt.Sprintf("%s@{push}", branch)}, + // alternative: git for-each-ref --format='%(push:short)' refs/heads/ + args: []string{"rev-parse", "--abbrev-ref", fmt.Sprintf("%s@{push}", branch)}, }) if err == nil { return types.ParseRemoteBranch(strings.TrimSpace(out)), nil