Skip to content

Commit 99c2c4b

Browse files
authored
Merge pull request #10120 from dolthub/nicktobey/commit_diff
Allow dolt_commit_diff_ to diff against HEAD
2 parents f73ec2d + 0601bbf commit 99c2c4b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

go/libraries/doltcore/sqle/database.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,11 @@ func (db Database) getTableInsensitiveWithRoot(ctx *sql.Context, head *doltdb.Co
478478
} else if err != nil {
479479
return nil, false, err
480480
}
481-
482-
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), tname, db.ddb, root, stagedRoot)
481+
headRef, err := db.rsr.CWBHeadRef(ctx)
482+
if err != nil {
483+
return nil, false, err
484+
}
485+
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), tname, db.ddb, root, stagedRoot, headRef)
483486
if err != nil {
484487
return nil, false, err
485488
}

go/libraries/doltcore/sqle/dtables/commit_diff_table.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/dolthub/go-mysql-server/sql"
2525

2626
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
27+
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
2728
"github.com/dolthub/dolt/go/libraries/doltcore/rowconv"
2829
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
2930
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
@@ -40,6 +41,7 @@ var ErrInvalidCommitDiffTableArgs = errors.New("commit_diff_<table> requires one
4041
type CommitDiffTable struct {
4142
workingRoot doltdb.RootValue
4243
stagedRoot doltdb.RootValue
44+
headRef ref.DoltRef
4345
requiredFilterErr error
4446
ddb *doltdb.DoltDB
4547
table *doltdb.Table
@@ -59,7 +61,7 @@ var _ sql.Table = (*CommitDiffTable)(nil)
5961
var _ sql.IndexAddressable = (*CommitDiffTable)(nil)
6062
var _ sql.StatisticsTable = (*CommitDiffTable)(nil)
6163

62-
func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableName, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue) (sql.Table, error) {
64+
func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableName, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue, headRef ref.DoltRef) (sql.Table, error) {
6365
diffTblName := doltdb.DoltCommitDiffTablePrefix + tblName.Name
6466

6567
var table *doltdb.Table
@@ -91,6 +93,7 @@ func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableNam
9193
ddb: ddb,
9294
workingRoot: wRoot,
9395
stagedRoot: sRoot,
96+
headRef: headRef,
9497
joiner: j,
9598
sqlSch: sqlSch,
9699
targetSchema: sch,
@@ -287,7 +290,7 @@ func (dt *CommitDiffTable) rootValForHash(ctx *sql.Context, hashStr string) (dol
287290
return nil, "", nil, err
288291
}
289292

290-
optCmt, err := dt.ddb.Resolve(ctx, cs, nil)
293+
optCmt, err := dt.ddb.Resolve(ctx, cs, dt.headRef)
291294
if err != nil {
292295
return nil, "", nil, err
293296
}

go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5428,25 +5428,36 @@ var CommitDiffSystemTableScriptTests = []queries.ScriptTest{
54285428
},
54295429
},
54305430
{
5431-
Name: "working and staged commits",
5431+
Name: "working, staged, and head commits",
54325432
SetUpScript: []string{
54335433
"create table t (pk int primary key, c1 int, c2 int);",
54345434
"call dolt_commit('-Am', 'created table');",
5435-
"set @Commit0 = HASHOF('HEAD');",
5435+
"call dolt_branch('Commit0');",
5436+
"insert into t values (7, 8, 9);",
5437+
"call dolt_commit('-Am', 'insert into table');",
5438+
"call dolt_branch('Commit1')",
54365439
"insert into t values (1, 2, 3);",
54375440
"call dolt_add('.');",
54385441
"insert into t values (4, 5, 6);",
54395442
},
54405443
Assertions: []queries.ScriptTestAssertion{
54415444
{
5442-
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;",
5445+
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';",
5446+
Expected: []sql.Row{
5447+
{1, 2, 3, nil, nil, nil, "added"},
5448+
{4, 5, 6, nil, nil, nil, "added"},
5449+
},
5450+
},
5451+
{
5452+
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~';",
54435453
Expected: []sql.Row{
54445454
{1, 2, 3, nil, nil, nil, "added"},
54455455
{4, 5, 6, nil, nil, nil, "added"},
5456+
{7, 8, 9, nil, nil, nil, "added"},
54465457
},
54475458
},
54485459
{
5449-
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;",
5460+
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';",
54505461
Expected: []sql.Row{
54515462
{1, 2, 3, nil, nil, nil, "added"},
54525463
},
@@ -5463,6 +5474,12 @@ var CommitDiffSystemTableScriptTests = []queries.ScriptTest{
54635474
{nil, nil, nil, 4, 5, 6, "removed"},
54645475
},
54655476
},
5477+
{
5478+
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';",
5479+
Expected: []sql.Row{
5480+
{7, 8, 9, nil, nil, nil, "added"},
5481+
},
5482+
},
54665483
},
54675484
},
54685485
{

0 commit comments

Comments
 (0)