@@ -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\n out/\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.
175216func 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