-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitignore.go
More file actions
69 lines (57 loc) · 1.39 KB
/
Copy pathgitignore.go
File metadata and controls
69 lines (57 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package utils
import (
"bufio"
"os"
"path/filepath"
"strings"
)
const oopsEntry = ".oops/"
// EnsureGitignore adds .oops/ to .gitignore if it exists and doesn't have the entry
func EnsureGitignore(dir string) error {
gitignorePath := filepath.Join(dir, ".gitignore")
// Check if .gitignore exists
if !FileExists(gitignorePath) {
return nil // No .gitignore, nothing to do
}
// Check if already has .oops/ entry
hasEntry, err := hasGitignoreEntry(gitignorePath, oopsEntry)
if err != nil {
return err
}
if hasEntry {
return nil // Already present
}
// Append .oops/ to .gitignore
f, err := os.OpenFile(gitignorePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
// Check if file ends with newline
content, err := os.ReadFile(gitignorePath)
if err != nil {
return err
}
prefix := "\n"
if len(content) == 0 || content[len(content)-1] == '\n' {
prefix = ""
}
_, err = f.WriteString(prefix + oopsEntry + "\n")
return err
}
// hasGitignoreEntry checks if .gitignore contains a specific entry
func hasGitignoreEntry(path, entry string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == entry || line == strings.TrimSuffix(entry, "/") {
return true, nil
}
}
return false, scanner.Err()
}