Skip to content

Commit

Permalink
feat: improve the overall reliability
Browse files Browse the repository at this point in the history
Spend directives now need to be at the beginning of a line.
We're reading from the raw body (subject + body) of a commit to make sure to catch directives
hidden in the subject as the newlines in the subject are removed upstream.

/spent 4h
  • Loading branch information
Goutte committed Apr 6, 2024
1 parent 203cfd9 commit 153c4db
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 73 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ sudo git spend man --install
Develop
-------

First, you'll need to [install Golang](https://go.dev/dl/).

```
git clone https://github.com/Goutte/git-spend.git
cd git-spend
Expand All @@ -258,6 +260,10 @@ make install
[`upx`]: https://upx.github.io/

---

You can compare the checksums, and they should be the same unless microsoft is being naughty.


### Build for other platforms

Expand Down
1 change: 1 addition & 0 deletions gitime/gitime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var expressions = []*regexp.Regexp{
// If no time unit is specified, minutes are assumed.
func CollectTimeSpent(message string) *TimeSpent {
ts := &TimeSpent{}
message = strings.ReplaceAll(message, "\r", "\n")
lines := strings.Split(message, "\n")

for _, line := range lines {
Expand Down
11 changes: 11 additions & 0 deletions gitime/gitime_test_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ collect:
expected:
minutes: 60

- rule: Allow alias /spent (/spent 0.5h)
message: |
/spent 0.5h
expected:
minutes: 30

- rule: Allow full units (/spend 1 hour 10 minutes)
message: |
/spend 1 hour 10 minutes
Expand Down Expand Up @@ -201,6 +207,11 @@ collect:
expected:
minutes: 120

- rule: Handle Windows carriage returns as newlines
message: "style: main menu fixed\r/spent 0.5h"
expected:
minutes: 30

- rule: Tolerate missing space typo (/spend3h)
message: |
/spend3h
Expand Down
2 changes: 1 addition & 1 deletion gitime/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gitime

import "regexp"

var commandRegex = "/spen[dt]\\s*:?\\s*"
var commandRegex = "^\\s*/spen[dt]\\s*:?\\s*"
var floatRegex = "[0-9]+[.]?[0-9]*|[0-9]*[.]?[0-9]+"

// no negative lookahead in regexp, so we hack around it (to ignore datetime suffix)
Expand Down
10 changes: 8 additions & 2 deletions gitime/reader/git_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ func ReadGitLog(onlyAuthors []string, excludeMerge bool, since string, until str
continue
}

s += commit.Subject + "\n"
s += commit.Body + "\n"
// We read from the raw body because some newlines are eaten when separating subject an body.
// My non-tech friend commits without separating subject and body, like this:
// > style: something amazing
// > /spent 0.5h
// … and the "/spend 0.5h" ends up at the end of the Subject, without newline.
s += commit.RawBody + "\n"
// We also read from the note, and it might or might not be correct.
s += commit.Note + "\n"
}

return s
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require (

replace github.com/spf13/cobra => github.com/Goutte/cobra v0.0.0-20230404093907-b279579671a9

replace github.com/tsuyoshiwada/go-gitlog => github.com/Goutte/go-gitlog v0.0.0-20240406102306-1efcfa30a305

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -31,6 +33,8 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.3.0 // indirect
github.com/tsuyoshiwada/go-gitcmd v0.0.0-20180205145712-5f1f5f9475df // indirect
golang.org/x/sys v0.19.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Goutte/cobra v0.0.0-20230404093907-b279579671a9 h1:6DqPA8KTHj+BmPBB5J7viX8jgPxuYcLqoUjxj+IfemQ=
github.com/Goutte/cobra v0.0.0-20230404093907-b279579671a9/go.mod h1:kKQTiXzwhSqS83iXVA/L4jqCpe2jy7X+14DyQ1wmImI=
github.com/Goutte/go-gitlog v0.0.0-20240406102306-1efcfa30a305 h1:IM41iOGTwRv6w/zV5emUaLia4j3a8AKsfIZ/OK/xDo8=
github.com/Goutte/go-gitlog v0.0.0-20240406102306-1efcfa30a305/go.mod h1:nVMD+1UEtGO3Dl5TrjnZUmawWuLs/fp83Vwq77MXO98=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -180,8 +182,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/tsuyoshiwada/go-gitlog v0.0.1 h1:ZBIfQG2IUmguQzP8llKgAxUvorcYnI+OhzWXi1vpv38=
github.com/tsuyoshiwada/go-gitlog v0.0.1/go.mod h1:kfjd4M5xSFPqL0Mp2pAk0WM9hP9pJkLQiAndAoQHSRc=
github.com/tsuyoshiwada/go-gitcmd v0.0.0-20180205145712-5f1f5f9475df h1:Y2l28Jr3vOEeYtxfVbMtVfOdAwuUqWaP9fvNKiBVeXY=
github.com/tsuyoshiwada/go-gitcmd v0.0.0-20180205145712-5f1f5f9475df/go.mod h1:pnyouUty/nBr/zm3GYwTIt+qFTLWbdjeLjZmJdzJOu8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -324,8 +326,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -488,8 +490,9 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
Loading

0 comments on commit 153c4db

Please sign in to comment.