Skip to content

Commit

Permalink
analysis: Detect "Code generated by" header.
Browse files Browse the repository at this point in the history
This is in line with https://golang.org/cl/15073.

Consider .go files that are shorter than 2 lines (i.e., no "\n" character)
to not be generated.
  • Loading branch information
dmitshur committed Nov 9, 2015
1 parent c0a4689 commit 43b2166
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions analysis/generated_detection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analysis

import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -22,10 +23,13 @@ func IsFileGenerated(rootDir, name string) (bool, error) {
defer f.Close()
r := bufio.NewReader(f)
s, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
// Empty file or exactly 1 line is not considered to be generated.
return false, nil
} else if err != nil {
return false, err
}
if s == "// Code generated by protoc-gen-gogo.\n" { // protoc-gen-gogo special case.
if strings.Contains(s, "Code generated by") { // Consistent with https://golang.org/cl/15073.
return true, nil
}
return (strings.Contains(s, "GENERATED") || strings.Contains(s, "generated")) && strings.Contains(s, "DO NOT EDIT"), nil
Expand Down
2 changes: 2 additions & 0 deletions analysis/generated_detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func ExampleIsFileGenerated() {

fmt.Println(analysis.IsFileGenerated(cwd, "testdata/generated_0.go"))
fmt.Println(analysis.IsFileGenerated(cwd, "testdata/handcrafted_0.go"))
fmt.Println(analysis.IsFileGenerated(cwd, "testdata/handcrafted_1.go"))

// Output:
// true <nil>
// false <nil>
// false <nil>
}
1 change: 1 addition & 0 deletions analysis/testdata/handcrafted_1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Code generated by protoc-gen-gogo. Actually it isn't, because it's only 1 line.

0 comments on commit 43b2166

Please sign in to comment.