Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand All @@ -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
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/what.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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}}
Expand Down
20 changes: 0 additions & 20 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
)

func Execute(script string, args ...string) error {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions common/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<branch>
args: []string{"rev-parse", "--abbrev-ref", fmt.Sprintf("%s@{push}", branch)},
})
if err == nil {
return types.ParseRemoteBranch(strings.TrimSpace(out)), nil
Expand Down