Skip to content

Commit a3f3261

Browse files
authored
Merge pull request tinkerbell#27 from tstromberg/gitignore-specific
Automatically update .gitignore with out/ entry
2 parents afea78c + 092576c commit a3f3261

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13-
out/
1413

1514
# Dependency directories (remove the comment below to include it)
1615
# vendor/
16+
17+
# added by lint-install
18+
out/

lint-install.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,47 @@ func updateGoLint(root string, dryRun bool) (string, error) {
171171
return fmt.Sprint(change), nil
172172
}
173173

174+
// updateGitignore updates the .gitignore file within a project to exclude installed linters.
175+
func updateGitignore(root string, dryRun bool) (string, error) {
176+
dest := filepath.Join(root, ".gitignore")
177+
var existing []byte
178+
var err error
179+
180+
if _, err = os.Stat(dest); err == nil {
181+
klog.Infof("Found existing %s", dest)
182+
existing, err = os.ReadFile(dest)
183+
if err != nil {
184+
return "", err
185+
}
186+
}
187+
proposed := []byte{}
188+
found := false
189+
for _, line := range bytes.Split(existing, []byte("\n")) {
190+
if bytes.Equal(line, []byte("out/")) || bytes.Equal(line, []byte("out")) {
191+
found = true
192+
}
193+
proposed = append(proposed, line...)
194+
proposed = append(proposed, []byte("\n")...)
195+
}
196+
197+
if !found {
198+
proposed = append(proposed, []byte("# added by lint-install\nout/\n")...)
199+
}
200+
201+
// Trim extra newlines at the end
202+
proposed = bytes.TrimRight(proposed, "\n")
203+
proposed = append(proposed, byte('\n'))
204+
205+
edits := myers.ComputeEdits(".gitignore", string(existing), string(proposed))
206+
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
207+
if !dryRun {
208+
if err := os.WriteFile(dest, proposed, 0o600); err != nil {
209+
return "", err
210+
}
211+
}
212+
return fmt.Sprint(change), nil
213+
}
214+
174215
// goLintCmd returns the appropriate golangci-lint command to run for a project.
175216
func goLintCmd(root string, level string, fix bool) string {
176217
klog.Infof("Searching for go modules within %s ...", root)
@@ -300,5 +341,15 @@ func main() {
300341
} else {
301342
klog.Infof("Makefile has no changes")
302343
}
344+
345+
diff, err = updateGitignore(root, *dryRunFlag)
346+
if err != nil {
347+
klog.Exitf("update .gitignore failed: %v", err)
348+
}
349+
if diff != "" {
350+
klog.Infof(".gitignore changes:\n%s", diff)
351+
} else {
352+
klog.Infof(".gitignore has no changes")
353+
}
303354
}
304355
}

0 commit comments

Comments
 (0)