Skip to content

Commit

Permalink
fix: Check for .chezmoiversion outside .chezmoiroot
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 29, 2021
1 parent 3560b20 commit d23b428
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/chezmoi/chezmoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ const (
Prefix = ".chezmoi"

RootName = Prefix + "root"
VersionName = Prefix + "version"
dataName = Prefix + "data"
externalName = Prefix + "external"
ignoreName = Prefix + "ignore"
removeName = Prefix + "remove"
scriptsDirName = Prefix + "scripts"
templatesDirName = Prefix + "templates"
versionName = Prefix + "version"
)

var (
Expand All @@ -80,7 +80,7 @@ var knownPrefixedFiles = newStringSet(
externalName+".yaml",
ignoreName,
removeName,
versionName,
VersionName,
)

var modeTypeNames = map[fs.FileMode]string{
Expand Down
11 changes: 10 additions & 1 deletion internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ func WithLogger(logger *zerolog.Logger) SourceStateOption {
}
}

// WithMinVersion sets the minimum version.
func WithMinVersion(minVersion *semver.Version) SourceStateOption {
return func(s *SourceState) {
if minVersion != nil && s.minVersion.LessThan(*minVersion) {
s.minVersion = *minVersion
}
}
}

// WithMode sets the mode.
func WithMode(mode Mode) SourceStateOption {
return func(s *SourceState) {
Expand Down Expand Up @@ -791,7 +800,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
return err
}
return vfs.SkipDir
case fileInfo.Name() == versionName:
case fileInfo.Name() == VersionName:
return s.addVersionFile(sourceAbsPath)
case strings.HasPrefix(fileInfo.Name(), Prefix):
fallthrough
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,19 @@ func (c *Config) newSourceState(

sourceStateLogger := c.logger.With().Str(logComponentKey, logComponentValueSourceState).Logger()

versionAbsPath := c.SourceDirAbsPath.JoinString(chezmoi.VersionName)
var minVersion *semver.Version
switch data, err := c.baseSystem.ReadFile(versionAbsPath); {
case errors.Is(err, fs.ErrNotExist):
case err != nil:
return nil, err
default:
minVersion, err = semver.NewVersion(strings.TrimSpace(string(data)))
if err != nil {
return nil, fmt.Errorf("%s: %w", versionAbsPath, err)
}
}

sourceDirAbsPath, err := c.sourceDirAbsPath()
if err != nil {
return nil, err
Expand All @@ -1374,6 +1387,7 @@ func (c *Config) newSourceState(
chezmoi.WithHTTPClient(httpClient),
chezmoi.WithInterpreters(c.Interpreters),
chezmoi.WithLogger(&sourceStateLogger),
chezmoi.WithMinVersion(minVersion),
chezmoi.WithMode(c.Mode),
chezmoi.WithPriorityTemplateData(c.Data),
chezmoi.WithSourceDir(sourceDirAbsPath),
Expand Down
13 changes: 11 additions & 2 deletions internal/cmd/testdata/scripts/errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ chhome home4/user
stderr 'source state requires version'

chhome home5/user
# test that chezmoi checks .chezmoiversion when .chezmoiroot is used
! chezmoi verify
stderr 'source state requires version'

chhome home6/user

# test duplicate script detection
! chezmoi verify
Expand All @@ -37,7 +42,11 @@ stderr 'inconsistent state'
# contents of .local/share/chezmoi
-- home4/user/.local/share/chezmoi/.chezmoiversion --
3.0.0
-- home5/user/.local/share/chezmoi/run_install_packages --
-- home5/user/.local/share/chezmoi/.chezmoiversion --
3.0.0
-- home5/user/.local/share/chezmoi/.chezmoiroot --
home
-- home6/user/.local/share/chezmoi/run_install_packages --
# contents of install_packages
-- home5/user/.local/share/chezmoi/run_once_install_packages --
-- home6/user/.local/share/chezmoi/run_once_install_packages --
# contents of install_packages

0 comments on commit d23b428

Please sign in to comment.