tools: Simplify gerrit-stats and snippets
Automatically try to infer 'user' from git config.
Default to 'dawn' repo over tint.
Usage:
tools/run snippets
tools/run gerrit-stats
Change-Id: I8c4617101447ac635e753b0a2a0b655ba6c64b54
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86300
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/tools/src/cmd/gerrit-stats/main.go b/tools/src/cmd/gerrit-stats/main.go
index 0b15709..504a73e 100644
--- a/tools/src/cmd/gerrit-stats/main.go
+++ b/tools/src/cmd/gerrit-stats/main.go
@@ -20,10 +20,12 @@
"fmt"
"net/url"
"os"
+ "os/exec"
"regexp"
"time"
"dawn.googlesource.com/dawn/tools/src/gerrit"
+ "dawn.googlesource.com/dawn/tools/src/git"
)
const yyyymmdd = "2006-01-02"
@@ -33,14 +35,29 @@
// username and password for gerrit.
gerritUser = flag.String("gerrit-user", "", "gerrit authentication username")
gerritPass = flag.String("gerrit-pass", "", "gerrit authentication password")
- repoFlag = flag.String("repo", "tint", "the project (tint or dawn)")
- userFlag = flag.String("user", "", "user name / email")
+ repoFlag = flag.String("repo", "dawn", "the project (tint or dawn)")
+ userFlag = flag.String("user", defaultUser(), "user name / email")
afterFlag = flag.String("after", "", "start date")
beforeFlag = flag.String("before", "", "end date")
daysFlag = flag.Int("days", 182, "interval in days (used if --after is not specified)")
verboseFlag = flag.Bool("v", false, "verbose mode - lists all the changes")
)
+func defaultUser() string {
+ if gitExe, err := exec.LookPath("git"); err == nil {
+ if g, err := git.New(gitExe); err == nil {
+ if cwd, err := os.Getwd(); err == nil {
+ if r, err := g.Open(cwd); err == nil {
+ if cfg, err := r.Config(nil); err == nil {
+ return cfg["user.email"]
+ }
+ }
+ }
+ }
+ }
+ return ""
+}
+
func main() {
flag.Parse()
if err := run(); err != nil {
diff --git a/tools/src/cmd/snippets/main.go b/tools/src/cmd/snippets/main.go
index a8ce846..78c6f5d 100644
--- a/tools/src/cmd/snippets/main.go
+++ b/tools/src/cmd/snippets/main.go
@@ -19,10 +19,12 @@
"flag"
"fmt"
"os"
+ "os/exec"
"strings"
"time"
"dawn.googlesource.com/dawn/tools/src/gerrit"
+ "dawn.googlesource.com/dawn/tools/src/git"
)
const yyyymmdd = "2006-01-02"
@@ -32,12 +34,27 @@
// username and password for gerrit.
gerritUser = flag.String("gerrit-user", "", "gerrit authentication username")
gerritPass = flag.String("gerrit-pass", "", "gerrit authentication password")
- userFlag = flag.String("user", "", "user name / email")
+ userFlag = flag.String("user", defaultUser(), "user name / email")
afterFlag = flag.String("after", "", "start date")
beforeFlag = flag.String("before", "", "end date")
daysFlag = flag.Int("days", 7, "interval in days (used if --after is not specified)")
)
+func defaultUser() string {
+ if gitExe, err := exec.LookPath("git"); err == nil {
+ if g, err := git.New(gitExe); err == nil {
+ if cwd, err := os.Getwd(); err == nil {
+ if r, err := g.Open(cwd); err == nil {
+ if cfg, err := r.Config(nil); err == nil {
+ return cfg["user.email"]
+ }
+ }
+ }
+ }
+ }
+ return ""
+}
+
func main() {
flag.Parse()
if err := run(); err != nil {
diff --git a/tools/src/git/git.go b/tools/src/git/git.go
index f81f328..4745929 100644
--- a/tools/src/git/git.go
+++ b/tools/src/git/git.go
@@ -306,6 +306,33 @@
return parseLog(out)
}
+// Optional settings for Repository.ConfigOptions
+type ConfigOptions struct {
+ // Timeout for the operation
+ Timeout time.Duration
+}
+
+// Config returns the git configuration values for the repo
+func (r Repository) Config(opt *ConfigOptions) (map[string]string, error) {
+ if opt == nil {
+ opt = &ConfigOptions{}
+ }
+ text, err := r.run(opt.Timeout, "config", "-l")
+ if err != nil {
+ return nil, err
+ }
+ lines := strings.Split(text, "\n")
+ out := make(map[string]string, len(lines))
+ for _, line := range lines {
+ idx := strings.Index(line, "=")
+ if idx > 0 {
+ key, value := line[:idx], line[idx+1:]
+ out[key] = value
+ }
+ }
+ return out, nil
+}
+
func (r Repository) run(timeout time.Duration, args ...string) (string, error) {
return r.Git.run(r.Path, timeout, args...)
}