From 66102cf090e67b80af91aea04971b5dcf71e0e8f Mon Sep 17 00:00:00 2001 From: lew Date: Thu, 18 Dec 2025 23:02:37 +0000 Subject: [PATCH] feat(GitignoreCmd): moves gitignore gen into its own command to gen separately, or --rewrite --- cmd/vcs.go | 65 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/cmd/vcs.go b/cmd/vcs.go index b287c34..c738efc 100644 --- a/cmd/vcs.go +++ b/cmd/vcs.go @@ -31,6 +31,26 @@ var vcsInitCmd = &cobra.Command{ RunE: vcsInit, } +var vcsGitignoreCmd = &cobra.Command{ + Use: "gitignore", + Short: "generates a suitable .gitignore file", + SilenceUsage: true, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + repoDir, err := ensureVCSInitialized() + if err != nil { + return err + } + + rewrite, err := cmd.Flags().GetBool("rewrite") + if err != nil { + return err + } + + return writeGitignore(repoDir, rewrite) + }, +} + var vcsSnapshotCmd = &cobra.Command{ Use: "snapshot", Short: "commit a snapshot into the vcs", @@ -165,6 +185,8 @@ var vcsPushCmd = &cobra.Command{ func init() { vcsInitCmd.Flags().Bool("clean", false, "Remove existing VCS directory before initialising") vcsCmd.AddCommand(vcsInitCmd) + vcsGitignoreCmd.Flags().BoolP("rewrite", "r", false, "Rewrite existing .gitignore, if present") + vcsCmd.AddCommand(vcsGitignoreCmd) vcsCmd.AddCommand(vcsSnapshotCmd) vcsCmd.AddCommand(vcsLogCmd) vcsPullCmd.Flags().Bool("clean", false, "Remove all existing stores before restoring snapshots") @@ -178,6 +200,7 @@ func vcsInit(cmd *cobra.Command, args []string) error { if err != nil { return err } + clean, err := cmd.Flags().GetBool("clean") if err != nil { return err @@ -221,11 +244,7 @@ func vcsInit(cmd *cobra.Command, args []string) error { return nil } - if err := writeGitignore(repoDir); err != nil { - return err - } - - return nil + return vcsGitignoreCmd.RunE(cmd, args) } func vcsRepoRoot() (string, error) { @@ -254,24 +273,34 @@ func ensureVCSInitialized() (string, error) { return repoDir, nil } -func writeGitignore(repoDir string) error { +func writeGitignore(repoDir string, rewrite bool) error { path := filepath.Join(repoDir, ".gitignore") - content := strings.Join([]string{ - "# generated by pda", - "*", - "!snapshots/", - "!snapshots/*.ndjson", - "", - }, "\n") - return os.WriteFile(path, []byte(content), 0o640) + if _, err := os.Stat(path); os.IsNotExist(err) || rewrite { + content := strings.Join([]string{ + "# generated by pda", + "*", + "!/", + "!.gitignore", + "", + }, "\n") + if err := os.WriteFile(path, []byte(content), 0o640); err != nil { + return err + } + + if err := runGit(repoDir, "add", ".gitignore"); err != nil { + return err + } + return runGit(repoDir, "commit", "--allow-empty", "-m", "generated gitignore") + } + fmt.Println("Existing .gitignore found.") + return nil } func snapshotDB(store *Store, repoDir, db string) error { - snapDir := filepath.Join(repoDir, "snapshots") - if err := os.MkdirAll(snapDir, 0o750); err != nil { + if err := os.MkdirAll(repoDir, 0o750); err != nil { return err } - target := filepath.Join(snapDir, fmt.Sprintf("%s.ndjson", db)) + target := filepath.Join(repoDir, fmt.Sprintf("%s.ndjson", db)) f, err := os.Create(target) if err != nil { return err @@ -504,7 +533,7 @@ func autoCommit(store *Store, dbs []string, message string) error { } } - if err := runGit(repoDir, "add", "snapshots"); err != nil { + if err := runGit(repoDir, "add", strings.Join(dbs, " ")); err != nil { return err }