Skip to content

Commit 5b68940

Browse files
Testclaude
andcommitted
Add getUncommittedChanges function to detect local git changes
- Implements function to detect staged, unstaged, and untracked files - Uses git diff --cached, git diff, and git ls-files commands - Returns deduplicated array of changed file paths - Includes comprehensive tests with beforeEach setup for git state - Foundation for making repository content aware of working directory changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9d686c8 commit 5b68940

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

www/repository/git-provider.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getContent,
1414
getDefaultBranch,
1515
getMatchingTags,
16+
getUncommittedChanges,
1617
lookupTagCommit,
1718
} from "./git-provider.ts";
1819
import { Repository, RepositoryRef } from "./types.ts";
@@ -293,6 +294,37 @@ describe("git-provider", () => {
293294
]);
294295
});
295296
});
297+
298+
describe("reading uncommitted changes", () => {
299+
beforeEach(function*() {
300+
// create a directory first
301+
yield* ensureDir(join(workspaceDir, "src"));
302+
303+
// checkout feature branch
304+
yield* cwd(workspaceDir, [
305+
$(`git checkout feature-branch`),
306+
// create a new file and stage it but don't commit
307+
$echo("staged content", "staged-file.txt"),
308+
$(`git add staged-file.txt`),
309+
// create a new file in directory but don't stage it
310+
$echo("unstaged content", "src/unstaged-file.txt"),
311+
// modify an existing file but don't stage it
312+
$echo("modified content", "file1.txt"),
313+
]);
314+
});
315+
316+
it("includes staged and unstaged changes", function*() {
317+
// verify that calling the function returns a list of both staged and unstaged files
318+
const [changes] = yield* cwd(workspaceDir, [
319+
getUncommittedChanges(),
320+
]);
321+
322+
expect(changes).toContain("staged-file.txt"); // staged new file
323+
expect(changes).toContain("src/unstaged-file.txt"); // unstaged new file in directory
324+
expect(changes).toContain("file1.txt"); // modified existing file
325+
expect(changes).toHaveLength(3);
326+
});
327+
})
296328
});
297329

298330
describe("createGitRepository", () => {

www/repository/git-provider.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,37 @@ export function* getMatchingTags(
8282
}
8383
}
8484

85+
/**
86+
* Get list of files with uncommitted changes (both staged and unstaged)
87+
*/
88+
export function* getUncommittedChanges(): Operation<string[]> {
89+
try {
90+
// Get staged files
91+
const stagedResult = yield* $(`git diff --cached --name-only`);
92+
const stagedFiles = stagedResult.stdout
93+
.split('\n')
94+
.filter(line => line.trim().length > 0);
95+
96+
// Get unstaged files (modified and untracked)
97+
const unstagedResult = yield* $(`git diff --name-only`);
98+
const modifiedFiles = unstagedResult.stdout
99+
.split('\n')
100+
.filter(line => line.trim().length > 0);
101+
102+
// Get untracked files
103+
const untrackedResult = yield* $(`git ls-files --others --exclude-standard`);
104+
const untrackedFiles = untrackedResult.stdout
105+
.split('\n')
106+
.filter(line => line.trim().length > 0);
107+
108+
// Combine all and remove duplicates
109+
const allChanges = [...stagedFiles, ...modifiedFiles, ...untrackedFiles];
110+
return [...new Set(allChanges)];
111+
} catch {
112+
return [];
113+
}
114+
}
115+
85116
/**
86117
* Get commit hash for a tag from a remote repository
87118
*/

0 commit comments

Comments
 (0)