Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,49 @@ function setupRepositoryWatcher(context: vscode.ExtensionContext, repository: Re
const indexPath = vscode.Uri.joinPath(repository.rootUri, '.git/index');
const watcher = vscode.workspace.createFileSystemWatcher(indexPath.fsPath);

let debounceHandle: NodeJS.Timeout | undefined;
let lastFingerprint = '';

const handleIndexChange = () => {
setTimeout(() => {
if (repository.state.indexChanges.length > 0) {
onFilesStaged();
if (debounceHandle) {
clearTimeout(debounceHandle);
}

debounceHandle = setTimeout(() => {
const staged = repository.state.indexChanges ?? [];

if (staged.length === 0) {
// Reset fingerprint so the next staging event is picked up.
lastFingerprint = '';
return;
}
}, 100);

const fingerprint = staged
.map(change => change.uri.fsPath)
.sort()
.join('|');

if (fingerprint === lastFingerprint) {
return;
}

lastFingerprint = fingerprint;
onFilesStaged();
}, 250);
};

watcher.onDidChange(handleIndexChange);
watcher.onDidCreate(handleIndexChange);
watcher.onDidDelete(handleIndexChange);

context.subscriptions.push(watcher);
context.subscriptions.push(
new vscode.Disposable(() => {
if (debounceHandle) {
clearTimeout(debounceHandle);
}
})
);
}

export async function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -226,4 +256,4 @@ export async function activate(context: vscode.ExtensionContext) {
});
}

export function deactivate() {}
export function deactivate() {}