From c399077019fa5c061af8e9bbfbfaa1e2d571d98a Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 5 Apr 2023 17:35:51 +0200 Subject: [PATCH 1/2] fix: Allow source dir to be a git worktree --- pkg/cmd/config.go | 2 +- pkg/cmd/initcmd.go | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/config.go b/pkg/cmd/config.go index 9bef2200699..949bab81dd0 100644 --- a/pkg/cmd/config.go +++ b/pkg/cmd/config.go @@ -1876,7 +1876,7 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error FOR: for { gitDirAbsPath := workingTreeAbsPath.JoinString(gogit.GitDirName) - if fileInfo, err := c.baseSystem.Stat(gitDirAbsPath); err == nil && fileInfo.IsDir() { + if _, err := c.baseSystem.Stat(gitDirAbsPath); err == nil { c.WorkingTreeAbsPath = workingTreeAbsPath break FOR } diff --git a/pkg/cmd/initcmd.go b/pkg/cmd/initcmd.go index beb7842755a..5771259362e 100644 --- a/pkg/cmd/initcmd.go +++ b/pkg/cmd/initcmd.go @@ -137,10 +137,7 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error { // If we're not in a working tree then init it or clone it. gitDirAbsPath := c.WorkingTreeAbsPath.JoinString(git.GitDirName) - switch fileInfo, err := c.baseSystem.Stat(gitDirAbsPath); { - case err == nil && fileInfo.IsDir(): - case err == nil && !fileInfo.IsDir(): - return fmt.Errorf("%s: not a directory", gitDirAbsPath) + switch _, err := c.baseSystem.Stat(gitDirAbsPath); { case errors.Is(err, fs.ErrNotExist): workingTreeRawPath, err := c.baseSystem.RawPath(c.WorkingTreeAbsPath) if err != nil { From 45a34e4da2bd2c1b84f2b1bfd1de11a21f053cf0 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 5 Apr 2023 17:36:19 +0200 Subject: [PATCH 2/2] chore: Tidy up internal names --- pkg/chezmoi/sourcestate.go | 8 ++++---- pkg/cmd/annotation.go | 2 +- pkg/cmd/bitwardentemplatefuncs.go | 4 ++-- pkg/cmd/editcmd.go | 4 ++-- pkg/cmd/forgetcmd.go | 6 +++--- pkg/cmd/readdcmd.go | 4 ++-- pkg/cmd/testdata/scripts/issue2628.txtar | 2 +- pkg/cmd/util.go | 6 +++--- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pkg/chezmoi/sourcestate.go b/pkg/chezmoi/sourcestate.go index 7148d63dfa3..69798f3440f 100644 --- a/pkg/chezmoi/sourcestate.go +++ b/pkg/chezmoi/sourcestate.go @@ -329,7 +329,7 @@ func (s *SourceState) Add( newSourceStateEntriesByTargetRelPath := make(map[RelPath]SourceStateEntry) nonEmptyDirs := make(map[SourceRelPath]struct{}) dirRenames := make(map[AbsPath]AbsPath) -DESTABSPATH: +DEST_ABS_PATH: for _, destAbsPath := range destAbsPaths { destAbsPathInfo := destAbsPathInfos[destAbsPath] if !options.Filter.IncludeFileInfo(destAbsPathInfo) { @@ -374,7 +374,7 @@ DESTABSPATH: if options.PreAddFunc != nil { switch err := options.PreAddFunc(targetRelPath); { case errors.Is(err, Skip): - continue DESTABSPATH + continue DEST_ABS_PATH case err != nil: return err } @@ -398,7 +398,7 @@ DESTABSPATH: if options.ReplaceFunc != nil { switch err := options.ReplaceFunc(targetRelPath, newSourceStateEntry, oldSourceStateEntry); { case errors.Is(err, Skip): - continue DESTABSPATH + continue DEST_ABS_PATH case err != nil: return err } @@ -413,7 +413,7 @@ DESTABSPATH: oldSourceAbsPath := s.sourceDirAbsPath.Join(oldSourceEntryRelPath.RelPath()) newSourceAbsPath := s.sourceDirAbsPath.Join(sourceEntryRelPath.RelPath()) dirRenames[oldSourceAbsPath] = newSourceAbsPath - continue DESTABSPATH + continue DEST_ABS_PATH } // Otherwise, remove the old entry. diff --git a/pkg/cmd/annotation.go b/pkg/cmd/annotation.go index 4b19b6a6c86..48b47414860 100644 --- a/pkg/cmd/annotation.go +++ b/pkg/cmd/annotation.go @@ -17,7 +17,7 @@ var ( // Persistent state modes. const ( - persistentStateModeKey = "chezoi_persistent_state_mode" + persistentStateModeKey = "chezmoi_persistent_state_mode" persistentStateModeEmpty persistentStateMode = "empty" persistentStateModeReadOnly persistentStateMode = "read-only" diff --git a/pkg/cmd/bitwardentemplatefuncs.go b/pkg/cmd/bitwardentemplatefuncs.go index 8985005b83c..e555268defe 100644 --- a/pkg/cmd/bitwardentemplatefuncs.go +++ b/pkg/cmd/bitwardentemplatefuncs.go @@ -14,8 +14,8 @@ type bitwardenConfig struct { outputCache map[string][]byte } -func (c *Config) bitwardenAttachmentTemplateFunc(name, itemid string) string { - output, err := c.bitwardenOutput([]string{"get", "attachment", name, "--itemid", itemid, "--raw"}) +func (c *Config) bitwardenAttachmentTemplateFunc(name, itemID string) string { + output, err := c.bitwardenOutput([]string{"get", "attachment", name, "--itemid", itemID, "--raw"}) if err != nil { panic(err) } diff --git a/pkg/cmd/editcmd.go b/pkg/cmd/editcmd.go index e5ddb15c8d3..7e5de8bfa82 100644 --- a/pkg/cmd/editcmd.go +++ b/pkg/cmd/editcmd.go @@ -90,7 +90,7 @@ func (c *Config) runEditCmd(cmd *cobra.Command, args []string) error { decryptedAbsPath chezmoi.AbsPath } var transparentlyDecryptedFiles []transparentlyDecryptedFile -TARGETRELPATH: +TARGET_REL_PATH: for _, targetRelPath := range targetRelPaths { sourceStateEntry := sourceState.MustEntry(targetRelPath) sourceRelPath := sourceStateEntry.SourceRelPath() @@ -154,7 +154,7 @@ TARGETRELPATH: } if err := c.baseSystem.Link(c.SourceDirAbsPath.Join(sourceRelPath.RelPath()), hardlinkAbsPath); err == nil { editorArgs = append(editorArgs, hardlinkAbsPath.String()) - continue TARGETRELPATH + continue TARGET_REL_PATH } // Otherwise, fall through to the default option of editing the diff --git a/pkg/cmd/forgetcmd.go b/pkg/cmd/forgetcmd.go index a100df6b62f..e9204824da1 100644 --- a/pkg/cmd/forgetcmd.go +++ b/pkg/cmd/forgetcmd.go @@ -35,7 +35,7 @@ func (c *Config) runForgetCmd(cmd *cobra.Command, args []string, sourceState *ch return err } -TARGETRELPATH: +TARGET_REL_PATH: for _, targetRelPath := range targetRelPaths { sourceStateEntry := sourceState.MustEntry(targetRelPath) @@ -46,10 +46,10 @@ TARGETRELPATH: // OK, keep going. case chezmoi.SourceStateOriginRemove: c.errorf("warning: %s: cannot forget entry from remove\n", targetRelPath) - continue TARGETRELPATH + continue TARGET_REL_PATH case *chezmoi.External: c.errorf("warning: %s: cannot forget entry from external %s\n", targetRelPath, sourceStateOrigin.OriginString()) - continue TARGETRELPATH + continue TARGET_REL_PATH default: panic(fmt.Sprintf("%s: %T: unknown source state origin type", targetRelPath, sourceStateOrigin)) } diff --git a/pkg/cmd/readdcmd.go b/pkg/cmd/readdcmd.go index 89db13d9e97..4d412164520 100644 --- a/pkg/cmd/readdcmd.go +++ b/pkg/cmd/readdcmd.go @@ -50,7 +50,7 @@ func (c *Config) runReAddCmd(cmd *cobra.Command, args []string, sourceState *che }) sort.Sort(targetRelPaths) -TARGETRELPATH: +TARGET_REL_PATH: for _, targetRelPath := range targetRelPaths { sourceStateFile, ok := sourceStateEntries[targetRelPath].(*chezmoi.SourceStateFile) if !ok { @@ -118,7 +118,7 @@ TARGETRELPATH: case choice == "yes": break FOR case choice == "no": - continue TARGETRELPATH + continue TARGET_REL_PATH case choice == "all": c.interactive = false break FOR diff --git a/pkg/cmd/testdata/scripts/issue2628.txtar b/pkg/cmd/testdata/scripts/issue2628.txtar index e9e985945cb..8de6551e177 100644 --- a/pkg/cmd/testdata/scripts/issue2628.txtar +++ b/pkg/cmd/testdata/scripts/issue2628.txtar @@ -6,7 +6,7 @@ cmp $HOME/.file golden/.file chhome home2/user -# test that .chezmoignore.tmpl is read +# test that .chezmoiignore.tmpl is read exec chezmoi apply ! exists $HOME/.file diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go index 990bfa2a66d..1243005b0e9 100644 --- a/pkg/cmd/util.go +++ b/pkg/cmd/util.go @@ -111,8 +111,8 @@ func pluralize(singular string) string { return singular + "s" } -// titleize returns s with its first rune titleized. -func titleize(s string) string { +// titleFirst returns s with its first rune converted to title case. +func titleFirst(s string) string { if s == "" { return s } @@ -128,7 +128,7 @@ func upperSnakeCaseToCamelCase(s string) string { if i == 0 { words[i] = strings.ToLower(word) } else if !isWellKnownAbbreviation(word) { - words[i] = titleize(strings.ToLower(word)) + words[i] = titleFirst(strings.ToLower(word)) } } return strings.Join(words, "")