Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parent info of children of deleted perennial branches #2540

Merged
merged 2 commits into from Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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