44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
/**
|
|
* Git Tracker — discover changed files via git.
|
|
*/
|
|
import { execSync } from 'node:child_process';
|
|
|
|
export function getGitRoot(cwd: string): string | null {
|
|
try {
|
|
return execSync('git rev-parse --show-toplevel', { cwd, encoding: 'utf-8' }).trim();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function getChangedFiles(cwd: string, sinceRef?: string): string[] {
|
|
try {
|
|
const cmd = sinceRef
|
|
? `git diff --name-only ${sinceRef} --`
|
|
: 'git diff --name-only HEAD~1 --';
|
|
const out = execSync(cmd, { cwd, encoding: 'utf-8' }).trim();
|
|
if (!out) return [];
|
|
return out.split('\n').filter(Boolean);
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export function getAllTrackedFiles(cwd: string): string[] {
|
|
try {
|
|
const out = execSync('git ls-files', { cwd, encoding: 'utf-8' }).trim();
|
|
if (!out) return [];
|
|
return out.split('\n').filter(Boolean);
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export function getLastCommitDate(cwd: string): string | null {
|
|
try {
|
|
return execSync('git log -1 --format=%cI', { cwd, encoding: 'utf-8' }).trim();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|