diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 748abae..28b3725 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -31,7 +31,6 @@ jobs: - uses: actions/checkout@v3 with: submodules: true -# clean: false fetch-depth: 0 - name: Fetch git history and tags (for fixtures) @@ -60,8 +59,6 @@ jobs: with: file: ./coverage-unit.txt flags: unittests -# token: 50ed170f-08b5-4484-97d8-37771615ae42 -# name: codecov-umbrella - name: Upload integration tests coverage report uses: codecov/codecov-action@v3 diff --git a/README.md b/README.md index 6c10409..c6efbc6 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ These can be configured at runtime if needed, using environment variables. The **complete specification** can be found in [the rules](./gitime/gitime_test_data.yaml) of the test data, and in excruciating detail in [the grammar](./gitime/grammar.go). +The [acceptance testing suite](./test/features.bats) also holds many usage examples. + Usage ----- @@ -57,7 +59,7 @@ Go into your git-versioned project's directory, and run: cd gitime sum ``` -> `> 2 days 1 hour 42 minutes` +> `2 days 1 hour 42 minutes` ### Format the output @@ -111,6 +113,14 @@ Or the time spent on a tag since previous tag : gitime sum --since 0.1.0 --until 0.1.1 ``` +You can also use _dates_ and _datetimes_, but remember to quote them if you specify the time: + +``` +gitime sum --since "21-03-2023 13:37:00" +``` + +> Other supported time formats: `RFC3339`, `RFC822`, `RFC850`, + Download -------- @@ -215,11 +225,6 @@ Merge requests are welcome. Make sure you record the time you `/spend` in your > You can pick and start any, or do something else entirely. -- [x] `gitime sum --since ` -- [x] `gitime sum --since ` -- [x] `gitime sum --until ` -- [x] `gitime sum --until ` -- [ ] `gitime sum --since ` - [ ] `curl install.sh | bash` - [ ] flatpak - [ ] git extension diff --git a/cmd/sum.go b/cmd/sum.go index a19dad6..ec5c8a4 100644 --- a/cmd/sum.go +++ b/cmd/sum.go @@ -8,6 +8,7 @@ import ( "io" "log" "os" + "time" ) var ( @@ -92,24 +93,81 @@ func formatTimeSpent(ts *gitime.TimeSpent) string { return out } +func parseTimePerhaps(input string) *time.Time { + if input == "" { + return nil + } + + layouts := []string{ + time.RFC3339, + time.DateTime, + time.DateOnly, + time.RFC822, + time.RFC850, + } + + for _, layout := range layouts { + parse, err := time.Parse(layout, input) + if err == nil { + return &parse + } + } + + return nil +} + func getRevArgsFromFlags() gitlog.RevArgs { var rev gitlog.RevArgs = nil if FlagSince != "" { + flagSinceDate := parseTimePerhaps(FlagSince) + if FlagUntil != "" { - rev = &gitlog.RevRange{ - New: FlagUntil, - Old: FlagSince, + flagUntilDate := parseTimePerhaps(FlagUntil) + + if flagUntilDate != nil { + if flagSinceDate != nil { + rev = &gitlog.RevTime{ + Since: *flagSinceDate, + Until: *flagUntilDate, + } + } else { + fmt.Println("you cannot mix dates and refs in --until and --since") + os.Exit(1) + } + } else { + if flagSinceDate != nil { + fmt.Println("you cannot mix dates and refs in --since and --until") + os.Exit(1) + } else { + rev = &gitlog.RevRange{ + New: FlagUntil, + Old: FlagSince, + } + } } } else { - rev = &gitlog.RevRange{ - New: "HEAD", - Old: FlagSince, + if flagSinceDate != nil { + rev = &gitlog.RevTime{ + Since: *flagSinceDate, + } + } else { + rev = &gitlog.RevRange{ + New: "HEAD", + Old: FlagSince, + } } } } else { if FlagUntil != "" { - rev = &gitlog.Rev{ - Ref: FlagUntil, + flagUntilDate := parseTimePerhaps(FlagUntil) + if flagUntilDate != nil { + rev = &gitlog.RevTime{ + Until: *flagUntilDate, + } + } else { + rev = &gitlog.Rev{ + Ref: FlagUntil, + } } } } @@ -127,7 +185,9 @@ func ReadGitLog(onlyAuthors []string, excludeMerge bool, directory string) strin } commits, err := git.Log(rev, params) if err != nil { - log.Fatalln("Cannot read git log:", err) + fmt.Println("Cannot read git log:", err) + os.Exit(1) + //log.Fatalln("Cannot read git log:", err) } s := "" diff --git a/test/features.bats b/test/features.bats index ebec21d..83929b4 100644 --- a/test/features.bats +++ b/test/features.bats @@ -2,11 +2,14 @@ # https://github.com/bats-core/bats-core # Run: -# bats test +# make test-acceptance +# We use gitime's own repo as fixture. +# We copy this project into a temporary fixture dir (in RAM), +# and then have it check out the appropriate fixture-XX tag, +# and finally run integration testing on that temporary repo. TMP_FIXTURE_DIR="/tmp/gitime-test" - setup() { load 'test_helper/bats-support/load' load 'test_helper/bats-assert/load' @@ -25,11 +28,6 @@ setup() { export GITIME_NO_STDIN=1 - # We use gitime's own repo as fixture. - # We copy this project into a temporary dir (in RAM), - # check out the appropriate fixture-XX tag, - # and run integration testing on that temporary repo. - cp -R "$PROJECT_DIR" "$TMP_FIXTURE_DIR" cd "$TMP_FIXTURE_DIR" || exit @@ -50,10 +48,10 @@ teardown() { @test "gitime" { run $gitime assert_success - #assert_output --partial 'Gather information about /spent time from commit messages' + assert_output --partial 'Gather information about /spent time from commit messages' } -@test "gitime hohohoooo" { +@test "gitime hohohoooo should fail" { run $gitime hohohoooo assert_failure } @@ -116,7 +114,7 @@ teardown() { assert_output "1 week 3 hours" } -@test "gitime sum --author notfound" { +@test "gitime sum --author notfound (should fail)" { run $gitime sum --author notfound # shouldn't we fail, here? TBD #assert_failure @@ -147,7 +145,7 @@ teardown() { assert_failure } -@test "gitime sum --since " { +@test "gitime sum --since (should fail)" { run $gitime sum --since lololololo assert_failure } @@ -204,6 +202,47 @@ teardown() { assert_output "30 minutes" } +@test "gitime sum --since " { + # Sun Mar 26 22:11:03 2023 of 4527140510c2b77a9f2a6eb947b5391d4e2173a9 + run $gitime sum --since 2023-03-27 + assert_success + assert_output "2 hours" +} + +@test "gitime sum --since " { + run $gitime sum --since "2023-03-26 22:15:00" + assert_success + assert_output "2 hours 1 minute" + # We'd want, but no cigar ; time parsing in Golang is quite weird + #run $gitime sum --since "2023-03-26 22:15" + #assert_success +} + +@test "gitime sum --since " { + run $gitime sum --since 2023-03-26T22:15:00Z + assert_success + assert_output "2 hours 1 minute" +} + +@test "gitime sum --until " { + run $gitime sum --until 2023-03-25 + assert_success + assert_output "1 day 3 hours 55 minutes" +} + +@test "gitime sum --since --until " { + run $gitime sum --since "2023-03-25 03:30:00" --until "2023-03-25 13:37:00" + assert_success + assert_output "2 hours 15 minutes" +} + +@test "gitime sum does not accept mixed dates and refs in ranges" { + run $gitime sum --until 2023-03-27 --since 0.1.0 + assert_failure + run $gitime sum --since 2023-03-24 --until 0.2.0 + assert_failure +} + @test "gitime sum using stdin" { export GITIME_NO_STDIN=0 run bash -c "cat fixture-00.log | $gitime sum"