Skip to content

Commit f7a7e94

Browse files
committed
amend commit staged props to get identity configuration from dolt session
1 parent 2c224ad commit f7a7e94

16 files changed

Lines changed: 299 additions & 182 deletions

File tree

go/cmd/dolt/commands/engine/sqlengine.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,33 @@ func doltSessionFactory(
545545
return nil, err
546546
}
547547

548+
// Initialize commit identity session variables from environment variables.
549+
initCommitIdentitySessionVars(doltSession)
550+
548551
return doltSession, nil
549552
}
550553
}
551554

555+
// initCommitIdentitySessionVars initializes commit identity session variables from environment variables.
556+
// This allows clients to set DOLT_AUTHOR_NAME, DOLT_AUTHOR_EMAIL, etc. in their environment and have
557+
// those values automatically available in SQL sessions.
558+
func initCommitIdentitySessionVars(sess *dsess.DoltSession) {
559+
envVarToSessionVar := map[string]string{
560+
dconfig.EnvDoltAuthorName: dsess.DoltAuthorName,
561+
dconfig.EnvDoltAuthorEmail: dsess.DoltAuthorEmail,
562+
dconfig.EnvDoltAuthorDate: dsess.DoltAuthorDate,
563+
dconfig.EnvDoltCommitterName: dsess.DoltCommitterName,
564+
dconfig.EnvDoltCommitterEmail: dsess.DoltCommitterEmail,
565+
dconfig.EnvDoltCommitterDate: dsess.DoltCommitterDate,
566+
}
567+
for envVar, sessionVar := range envVarToSessionVar {
568+
if val := os.Getenv(envVar); val != "" {
569+
// Ignore errors since these are optional session variables.
570+
_ = sess.SetSessionVariable(nil, sessionVar, val)
571+
}
572+
}
573+
}
574+
552575
type ConfigOption func(*SqlEngineConfig)
553576

554577
// NewSqlEngineForEnv returns a SqlEngine configured for the environment provided, with a single root user.

go/cmd/dolt/commands/schcmds/copy-tags.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
3232
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
3333
"github.com/dolthub/dolt/go/libraries/utils/argparser"
34-
"github.com/dolthub/dolt/go/libraries/utils/config"
3534
"github.com/dolthub/dolt/go/store/datas"
3635
"github.com/dolthub/dolt/go/store/types"
3736
)
@@ -276,18 +275,13 @@ func doltCommitUpdatedTags(ctx *sql.Context, tableResolver doltdb.TableResolver,
276275
workingSet = workingSet.WithWorkingRoot(workingRoot)
277276
workingSet = workingSet.WithStagedRoot(workingRoot)
278277

279-
email, err := dEnv.Config.GetString(config.UserEmailKey)
280-
if err != nil {
281-
return err
282-
}
283-
284-
name, err := dEnv.Config.GetString(config.UserNameKey)
278+
doltDB := dEnv.DoltDB(ctx)
279+
dSess := dsess.DSessFromSess(ctx.Session)
280+
commitStagedProps, err := dSess.NewCommitStagedPropsFromSession(ctx, "Syncing column tags from "+fromBranchName+" branch")
285281
if err != nil {
286282
return err
287283
}
288284

289-
doltDB := dEnv.DoltDB(ctx)
290-
commitStagedProps := actions.NewCommitStagedProps(name, email, datas.AuthorDate(), "Syncing column tags from "+fromBranchName+" branch")
291285
pendingCommit, err := actions.GetCommitStaged(ctx, tableResolver, roots, workingSet, nil, doltDB, commitStagedProps)
292286
if err != nil {
293287
return err
@@ -303,8 +297,8 @@ func doltCommitUpdatedTags(ctx *sql.Context, tableResolver doltdb.TableResolver,
303297
}
304298

305299
_, err = doltDB.CommitWithWorkingSet(ctx, headRef, workingSet.Ref(), pendingCommit, workingSet, prevHash, &datas.WorkingSetMeta{
306-
Name: name,
307-
Email: email,
300+
Name: commitStagedProps.Name,
301+
Email: commitStagedProps.Email,
308302
}, nil)
309303
return err
310304
}

go/cmd/dolt/commands/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ func getCommitInfoWithOptions(queryist cli.Queryist, sqlCtx *sql.Context, ref st
760760
if !ok {
761761
return nil, fmt.Errorf("unexpected type for committer email: %T", row[2])
762762
}
763-
committerTs, err := getTimestampColAsUint64(row[3])
763+
committerDate, err := getTimestampColAsUint64(row[3])
764764
if err != nil {
765765
return nil, fmt.Errorf("error parsing committer timestamp '%v': %w", row[3], err)
766766
}
@@ -797,7 +797,7 @@ func getCommitInfoWithOptions(queryist cli.Queryist, sqlCtx *sql.Context, ref st
797797
// Older Dolt versions store author metadata in the committer columns so we offer this default.
798798
authorName := committerName
799799
authorEmail := committerEmail
800-
authorTs := committerTs
800+
authorDate := committerDate
801801
if supportsAuthorColumns {
802802
authorName, ok = row[8+authorColumnsOffset].(string)
803803
if !ok {
@@ -807,19 +807,20 @@ func getCommitInfoWithOptions(queryist cli.Queryist, sqlCtx *sql.Context, ref st
807807
if !ok {
808808
return nil, fmt.Errorf("unexpected type for author email: %T", row[9+authorColumnsOffset])
809809
}
810-
authorTs, err = getTimestampColAsUint64(row[10+authorColumnsOffset])
810+
authorDate, err = getTimestampColAsUint64(row[10+authorColumnsOffset])
811811
if err != nil {
812812
return nil, fmt.Errorf("error parsing author timestamp '%v': %w", row[10+authorColumnsOffset], err)
813813
}
814814
}
815815

816+
i64AuthorDate := int64(authorDate)
816817
commitMeta := &datas.CommitMeta{
817818
Name: authorName,
818819
Email: authorEmail,
819820
Description: message,
820821
Signature: signature,
821-
Timestamp: &committerTs,
822-
UserTimestamp: int64(authorTs),
822+
Timestamp: &committerDate,
823+
UserTimestamp: &i64AuthorDate,
823824
CommitterName: committerName,
824825
CommitterEmail: committerEmail,
825826
}

go/libraries/doltcore/cherry_pick/cherry_pick.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,26 @@ func CherryPick(ctx *sql.Context, commit string, options CherryPickOptions) (str
151151
}
152152

153153
// CreateCommitStagedPropsFromCherryPickOptions converts the specified cherry-pick |options| into a CommitStagedProps
154-
// instance that can be used to create a pending commit.
154+
// instance that can be used to create a pending commit. The author identity comes from the original commit being
155+
// cherry-picked (to preserve authorship), while the committer identity comes from session/env/config.
155156
func CreateCommitStagedPropsFromCherryPickOptions(ctx *sql.Context, options CherryPickOptions, originalCommit *doltdb.Commit) (*actions.CommitStagedProps, error) {
156157
originalMeta, err := originalCommit.GetCommitMeta(ctx)
157158
if err != nil {
158159
return nil, err
159160
}
160161

161-
commitProps := actions.NewCommitStagedProps(originalMeta.Name, originalMeta.Email, originalMeta.Time(), "")
162+
// Get committer identity from session/env/config.
163+
doltSession := dsess.DSessFromSess(ctx.Session)
164+
commitProps, err := doltSession.NewCommitStagedPropsFromSession(ctx, "")
165+
if err != nil {
166+
return nil, err
167+
}
168+
169+
// Override author with original commit's author (cherry-pick preserves authorship).
170+
commitProps.Name = originalMeta.Name
171+
commitProps.Email = originalMeta.Email
172+
authorDate := originalMeta.Time()
173+
commitProps.Date = &authorDate
162174

163175
if options.CommitMessage != "" {
164176
commitProps.Message = options.CommitMessage

go/libraries/doltcore/dtestutils/testcommands/multienv.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/dolthub/dolt/go/libraries/doltcore/table"
3636
"github.com/dolthub/dolt/go/libraries/utils/config"
3737
"github.com/dolthub/dolt/go/libraries/utils/filesys"
38-
"github.com/dolthub/dolt/go/store/datas"
3938
"github.com/dolthub/dolt/go/store/types"
4039
)
4140

@@ -258,12 +257,11 @@ func (mr *MultiRepoTestSetup) CommitWithWorkingSet(dbName string) *doltdb.Commit
258257
mergeParentCommits = []*doltdb.Commit{ws.MergeState().Commit()}
259258
}
260259

261-
t := datas.CommitterDate()
262260
roots, err := dEnv.Roots(ctx)
263261
if err != nil {
264262
panic("couldn't get roots: " + err.Error())
265263
}
266-
commitStagedProps := actions.NewCommitStagedProps(name, email, t, "auto commit")
264+
commitStagedProps := actions.NewCommitStagedProps(name, email, nil, "auto commit")
267265
commitStagedProps.AllowEmpty = true
268266
pendingCommit, err := actions.GetCommitStaged(ctx, doltdb.SimpleTableResolver{}, roots, ws, mergeParentCommits, dEnv.DbData(ctx).Ddb, commitStagedProps)
269267
if err != nil {

go/libraries/doltcore/env/actions/commit.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
"github.com/dolthub/dolt/go/store/datas"
2525
)
2626

27+
// CommitStagedProps contains the parameters for a staged commit operation.
2728
type CommitStagedProps struct {
2829
Message string
29-
Date time.Time
30+
Date *time.Time
3031
AllowEmpty bool
3132
SkipEmpty bool
3233
Amend bool
@@ -40,25 +41,16 @@ type CommitStagedProps struct {
4041
}
4142

4243
// NewCommitStagedProps creates a new CommitStagedProps with the given author information. Committer fields are
43-
// automatically populated from environment variables (DOLT_COMMITTER_NAME, DOLT_COMMITTER_EMAIL, DOLT_COMMITTER_DATE)
44-
// if set, otherwise they default to the author values.
45-
func NewCommitStagedProps(name, email string, date time.Time, message string) CommitStagedProps {
46-
committerName := datas.CommitterName
47-
if committerName == "" {
48-
committerName = name
49-
}
50-
committerEmail := datas.CommitterEmail
51-
if committerEmail == "" {
52-
committerEmail = email
53-
}
54-
44+
// automatically populated with the author information. The committer date is left empty to indicate it should be
45+
// written before serialization.
46+
func NewCommitStagedProps(name, email string, date *time.Time, message string) CommitStagedProps {
5547
return CommitStagedProps{
5648
Message: message,
5749
Date: date,
5850
Name: name,
5951
Email: email,
60-
CommitterName: committerName,
61-
CommitterEmail: committerEmail,
52+
CommitterName: name,
53+
CommitterEmail: email,
6254
// CommitterDate if defined overrides time.Now or env var set by CommitterDate(). Caller is responsible for
6355
// setting this field explicitly atm.
6456
}

go/libraries/doltcore/env/actions/commitwalk/commitwalk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (q *q) Less(i, j int) bool {
8787
}
8888

8989
if cI.height == cJ.height {
90-
return cI.meta.UserTimestamp > cJ.meta.UserTimestamp
90+
return *cI.meta.UserTimestamp > *cJ.meta.UserTimestamp
9191
}
9292
return false
9393
}

go/libraries/doltcore/sqle/database_provider.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,10 @@ func (p *DoltDatabaseProvider) CreateCollatedDatabase(ctx *sql.Context, name str
609609
return fmt.Errorf("unable to get roots for database %s", name)
610610
}
611611

612-
t := ctx.QueryTime()
613-
userName := ctx.Client().User
614-
userEmail := fmt.Sprintf("%s@%s", ctx.Client().User, ctx.Client().Address)
615-
616-
commitStagedProps := actions.NewCommitStagedProps(userName, userEmail, t, "CREATE DATABASE")
612+
commitStagedProps, err := sess.NewCommitStagedPropsFromSession(ctx, "CREATE DATABASE")
613+
if err != nil {
614+
return err
615+
}
617616
pendingCommit, err := sess.NewPendingCommit(ctx, name, roots, commitStagedProps)
618617
if err != nil {
619618
return err

go/libraries/doltcore/sqle/dprocedures/dolt_commit.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/dolthub/dolt/go/libraries/doltcore/env/actions"
3030
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
3131
"github.com/dolthub/dolt/go/libraries/utils/gpg"
32-
"github.com/dolthub/dolt/go/store/datas"
3332
)
3433

3534
// doltCommit is the stored procedure version for the CLI command `dolt commit`.
@@ -111,19 +110,6 @@ func doDoltCommit(ctx *sql.Context, args []string) (string, bool, error) {
111110
}
112111
}
113112

114-
var name, email string
115-
if authorStr, ok := apr.GetValue(cli.AuthorParam); ok {
116-
name, email, err = cli.ParseAuthor(authorStr)
117-
if err != nil {
118-
return "", false, err
119-
}
120-
} else {
121-
// In SQL mode, use the current SQL user as the commit author, instead of the `dolt config` configured values.
122-
// We won't have an email address for the SQL user though, so instead use the MySQL user@address notation.
123-
name = ctx.Client().User
124-
email = fmt.Sprintf("%s@%s", ctx.Client().User, ctx.Client().Address)
125-
}
126-
127113
amend := apr.Contains(cli.AmendFlag)
128114

129115
msg, msgOk := apr.GetValue(cli.MessageArg)
@@ -143,23 +129,31 @@ func doDoltCommit(ctx *sql.Context, args []string) (string, bool, error) {
143129
}
144130
}
145131

146-
t := ctx.QueryTime()
147-
commitStagedProps := actions.NewCommitStagedProps(name, email, t, msg)
132+
commitStagedProps, err := dSess.NewCommitStagedPropsFromSession(ctx, msg)
133+
if err != nil {
134+
return "", false, err
135+
}
148136
commitStagedProps.AllowEmpty = apr.Contains(cli.AllowEmptyFlag)
149137
commitStagedProps.SkipEmpty = apr.Contains(cli.SkipEmptyFlag)
150138
commitStagedProps.Amend = amend
151139

140+
// Override author if --author flag is provided.
141+
if authorStr, ok := apr.GetValue(cli.AuthorParam); ok {
142+
name, email, err := cli.ParseAuthor(authorStr)
143+
if err != nil {
144+
return "", false, err
145+
}
146+
commitStagedProps.Name = name
147+
commitStagedProps.Email = email
148+
}
149+
150+
// Override author date if --date flag is provided.
152151
if commitTimeStr, ok := apr.GetValue(cli.DateParam); ok {
153-
var err error
154-
t, err = dconfig.ParseDate(commitTimeStr)
155-
commitStagedProps.Date = t
156-
commitStagedProps.CommitterDate = &t
152+
t, err := dconfig.ParseDate(commitTimeStr)
157153
if err != nil {
158154
return "", false, err
159155
}
160-
} else if datas.CustomAuthorDate {
161-
t = datas.AuthorDate()
162-
commitStagedProps.Date = t
156+
commitStagedProps.Date = &t
163157
}
164158

165159
if apr.Contains(cli.ForceFlag) {

go/libraries/doltcore/sqle/dprocedures/dolt_merge.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,14 @@ func executeNoFFMerge(
443443
return ws.WithStagedRoot(roots.Staged), nil, nil
444444
}
445445

446-
committerStagedProps := actions.NewCommitStagedProps(spec.Name, spec.Email, spec.Date, msg)
446+
committerStagedProps, err := dSess.NewCommitStagedPropsFromSession(ctx, msg)
447+
if err != nil {
448+
return nil, nil, err
449+
}
450+
// Override author from spec (already resolved via --author flag or session/env/config fallback).
451+
committerStagedProps.Name = spec.Name
452+
committerStagedProps.Email = spec.Email
453+
committerStagedProps.Date = &spec.Date
447454
committerStagedProps.Force = spec.Force
448455
pendingCommit, err := dSess.NewPendingCommit(ctx, dbName, roots, committerStagedProps)
449456
if err != nil {
@@ -515,18 +522,16 @@ func createMergeSpec(ctx *sql.Context, sess *dsess.DoltSession, dbName string, a
515522
}
516523

517524
func getNameAndEmail(ctx *sql.Context, apr *argparser.ArgParseResults) (string, string, error) {
518-
var err error
519-
var name, email string
520525
if authorStr, ok := apr.GetValue(cli.AuthorParam); ok {
521-
name, email, err = cli.ParseAuthor(authorStr)
522-
if err != nil {
523-
return "", "", err
524-
}
525-
} else {
526-
name = ctx.Client().User
527-
email = fmt.Sprintf("%s@%s", ctx.Client().User, ctx.Client().Address)
526+
return cli.ParseAuthor(authorStr)
527+
}
528+
// Fall back to session/env/config values.
529+
dSess := dsess.DSessFromSess(ctx.Session)
530+
props, err := dSess.NewCommitStagedPropsFromSession(ctx, "")
531+
if err != nil {
532+
return "", "", err
528533
}
529-
return name, email, nil
534+
return props.Name, props.Email, nil
530535
}
531536

532537
func mergeRootToWorking(

0 commit comments

Comments
 (0)