diff --git a/main.go b/main.go index 53baa2b..8cd8df6 100644 --- a/main.go +++ b/main.go @@ -12,17 +12,15 @@ import ( "github.com/gookit/color" ) -var email string -var since, until time.Time - func main() { + var email string var sinceflag, untilflag string flag.StringVar(&sinceflag, "since", "", "start date") flag.StringVar(&untilflag, "until", "", "end date") flag.StringVar(&email, "email", strings.TrimSpace(getGlobalEmailFromGit()), "you Git email") flag.Parse() - err := setTimeFlags(sinceflag, untilflag) + b, err := setTimeFlags(sinceflag, untilflag) if err != nil { fmt.Fprint(os.Stderr, color.Red.Sprintf("gitcs: %s\n", err.Error())) os.Exit(1) @@ -53,8 +51,13 @@ func main() { os.Exit(1) } - commits := processRepos(repos, email) + commits := processRepos(repos, email, b.Since, b.Until) fmt.Print("\n\n") - printTable(commits) + printTable(commits, b.Since, b.Until) fmt.Print("\n\n") } + +type Boundary struct { + Since time.Time + Until time.Time +} diff --git a/print.go b/print.go index 0cb6dd1..ca41607 100644 --- a/print.go +++ b/print.go @@ -30,7 +30,7 @@ func getDay(i int) string { return strings.Repeat(" ", 3) } -func printTable(commits map[int]int) { +func printTable(commits map[int]int, since, until time.Time) { for since.Weekday() != time.Sunday { since = since.AddDate(0, 0, -1) } diff --git a/print_test.go b/print_test.go index adb1bdf..78dd243 100644 --- a/print_test.go +++ b/print_test.go @@ -153,14 +153,14 @@ 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) + 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) + printTable(commits, since, until) w.Close() dat, err := io.ReadAll(r) @@ -173,6 +173,13 @@ 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 until.Weekday() != time.Saturday { + until = until.AddDate(0, 0, 1) + } + s := strings.Builder{} s1 := since diff --git a/stats.go b/stats.go index 3a99103..58aed50 100644 --- a/stats.go +++ b/stats.go @@ -12,7 +12,7 @@ const sixMonthsInDays int = 182 var now = time.Now() -func fillCommits(path, email string, commits map[int]int) error { +func fillCommits(path, email string, commits map[int]int, since, until time.Time) error { repo, err := git.PlainOpen(path) if err != nil { return err @@ -38,11 +38,11 @@ func fillCommits(path, email string, commits map[int]int) error { return err } -func processRepos(repos []string, email string) map[int]int { +func processRepos(repos []string, email string, since, until time.Time) map[int]int { m := map[int]int{} var err error for _, repo := range repos { - err = fillCommits(repo, email, m) + err = fillCommits(repo, email, m, since, until) 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 0288fb6..db70c50 100644 --- a/stats_test.go +++ b/stats_test.go @@ -42,10 +42,13 @@ func TestFillCommits(t *testing.T) { }, } + 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) + err = fillCommits(tt.Path, tt.Email, commits, since, until) if err != nil { t.Fatalf("failed to fill commits in %q: %v", tt.Path, err) } @@ -94,9 +97,12 @@ func TestProcessRepos(t *testing.T) { }, } + 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) + commits := processRepos(tt.Repos, tt.Email, since, until) if len(commits) != len(tt.Expected) { t.Errorf("processRepos11() = %v, want %v", commits, tt.Expected) } diff --git a/utils.go b/utils.go index 65570fe..fc7a443 100644 --- a/utils.go +++ b/utils.go @@ -61,26 +61,28 @@ func getGlobalEmailFromGit() string { return string(localEmail) } -func setTimeFlags(sinceflag, untilflag string) error { +func setTimeFlags(sinceflag, untilflag string) (*Boundary, error) { var err error + var boundary Boundary if untilflag != "" { - until, err = time.Parse("2006-01-02", untilflag) + boundary.Until, err = time.Parse("2006-01-02", untilflag) if err != nil { - return fmt.Errorf("invalid 'until' date format. please use the format: 2006-01-02") + return nil, fmt.Errorf("invalid 'until' date format. please use the format: 2006-01-02") } - if until.After(now) { - until = now + if boundary.Until.After(now) { + boundary.Until = now } } else { - until = now + boundary.Until = now } if sinceflag != "" { - since, err = time.Parse("2006-01-02", sinceflag) + boundary.Since, err = time.Parse("2006-01-02", sinceflag) if err != nil { - return fmt.Errorf("invalid 'since' date format. please use the format: 2006-01-02") + return nil, fmt.Errorf("invalid 'since' date format. please use the format: 2006-01-02") } } else { - since = time.Date(until.Year(), until.Month(), until.Day(), 0, 0, 0, 0, until.Location()).AddDate(0, 0, -sixMonthsInDays) + boundary.Since = time.Date(boundary.Until.Year(), boundary.Until.Month(), boundary.Until.Day(), 0, 0, 0, 0, boundary.Until.Location()).AddDate(0, 0, -sixMonthsInDays) } - return nil + + return &boundary, nil } diff --git a/utils_test.go b/utils_test.go index cdba8f0..c92a991 100644 --- a/utils_test.go +++ b/utils_test.go @@ -252,7 +252,7 @@ func TestSetTimeFlags(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := setTimeFlags(tt.sinceflag, tt.untilflag) + b, err := setTimeFlags(tt.sinceflag, tt.untilflag) if err != nil { if err.Error() != tt.expectedError { @@ -261,12 +261,12 @@ func TestSetTimeFlags(t *testing.T) { return } - if since != tt.expectedSince { - t.Errorf("Unexpected value of 'since'. Expected: %v, Got: %v", tt.expectedSince, since) + if b.Since != tt.expectedSince { + t.Errorf("Unexpected value of 'since'. Expected: %v, Got: %v", tt.expectedSince, b.Since) } - if until != tt.expectedUntil { - t.Errorf("Unexpected value of 'until'. Expected: %v, Got: %v", tt.expectedUntil, until) + if b.Until != tt.expectedUntil { + t.Errorf("Unexpected value of 'until'. Expected: %v, Got: %v", tt.expectedUntil, b.Until) } }) }