Skip to content

Commit

Permalink
Refactor GitClient to use a unified exec method for executing git com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
pelikhan committed Sep 30, 2024
1 parent 81f2cb5 commit 682c2d3
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 8 deletions.
6 changes: 6 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.

6 changes: 6 additions & 0 deletions genaisrc/genaiscript.d.ts

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

6 changes: 6 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.

6 changes: 6 additions & 0 deletions packages/core/src/genaisrc/genaiscript.d.ts

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

28 changes: 20 additions & 8 deletions packages/core/src/git.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolveFileContents } from "./file"
import { isGlobMatch } from "./glob"
import { runtimeHost } from "./host"
import { shellParse } from "./shell"
import { arrayify } from "./util"

export class GitClient implements Git {
Expand All @@ -10,20 +11,31 @@ export class GitClient implements Git {
async defaultBranch(): Promise<string> {
if (!this._defaultBranch) {
const res = (
await runtimeHost.exec(
undefined,
this.git,
await this.exec(
["symbolic-ref", "refs/remotes/origin/HEAD"],
{}
)
).stdout
)
.replace("refs/remotes/origin/", "")
.trim()
this._defaultBranch = res
}
return this._defaultBranch
}

async exec(
args: string | string[],
options?: { label?: string }
): Promise<string> {
const res = await runtimeHost.exec(
undefined,
this.git,
Array.isArray(args) ? args : shellParse(args),
options
)
return res.stdout

Check failure on line 36 in packages/core/src/git.ts

View workflow job for this annotation

GitHub Actions / build

The `exec` method does not handle errors. It's recommended to add a try-catch block to handle potential errors during the execution of the git command. 🛠️
}

async findModifiedFiles(
scope: "branch" | "staged" | "modified",
options?: {
Expand All @@ -49,18 +61,18 @@ export class GitClient implements Git {
args.push(...paths)
args.push(...excludedPaths.map((p) => ":!" + p))
}
const res = await runtimeHost.exec(undefined, this.git, args, {
const res = await this.exec(args, {
label: `git list modified files in ${scope}`,
})
filenames = res.stdout.split("\n").filter((f) => f)
filenames = res.split("\n").filter((f) => f)
} else {
// ignore deleted files
const rx = /^\s*(A|M|\?{1,2})\s+/gm
const args = ["status", "--porcelain"]
const res = await runtimeHost.exec(undefined, this.git, args, {
const res = await this.exec(args, {
label: `git list modified files`,
})
filenames = res.stdout
filenames = res
.split("\n")
.filter((f) => rx.test(f))
.map((f) => f.replace(rx, "").trim())
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,12 @@ interface Git {
*/
defaultBranch(): Promise<string>

/**
* Executes a git command in the repository and returns the stdout
* @param cmd
*/
exec(args: string[] | string, options?: { label?: string }): Promise<string>

/**
* Finds specific files in the git repository.
* By default, work
Expand Down
6 changes: 6 additions & 0 deletions packages/sample/genaisrc/blog/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/genaisrc/genaiscript.d.ts

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

3 changes: 3 additions & 0 deletions packages/sample/genaisrc/git.genai.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const defaultBranch = await git.defaultBranch()
console.log({ defaultBranch })

const mods = await git.findModifiedFiles("modified", {
paths: ["**/*.ts"],
excludedPaths: ["**/genaiscript.d.ts"],
Expand Down
6 changes: 6 additions & 0 deletions packages/sample/genaisrc/node/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/genaisrc/python/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/genaisrc/style/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/src/aici/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/src/errors/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/src/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/src/makecode/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/src/tla/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/sample/src/vision/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions packages/vscode/genaisrc/genaiscript.d.ts

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

6 changes: 6 additions & 0 deletions slides/genaisrc/genaiscript.d.ts

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

0 comments on commit 682c2d3

Please sign in to comment.