set upstream from sql session (#4151)

This commit is contained in:
jennifersp
2022-08-22 22:27:26 -07:00
committed by GitHub
parent f477f19e56
commit c135e3eefb
5 changed files with 60 additions and 33 deletions
+2
View File
@@ -231,6 +231,8 @@ func NewPushOpts(ctx context.Context, apr *argparser.ArgParseResults, rsr RepoSt
return opts, nil
}
// NewFetchOpts returns remote and refSpec for given remote name. If remote name is not defined,
// default remote is used. Default remote is "origin" if there are multiple remotes for now.
func NewFetchOpts(args []string, rsr RepoStateReader) (Remote, []ref.RemoteRefSpec, error) {
var err error
remotes, err := rsr.GetRemotes()
@@ -76,18 +76,13 @@ func DoDoltCheckout(ctx *sql.Context, args []string) (int, error) {
return 1, fmt.Errorf("Could not load database %s", dbName)
}
roots, ok := dSess.GetRoots(ctx, dbName)
if !ok {
return 1, fmt.Errorf("Could not load database %s", dbName)
}
if newBranch, newBranchOk := apr.GetValue(cli.CheckoutCoBranch); newBranchOk {
if len(newBranch) == 0 {
err = errors.New("error: cannot checkout empty string")
} else if len(apr.Args) > 0 {
err = checkoutNewBranch(ctx, dbName, dbData, roots, newBranch, apr.Arg(0))
err = checkoutNewBranch(ctx, dbName, dbData, newBranch, apr.Arg(0))
} else {
err = checkoutNewBranch(ctx, dbName, dbData, roots, newBranch, "")
err = checkoutNewBranch(ctx, dbName, dbData, newBranch, "")
}
if err != nil {
@@ -106,9 +101,9 @@ func DoDoltCheckout(ctx *sql.Context, args []string) (int, error) {
if isBranch, err := actions.IsBranch(ctx, dbData.Ddb, name); err != nil {
return 1, err
} else if isBranch {
err = checkoutBranch(ctx, dbName, roots, dbData, name)
err = checkoutBranch(ctx, dbName, name)
if errors.Is(err, doltdb.ErrWorkingSetNotFound) {
err = checkoutRemoteBranch(ctx, dbName, dbData, roots, name)
err = checkoutRemoteBranch(ctx, dbName, dbData, name)
}
if err != nil {
return 1, err
@@ -116,9 +111,14 @@ func DoDoltCheckout(ctx *sql.Context, args []string) (int, error) {
return 0, nil
}
roots, ok := dSess.GetRoots(ctx, dbName)
if !ok {
return 1, fmt.Errorf("Could not load database %s", dbName)
}
err = checkoutTables(ctx, roots, dbName, args)
if err != nil && apr.NArg() == 1 {
err = checkoutRemoteBranch(ctx, dbName, dbData, roots, name)
err = checkoutRemoteBranch(ctx, dbName, dbData, name)
}
if err != nil {
@@ -139,7 +139,9 @@ func getRevisionForRevisionDatabase(ctx *sql.Context, dbName string) (string, st
return provider.GetRevisionForRevisionDatabase(ctx, dbName)
}
func checkoutRemoteBranch(ctx *sql.Context, dbName string, dbData env.DbData, roots doltdb.Roots, branchName string) error {
// checkoutRemoteBranch checks out a remote branch creating a new local branch with the same name as the remote branch
// and set its upstream. The upstream persists out of sql session.
func checkoutRemoteBranch(ctx *sql.Context, dbName string, dbData env.DbData, branchName string) error {
remoteRefs, err := actions.GetRemoteBranchRef(ctx, dbData.Ddb, branchName)
if err != nil {
return errors.New("fatal: unable to read from data repository")
@@ -149,7 +151,7 @@ func checkoutRemoteBranch(ctx *sql.Context, dbName string, dbData env.DbData, ro
return fmt.Errorf("error: could not find %s", branchName)
} else if len(remoteRefs) == 1 {
remoteRef := remoteRefs[0]
err = checkoutNewBranch(ctx, dbName, dbData, roots, branchName, remoteRef.String())
err := checkoutNewBranch(ctx, dbName, dbData, branchName, remoteRef.String())
if err != nil {
return err
}
@@ -162,20 +164,18 @@ func checkoutRemoteBranch(ctx *sql.Context, dbName string, dbData env.DbData, ro
src := refSpec.SrcRef(dbData.Rsr.CWBHeadRef())
dest := refSpec.DestRef(src)
err = dbData.Rsw.UpdateBranch(src.GetPath(), env.BranchConfig{
return dbData.Rsw.UpdateBranch(src.GetPath(), env.BranchConfig{
Merge: ref.MarshalableRef{
Ref: dest,
},
Remote: remoteRef.GetRemote(),
})
// TODO : set upstream should be persisted outside of session
return err
} else {
return fmt.Errorf("'%s' matched multiple (%v) remote tracking branches", branchName, len(remoteRefs))
}
}
func checkoutNewBranch(ctx *sql.Context, dbName string, dbData env.DbData, roots doltdb.Roots, branchName, startPt string) error {
func checkoutNewBranch(ctx *sql.Context, dbName string, dbData env.DbData, branchName, startPt string) error {
if len(branchName) == 0 {
return ErrEmptyBranchName
}
@@ -189,10 +189,10 @@ func checkoutNewBranch(ctx *sql.Context, dbName string, dbData env.DbData, roots
return err
}
return checkoutBranch(ctx, dbName, roots, dbData, branchName)
return checkoutBranch(ctx, dbName, branchName)
}
func checkoutBranch(ctx *sql.Context, dbName string, roots doltdb.Roots, dbData env.DbData, branchName string) error {
func checkoutBranch(ctx *sql.Context, dbName string, branchName string) error {
wsRef, err := ref.WorkingSetRefForHead(ref.NewBranchRef(branchName))
if err != nil {
return err
@@ -132,7 +132,19 @@ func (s SessionStateAdapter) GetBranches() (map[string]env.BranchConfig, error)
func (s SessionStateAdapter) UpdateBranch(name string, new env.BranchConfig) error {
s.branches[name] = new
return nil
fs, err := s.session.Provider().FileSystemForDatabase(s.dbName)
if err != nil {
return err
}
repoState, err := env.LoadRepoState(fs)
if err != nil {
return err
}
repoState.Branches[name] = new
return repoState.Save(fs)
}
func (s SessionStateAdapter) AddRemote(remote env.Remote) error {
+15 -6
View File
@@ -228,11 +228,16 @@ teardown() {
dolt sql -q "CREATE TABLE test (pk INT)"
dolt commit -am "main commit"
dolt push test-remote main
run dolt branch -a
[ "$status" -eq 0 ]
[[ ! "$output" =~ "remotes/test-remote/test-branch" ]] || false
cd ..
run dolt clone http://localhost:50051/test-org/test-repo repo2
dolt clone --remote=test-remote http://localhost:50051/test-org/test-repo repo2
run dolt branch -a
[ "$status" -eq 0 ]
[[ "$output" =~ "cloning http://localhost:50051/test-org/test-repo" ]] || false
[[ ! "$output" =~ "test-branch" ]] || false
[[ ! "$output" =~ "remotes/test-remote/test-branch" ]] || false
cd repo1
dolt checkout -b test-branch
@@ -241,8 +246,10 @@ teardown() {
dolt push test-remote test-branch
cd ../repo2
dolt fetch
# Checkout with DOLT_CHECKOUT and confirm the table has the row added in the remote
dolt fetch test-remote
run dolt branch
[[ ! "$output" =~ "test-branch" ]] || false
run dolt sql << SQL
SELECT DOLT_CHECKOUT('test-branch');
SELECT * FROM test;
@@ -252,11 +259,13 @@ SQL
[[ "$output" =~ "pk" ]] || false
[[ "$output" =~ "1" ]] || false
skip # above checkout command should set upstream persisting outside of session
run dolt branch
[[ "$output" =~ "test-branch" ]] || false
dolt checkout test-branch
run dolt status
[ "$status" -eq 0 ]
[[ "$output" =~ "branch 'test-branch' set up to track 'origin/test-branch'." ]] || false
[[ "$output" =~ "Your branch is up to date with 'test-remote/test-branch'." ]] || false
}
@test "remotes: select 'DOLT_CHECKOUT('-b','new_branch') should not set upstream if there is a remote branch with matching name" {
+12 -8
View File
@@ -159,27 +159,31 @@ teardown() {
}
@test "sql-push: dolt_push --set-upstream persists outside of session" {
skip # setting upstream run in a session should persist outside of session
cd repo1
dolt push
dolt checkout -b other
dolt sql -q "select dolt_push('-u', 'origin', 'other')"
run dolt push
[ "$status" -eq 1 ]
[[ "$output" =~ "The current branch other has no upstream branch." ]] || false
dolt sql -q "select dolt_push('-u', 'origin', 'other')"
# upstream should be set still
run dolt sql -q "select dolt_push()"
run dolt push
[ "$status" -eq 0 ]
[[ ! "$output" =~ "The current branch main has no upstream branch." ]] || false
}
@test "sql-push: CALL dolt_push --set-upstream persists outside of session" {
skip # setting upstream run in a session should persist outside of session
cd repo1
dolt push
dolt checkout -b other
dolt sql -q "CALL dolt_push('-u', 'origin', 'other')"
run dolt push
[ "$status" -eq 1 ]
[[ "$output" =~ "The current branch other has no upstream branch." ]] || false
dolt sql -q "call dolt_push('-u', 'origin', 'other')"
# upstream should be set still
run dolt sql -q "CALL dolt_push()"
run dolt push
[ "$status" -eq 0 ]
[[ ! "$output" =~ "The current branch main has no upstream branch." ]] || false
}
@test "sql-push: dolt_push --force flag" {