-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1675232
commit 75400b9
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |