gen: don't update json files on disk when only the etag changes
LGTM=gmlewis
R=gmlewis
CC=golang-codereviews
https://codereview.appspot.com/131880043
diff --git a/google-api-go-generator/gen.go b/google-api-go-generator/gen.go
index 6f5b907..87dde1a 100644
--- a/google-api-go-generator/gen.go
+++ b/google-api-go-generator/gen.go
@@ -18,6 +18,7 @@
"os"
"os/exec"
"path/filepath"
+ "regexp"
"sort"
"strings"
"unicode"
@@ -213,12 +214,21 @@
func writeFile(file string, contents []byte) error {
// Don't write it if the contents are identical.
existing, err := ioutil.ReadFile(file)
- if err == nil && bytes.Equal(existing, contents) {
+ if err == nil && (bytes.Equal(existing, contents) || basicallyEqual(existing, contents)) {
return nil
}
return ioutil.WriteFile(file, contents, 0644)
}
+var etagLine = regexp.MustCompile(`(?m)^\s+"etag": ".+\n`)
+
+// basicallyEqual reports whether a and b are equal except for boring
+// differences like ETag updates.
+func basicallyEqual(a, b []byte) bool {
+ return etagLine.Match(a) && etagLine.Match(b) &&
+ bytes.Equal(etagLine.ReplaceAll(a, nil), etagLine.ReplaceAll(b, nil))
+}
+
func slurpURL(urlStr string) []byte {
diskFile := filepath.Join(os.TempDir(), "google-api-cache-"+url.QueryEscape(urlStr))
if *useCache {