Skip to content

Commit

Permalink
Reparent branches
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljs1990 committed Jul 1, 2021
1 parent 75400b9 commit 62450f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/git/parent.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ func ReparentBranches(repo *gogit.Repository,

localRepoPreDelete := preRemovedState.FullGraphBeforeDelete.BranchMap
for _, deletedBranch := range preRemovedState.BranchesForRemoval {
rmedNode := localRepoPreDelete[deletedBranch.Name]
newParent := getFirstLivingParent(rmedNode, preRemovedState.BranchesForRemoval)

fmt.Println(newParent)
rmedNode := localRepoPreDelete[deletedBranch.Name]
for _, upstream := range rmedNode.Downstream {

newParent := getFirstLivingParent(upstream.Upstream, preRemovedState.BranchesForRemoval)
if newParent != "" && !isDeletedBranch(upstream.Name, preRemovedState.BranchesForRemoval) {
err := SetBranchUpstream(upstream.Name, newParent)
if err != nil {
return nil, fmt.Errorf("Unable to set git branch -u %s %s"+err.Error(), newParent, upstream.Name)
}
}
}
}

return nil, nil
return &ReparentBranchesStatus{}, nil
}

// Find the first upstream branch of a given node provided it doesn't exist in the deletedBranches list
Expand Down
16 changes: 16 additions & 0 deletions lib/git/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ func DeleteBranch(branch string) error {

return nil
}

func SetBranchUpstream(branch string, upstream string) error {
// cmd := exec.Command("git", "branch", "--delete", branch)
cmd := exec.Command("git", "branch", "-u", upstream, branch)

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 62450f7

Please sign in to comment.