Skip to content

Commit

Permalink
git helpers (#741)
Browse files Browse the repository at this point in the history
* Add Git interface for file selection and declare git variable for repository operations

* wiring up globlas

* Add git helper functions for branch and file modifications detection

* Use `--porcelain` format in git status to ignore deleted files

* Refactor GitClient to use a unified exec method for executing git commands

* Refactor GitClient for scope handling and add diff method with filters

* Refactor git diff command to use object-based parameters and exclude specific paths

* Refactor diff parsing and update GitClient to handle excluded paths correctly

* Refactor git diff commands to use new API methods for consistency and readability

* refactor: replace host.exec with git.diff and git.exec for cleaner code

* Refactor git diff argument handling for clarity and correctness

* Enhance chat output by applying prettifyMarkdown function

* Add verbose logging for default branch retrieval and console output in script

* Update defaultBranch method to use 'remote show' and add setDefaultBranch method

* Change `findModifiedFiles` argument from "branch" to "base" for better filtering

* Make glob input dynamic and add prompts for missing variables

* Add askStageOnEmpty option to GitClient and update scripts to use git.diff method

* Add documentation and refactor diff handling logic in core and vscode packages

* Add trace filename logging and return it from setupTraceWriting function

* Refactor git diff execution to use git library and improve context definition in checkModifications function.

* Update git.findModifiedFiles to use "base" instead of "branch"
  • Loading branch information
pelikhan authored Oct 1, 2024
1 parent 7296b07 commit 87f6694
Show file tree
Hide file tree
Showing 43 changed files with 1,368 additions and 186 deletions.
53 changes: 53 additions & 0 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Steps } from "@astrojs/starlight/components"
import { FileTree } from "@astrojs/starlight/components"
import { Image } from "astro:assets"
import { Code } from "@astrojs/starlight/components"
import prDescribeSrc from "../../../../../packages/sample/genaisrc/pr-describe.genai.js?raw"
import prDescribeSrc from "../../../../../packages/sample/genaisrc/pr-describe.genai.mjs?raw"

Once you have a script that you are happy with, you can automate it using the [command line interface](/genaiscript/reference/cli).

Expand Down
16 changes: 16 additions & 0 deletions docs/src/content/docs/reference/scripts/git.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Git
description: git helpers
sidebar:
order: 51
---

The `git` helper provides a thin wrapper around invoking the [git](https://git-scm.com/) executable.

## defaultBranch

Resolve the default branch, typically `main` or `master` in the repository.

```js
const df = await git.defaultBranch()
```
4 changes: 2 additions & 2 deletions genaisrc/commit-msg.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const msgContent = msg.content
if (msgContent) cancel("commit message already exists")

// Check for staged changes and stage all changes if none are staged
let diff = await host.exec("git diff --cached")
const diff = await git.diff({ staged: true, askStageOnEmpty: true })

if (!diff.stdout) cancel("no staged changes")
if (!diff) cancel("no staged changes")
// Generate commit message
const res = await runPrompt(
(_) => {
Expand Down
53 changes: 53 additions & 0 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async function setupTraceWriting(trace: MarkdownTrace, filename: string) {
const content = trace.content
writeFileSync(filename, content, { encoding: "utf-8" })
})
return filename
}

export async function runScriptWithExitCode(
Expand Down Expand Up @@ -179,12 +180,13 @@ export async function runScript(
if (removeOut) await emptyDir(out)
await ensureDir(out)
}
let outTraceFilename
if (outTrace && !/^false$/i.test(outTrace) && trace)
await setupTraceWriting(trace, outTrace)
outTraceFilename = await setupTraceWriting(trace, outTrace)
if (out && trace) {
const ofn = join(out, "res.trace.md")
if (ofn !== outTrace) {
await setupTraceWriting(trace, ofn)
outTraceFilename = await setupTraceWriting(trace, ofn)
}
}

Expand Down Expand Up @@ -521,6 +523,7 @@ export async function runScript(
if (failOnErrors && result.annotations?.some((a) => a.severity === "error"))
return fail("error annotations found", ANNOTATION_ERROR_CODE)

if (outTraceFilename) logVerbose(`trace: ${outTraceFilename}`)
logVerbose("genaiscript: done\n")
return { exitCode: 0, result }
}
4 changes: 2 additions & 2 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
renderShellOutput,
} from "./chatrender"
import { promptParametersSchemaToJSONSchema } from "./parameters"
import { fenceMD } from "./markdown"
import { fenceMD, prettifyMarkdown } from "./markdown"
import { YAMLStringify } from "./yaml"

export function toChatCompletionUserMessage(
Expand Down Expand Up @@ -647,6 +647,6 @@ export async function executeChatSession(

export function tracePromptResult(trace: MarkdownTrace, resp: RunPromptResult) {
const { json, text } = resp
trace.details(`🔠 output`, text, { expanded: true })
trace.details(`🔠 output`, prettifyMarkdown(text), { expanded: true })
if (resp.json) trace.detailsFenced("📩 JSON (parsed)", json, "json")
}
Loading

0 comments on commit 87f6694

Please sign in to comment.