Skip to content

Commit 172c41d

Browse files
Copilotpelikhan
andcommitted
Refactor changedFilesSince to changedFiles with optional since parameter and Set for deduplication
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent da5a204 commit 172c41d

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

packages/cli/genaisrc/system.git.genai.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export default function (ctx: ChatGenerationContext) {
137137
})
138138

139139
defTool(
140-
"git_changed_files_since",
140+
"git_changed_files",
141141
"Lists files that have been modified since a specific date or elapsed time.",
142142
{
143143
type: "object",
@@ -163,11 +163,11 @@ export default function (ctx: ChatGenerationContext) {
163163
},
164164
},
165165
},
166-
required: ["since"],
167166
},
168167
async (args) => {
169168
const { since, paths, excludedPaths } = args
170-
const files = await client.changedFilesSince(since, {
169+
const files = await client.changedFiles({
170+
since,
171171
paths,
172172
excludedPaths,
173173
})

packages/core/src/git.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@ import assert from "node:assert/strict"
33
import { GitClient } from "./git"
44

55
describe("GitClient", () => {
6-
test("changedFilesSince method exists", () => {
6+
test("changedFiles method exists", () => {
77
const client = new GitClient(".")
8-
assert(typeof client.changedFilesSince === "function", "changedFilesSince method should exist")
8+
assert(typeof client.changedFiles === "function", "changedFiles method should exist")
99
})
1010

11-
test("changedFilesSince method signature", () => {
11+
test("changedFiles method signature", () => {
1212
const client = new GitClient(".")
1313
// Test that the method accepts the expected parameters
14-
const method = client.changedFilesSince
15-
assert.equal(method.length, 2, "changedFilesSince should accept 2 parameters")
14+
const method = client.changedFiles
15+
assert.equal(method.length, 1, "changedFiles should accept 1 parameter")
1616
})
1717

18-
test("changedFilesSince returns Promise", () => {
18+
test("changedFiles returns Promise", () => {
1919
const client = new GitClient(".")
20-
const result = client.changedFilesSince("1 hour ago")
21-
assert(result instanceof Promise, "changedFilesSince should return a Promise")
20+
const result = client.changedFiles({ since: "1 hour ago" })
21+
assert(result instanceof Promise, "changedFiles should return a Promise")
22+
})
23+
24+
test("changedFiles returns empty array when no since parameter", async () => {
25+
const client = new GitClient(".")
26+
const result = await client.changedFiles()
27+
assert(Array.isArray(result), "changedFiles should return an array")
28+
assert.equal(result.length, 0, "changedFiles should return empty array when no since parameter")
2229
})
2330
})

packages/core/src/git.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,24 @@ export class GitClient implements Git {
403403

404404
/**
405405
* Lists files that have been modified since a specific date or elapsed time.
406-
* @param since Date string (ISO format) or elapsed time (e.g., "2 hours ago", "1 day ago")
407-
* @param options Optional settings such as paths and exclusions
406+
* @param options Optional settings including since date, paths and exclusions
408407
* @returns {Promise<WorkspaceFile[]>} List of modified files since the specified time
409408
*/
410-
async changedFilesSince(
411-
since: string,
412-
options?: {
413-
paths?: ElementOrArray<string>
414-
excludedPaths?: ElementOrArray<string>
409+
async changedFiles(options?: {
410+
since?: string
411+
paths?: ElementOrArray<string>
412+
excludedPaths?: ElementOrArray<string>
413+
}): Promise<WorkspaceFile[]> {
414+
const { since, paths: optionPaths, excludedPaths: optionExcludedPaths } = options || {}
415+
416+
if (!since) {
417+
dbg(`no since parameter provided, returning empty list`)
418+
return []
415419
}
416-
): Promise<WorkspaceFile[]> {
420+
417421
dbg(`listing files changed since: ${since}`)
418-
const paths = arrayify(options?.paths, { filterEmpty: true })
419-
const excludedPaths = await this.resolveExcludedPaths(options)
422+
const paths = arrayify(optionPaths, { filterEmpty: true })
423+
const excludedPaths = await this.resolveExcludedPaths({ excludedPaths: optionExcludedPaths })
420424

421425
// Use git log to get files that changed since the specified date/time
422426
const args = ["log", "--name-only", "--pretty=format:", `--since=${since}`]
@@ -427,11 +431,12 @@ export class GitClient implements Git {
427431
label: `git list files changed since ${since}`,
428432
})
429433

430-
// Parse the output and remove duplicates
431-
const filenames = res
432-
.split("\n")
433-
.filter((f) => f.trim() !== "")
434-
.filter((f, index, arr) => arr.indexOf(f) === index) // Remove duplicates
434+
// Parse the output and remove duplicates using Set
435+
const uniqueFilenames = new Set(
436+
res.split("\n")
437+
.filter((f) => f.trim() !== "")
438+
)
439+
const filenames = Array.from(uniqueFilenames)
435440

436441
dbg(`found ${filenames.length} files changed since ${since}`)
437442

packages/core/src/types/prompt_template.d.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,16 +3258,13 @@ interface Git {
32583258

32593259
/**
32603260
* Lists files that have been modified since a specific date or elapsed time
3261-
* @param since Date string (ISO format) or elapsed time (e.g., "2 hours ago", "1 day ago")
3262-
* @param options Optional settings such as paths and exclusions
3261+
* @param options Optional settings including since date, paths and exclusions
32633262
*/
3264-
changedFilesSince(
3265-
since: string,
3266-
options?: {
3267-
paths?: ElementOrArray<string>
3268-
excludedPaths?: ElementOrArray<string>
3269-
}
3270-
): Promise<WorkspaceFile[]>
3263+
changedFiles(options?: {
3264+
since?: string
3265+
paths?: ElementOrArray<string>
3266+
excludedPaths?: ElementOrArray<string>
3267+
}): Promise<WorkspaceFile[]>
32713268

32723269
/**
32733270
* Create a shallow git clone

0 commit comments

Comments
 (0)