diff --git a/.github/aw/.gitignore b/.github/aw/.gitignore new file mode 100644 index 00000000000..a7fe32e7e32 --- /dev/null +++ b/.github/aw/.gitignore @@ -0,0 +1 @@ +imports/ diff --git a/pkg/cli/git.go b/pkg/cli/git.go index 0ca4c85b406..fd0b81f6b3c 100644 --- a/pkg/cli/git.go +++ b/pkg/cli/git.go @@ -334,6 +334,74 @@ func ensureLogsGitignore() error { return nil } +// awGitignoreEntries lists all entries that must be present in .github/aw/.gitignore. +// Note: the logs/ directory is handled separately by ensureLogsGitignore() which creates +// .github/aw/logs/.gitignore so that the directory itself remains tracked by git. +var awGitignoreEntries = []string{"imports/"} + +// ensureAwGitignore ensures that .github/aw/.gitignore exists and contains the required entries. +// If the file already exists, any missing entries are appended. +func ensureAwGitignore() error { + gitLog.Print("Ensuring .github/aw/.gitignore exists") + gitRoot, err := findGitRoot() + if err != nil { + return err + } + + awDir := filepath.Join(gitRoot, ".github", "aw") + gitignorePath := filepath.Join(awDir, ".gitignore") + + if err := os.MkdirAll(awDir, 0755); err != nil { + gitLog.Printf("Failed to create .github/aw directory: %v", err) + return fmt.Errorf("failed to create .github/aw directory: %w", err) + } + + // Read existing content (if any) + existing, readErr := os.ReadFile(gitignorePath) + existingContent := "" + if readErr == nil { + existingContent = string(existing) + gitLog.Print(".github/aw/.gitignore already exists, checking for missing entries") + } + + // Collect any entries that are not yet present + var missing []string + for _, entry := range awGitignoreEntries { + if !strings.Contains(existingContent, entry) { + missing = append(missing, entry) + } + } + + if len(missing) == 0 { + gitLog.Print(".github/aw/.gitignore already contains all required entries") + return nil + } + + // Append missing entries (ensure file ends with a newline before appending) + appendContent := "" + if existingContent != "" && !strings.HasSuffix(existingContent, "\n") { + appendContent = "\n" + } + for _, entry := range missing { + appendContent += entry + "\n" + } + + f, err := os.OpenFile(gitignorePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) + if err != nil { + gitLog.Printf("Failed to open .gitignore for writing: %v", err) + return fmt.Errorf("failed to write .github/aw/.gitignore: %w", err) + } + defer f.Close() + + if _, err := f.WriteString(appendContent); err != nil { + gitLog.Printf("Failed to write .gitignore: %v", err) + return fmt.Errorf("failed to write .github/aw/.gitignore: %w", err) + } + + gitLog.Printf("Successfully updated .github/aw/.gitignore (added: %v)", missing) + return nil +} + // getCurrentBranch gets the current git branch name func getCurrentBranch() (string, error) { gitLog.Print("Getting current git branch") diff --git a/pkg/cli/init.go b/pkg/cli/init.go index 21cb5453b3e..dcd7aa50cf4 100644 --- a/pkg/cli/init.go +++ b/pkg/cli/init.go @@ -58,6 +58,16 @@ func InitRepository(opts InitOptions) error { fmt.Fprintln(os.Stderr, console.FormatSuccessMessage("Configured .gitattributes")) } + // Ensure .github/aw/.gitignore exists with imports/ entry + initLog.Print("Ensuring .github/aw/.gitignore exists") + if err := ensureAwGitignore(); err != nil { + initLog.Printf("Failed to ensure .github/aw/.gitignore: %v", err) + return fmt.Errorf("failed to ensure .github/aw/.gitignore: %w", err) + } + if opts.Verbose { + fmt.Fprintln(os.Stderr, console.FormatSuccessMessage("Ensured .github/aw/.gitignore")) + } + // Write dispatcher agent initLog.Print("Writing agentic workflows dispatcher agent") if err := ensureAgenticWorkflowsDispatcher(opts.Verbose, false); err != nil {