feat(GitignoreCmd): moves gitignore gen into its own command to gen separately, or --rewrite

This commit is contained in:
Lewis Wynne 2025-12-18 23:02:37 +00:00
parent e6d39e84d8
commit 66102cf090

View file

@ -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")
if _, err := os.Stat(path); os.IsNotExist(err) || rewrite {
content := strings.Join([]string{
"# generated by pda",
"*",
"!snapshots/",
"!snapshots/*.ndjson",
"!/",
"!.gitignore",
"",
}, "\n")
return os.WriteFile(path, []byte(content), 0o640)
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
}