From 62450f7757ec121698331095e947ae8eb68c824e Mon Sep 17 00:00:00 2001 From: Michael Schuett Date: Thu, 1 Jul 2021 18:34:35 -0500 Subject: [PATCH] Reparent branches --- lib/git/parent.go | 16 ++++++++++++---- lib/git/raw.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) 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 +}