Merge pull request #10120 from dolthub/nicktobey/commit_diff

Allow dolt_commit_diff_ to diff against HEAD
This commit is contained in:
Nick Tobey
2025-12-04 18:40:38 -08:00
committed by GitHub
3 changed files with 31 additions and 8 deletions
+5 -2
View File
@@ -478,8 +478,11 @@ func (db Database) getTableInsensitiveWithRoot(ctx *sql.Context, head *doltdb.Co
} else if err != nil {
return nil, false, err
}
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), tname, db.ddb, root, stagedRoot)
headRef, err := db.rsr.CWBHeadRef(ctx)
if err != nil {
return nil, false, err
}
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), tname, db.ddb, root, stagedRoot, headRef)
if err != nil {
return nil, false, err
}
@@ -24,6 +24,7 @@ import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
"github.com/dolthub/dolt/go/libraries/doltcore/rowconv"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
@@ -40,6 +41,7 @@ var ErrInvalidCommitDiffTableArgs = errors.New("commit_diff_<table> requires one
type CommitDiffTable struct {
workingRoot doltdb.RootValue
stagedRoot doltdb.RootValue
headRef ref.DoltRef
requiredFilterErr error
ddb *doltdb.DoltDB
table *doltdb.Table
@@ -59,7 +61,7 @@ var _ sql.Table = (*CommitDiffTable)(nil)
var _ sql.IndexAddressable = (*CommitDiffTable)(nil)
var _ sql.StatisticsTable = (*CommitDiffTable)(nil)
func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableName, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue) (sql.Table, error) {
func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableName, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue, headRef ref.DoltRef) (sql.Table, error) {
diffTblName := doltdb.DoltCommitDiffTablePrefix + tblName.Name
var table *doltdb.Table
@@ -91,6 +93,7 @@ func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableNam
ddb: ddb,
workingRoot: wRoot,
stagedRoot: sRoot,
headRef: headRef,
joiner: j,
sqlSch: sqlSch,
targetSchema: sch,
@@ -287,7 +290,7 @@ func (dt *CommitDiffTable) rootValForHash(ctx *sql.Context, hashStr string) (dol
return nil, "", nil, err
}
optCmt, err := dt.ddb.Resolve(ctx, cs, nil)
optCmt, err := dt.ddb.Resolve(ctx, cs, dt.headRef)
if err != nil {
return nil, "", nil, err
}
@@ -5428,25 +5428,36 @@ var CommitDiffSystemTableScriptTests = []queries.ScriptTest{
},
},
{
Name: "working and staged commits",
Name: "working, staged, and head commits",
SetUpScript: []string{
"create table t (pk int primary key, c1 int, c2 int);",
"call dolt_commit('-Am', 'created table');",
"set @Commit0 = HASHOF('HEAD');",
"call dolt_branch('Commit0');",
"insert into t values (7, 8, 9);",
"call dolt_commit('-Am', 'insert into table');",
"call dolt_branch('Commit1')",
"insert into t values (1, 2, 3);",
"call dolt_add('.');",
"insert into t values (4, 5, 6);",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "SELECT to_pk, to_c1, to_c2, from_pk, from_c1, from_c2, diff_type FROM DOLT_COMMIT_DIFF_t WHERE TO_COMMIT='WORKING' and FROM_COMMIT=@Commit0;",
Query: "SELECT to_pk, to_c1, to_c2, from_pk, from_c1, from_c2, diff_type FROM DOLT_COMMIT_DIFF_t WHERE TO_COMMIT='WORKING' and FROM_COMMIT='HEAD';",
Expected: []sql.Row{
{1, 2, 3, nil, nil, nil, "added"},
{4, 5, 6, nil, nil, nil, "added"},
},
},
{
Query: "SELECT to_pk, to_c1, to_c2, from_pk, from_c1, from_c2, diff_type FROM DOLT_COMMIT_DIFF_t WHERE TO_COMMIT='STAGED' and FROM_COMMIT=@Commit0;",
Query: "SELECT to_pk, to_c1, to_c2, from_pk, from_c1, from_c2, diff_type FROM DOLT_COMMIT_DIFF_t WHERE TO_COMMIT='WORKING' and FROM_COMMIT='HEAD~';",
Expected: []sql.Row{
{1, 2, 3, nil, nil, nil, "added"},
{4, 5, 6, nil, nil, nil, "added"},
{7, 8, 9, nil, nil, nil, "added"},
},
},
{
Query: "SELECT to_pk, to_c1, to_c2, from_pk, from_c1, from_c2, diff_type FROM DOLT_COMMIT_DIFF_t WHERE TO_COMMIT='STAGED' and FROM_COMMIT='HEAD';",
Expected: []sql.Row{
{1, 2, 3, nil, nil, nil, "added"},
},
@@ -5463,6 +5474,12 @@ var CommitDiffSystemTableScriptTests = []queries.ScriptTest{
{nil, nil, nil, 4, 5, 6, "removed"},
},
},
{
Query: "SELECT to_pk, to_c1, to_c2, from_pk, from_c1, from_c2, diff_type FROM DOLT_COMMIT_DIFF_t AS OF Commit1 WHERE TO_COMMIT='HEAD' and FROM_COMMIT='Commit0';",
Expected: []sql.Row{
{7, 8, 9, nil, nil, nil, "added"},
},
},
},
},
{