Skip to content

Commit

Permalink
Remove parent info of children of deleted perennial branches (#2540)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Oct 5, 2023
1 parent 0c70c19 commit 505b083
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
@@ -0,0 +1,35 @@
Feature: remove parent info of children of deleted perennial branches

Background:
Given the perennial branches "old" and "other"
And a feature branch "old1" as a child of "old"
And a feature branch "old2" as a child of "old"
And a feature branch "other1" as a child of "other"
And origin deletes the "old" branch
And the current branch is "old"
When I run "git-town prune-branches"

Scenario: result
Then it runs the commands
| BRANCH | COMMAND |
| old | git fetch --prune --tags |
| | git checkout main |
| main | git branch -d old |
And the current branch is now "main"
And the branches are now
| REPOSITORY | BRANCHES |
| local, origin | main, old1, old2, other, other1 |
And the perennial branches are now "other"
And this branch lineage exists now
| BRANCH | PARENT |
| other1 | other |

Scenario: undo
When I run "git-town undo"
Then it runs the commands
| BRANCH | COMMAND |
| main | git branch old {{ sha 'Initial commit' }} |
| | git checkout old |
And the current branch is now "old"
And the initial branches and hierarchy exist
And the perennial branches are now "old" and "other"
34 changes: 19 additions & 15 deletions src/cmd/prune_branches.go
Expand Up @@ -68,12 +68,12 @@ func executePruneBranches(debug bool) error {
}

type pruneBranchesConfig struct {
branches domain.Branches
lineage config.Lineage
branchesToDelete domain.LocalBranchNames
mainBranch domain.LocalBranchName
previousBranch domain.LocalBranchName
pushHook bool
branches domain.Branches
lineage config.Lineage
branchesWithDeletedRemote domain.LocalBranchNames
mainBranch domain.LocalBranchName
previousBranch domain.LocalBranchName
pushHook bool
}

func determinePruneBranchesConfig(repo *execute.OpenRepoResult, debug bool) (*pruneBranchesConfig, domain.BranchesSnapshot, domain.StashSnapshot, bool, error) {
Expand All @@ -93,26 +93,30 @@ func determinePruneBranchesConfig(repo *execute.OpenRepoResult, debug bool) (*pr
ValidateNoOpenChanges: false,
})
return &pruneBranchesConfig{
branches: branches,
lineage: lineage,
branchesToDelete: branches.All.LocalBranchesWithDeletedTrackingBranches().Names(),
mainBranch: repo.Runner.Config.MainBranch(),
previousBranch: repo.Runner.Backend.PreviouslyCheckedOutBranch(),
pushHook: pushHook,
branches: branches,
lineage: lineage,
branchesWithDeletedRemote: branches.All.LocalBranchesWithDeletedTrackingBranches().Names(),
mainBranch: repo.Runner.Config.MainBranch(),
previousBranch: repo.Runner.Backend.PreviouslyCheckedOutBranch(),
pushHook: pushHook,
}, branchesSnapshot, stashSnapshot, exit, err
}

func pruneBranchesSteps(config *pruneBranchesConfig) steps.List {
result := steps.List{}
for _, branchWithDeletedRemote := range config.branchesToDelete {
for _, branchWithDeletedRemote := range config.branchesWithDeletedRemote {
if config.branches.Initial == branchWithDeletedRemote {
result.Add(&step.Checkout{Branch: config.mainBranch})
}
parent := config.lineage.Parent(branchWithDeletedRemote)
if !parent.IsEmpty() {
for _, child := range config.lineage.Children(branchWithDeletedRemote) {
for _, child := range config.lineage.Children(branchWithDeletedRemote) {
if parent.IsEmpty() {
result.Add(&step.DeleteParentBranch{Branch: child})
} else {
result.Add(&step.SetParent{Branch: child, ParentBranch: parent})
}
}
if config.branches.Types.IsFeatureBranch(branchWithDeletedRemote) {
result.Add(&step.DeleteParentBranch{Branch: branchWithDeletedRemote})
}
if config.branches.Types.IsPerennialBranch(branchWithDeletedRemote) {
Expand Down

0 comments on commit 505b083

Please sign in to comment.