diff --git a/lib/git/parent.go b/lib/git/parent.go index 22bef80..2fe623d 100644 --- a/lib/git/parent.go +++ b/lib/git/parent.go @@ -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 diff --git a/lib/git/raw.go b/lib/git/raw.go index 0374c02..7a07593 100644 --- a/lib/git/raw.go +++ b/lib/git/raw.go @@ -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 +}