Skip to content

Commit

Permalink
Refactor GitClient for scope handling and add diff method with filters
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 30, 2024
1 parent 682c2d3 commit 6897762
Show file tree
Hide file tree
Showing 20 changed files with 386 additions and 46 deletions.
19 changes: 18 additions & 1 deletion docs/genaisrc/genaiscript.d.ts

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

19 changes: 18 additions & 1 deletion genaisrc/genaiscript.d.ts

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

19 changes: 18 additions & 1 deletion packages/auto/genaiscript.d.ts

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

19 changes: 18 additions & 1 deletion packages/core/src/genaisrc/genaiscript.d.ts

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

64 changes: 55 additions & 9 deletions packages/core/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class GitClient implements Git {
}

async findModifiedFiles(
scope: "branch" | "staged" | "modified",
scope: "base" | "staged" | "modified",
options?: {
base?: string
paths?: ElementOrArray<string>
Expand All @@ -49,18 +49,13 @@ export class GitClient implements Git {
(f) => !!f
)
let filenames: string[]
if (scope === "branch" || scope === "staged") {
if (scope === "base" || scope === "staged") {
const args = ["diff", "--name-only", "--diff-filter=AM"]
if (scope === "branch") {
if (scope === "base") {
const base = options?.base || (await this.defaultBranch())
args.push(base)
} else args.push("--cached")
if (paths.length > 0 || excludedPaths.length > 0) {
if (!paths.length) paths.push(".")
args.push("--")
args.push(...paths)
args.push(...excludedPaths.map((p) => ":!" + p))
}
GitClient.addFileFilters(paths, excludedPaths, args)
const res = await this.exec(args, {
label: `git list modified files in ${scope}`,
})
Expand Down Expand Up @@ -88,4 +83,55 @@ export class GitClient implements Git {
await resolveFileContents(files)
return files
}

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

View workflow job for this annotation

GitHub Actions / build

The `findModifiedFiles` function does not handle errors. Consider adding a try-catch block to handle potential exceptions. 🛠️

private static addFileFilters(
paths: string[],
excludedPaths: string[],
args: string[]
) {
if (paths.length > 0 || excludedPaths.length > 0) {
if (!paths.length) paths.push(".")
args.push("--")
args.push(...paths)
args.push(...excludedPaths.map((p) => ":!" + p))
}
}

async diff(options?: {
staged?: boolean
askStageOnEmpty?: boolean
base?: string
head?: string
paths?: ElementOrArray<string>
excludedPaths?: ElementOrArray<string>
unified?: number
}): Promise<string> {
const paths = arrayify(options?.paths).filter((f) => !!f)
const excludedPaths = arrayify(options?.excludedPaths).filter(
(f) => !!f
)
let { staged, base, head, unified, askStageOnEmpty } = options || {}
const args = ["diff"]
if (staged) args.push("--staged")
args.push("--ignore-all-space")
if (unified > 0) args.push(`--unified=${unified}`)
if (base && !head) head = "head"
if (head && !base) base = head + "^"
if (base && head) args.push(`${base}..${head}`)
GitClient.addFileFilters(paths, excludedPaths, args)
let res = await this.exec(args)
if (!res && staged && askStageOnEmpty) {
const stage = await host.confirm(
"No staged changes. Stage all changes?",
{
default: true,
}
)
if (stage) {
await this.exec(["add", "."])
res = await this.diff(options)
}
}
return res
}

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

View workflow job for this annotation

GitHub Actions / build

The `diff` function does not handle errors. Consider adding a try-catch block to handle potential exceptions. 🛠️
}
19 changes: 18 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1291,13 +1291,30 @@ interface Git {
* @param options
*/
findModifiedFiles(
scope: "branch" | "staged" | "modified",
scope: "base" | "staged" | "modified",
options?: {
base?: string
paths?: ElementOrArray<string>
excludedPaths?: ElementOrArray<string>
}
): Promise<WorkspaceFile[]>

/**
*
* @param options
*/
diff(options?: {
staged?: boolean
/**
* Ask the user to stage the changes if the diff is empty.
*/
askStageOnEmpty?: boolean
base?: string
head?: string
paths?: ElementOrArray<string>
excludedPaths?: ElementOrArray<string>
unified?: number
}): Promise<string>
}

interface GitHubOptions {
Expand Down
19 changes: 18 additions & 1 deletion packages/sample/genaisrc/blog/genaiscript.d.ts

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

19 changes: 18 additions & 1 deletion packages/sample/genaisrc/genaiscript.d.ts

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

19 changes: 18 additions & 1 deletion packages/sample/genaisrc/node/genaiscript.d.ts

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

19 changes: 18 additions & 1 deletion packages/sample/genaisrc/python/genaiscript.d.ts

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

Loading

0 comments on commit 6897762

Please sign in to comment.