Files
skillshare/internal/install/gitignore.go
T
Willie 0c2d700fb9 feat(project): add --config local for centralized skills repo workflow
Enable teams to use one project as a shared skills repo where each
developer manages their own targets independently.

- Add `--config local` flag to `init -p` that gitignores config.yaml
- Auto-detect shared repo on clone via .gitignore check (no flag needed)
- Fix partialInitRepair to run interactive prompt instead of auto-select
- Export GitignoreContains, add UpdateGitIgnoreFiles for file-mode entries
- Refactor gitignore helpers to share addGitIgnoreEntries private helper
- Add 5 integration tests covering all new flows
- Add recipe docs page, update init reference, changelogs, release notes
2026-03-17 04:48:42 +08:00

331 lines
8.5 KiB
Go

package install
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
gitignoreMarkerStart = "# BEGIN SKILLSHARE MANAGED - DO NOT EDIT"
gitignoreMarkerEnd = "# END SKILLSHARE MANAGED"
)
// UpdateGitIgnore adds an entry to the .gitignore file in the given directory.
// If the entry already exists, it does nothing.
// Creates the .gitignore file if it doesn't exist.
func UpdateGitIgnore(dir, entry string) error {
gitignorePath := filepath.Join(dir, ".gitignore")
// Normalize to forward slashes (Windows paths use backslashes)
entry = strings.ReplaceAll(entry, "\\", "/")
// Ensure entry ends with / for directory
if !strings.HasSuffix(entry, "/") {
entry = entry + "/"
}
lines, err := readGitignoreLines(gitignorePath)
if err != nil {
return err
}
lines, startIdx, endIdx := ensureMarkerBlock(lines)
if containsGitignoreEntry(lines[startIdx+1:endIdx], entry) {
return nil
}
updated := make([]string, 0, len(lines)+1)
updated = append(updated, lines[:endIdx]...)
updated = append(updated, entry)
updated = append(updated, lines[endIdx:]...)
return writeGitignoreLines(gitignorePath, updated)
}
// UpdateGitIgnoreBatch adds multiple directory entries to the .gitignore file in one read/write pass.
// Each entry gets a trailing "/" appended (directory mode).
func UpdateGitIgnoreBatch(dir string, entries []string) error {
normalized := make([]string, 0, len(entries))
for _, entry := range entries {
entry = strings.ReplaceAll(entry, "\\", "/")
if !strings.HasSuffix(entry, "/") {
entry += "/"
}
normalized = append(normalized, entry)
}
return addGitIgnoreEntries(dir, normalized)
}
// UpdateGitIgnoreFiles adds file entries (not directories) to .gitignore without appending
// a trailing slash. Use this instead of UpdateGitIgnoreBatch when ignoring specific files
// like "config.yaml" rather than directories.
func UpdateGitIgnoreFiles(dir string, entries []string) error {
normalized := make([]string, 0, len(entries))
for _, entry := range entries {
normalized = append(normalized, strings.ReplaceAll(entry, "\\", "/"))
}
return addGitIgnoreEntries(dir, normalized)
}
// addGitIgnoreEntries is the shared implementation for adding entries to .gitignore.
// Entries must be pre-normalized (slashes, trailing slash policy already applied).
func addGitIgnoreEntries(dir string, entries []string) error {
gitignorePath := filepath.Join(dir, ".gitignore")
lines, err := readGitignoreLines(gitignorePath)
if err != nil {
return err
}
lines, startIdx, endIdx := ensureMarkerBlock(lines)
managed := lines[startIdx+1 : endIdx]
existing := make(map[string]struct{}, len(managed))
for _, line := range managed {
existing[strings.TrimSpace(line)] = struct{}{}
}
var newEntries []string
for _, entry := range entries {
// Check both with and without trailing slash to handle either form.
entryNoSlash := strings.TrimSuffix(entry, "/")
if _, ok := existing[entry]; ok {
continue
}
if entry != entryNoSlash {
if _, ok := existing[entryNoSlash]; ok {
continue
}
} else {
if _, ok := existing[entry+"/"]; ok {
continue
}
}
newEntries = append(newEntries, entry)
}
if len(newEntries) == 0 {
return nil
}
updated := make([]string, 0, len(lines)+len(newEntries))
updated = append(updated, lines[:endIdx]...)
updated = append(updated, newEntries...)
updated = append(updated, lines[endIdx:]...)
return writeGitignoreLines(gitignorePath, updated)
}
// RemoveFromGitIgnore removes an entry from the .gitignore file.
// Returns true if the entry was found and removed.
func RemoveFromGitIgnore(dir, entry string) (bool, error) {
gitignorePath := filepath.Join(dir, ".gitignore")
// Normalize to forward slashes (Windows paths use backslashes)
entry = strings.ReplaceAll(entry, "\\", "/")
// Ensure entry ends with / for directory
if !strings.HasSuffix(entry, "/") {
entry = entry + "/"
}
lines, err := readGitignoreLines(gitignorePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
startIdx, endIdx := findMarkerBlock(lines)
if startIdx == -1 || endIdx == -1 || startIdx >= endIdx {
return false, nil
}
found := false
for i := startIdx + 1; i < endIdx; i++ {
trimmed := strings.TrimSpace(lines[i])
if trimmed == entry || trimmed == strings.TrimSuffix(entry, "/") {
found = true
continue
}
}
if !found {
return false, nil
}
updated := make([]string, 0, len(lines))
updated = append(updated, lines[:startIdx+1]...)
for i := startIdx + 1; i < endIdx; i++ {
trimmed := strings.TrimSpace(lines[i])
if trimmed == entry || trimmed == strings.TrimSuffix(entry, "/") {
continue
}
updated = append(updated, lines[i])
}
updated = append(updated, lines[endIdx:]...)
if err := writeGitignoreLines(gitignorePath, updated); err != nil {
return false, err
}
return true, nil
}
// RemoveFromGitIgnoreBatch removes multiple entries from .gitignore in one read/write pass.
// Returns the number of entries actually removed.
func RemoveFromGitIgnoreBatch(dir string, entries []string) (int, error) {
gitignorePath := filepath.Join(dir, ".gitignore")
lines, err := readGitignoreLines(gitignorePath)
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return 0, err
}
startIdx, endIdx := findMarkerBlock(lines)
if startIdx == -1 || endIdx == -1 || startIdx >= endIdx {
return 0, nil
}
// Build set of entries to remove (both with and without trailing slash).
removeSet := make(map[string]struct{}, len(entries)*2)
for _, entry := range entries {
entry = strings.ReplaceAll(entry, "\\", "/")
if !strings.HasSuffix(entry, "/") {
entry += "/"
}
removeSet[entry] = struct{}{}
removeSet[strings.TrimSuffix(entry, "/")] = struct{}{}
}
removed := 0
updated := make([]string, 0, len(lines))
updated = append(updated, lines[:startIdx+1]...)
for i := startIdx + 1; i < endIdx; i++ {
trimmed := strings.TrimSpace(lines[i])
if _, ok := removeSet[trimmed]; ok {
removed++
continue
}
updated = append(updated, lines[i])
}
updated = append(updated, lines[endIdx:]...)
if removed == 0 {
return 0, nil
}
if err := writeGitignoreLines(gitignorePath, updated); err != nil {
return 0, err
}
return removed, nil
}
// GitignoreContains checks if an entry exists in .gitignore (with or without trailing slash).
func GitignoreContains(path, entry string) (bool, error) {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
defer f.Close()
// Also check without trailing slash
entryNoSlash := strings.TrimSuffix(entry, "/")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == entry || line == entryNoSlash {
return true, nil
}
}
return false, scanner.Err()
}
func readGitignoreLines(path string) ([]string, error) {
content, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return []string{}, nil
}
return nil, fmt.Errorf("failed to read .gitignore: %w", err)
}
normalized := strings.ReplaceAll(string(content), "\r\n", "\n")
lines := strings.Split(normalized, "\n")
for len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
return lines, nil
}
func writeGitignoreLines(path string, lines []string) error {
content := strings.Join(lines, "\n")
if content != "" {
content += "\n"
}
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to write .gitignore: %w", err)
}
return nil
}
func findMarkerBlock(lines []string) (int, int) {
startIdx := -1
endIdx := -1
for i, line := range lines {
if strings.TrimSpace(line) == gitignoreMarkerStart {
startIdx = i
break
}
}
if startIdx == -1 {
return -1, -1
}
for i := startIdx + 1; i < len(lines); i++ {
if strings.TrimSpace(lines[i]) == gitignoreMarkerEnd {
endIdx = i
break
}
}
return startIdx, endIdx
}
func ensureMarkerBlock(lines []string) ([]string, int, int) {
startIdx, endIdx := findMarkerBlock(lines)
if startIdx != -1 && endIdx != -1 && startIdx < endIdx {
return lines, startIdx, endIdx
}
if len(lines) > 0 {
lines = append(lines, "")
}
startIdx = len(lines)
lines = append(lines, gitignoreMarkerStart, gitignoreMarkerEnd)
endIdx = startIdx + 1
return lines, startIdx, endIdx
}
func containsGitignoreEntry(lines []string, entry string) bool {
entryNoSlash := strings.TrimSuffix(entry, "/")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed == entry || trimmed == entryNoSlash {
return true
}
}
return false
}