Start of Readme, More Cleanup
This commit is contained in:
@@ -25,7 +25,7 @@ func Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "auto-patch",
|
||||
Usage: "this command accepts a repository and a prompt and will generate a git commit attempting code modifications to satisfy the prompt.",
|
||||
Action: (&agent{}).run,
|
||||
Action: (&autoPatch{}).run,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "repo",
|
||||
@@ -37,17 +37,23 @@ func Command() *cli.Command {
|
||||
Usage: "task to perform, e.g. \"add a test for a function\"",
|
||||
Required: true,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "execute",
|
||||
Usage: "actually execute the commit, otherwise just print the code to be committed",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type agent struct {
|
||||
llm *llm.LLM
|
||||
type autoPatch struct {
|
||||
llm *llm.LLM
|
||||
execute bool
|
||||
}
|
||||
|
||||
func (a *agent) run(ctx context.Context, cmd *cli.Command) error {
|
||||
func (a *autoPatch) run(ctx context.Context, cmd *cli.Command) error {
|
||||
llmRef := llm.FromContext(ctx)
|
||||
a.llm = llmRef
|
||||
a.execute = cmd.Bool("execute")
|
||||
|
||||
// Make sure we're indexed.
|
||||
idx := indexer.New(ctx, cmd.String("repo"), config.FromContext(ctx).IndexChunkSize, false)
|
||||
@@ -64,7 +70,7 @@ func (a *agent) run(ctx context.Context, cmd *cli.Command) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) generateGitCommit(ctx context.Context, repoPath, prompt string) error {
|
||||
func (a *autoPatch) generateGitCommit(ctx context.Context, repoPath, prompt string) error {
|
||||
var affectedFiles []string
|
||||
|
||||
fileName, newCode, err := a.generateCodePatch(ctx, repoPath, prompt)
|
||||
@@ -88,12 +94,10 @@ func (a *agent) generateGitCommit(ctx context.Context, repoPath, prompt string)
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("committed changes to git repo", "repo", repoPath)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) commit(ctx context.Context, prompt, repoPath string, files ...string) error {
|
||||
func (a *autoPatch) commit(ctx context.Context, prompt, repoPath string, files ...string) error {
|
||||
gitPath := osfs.New(filepath.Join(repoPath, ".git"))
|
||||
|
||||
gitRepo, err := git.Open(filesystem.NewStorage(gitPath, cache.NewObjectLRUDefault()), osfs.New(repoPath))
|
||||
@@ -129,14 +133,20 @@ func (a *agent) commit(ctx context.Context, prompt, repoPath string, files ...st
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := workTree.Commit(rsp, &git.CommitOptions{}); err != nil {
|
||||
return err
|
||||
if a.execute {
|
||||
if _, err := workTree.Commit(rsp, &git.CommitOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("committed changes to git repo", "repo", repoPath)
|
||||
} else {
|
||||
fmt.Printf("Commit Message:\n%s\n", rsp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) generateCodePatch(ctx context.Context, repoPath, prompt string) (string, string, error) {
|
||||
func (a *autoPatch) generateCodePatch(ctx context.Context, repoPath, prompt string) (string, string, error) {
|
||||
db := database.FromContext(ctx)
|
||||
cfg := config.FromContext(ctx)
|
||||
|
||||
@@ -175,8 +185,6 @@ func (a *agent) generateCodePatch(ctx context.Context, repoPath, prompt string)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
fmt.Printf("Code block:\n%s\n", codeBlock)
|
||||
|
||||
fileName := chunks[0].Name
|
||||
originalFile, err := os.ReadFile(fileName)
|
||||
if err != nil {
|
||||
@@ -187,17 +195,21 @@ func (a *agent) generateCodePatch(ctx context.Context, repoPath, prompt string)
|
||||
diffs := dmp.DiffMain(string(originalFile), codeBlock, true)
|
||||
diffs = cleanDiffs(diffs)
|
||||
|
||||
fmt.Printf("File to patch: %s\n", fileName)
|
||||
fmt.Println(dmp.DiffPrettyText(diffs))
|
||||
|
||||
if err := patchFile(fileName, diffs); err != nil {
|
||||
return "", "", err
|
||||
if a.execute {
|
||||
slog.Info("applying generated patch to file", "file", fileName)
|
||||
if err := patchFile(fileName, diffs); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Code block:\n%s\n", codeBlock)
|
||||
fmt.Printf("File to patch: %s\n", fileName)
|
||||
fmt.Println(dmp.DiffPrettyText(diffs))
|
||||
}
|
||||
|
||||
return fileName, codeBlock, err
|
||||
}
|
||||
|
||||
func (a *agent) generateUnitTest(ctx context.Context, prompt, fileName, newCode string) (string, error) {
|
||||
func (a *autoPatch) generateUnitTest(ctx context.Context, prompt, fileName, newCode string) (string, error) {
|
||||
// Check to see if a test file for this already exists.
|
||||
testFileExists := false
|
||||
|
||||
@@ -220,34 +232,38 @@ func (a *agent) generateUnitTest(ctx context.Context, prompt, fileName, newCode
|
||||
return "", err
|
||||
}
|
||||
|
||||
fmt.Printf("Unit Test Code block:\n%s\n", codeBlock)
|
||||
if a.execute {
|
||||
slog.Info("applying generated unit test to file", "file", testFile)
|
||||
|
||||
if testFileExists {
|
||||
fp, err := os.OpenFile(testFile, os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer fp.Close()
|
||||
if testFileExists {
|
||||
fp, err := os.OpenFile(testFile, os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
if _, err := fp.WriteString("\n" + codeBlock); err != nil {
|
||||
return "", err
|
||||
if _, err := fp.WriteString("\n" + codeBlock); err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
fp, err := os.Open(testFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
if _, err := fp.WriteString(codeBlock); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fp, err := os.Open(testFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
if _, err := fp.WriteString(codeBlock); err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Printf("Unit Test Code block for %s:\n%s\n", testFile, codeBlock)
|
||||
}
|
||||
|
||||
return testFile, nil
|
||||
}
|
||||
|
||||
func (a *agent) generateCode(ctx context.Context, prompt string) (string, error) {
|
||||
func (a *autoPatch) generateCode(ctx context.Context, prompt string) (string, error) {
|
||||
rsp, err := a.llm.CodePrompt(ctx, prompt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user