diff --git a/main.go b/main.go index 8cd8df6..ba210b2 100644 --- a/main.go +++ b/main.go @@ -51,9 +51,9 @@ func main() { os.Exit(1) } - commits := processRepos(repos, email, b.Since, b.Until) + commits := processRepos(repos, email, *b) fmt.Print("\n\n") - printTable(commits, b.Since, b.Until) + printTable(commits, *b) fmt.Print("\n\n") } diff --git a/print.go b/print.go index ca41607..cf22edd 100644 --- a/print.go +++ b/print.go @@ -30,24 +30,24 @@ func getDay(i int) string { return strings.Repeat(" ", 3) } -func printTable(commits map[int]int, since, until time.Time) { - for since.Weekday() != time.Sunday { - since = since.AddDate(0, 0, -1) +func printTable(commits map[int]int, b Boundary) { + for b.Since.Weekday() != time.Sunday { + b.Since = b.Since.AddDate(0, 0, -1) } - for until.Weekday() != time.Saturday { - until = until.AddDate(0, 0, 1) + for b.Until.Weekday() != time.Saturday { + b.Until = b.Until.AddDate(0, 0, 1) } - fmt.Printf("%s %s\n", sixEmptySpaces, buildHeader(since, until)) + fmt.Printf("%s %s\n", sixEmptySpaces, buildHeader(b.Since, b.Until)) max := getMaxValue(commits) s := strings.Builder{} - s1 := since + s1 := b.Since for i := 0; i < 7; i++ { s.WriteString(fmt.Sprintf("%-5s", getDay(i))) sn2 := s1 - for !sn2.After(until) { + for !sn2.After(b.Until) { d := daysAgo(sn2) s.WriteString(printCell(commits[d], max)) sn2 = sn2.AddDate(0, 0, 7) diff --git a/print_test.go b/print_test.go index 78dd243..5b139b9 100644 --- a/print_test.go +++ b/print_test.go @@ -153,14 +153,16 @@ func TestPrintTable(t *testing.T) { 13: 0, } - since := time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC) - until := time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC) + b := Boundary{ + Since: time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC), + Until: time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC), + } oldStdout := os.Stdout r, w, _ := os.Pipe() os.Stdout = w - printTable(commits, since, until) + printTable(commits, b) w.Close() dat, err := io.ReadAll(r) @@ -173,23 +175,23 @@ func TestPrintTable(t *testing.T) { var buf strings.Builder _, _ = fmt.Fprint(&buf, string(dat)) - for since.Weekday() != time.Sunday { - since = since.AddDate(0, 0, -1) + for b.Since.Weekday() != time.Sunday { + b.Since = b.Since.AddDate(0, 0, -1) } - for until.Weekday() != time.Saturday { - until = until.AddDate(0, 0, 1) + for b.Until.Weekday() != time.Saturday { + b.Until = b.Until.AddDate(0, 0, 1) } s := strings.Builder{} - s1 := since + s1 := b.Since - s.WriteString(fmt.Sprintf("%s %s\n", sixEmptySpaces, buildHeader(since, until))) + s.WriteString(fmt.Sprintf("%s %s\n", sixEmptySpaces, buildHeader(b.Since, b.Until))) max := getMaxValue(commits) for i := 0; i < 7; i++ { s.WriteString(fmt.Sprintf("%-5s", getDay(i))) sn2 := s1 - for !sn2.After(until) { + for !sn2.After(b.Until) { d := daysAgo(sn2) s.WriteString(printCell(commits[d], max)) sn2 = sn2.AddDate(0, 0, 7) diff --git a/stats.go b/stats.go index 58aed50..5450d4f 100644 --- a/stats.go +++ b/stats.go @@ -12,13 +12,13 @@ const sixMonthsInDays int = 182 var now = time.Now() -func fillCommits(path, email string, commits map[int]int, since, until time.Time) error { +func fillCommits(path, email string, commits map[int]int, b Boundary) error { repo, err := git.PlainOpen(path) if err != nil { return err } - commitIterator, err := repo.Log(&git.LogOptions{Since: &since, Until: &until}) + commitIterator, err := repo.Log(&git.LogOptions{Since: &b.Since, Until: &b.Until}) if err != nil { return err } @@ -38,11 +38,11 @@ func fillCommits(path, email string, commits map[int]int, since, until time.Time return err } -func processRepos(repos []string, email string, since, until time.Time) map[int]int { +func processRepos(repos []string, email string, b Boundary) map[int]int { m := map[int]int{} var err error for _, repo := range repos { - err = fillCommits(repo, email, m, since, until) + err = fillCommits(repo, email, m, b) if err != nil { fmt.Printf("failed to fill commits in %q: %v", repo, err) } diff --git a/stats_test.go b/stats_test.go index db70c50..c5f936c 100644 --- a/stats_test.go +++ b/stats_test.go @@ -42,13 +42,15 @@ func TestFillCommits(t *testing.T) { }, } - since := time.Now().AddDate(0, 0, -1) - until := time.Now().AddDate(0, 0, 1) + b := Boundary{ + Since: time.Now().AddDate(0, 0, -1), + Until: time.Now().AddDate(0, 0, 1), + } for _, tt := range tests { t.Run(tt.Name, func(t *testing.T) { commits := map[int]int{} - err = fillCommits(tt.Path, tt.Email, commits, since, until) + err = fillCommits(tt.Path, tt.Email, commits, b) if err != nil { t.Fatalf("failed to fill commits in %q: %v", tt.Path, err) } @@ -97,12 +99,14 @@ func TestProcessRepos(t *testing.T) { }, } - since := time.Now().AddDate(0, 0, -1) - until := time.Now().AddDate(0, 0, 1) + b := Boundary{ + Since: time.Now().AddDate(0, 0, -1), + Until: time.Now().AddDate(0, 0, 1), + } for _, tt := range tests { t.Run(tt.Name, func(t *testing.T) { - commits := processRepos(tt.Repos, tt.Email, since, until) + commits := processRepos(tt.Repos, tt.Email, b) if len(commits) != len(tt.Expected) { t.Errorf("processRepos11() = %v, want %v", commits, tt.Expected) }