prevent filter-branch when there are local changes (#7895)

This commit is contained in:
James Cor
2024-05-23 17:23:53 -07:00
committed by GitHub
parent a9f87d2dd9
commit 018e1c52b7
3 changed files with 60 additions and 0 deletions

View File

@@ -118,6 +118,12 @@ func (cmd FilterBranchCmd) Exec(ctx context.Context, commandStr string, args []s
queryString = string(queryStringBytes)
}
if hasChanges, err := HasLocalChanges(ctx, dEnv); err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
} else if hasChanges {
verr := errhand.BuildDError("local changes detected, use dolt stash or commit changes before using filter-branch").Build()
return HandleVErrAndExitCode(verr, usage)
}
replay := func(ctx context.Context, commit, _, _ *doltdb.Commit) (doltdb.RootValue, error) {
var cmHash, before hash.Hash
var root doltdb.RootValue

View File

@@ -789,3 +789,32 @@ func HandleVErrAndExitCode(verr errhand.VerboseError, usage cli.UsagePrinter) in
return 0
}
// HasLocalChanges compares the working and staged hash against the head hash to determine if there are uncommitted changes.
func HasLocalChanges(ctx context.Context, dEnv *env.DoltEnv) (bool, error) {
hRoot, err := dEnv.HeadRoot(ctx)
if err != nil {
return false, err
}
wRoot, err := dEnv.WorkingRoot(ctx)
if err != nil {
return false, err
}
sRoot, err := dEnv.StagedRoot(ctx)
if err != nil {
return false, err
}
hHash, err := hRoot.HashOf()
if err != nil {
return false, err
}
wHash, err := wRoot.HashOf()
if err != nil {
return false, err
}
sHash, err := sRoot.HashOf()
if err != nil {
return false, err
}
return !hHash.Equal(wHash) || !hHash.Equal(sHash), nil
}

View File

@@ -400,5 +400,30 @@ SQL
[[ "$output" =~ "does not exist" ]] || false
}
@test "filter-branch: fails with working and staged changes" {
dolt sql -q "insert into test values (3, 3)"
run dolt filter-branch -q "alter table test add column filter int"
[ "$status" -eq 1 ]
[[ "$output" =~ "local changes detected, use dolt stash or commit changes before using filter-branch" ]] || false
dolt add .
run dolt filter-branch -q "alter table test add column filter int"
[ "$status" -eq 1 ]
[[ "$output" =~ "local changes detected, use dolt stash or commit changes before using filter-branch" ]] || false
dolt commit -m "added row"
run dolt filter-branch -q "alter table test add column filter int"
[ "$status" -eq 0 ]
run dolt sql -q "select * from test as of 'HEAD'"
[ "$status" -eq 0 ]
[[ "$output" =~ "+----+----+--------+" ]] || false
[[ "$output" =~ "| pk | c0 | filter |" ]] || false
[[ "$output" =~ "+----+----+--------+" ]] || false
[[ "$output" =~ "| 0 | 0 | NULL |" ]] || false
[[ "$output" =~ "| 1 | 1 | NULL |" ]] || false
[[ "$output" =~ "| 2 | 2 | NULL |" ]] || false
[[ "$output" =~ "| 3 | 3 | NULL |" ]] || false
[[ "$output" =~ "+----+----+--------+" ]] || false
}