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>
This commit is contained in:
Ben Clayton 2022-04-11 10:49:50 +00:00 committed by Dawn LUCI CQ
parent db911a24de
commit 4127abfd41
3 changed files with 64 additions and 3 deletions

View File

@ -20,10 +20,12 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
"os/exec"
"regexp" "regexp"
"time" "time"
"dawn.googlesource.com/dawn/tools/src/gerrit" "dawn.googlesource.com/dawn/tools/src/gerrit"
"dawn.googlesource.com/dawn/tools/src/git"
) )
const yyyymmdd = "2006-01-02" const yyyymmdd = "2006-01-02"
@ -33,14 +35,29 @@ var (
// username and password for gerrit. // username and password for gerrit.
gerritUser = flag.String("gerrit-user", "", "gerrit authentication username") gerritUser = flag.String("gerrit-user", "", "gerrit authentication username")
gerritPass = flag.String("gerrit-pass", "", "gerrit authentication password") gerritPass = flag.String("gerrit-pass", "", "gerrit authentication password")
repoFlag = flag.String("repo", "tint", "the project (tint or dawn)") repoFlag = flag.String("repo", "dawn", "the project (tint or dawn)")
userFlag = flag.String("user", "", "user name / email") userFlag = flag.String("user", defaultUser(), "user name / email")
afterFlag = flag.String("after", "", "start date") afterFlag = flag.String("after", "", "start date")
beforeFlag = flag.String("before", "", "end date") beforeFlag = flag.String("before", "", "end date")
daysFlag = flag.Int("days", 182, "interval in days (used if --after is not specified)") 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") 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() { func main() {
flag.Parse() flag.Parse()
if err := run(); err != nil { if err := run(); err != nil {

View File

@ -19,10 +19,12 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"os/exec"
"strings" "strings"
"time" "time"
"dawn.googlesource.com/dawn/tools/src/gerrit" "dawn.googlesource.com/dawn/tools/src/gerrit"
"dawn.googlesource.com/dawn/tools/src/git"
) )
const yyyymmdd = "2006-01-02" const yyyymmdd = "2006-01-02"
@ -32,12 +34,27 @@ var (
// username and password for gerrit. // username and password for gerrit.
gerritUser = flag.String("gerrit-user", "", "gerrit authentication username") gerritUser = flag.String("gerrit-user", "", "gerrit authentication username")
gerritPass = flag.String("gerrit-pass", "", "gerrit authentication password") 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") afterFlag = flag.String("after", "", "start date")
beforeFlag = flag.String("before", "", "end date") beforeFlag = flag.String("before", "", "end date")
daysFlag = flag.Int("days", 7, "interval in days (used if --after is not specified)") 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() { func main() {
flag.Parse() flag.Parse()
if err := run(); err != nil { if err := run(); err != nil {

View File

@ -306,6 +306,33 @@ func (r Repository) Log(opt *LogOptions) ([]CommitInfo, error) {
return parseLog(out) 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) { func (r Repository) run(timeout time.Duration, args ...string) (string, error) {
return r.Git.run(r.Path, timeout, args...) return r.Git.run(r.Path, timeout, args...)
} }