Making dolt init work with the --data-dir param

This commit is contained in:
Jason Fulghum
2024-05-06 09:15:13 -07:00
parent 9d9eaf26c5
commit a9e8fc94ed
6 changed files with 45 additions and 7 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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())
}

View File

@@ -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() {

View File

@@ -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 ]