Skip to content

Commit

Permalink
chore: Fix panic when .chezmoidata or .chezmoiexternal did not have a…
Browse files Browse the repository at this point in the history
…n extension
  • Loading branch information
twpayne committed Oct 12, 2021
1 parent 92ce33c commit bb5f4a3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/chezmoi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (p AbsPath) Empty() bool {
return p.absPath == ""
}

// Ext returns p's extension.
func (p AbsPath) Ext() string {
return path.Ext(p.absPath)
}

// Join appends elems to p.
func (p AbsPath) Join(elems ...RelPath) AbsPath {
elemStrs := make([]string, 0, len(elems)+1)
Expand Down
9 changes: 3 additions & 6 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ func (s *SourceState) TemplateData() map[string]interface{} {

// addExternal adds external source entries to s.
func (s *SourceState) addExternal(sourceAbsPath AbsPath) error {
parentAbsPath, name := sourceAbsPath.Split()
parentAbsPath, _ := sourceAbsPath.Split()

parentRelPath, err := parentAbsPath.TrimDirPrefix(s.sourceDirAbsPath)
if err != nil {
Expand All @@ -941,8 +941,7 @@ func (s *SourceState) addExternal(sourceAbsPath AbsPath) error {
parentSourceRelPath := NewSourceRelDirPath(parentRelPath)
parentTargetSourceRelPath := parentSourceRelPath.TargetRelPath(s.encryption.EncryptedSuffix())

suffix := mustTrimPrefix(string(name), externalName+".")
format, ok := Formats[suffix]
format, ok := Formats[strings.TrimPrefix(sourceAbsPath.Ext(), ".")]
if !ok {
return fmt.Errorf("%s: unknown format", sourceAbsPath)
}
Expand Down Expand Up @@ -1002,9 +1001,7 @@ func (s *SourceState) addPatterns(patternSet *patternSet, sourceAbsPath AbsPath,

// addTemplateData adds all template data in sourceAbsPath to s.
func (s *SourceState) addTemplateData(sourceAbsPath AbsPath) error {
_, name := sourceAbsPath.Split()
suffix := mustTrimPrefix(string(name), dataName+".")
format, ok := Formats[suffix]
format, ok := Formats[strings.TrimPrefix(sourceAbsPath.Ext(), ".")]
if !ok {
return fmt.Errorf("%s: unknown format", sourceAbsPath)
}
Expand Down

0 comments on commit bb5f4a3

Please sign in to comment.