-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is similar to git's sequencer, but it's for re-stacking branches. https://github.com/git/git/blob/master/sequencer.h This object is responsible for re-stacking branches based on the planned operations.
- Loading branch information
Showing
4 changed files
with
461 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package planner | ||
|
||
import ( | ||
"github.com/aviator-co/av/internal/git" | ||
"github.com/aviator-co/av/internal/meta" | ||
"github.com/aviator-co/av/internal/sequencer" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
func PlanForRestack(tx meta.ReadTx, repo *git.Repo, targetBranches []plumbing.ReferenceName) ([]sequencer.RestackOp, error) { | ||
var ret []sequencer.RestackOp | ||
for _, br := range targetBranches { | ||
avbr, _ := tx.Branch(br.Short()) | ||
if avbr.MergeCommit != "" { | ||
// Skip rebasing branches that have merge commits. | ||
continue | ||
} | ||
ret = append(ret, sequencer.RestackOp{ | ||
Name: br, | ||
NewParent: plumbing.NewBranchReferenceName(avbr.Parent.Name), | ||
NewParentIsTrunk: avbr.Parent.Trunk, | ||
}) | ||
} | ||
return ret, nil | ||
} | ||
|
||
func PlanForSync(tx meta.ReadTx, repo *git.Repo, targetBranches []plumbing.ReferenceName, syncToTrunkInsteadOfMergeCommit bool) ([]sequencer.RestackOp, error) { | ||
var ret []sequencer.RestackOp | ||
for _, br := range targetBranches { | ||
avbr, _ := tx.Branch(br.Short()) | ||
if avbr.MergeCommit != "" { | ||
// Skip rebasing branches that have merge commits. | ||
continue | ||
} | ||
if !avbr.Parent.Trunk { | ||
// Check if the parent branch is merged. | ||
avpbr, _ := tx.Branch(avbr.Parent.Name) | ||
if avpbr.MergeCommit != "" { | ||
// The parent is merged. Sync to either trunk or merge commit. | ||
trunk, _ := meta.Trunk(tx, br.Short()) | ||
var newParentHash plumbing.Hash | ||
if syncToTrunkInsteadOfMergeCommit { | ||
// By setting this to ZeroHash, the sequencer will sync to | ||
// the remote tracking branch. | ||
newParentHash = plumbing.ZeroHash | ||
} else { | ||
newParentHash = plumbing.NewHash(avpbr.MergeCommit) | ||
} | ||
ret = append(ret, sequencer.RestackOp{ | ||
Name: br, | ||
NewParent: plumbing.NewBranchReferenceName(trunk), | ||
NewParentIsTrunk: true, | ||
NewParentHash: newParentHash, | ||
}) | ||
continue | ||
} | ||
} | ||
ret = append(ret, sequencer.RestackOp{ | ||
Name: br, | ||
NewParent: plumbing.NewBranchReferenceName(avbr.Parent.Name), | ||
NewParentIsTrunk: avbr.Parent.Trunk, | ||
}) | ||
} | ||
return ret, nil | ||
} |
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,93 @@ | ||
package planner | ||
|
||
import ( | ||
"github.com/aviator-co/av/internal/git" | ||
"github.com/aviator-co/av/internal/meta" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
type TargetBranchMode int | ||
|
||
const ( | ||
// Target all branches in the repository. | ||
AllBranches TargetBranchMode = iota | ||
// The current branch and all its predecessors. | ||
CurrentAndParents | ||
// The current branch and all its successors. | ||
CurrentAndChildren | ||
// Branches of the current stack. (The stack root and all its successors.) | ||
CurrentStack | ||
) | ||
|
||
// GetTargetBranches returns the branches to be restacked. | ||
// | ||
// If `includeStackRoots` is true, the stack root branches (the immediate children of the trunk | ||
// branches) are included in the result. | ||
func GetTargetBranches(tx meta.ReadTx, repo *git.Repo, includeStackRoots bool, mode TargetBranchMode) ([]plumbing.ReferenceName, error) { | ||
var ret []plumbing.ReferenceName | ||
if mode == AllBranches { | ||
for _, br := range tx.AllBranches() { | ||
if !br.IsStackRoot() { | ||
continue | ||
} | ||
if includeStackRoots { | ||
ret = append(ret, plumbing.NewBranchReferenceName(br.Name)) | ||
} | ||
for _, n := range meta.SubsequentBranches(tx, br.Name) { | ||
ret = append(ret, plumbing.NewBranchReferenceName(n)) | ||
} | ||
} | ||
return ret, nil | ||
} | ||
if mode == CurrentAndParents { | ||
curr, err := repo.CurrentBranchName() | ||
if err != nil { | ||
return nil, err | ||
} | ||
prevs, err := meta.PreviousBranches(tx, curr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, n := range prevs { | ||
br, _ := tx.Branch(n) | ||
if !br.IsStackRoot() || includeStackRoots { | ||
ret = append(ret, plumbing.NewBranchReferenceName(n)) | ||
} | ||
} | ||
br, _ := tx.Branch(curr) | ||
if !br.IsStackRoot() || includeStackRoots { | ||
ret = append(ret, plumbing.NewBranchReferenceName(curr)) | ||
} | ||
return ret, nil | ||
} | ||
if mode == CurrentAndChildren { | ||
curr, err := repo.CurrentBranchName() | ||
if err != nil { | ||
return nil, err | ||
} | ||
br, _ := tx.Branch(curr) | ||
if !br.IsStackRoot() || includeStackRoots { | ||
ret = append(ret, plumbing.NewBranchReferenceName(curr)) | ||
} | ||
// The rest of the branches cannot be a stack root. | ||
for _, n := range meta.SubsequentBranches(tx, curr) { | ||
ret = append(ret, plumbing.NewBranchReferenceName(n)) | ||
} | ||
return ret, nil | ||
} | ||
curr, err := repo.CurrentBranchName() | ||
if err != nil { | ||
return nil, err | ||
} | ||
brs, err := meta.StackBranches(tx, curr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, n := range brs { | ||
br, _ := tx.Branch(n) | ||
if !br.IsStackRoot() || includeStackRoots { | ||
ret = append(ret, plumbing.NewBranchReferenceName(n)) | ||
} | ||
} | ||
return ret, nil | ||
} |
Oops, something went wrong.