Skip to content

Commit

Permalink
Test reparent branch
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljs1990 committed Jul 1, 2021
1 parent 1675232 commit 75400b9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cli/prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (f *pruneCmd) run(cmd *cobra.Command, args []string) {
fmt.Println(err)
os.Exit(1)
}

_, err := git.ReparentBranches(repo, *status)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/git/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
)

type BranchesAvailableForRemovalStatus struct {
BranchesForRemoval []BranchNode
BranchesForRemoval []BranchNode
FullGraphBeforeDelete *BranchNodeWrapper
}

func BranchesAvailableForRemoval(repo *gogit.Repository) (*BranchesAvailableForRemovalStatus, error) {
Expand All @@ -20,7 +21,8 @@ func BranchesAvailableForRemoval(repo *gogit.Repository) (*BranchesAvailableForR
}

return &BranchesAvailableForRemovalStatus{
BranchesForRemoval: toDelete,
BranchesForRemoval: toDelete,
FullGraphBeforeDelete: bnw,
}, nil
}

Expand Down
65 changes: 65 additions & 0 deletions lib/git/parent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package git

import (
"fmt"

gogit "github.com/go-git/go-git/v5"
)

type ReparentBranchesStatus struct {
}

func ReparentBranches(repo *gogit.Repository,
preRemovedState BranchesAvailableForRemovalStatus) (*ReparentBranchesStatus, error) {

if len(preRemovedState.BranchesForRemoval) == 0 {
return nil, nil
}

ref, err := repo.Head()
if err != nil {
return nil, err
}

if !ref.Name().IsBranch() {
return nil, fmt.Errorf("You must be on a branch. Check if you are in a detatched state.")
}

if preRemovedState.FullGraphBeforeDelete == nil {
return nil, fmt.Errorf("No branch graph from pre delete")
}

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

fmt.Println(newParent)
}

return nil, nil
}

// Find the first upstream branch of a given node provided it doesn't exist in the deletedBranches list
func getFirstLivingParent(node *BranchNode, deletedBranches []BranchNode) string {
// This branch has no parent :(
if node.Upstream == nil {
return ""
}

if isDeletedBranch(node.Name, deletedBranches) {
return getFirstLivingParent(node.Upstream, deletedBranches)
}

return node.Name
}

func isDeletedBranch(branch string, branches []BranchNode) bool {
for _, b := range branches {
if b.Name == branch {
return true
}
}

return false
}

0 comments on commit 75400b9

Please sign in to comment.