Skip to content

Commit

Permalink
fix: Ensure the sourceDir is updated before creating config file
Browse files Browse the repository at this point in the history
  • Loading branch information
4ooc authored and twpayne committed May 11, 2023
1 parent 08e8ebd commit a06f766
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
10 changes: 10 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,16 @@ func (c *Config) colorAutoFunc() bool {
// createAndReloadConfigFile creates a config file if it there is a config file
// template and reloads it.
func (c *Config) createAndReloadConfigFile(cmd *cobra.Command) error {
// Refresh the source directory, as there might be a .chezmoiroot file and
// the template data is set before .chezmoiroot is read.
sourceDirAbsPath, err := c.getSourceDirAbsPath(&getSourceDirAbsPathOptions{
refresh: true,
})
if err != nil {
return err
}
c.templateData.sourceDir = sourceDirAbsPath

// Find config template, execute it, and create config file.
configTemplate, err := c.findConfigTemplate()
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions pkg/cmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,6 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
return err
}

var err error
c.SourceDirAbsPath, err = c.getSourceDirAbsPath(&getSourceDirAbsPathOptions{
refresh: true,
})
if err != nil {
return err
}

if err := c.createAndReloadConfigFile(cmd); err != nil {
return err
}
Expand Down
37 changes: 29 additions & 8 deletions pkg/cmd/testdata/scripts/issue2865.txtar
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
[windows] skip 'UNIX only'
[!exec:git] skip 'git not found in $PATH'

# test that .chezmoi.sourceDir is set correctly when .chezmoiroot is present
exec chezmoi execute-template '{{ .chezmoi.sourceDir }}'
stdout ${CHEZMOISOURCEDIR@R}/home
mkgitconfig

# test that .chezmoi.sourceDir is set correctly in chezmoi apply
exec chezmoi apply
stdout ${CHEZMOISOURCEDIR@R}/home
# create a commit
exec chezmoi git init
exec chezmoi git add .
exec chezmoi git commit -- --message 'Initial commit'

chhome home2/user

# test that .chezmoi.sourceDir is set correctly when .chezmoiroot is present (chezmoi init --apply in a clean home directory)
mkgitconfig
exec chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
stdout '/.local/share/chezmoi/home$'

# test that .chezmoi.sourceDir is set correctly in config file
exec chezmoi execute-template '{{ .testDir }}'
stdout '/.local/share/chezmoi/home$'

-- home/user/.local/share/chezmoi/.chezmoiroot --
home
-- home/user/.local/share/chezmoi/home/.chezmoi.toml.tmpl --
[data]
testDir = {{ .chezmoi.sourceDir | quote }}
-- home/user/.local/share/chezmoi/home/.chezmoiignore --
{{- if eq .chezmoi.os "windows" }}
.chezmoiscripts/echo.sh
{{- else }}
.chezmoiscripts/echo.ps1
{{- end }}
-- home/user/.local/share/chezmoi/home/.chezmoiscripts/run_once_echo.ps1.tmpl --
Write-Host {{ .chezmoi.sourceDir | quote }}
-- home/user/.local/share/chezmoi/home/.chezmoiscripts/run_once_echo.sh.tmpl --
#!/bin/sh

echo {{ .chezmoi.sourceDir }}
echo {{ .chezmoi.sourceDir | quote }}

0 comments on commit a06f766

Please sign in to comment.