Merge pull request #4967 from dolthub/andy/filter-branch-tags

go/commands: `filter-branch --all` now rewrites tags, added `--branches` flag
This commit is contained in:
AndyA
2022-12-12 12:47:34 -08:00
committed by GitHub
3 changed files with 104 additions and 33 deletions

View File

@@ -43,7 +43,8 @@ import (
)
const (
dbName = "filterDB"
dbName = "filterDB"
branchesFlag = "branches"
)
var filterBranchDocs = cli.CommandDocumentationContent{
@@ -52,7 +53,9 @@ var filterBranchDocs = cli.CommandDocumentationContent{
If a {{.LessThan}}commit-spec{{.GreaterThan}} is provided, the traversal will stop when the commit is reached and rewriting will begin at that commit, or will error if the commit is not found.
If the {{.EmphasisLeft}}--all{{.EmphasisRight}} flag is supplied, the traversal starts with the HEAD commits of all branches.
If the {{.EmphasisLeft}}--branches{{.EmphasisRight}} flag is supplied, filter-branch traverses and rewrites commits for all branches.
If the {{.EmphasisLeft}}--all{{.EmphasisRight}} flag is supplied, filter-branch traverses and rewrites commits for all branches and tags.
`,
Synopsis: []string{
@@ -81,8 +84,9 @@ func (cmd FilterBranchCmd) Docs() *cli.CommandDocumentation {
func (cmd FilterBranchCmd) ArgParser() *argparser.ArgParser {
ap := argparser.NewArgParser()
ap.SupportsFlag(allFlag, "a", "filter all branches")
ap.SupportsFlag(verboseFlag, "v", "logs more information")
ap.SupportsFlag(branchesFlag, "b", "filter all branches")
ap.SupportsFlag(allFlag, "a", "filter all branches and tags")
return ap
}
@@ -153,9 +157,12 @@ func (cmd FilterBranchCmd) Exec(ctx context.Context, commandStr string, args []s
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}
if apr.Contains(allFlag) {
switch {
case apr.Contains(branchesFlag):
err = rebase.AllBranches(ctx, dEnv, replay, nerf)
} else {
case apr.Contains(allFlag):
err = rebase.AllBranchesAndTags(ctx, dEnv, replay, nerf)
default:
err = rebase.CurrentBranch(ctx, dEnv, replay, nerf)
}
if err != nil {

View File

@@ -87,6 +87,20 @@ func wrapReplayRootFn(fn ReplayRootFn) ReplayCommitFn {
}
}
// AllBranchesAndTags rewrites the history of all branches and tags in the repo using the |replay| function.
func AllBranchesAndTags(ctx context.Context, dEnv *env.DoltEnv, replay ReplayCommitFn, nerf NeedsRebaseFn) error {
branches, err := dEnv.DoltDB.GetBranches(ctx)
if err != nil {
return err
}
tags, err := dEnv.DoltDB.GetTags(ctx)
if err != nil {
return err
}
return rebaseRefs(ctx, dEnv.DbData(), replay, nerf, append(branches, tags...)...)
}
// AllBranches rewrites the history of all branches in the repo using the |replay| function.
func AllBranches(ctx context.Context, dEnv *env.DoltEnv, replay ReplayCommitFn, nerf NeedsRebaseFn) error {
branches, err := dEnv.DoltDB.GetBranches(ctx)
@@ -121,11 +135,6 @@ func CurrentBranchByRoot(ctx context.Context, dEnv *env.DoltEnv, replay ReplayRo
func rebaseRefs(ctx context.Context, dbData env.DbData, replay ReplayCommitFn, nerf NeedsRebaseFn, refs ...ref.DoltRef) error {
ddb := dbData.Ddb
rsr := dbData.Rsr
rsw := dbData.Rsw
cwbRef := rsr.CWBHeadRef()
heads := make([]*doltdb.Commit, len(refs))
for i, dRef := range refs {
var err error
@@ -140,41 +149,30 @@ func rebaseRefs(ctx context.Context, dbData env.DbData, replay ReplayCommitFn, n
return err
}
for i, dRef := range refs {
switch dRef.(type) {
for i, r := range refs {
switch dRef := r.(type) {
case ref.BranchRef:
err = ddb.NewBranchAtCommit(ctx, dRef, newHeads[i])
if err != nil {
case ref.TagRef:
// rewrite tag with new commit
var tag *doltdb.Tag
if tag, err = ddb.ResolveTag(ctx, dRef); err != nil {
return err
}
if err = ddb.DeleteTag(ctx, dRef); err != nil {
return err
}
err = ddb.NewTagAtCommit(ctx, dRef, newHeads[i], tag.Meta)
default:
return fmt.Errorf("cannot rebase ref: %s", ref.String(dRef))
}
if err != nil {
return err
}
}
cm, err := ddb.ResolveCommitRef(ctx, cwbRef)
if err != nil {
return err
}
r, err := cm.GetRootValue(ctx)
if err != nil {
return err
}
// TODO: this should be a single update to repo state, not two
err = rsw.UpdateStagedRoot(ctx, r)
if err != nil {
return err
}
return rsw.UpdateWorkingRoot(ctx, r)
return nil
}
func rebase(ctx context.Context, ddb *doltdb.DoltDB, replay ReplayCommitFn, nerf NeedsRebaseFn, origins ...*doltdb.Commit) ([]*doltdb.Commit, error) {

View File

@@ -83,6 +83,72 @@ teardown() {
[[ "$output" =~ "4,4" ]] || false
}
@test "filter-branch: filter tags" {
dolt sql <<SQL
create table t (pk int primary key);
insert into t values (1),(2);
call dcommit('-Am', 'msg');
insert into t values (3);
call dcommit('-Am', 'three');
call dtag('myTag');
insert into t values (4);
call dcommit('-Am', 'four');
SQL
run dolt sql -q "select * from t as of 'myTag'" -r csv
[ "$status" -eq 0 ]
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ "$output" =~ "3" ]] || false
dolt filter-branch --all "delete from t where pk >= 3"
run dolt sql -q "select * from t as of 'myTag'" -r csv
[ "$status" -eq 0 ]
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ ! "$output" =~ "3" ]] || false
}
@test "filter-branch: filter branches only" {
dolt sql <<SQL
create table t (pk int primary key);
insert into t values (1),(2);
call dcommit('-Am', 'msg');
insert into t values (3);
call dcommit('-Am', 'three');
call dtag('myTag');
insert into t values (4);
call dcommit('-Am', 'four');
SQL
run dolt sql -q "select * from t" -r csv
[ "$status" -eq 0 ]
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ "$output" =~ "3" ]] || false
[[ "$output" =~ "4" ]] || false
run dolt sql -q "select * from t as of 'myTag'" -r csv
[ "$status" -eq 0 ]
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ "$output" =~ "3" ]] || false
dolt filter-branch --branches "delete from t where pk >= 3"
run dolt sql -q "select * from t" -r csv
[ "$status" -eq 0 ]
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ ! "$output" =~ "3" ]] || false
[[ ! "$output" =~ "4" ]] || false
run dolt sql -q "select * from t as of 'myTag'" -r csv
[ "$status" -eq 0 ]
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ "$output" =~ "3" ]] || false
}
@test "filter-branch: with missing table" {
dolt sql -q "DROP TABLE test;"
dolt add -A && dolt commit -m "dropped test"