Skip to content

Commit

Permalink
Working graft command
Browse files Browse the repository at this point in the history
Might have some rough edges but works fine for what I need right now.
  • Loading branch information
michaeljs1990 committed Jul 4, 2021
1 parent a6b0573 commit 661a51e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/git/graft.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,28 @@ func Graft(repo *gogit.Repository, bnw *BranchNodeWrapper,
}

// Checkout a new branch
// TODO: this
if localBranchName == "" {
localBranchName = fmt.Sprintf("graft/%s", branch)
}
err = CheckoutTrackRaw(localBranchName, ref.Name().Short())
if err != nil {
return fmt.Errorf("Unable to checkout branch it might already exist: %s", err)
}

// Apply all commits to the current branch
mergePoint, err := MergeBase(ref.Name().Short(), dest)
if err != nil {
DeleteBranch(localBranchName)
return fmt.Errorf("No common merge-point: %s", err)
}

fmt.Println(mergePoint)
err = CherryPick(mergePoint, dest)
if err != nil {
// Try to cleanup the working dir
CherryPickAbort()
DeleteBranch(localBranchName)
return fmt.Errorf("Uable to cherry-pick we tried to abort for you: %s", err)
}

return nil
}
40 changes: 39 additions & 1 deletion lib/git/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os/exec"
"strconv"
"strings"

"github.com/pinkluz/arcanist/lib/util"
)

// Wrapper around raw git commands. These should be replaced when a go-git version
Expand Down Expand Up @@ -167,6 +169,42 @@ func MergeBase(refOne string, refTwo string) (string, error) {
return "", fmt.Errorf(string(output))
}

return string(output), nil
// Windox/Nix compatible new line removal
lines := util.SplitLines(string(output))

if len(lines) < 1 {
return "", fmt.Errorf("Merge base didn't return any output")
}

return lines[0], nil
}

func CherryPick(refOne string, refTwo string) error {
cmd := exec.Command("git", "cherry-pick", fmt.Sprintf("%s..%s", refOne, refTwo))

output, err := cmd.CombinedOutput()
if err != nil {
return err
}

if cmd.ProcessState.ExitCode() != 0 {
return fmt.Errorf(string(output))
}

return nil
}

func CherryPickAbort() error {
cmd := exec.Command("git", "cherry-pick", "--abort")

output, err := cmd.CombinedOutput()
if err != nil {
return err
}

if cmd.ProcessState.ExitCode() != 0 {
return fmt.Errorf(string(output))
}

return nil
}

0 comments on commit 661a51e

Please sign in to comment.