diff --git a/v3/core/api.go b/v3/core/api.go index e3cd13e..ab2c740 100644 --- a/v3/core/api.go +++ b/v3/core/api.go @@ -359,7 +359,9 @@ func (g *GrokkerInternal) ListDocuments() (paths []string) { v100, err := semver.Parse([]byte("1.0.0")) current, err := semver.Parse([]byte(g.Version)) Ck(err) - if semver.Cmp(current, v100) > 0 { + cmp, err := semver.Cmp(current, v100) + Ck(err) + if cmp > 0 { path = doc.RelPath } paths = append(paths, path) diff --git a/v3/core/cli.go b/v3/core/cli.go index 7b8a22a..ae97a76 100644 --- a/v3/core/cli.go +++ b/v3/core/cli.go @@ -122,7 +122,9 @@ type cmdChat struct { NoAddToDb bool `short:"D" help:"Do not add the chat history file to the knowledge base."` } -type cmdCommit struct{} +type cmdCommit struct { + Diffargs []string `arg:"" optional:"" type:"string" help:"Arguments to pass to git diff. If not provided, defaults to '--staged'."` +} type cmdCtx struct { Tokenlimit int `arg:"" type:"int" help:"Maximum number of tokens to include in the context."` @@ -572,8 +574,13 @@ func Cli(args []string, config *CliConfig) (rc int, err error) { Ck(err) Pl(res) case "commit": + fallthrough + case "commit ": // generate a git commit message - summary, err := commitMessage(grok) + if len(cli.Commit.Diffargs) < 1 { + cli.Commit.Diffargs = []string{"--staged"} + } + summary, err := commitMessage(grok, cli.Commit.Diffargs...) Ck(err) Pl(summary) case "models": @@ -672,11 +679,12 @@ func msg(g *GrokkerInternal, sysmsg string, input string) (res string, err error } // generate a git commit message -func commitMessage(grok *GrokkerInternal) (summary string, err error) { +func commitMessage(grok *GrokkerInternal, args ...string) (summary string, err error) { defer Return(&err) - // run `git diff --staged` - cmd := exec.Command("git", "diff", "--staged") + // run `git diff @args + args = append([]string{"diff"}, args...) + cmd := exec.Command("git", args...) cmd.Stderr = os.Stderr out, err := cmd.Output() Ck(err) diff --git a/v3/core/cli_test.go b/v3/core/cli_test.go index 4b531f0..d5f69f2 100644 --- a/v3/core/cli_test.go +++ b/v3/core/cli_test.go @@ -406,7 +406,7 @@ func TestCli(t *testing.T) { run(t, "git", "add", ".") // git diff stdout, stderr, err = grok(emptyStdin, "commit") - Tassert(t, err == nil, "CLI returned unexpected error: %v", err) + Tassert(t, err == nil, "CLI returned unexpected error: %v\nstdout: %v\nstderr: %v", err, stdout.String(), stderr.String()) // check that the stdout buffer contains the expected output match = strings.Contains(stdout.String(), "diff --git") Tassert(t, match, "CLI did not return expected output: %s", stdout.String()) diff --git a/v3/core/grokker.go b/v3/core/grokker.go index 88c7055..c2786a3 100644 --- a/v3/core/grokker.go +++ b/v3/core/grokker.go @@ -50,7 +50,7 @@ import ( const ( // See the "Semantic Versioning" section of the README for // information on API and db stability and versioning. - version = "3.0.10" + version = "3.0.11" ) type GrokkerInternal struct { diff --git a/v3/core/migrate.go b/v3/core/migrate.go index e63e16d..d8ba351 100644 --- a/v3/core/migrate.go +++ b/v3/core/migrate.go @@ -47,13 +47,16 @@ func (g *GrokkerInternal) migrate() (migrated bool, was, now string, err error) Ck(err) codever, err = semver.Parse([]byte(version)) Ck(err) - if semver.Cmp(dbver, codever) == 0 { + var cmp int + cmp, err = semver.Cmp(dbver, codever) + Ck(err) + if cmp == 0 { // no migration necessary break } // see if db is newer version than code - if semver.Cmp(dbver, codever) > 0 { + if cmp > 0 { // db is newer than code err = fmt.Errorf("grokker db is version %s, but you're running version %s -- upgrade grokker", g.Version, version) return @@ -62,7 +65,8 @@ func (g *GrokkerInternal) migrate() (migrated bool, was, now string, err error) Fpf(os.Stderr, "migrating from %s to %s\n", g.Version, version) // if we get here, then dbver < codever - _, minor, patch := semver.Upgrade(dbver, codever) + var minor, patch bool + _, minor, patch, _, err = semver.Upgrade(dbver, codever) Assert(patch, "patch should be true: %s -> %s", dbver, codever) // figure out what kind of migration we need to do diff --git a/v3/core/migration_test.go b/v3/core/migration_test.go index 6e4a5de..04c7dc8 100644 --- a/v3/core/migration_test.go +++ b/v3/core/migration_test.go @@ -163,13 +163,25 @@ func TestMigration_2_1_2(t *testing.T) { // create a file with 1 chunk of 20000 bytes // (about 11300 tokens each chunk depending on hash content) mkDataFile("testfile-1-20000.txt", 1, 20000) - run(t, "grok", "add", "testfile-1-20000.txt") + run(t, "./grok", "add", "testfile-1-20000.txt") // test with 3 chunks much larger than GPT-4 token size // create a file with 3 chunks of 300000 bytes // (about 167600 tokens each chunk depending on hash content) mkDataFile("testfile-3-300000.txt", 3, 300000) - run(t, "grok", "add", "testfile-3-300000.txt") + run(t, "./grok", "add", "testfile-3-300000.txt") +} + +// test migration from v3.0.9 to 3.0.10 so we know integer ordering +// works as expected +func TestMigration_3_0_10(t *testing.T) { + mkGrok(t, "v3.0.9", "v3/cmd/grok") + // run ls to get upgraded to 3.0.9 + run(t, "./grok", "ls") + // now build 3.0.10 + mkGrok(t, "v3.0.10", "v3/cmd/grok") + // run ls to get upgraded to 3.0.10 + run(t, "./grok", "ls") } func TestMigrationHead(t *testing.T) { diff --git a/v3/go.mod b/v3/go.mod index 73cb3c0..52077da 100644 --- a/v3/go.mod +++ b/v3/go.mod @@ -9,13 +9,13 @@ require ( // github.com/sashabaranov/go-openai v1.9.0 github.com/sashabaranov/go-openai v1.19.1 github.com/stevegt/goadapt v0.7.0 - github.com/stevegt/semver v0.0.0-20230512043732-92220054a49f github.com/tiktoken-go/tokenizer v0.1.0 ) require ( github.com/sergi/go-diff v1.3.1 github.com/stevegt/envi v0.2.0 + github.com/stevegt/semver v0.0.0-20240217000820-5913d1a31c26 ) require ( diff --git a/v3/go.sum b/v3/go.sum index 44c8507..0837dba 100644 --- a/v3/go.sum +++ b/v3/go.sum @@ -29,8 +29,8 @@ github.com/stevegt/envi v0.2.0/go.mod h1:Z8w7bE5V9Ce3H02CWNTEYkW3zWE8ulgfROtEt6y github.com/stevegt/goadapt v0.0.13/go.mod h1:BWNnTsXdIxaseRo0W/MoVgDeLNf+6L4S4fPhyAsBTi0= github.com/stevegt/goadapt v0.7.0 h1:brUmaaA4mr3hqQfglDAQh7/MVSWak52mEAOzfbSoMDg= github.com/stevegt/goadapt v0.7.0/go.mod h1:vquRbAl0Ek4iJHCvFUEDxziTsETR2HOT7r64NolhDKs= -github.com/stevegt/semver v0.0.0-20230512043732-92220054a49f h1:erQJkdWx1bhOImDDPiVoNy+qP8sBoOJ8EsJ7gUiy8S8= -github.com/stevegt/semver v0.0.0-20230512043732-92220054a49f/go.mod h1:OgjZERzdomhsWRdqEbqfC+2DI1DpdmZwc1CF7vhqAZ8= +github.com/stevegt/semver v0.0.0-20240217000820-5913d1a31c26 h1:z1tzm2Q22jtCN87NlApplnTx/EPQQ4zyZaDgNvw3wg8= +github.com/stevegt/semver v0.0.0-20240217000820-5913d1a31c26/go.mod h1:Jm8NvUiaWMGzaCtI0Ja7Vy7/7ZESxEMWYGswGyqW1+A= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=