diff --git a/internal/gapicgen/generator/generator.go b/internal/gapicgen/generator/generator.go index 7b1f68ce986a..497c7762e492 100644 --- a/internal/gapicgen/generator/generator.go +++ b/internal/gapicgen/generator/generator.go @@ -21,6 +21,7 @@ package generator import ( "context" "fmt" + "log" "os" "path/filepath" "sort" @@ -86,7 +87,18 @@ func gatherChanges(googleapisDir, genprotoDir string) ([]*git.ChangeInfo, error) if !strings.HasSuffix(file, ".proto") { continue } - pkg, err := goPkg(filepath.Join(googleapisDir, file)) + content, err := git.GetFileContentAtCommit(googleapisDir, commit, file) + if err != nil { + // It's possible the file was deleted in this commit, so we check the parent. + originalErr := err + content, err = git.GetFileContentAtCommit(googleapisDir, commit+"^", file) + if err != nil { + // We don't want to fail here, just log the error and continue. + log.Printf("could not get content for %s at commit %s (%v) or its parent (%v)", file, commit, originalErr, err) + continue + } + } + pkg, err := parseGoPkg(content) if err != nil { return nil, err } diff --git a/internal/gapicgen/generator/genproto.go b/internal/gapicgen/generator/genproto.go index 11badb3c9d23..b8131530b1c5 100644 --- a/internal/gapicgen/generator/genproto.go +++ b/internal/gapicgen/generator/genproto.go @@ -220,14 +220,9 @@ func filterPackages(in map[string][]string) (map[string][]string, error) { return out, nil } -// goPkg reports the import path declared in the given file's `go_package` -// option. If the option is missing, goPkg returns empty string. -func goPkg(fileName string) (string, error) { - content, err := os.ReadFile(fileName) - if err != nil { - return "", err - } - +// parseGoPkg parses the import path declared in the given file's `go_package` +// option. If the option is missing, parseGoPkg returns empty string. +func parseGoPkg(content []byte) (string, error) { var pkgName string if match := goPkgOptRe.FindSubmatch(content); len(match) > 0 { pn, err := strconv.Unquote(string(match[1])) @@ -242,6 +237,16 @@ func goPkg(fileName string) (string, error) { return pkgName, nil } +// goPkg reports the import path declared in the given file's `go_package` +// option. If the option is missing, goPkg returns empty string. +func goPkg(fileName string) (string, error) { + content, err := os.ReadFile(fileName) + if err != nil { + return "", err + } + return parseGoPkg(content) +} + // protoc executes the "protoc" command on files named in fileNames, and outputs // to "/generated". func (g *GenprotoGenerator) protoc(fileNames []string) error { diff --git a/internal/gapicgen/git/git.go b/internal/gapicgen/git/git.go index 56d7e020deff..6c0a3002f0f0 100644 --- a/internal/gapicgen/git/git.go +++ b/internal/gapicgen/git/git.go @@ -202,3 +202,14 @@ func FilesChanged(gitDir, hash string) ([]string, error) { } return strings.Split(string(b), "\n"), nil } + +// GetFileContentAtCommit returns the content of a file at a specific commit. +func GetFileContentAtCommit(gitDir, hash, filePath string) ([]byte, error) { + c := execv.Command("git", "show", fmt.Sprintf("%s:%s", hash, filePath)) + c.Dir = gitDir + b, err := c.Output() + if err != nil { + return nil, err + } + return b, nil +}