Skip to content

Commit

Permalink
Add GitClient methods for lastTag and log; update yarn.lock exclusion…
Browse files Browse the repository at this point in the history
… patterns in scripts
  • Loading branch information
pelikhan committed Oct 1, 2024
1 parent cd9d0d8 commit 04def7d
Show file tree
Hide file tree
Showing 25 changed files with 455 additions and 9 deletions.
22 changes: 22 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.

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

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

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

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

46 changes: 46 additions & 0 deletions packages/core/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export class GitClient implements Git {
excludedPaths: string[],
args: string[]
) {
paths = paths || []
excludedPaths = excludedPaths || []
if (paths.length > 0 || excludedPaths.length > 0) {
if (!paths.length) paths.push(".")
args.push("--")
Expand All @@ -158,6 +160,50 @@ export class GitClient implements Git {
}
}

async lastTag(): Promise<string> {
const res = await this.exec([
"describe",
"--tags",
"--abbrev=0",
"HEAD^",
])
return res.split("\n")[0]
}

async log(options?: {
base?: string
head?: string
merges?: boolean
excludedGrep?: string | RegExp
paths: string[]
excludedPaths: string[]
}): Promise<GitCommit[]> {
const { base, head, merges, excludedGrep, paths, excludedPaths } =
options || {}
const args = ["log", "--pretty=oneline"]
if (!merges) args.push("--no-merges")
if (excludedGrep) {
const pattern =
typeof excludedGrep === "string"
? excludedGrep
: excludedGrep.source
args.push(`--grep='${pattern}'`, "--invert-grep")
}
if (base && head) args.push(`${base}..${head}`)
GitClient.addFileFilters(paths, excludedPaths, args)
const res = await this.exec(args)
const commits = res
.split("\n")
.map(
(line) =>
/^(?<sha>[a-z0-9]{40,40})\s+(?<message>.*)$/.exec(line)
?.groups
)
.filter((g) => !!g)
.map((g) => <GitCommit>{ sha: g?.sha, message: g?.message })
return commits
}

/**
* Generates a diff of changes based on provided options.
* @param options Options such as staged flag, base, head, paths, and exclusions.
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,12 +1274,22 @@ interface HTML {
convertToMarkdown(html: string): Promise<string>
}

interface GitCommit {
sha: string
message: string
}

interface Git {
/**
* Resolves the default branch for this repository
*/
defaultBranch(): Promise<string>

/**
* Gets the last tag in the repository
*/
lastTag(): Promise<string>

/**
* Gets the current branch of the repository
*/
Expand Down Expand Up @@ -1334,6 +1344,18 @@ interface Git {
*/
llmify?: boolean
}): Promise<string>

/**
* Lists the commits in the git repository
*/
log(options?: {
base?: string
head?: string
merges?: boolean
excludedGrep?: string | RegExp
paths: string[]
excludedPaths: string[]
}): Promise<GitCommit[]>
}

interface GitHubOptions {
Expand Down
22 changes: 22 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.

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

8 changes: 4 additions & 4 deletions packages/sample/genaisrc/git-release-notes.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
script({ system: ["system"], temperature: 0.5, model: "openai:gpt-4-turbo" })
script({ system: ["system"], temperature: 0.5, model: "openai:gpt-4o" })

const product = env.vars.product || "GenAIScript"

Expand All @@ -7,7 +7,7 @@ const { version } = await workspace.readJSON("package.json")
const tag = await git.exec(["describe", "--tags", "--abbrev=0", "HEAD^"])
const commits = await git.exec([
"log",
"--grep='(skip ci|THIRD_PARTY_NOTICES)'",
"--grep='(skip ci|THIRD_PARTY_NOTICES|genai)'",
"--invert-grep",
"--no-merges",
`HEAD...${tag}`,
Expand All @@ -22,8 +22,8 @@ const diff = await git.diff({
"docs/**",
".github/*",
".vscode/*",
"*yarn.lock",
"*THIRD_PARTY_NOTICES.md",
"**/yarn.lock",
"THIRD_PARTY_NOTICES.md",
],
})

Expand Down
6 changes: 5 additions & 1 deletion packages/sample/genaisrc/git.genai.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const defaultBranch = await git.defaultBranch()
const branch = await git.branch()
const tag = await git.lastTag()
const branches = await git.listBranches()
console.log({ defaultBranch, branch, branches })
console.log({ defaultBranch, branch, branches, tag })

const mods = await git.listFiles("modified", {
paths: ["**/*.ts"],
Expand All @@ -20,3 +21,6 @@ const files = await git.listFiles("modified-base", {
excludedPaths: ["**/genaiscript.d.ts"],
})
console.log({ files })

const log = await git.log()
console.log({ log })
Loading

0 comments on commit 04def7d

Please sign in to comment.