Skip to content

Commit

Permalink
feat: implement --since <date> and --until <date>
Browse files Browse the repository at this point in the history
/spend 3h
  • Loading branch information
Goutte committed Mar 28, 2023
1 parent 8e73271 commit c39f307
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 29 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand All @@ -57,7 +59,7 @@ Go into your git-versioned project's directory, and run:
cd <some git versioned project with commits using /spend directives>
gitime sum
```
> `> 2 days 1 hour 42 minutes`
> `2 days 1 hour 42 minutes`

### Format the output
Expand Down Expand Up @@ -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
--------
Expand Down Expand Up @@ -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 <commit>`
- [x] `gitime sum --since <tag>`
- [x] `gitime sum --until <ref>`
- [x] `gitime sum --until <tag>`
- [ ] `gitime sum --since <datetime>`
- [ ] `curl install.sh | bash`
- [ ] flatpak
- [ ] git extension
Expand Down
78 changes: 69 additions & 9 deletions cmd/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log"
"os"
"time"
)

var (
Expand Down Expand Up @@ -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,
}
}
}
}
Expand All @@ -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 := ""
Expand Down
61 changes: 50 additions & 11 deletions test/features.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -147,7 +145,7 @@ teardown() {
assert_failure
}

@test "gitime sum --since <wrong>" {
@test "gitime sum --since <wrong> (should fail)" {
run $gitime sum --since lololololo
assert_failure
}
Expand Down Expand Up @@ -204,6 +202,47 @@ teardown() {
assert_output "30 minutes"
}

@test "gitime sum --since <date>" {
# 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 <date time>" {
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 <date rfc3339>" {
run $gitime sum --since 2023-03-26T22:15:00Z
assert_success
assert_output "2 hours 1 minute"
}

@test "gitime sum --until <date>" {
run $gitime sum --until 2023-03-25
assert_success
assert_output "1 day 3 hours 55 minutes"
}

@test "gitime sum --since <date> --until <date>" {
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"
Expand Down

0 comments on commit c39f307

Please sign in to comment.