Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/gapicgen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package generator
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 13 additions & 8 deletions internal/gapicgen/generator/genproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand All @@ -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 "<genprotoDir>/generated".
func (g *GenprotoGenerator) protoc(fileNames []string) error {
Expand Down
11 changes: 11 additions & 0 deletions internal/gapicgen/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}