Skip to content

Commit

Permalink
Merge pull request #7828 from dolthub/fulghum/init-fix
Browse files Browse the repository at this point in the history
Bug fix: Allow `dolt init` to use `--data-dir` param
  • Loading branch information
fulghum authored May 7, 2024
2 parents a85cb31 + 2933f44 commit 20de5be
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
7 changes: 6 additions & 1 deletion go/cmd/dolt/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,12 @@ func addOperation(dEnv *env.DoltEnv, setCfgTypes *set.StrSet, args []string, usa
case globalParamName:
panic("Should not have been able to get this far without a global config.")
case localParamName:
err := dEnv.Config.CreateLocalConfig(updates)
configDir, err := dEnv.FS.Abs(".")
if err != nil {
cli.PrintErrln(color.RedString("Unable to resolve current path to create repo local config file"))
return 1
}
err = dEnv.Config.CreateLocalConfig(configDir, updates)
if err != nil {
cli.PrintErrln(color.RedString("Unable to create repo local config file"))
return 1
Expand Down
6 changes: 5 additions & 1 deletion go/cmd/dolt/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func initializeConfigs(dEnv *env.DoltEnv, element env.ConfigScope) {
globalCfg, _ := dEnv.Config.GetConfig(env.GlobalConfig)
globalCfg.SetStrings(map[string]string{"title": "senior dufus"})
case env.LocalConfig:
dEnv.Config.CreateLocalConfig(map[string]string{"title": "senior dufus"})
configDir, err := dEnv.FS.Abs(".")
if err != nil {
panic("Unable to resolve current path to create repo local config file: " + err.Error())
}
dEnv.Config.CreateLocalConfig(configDir, map[string]string{"title": "senior dufus"})
}
}
func TestConfigAdd(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions go/libraries/doltcore/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ func ensureGlobalConfig(path string, fs filesys.ReadWriteFS) (config.ReadWriteCo
return config.NewFileConfig(path, fs, map[string]string{})
}

// CreateLocalConfig creates a new repository local config file. The current directory must have already been initialized
// CreateLocalConfig creates a new repository local config file with the values from |val|
// at the directory |dir|. The |dir| directory must have already been initialized
// as a data repository before a local config can be created.
func (dcc *DoltCliConfig) CreateLocalConfig(vals map[string]string) error {
return dcc.createLocalConfigAt(".", vals)
func (dcc *DoltCliConfig) CreateLocalConfig(dir string, vals map[string]string) error {
return dcc.createLocalConfigAt(dir, vals)
}

func (dcc *DoltCliConfig) createLocalConfigAt(dir string, vals map[string]string) error {
Expand Down
6 changes: 5 additions & 1 deletion go/libraries/doltcore/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,12 @@ func (dEnv *DoltEnv) createDirectories(dir string) (string, error) {
}

func (dEnv *DoltEnv) configureRepo(doltDir string) error {
err := dEnv.Config.CreateLocalConfig(map[string]string{})
configDir, err := dEnv.FS.Abs(".")
if err != nil {
return fmt.Errorf("unable to resolve current path to create repo local config file: %s", err.Error())
}

err = dEnv.Config.CreateLocalConfig(configDir, map[string]string{})
if err != nil {
return fmt.Errorf("failed creating file %s", getLocalConfigPath())
}
Expand Down
5 changes: 4 additions & 1 deletion go/libraries/doltcore/env/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func TestRepoDirNoLocal(t *testing.T) {
require.NoError(t, dEnv.CfgLoadErr)
// RSLoadErr will be set because the above method of creating the repo doesn't initialize a valid working or staged

err := dEnv.Config.CreateLocalConfig(map[string]string{"user.name": "bheni"})
configDir, err := dEnv.FS.Abs(".")
require.NoError(t, err)

err = dEnv.Config.CreateLocalConfig(configDir, map[string]string{"user.name": "bheni"})
require.NoError(t, err)

if !dEnv.HasLocalConfig() {
Expand Down
21 changes: 21 additions & 0 deletions integration-tests/bats/init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,27 @@ teardown() {
[[ $output =~ "commit dolt" ]] || [[ $output =~ "commit do1t" ]] || [[ $output =~ "commit d0lt" ]] || [[ $output =~ "commit d01t" ]] || false
}

@test "init: initialize a non-working directory with --data-dir" {
baseDir=$(pwd)
mkdir not_a_repo
mkdir repo_dir
cd not_a_repo
dolt --data-dir $baseDir/repo_dir init

# Assert that the current directory has NOT been initialized
run dolt status
[ $status -eq 1 ]
[[ $output =~ "The current directory is not a valid dolt repository" ]] || false
[ ! -d "$baseDir/not_a_repo/.dolt" ]

# Assert that ../repo_dir HAS been initialized
cd $baseDir/repo_dir
run dolt status
[ $status -eq 0 ]
[[ $output =~ "On branch main" ]] || false
[ -d "$baseDir/repo_dir/.dolt" ]
}

assert_valid_repository () {
run dolt log
[ "$status" -eq 0 ]
Expand Down

0 comments on commit 20de5be

Please sign in to comment.