Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 16 additions & 16 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"chalk": "^5.6.2",
"commander": "^14.0.3",
"cosmiconfig": "^9.0.0",
"ignore": "^7.0.5",
"inquirer": "^13.2.4",
"node-fetch": "^3.3.2",
"ora": "^9.3.0",
Expand Down
15 changes: 15 additions & 0 deletions src/analyzer/fileFilter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { execFileSync } from "node:child_process";
import fs from 'fs';
import path from 'path';
import ignore from 'ignore';

export function getFilteredFiles(files: string[], rootDir: string): string[] {
const ig = ignore();

const ignorePath = path.join(rootDir, '.gitbunignore');
if (fs.existsSync(ignorePath)) {
ig.add(fs.readFileSync(ignorePath).toString());
} else {
ig.add(['package-lock.json', 'dist/', 'build/', 'node_modules/']);
}

return files.filter(file => !ig.ignores(file));
}
export type FileChange = {
path: string;
additions: number;
Expand Down
55 changes: 49 additions & 6 deletions src/git/getStagedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,76 @@
import simpleGit from "simple-git";
import fs from "fs";
import path from "path";
import ignore from "ignore";
import { execSync } from "child_process";

const git = simpleGit();

export type FileStatus = "A" | "M" | "D";

export async function getStagedFiles(): Promise<{ path: string; status: FileStatus }[]> {
/**
* Helper function to safely fetch the root absolute path of the current Git repository.
*/
function getGitRepoRoot(): string {
try {
return execSync("git rev-parse --show-toplevel", { encoding: "utf8" }).trim();
} catch (error) {
// Fallback to process.cwd() if not inside a valid git repo
return process.cwd();
}
}

export async function getStagedFiles(): Promise<
{ path: string; status: FileStatus }[]
> {
const output = await git.diff(["--cached", "--name-status"]);

const lines = output
.split("\n")
.map(l => l.trim())
.map((l) => l.trim())
.filter(Boolean);

return lines.map(line => {
const files = lines.map((line) => {
const parts = line.split(/\s+/);
let status = parts[0];
if (status === "R") {
return {
path: parts[2],
status: "M" as FileStatus
status: "M" as FileStatus,
};
}

if (status !== "A" && status !== "M" && status !== "D") {
status = "M";
}
return {
path: parts[1],
status: status as FileStatus
status: status as FileStatus,
};
});

// --- GITBUNIGNORE FILTERING LOGIC ---
const ig = ignore();

// FIXED: Now dynamically resolves the actual git repository root directory
const repoRoot = getGitRepoRoot();
const ignorePath = path.join(repoRoot, ".gitbunignore");

// 1. Check if .gitbunignore exists at the root
if (fs.existsSync(ignorePath)) {
ig.add(fs.readFileSync(ignorePath).toString());
} else {
// 2. Default out-of-the-box exclusions to save tokens
ig.add([
"package-lock.json",
"yarn.lock",
"pnpm-lock.yaml",
"dist/",
"build/",
"node_modules/",
]);
}

// 3. Filter the staged files array before returning it
return files.filter((file) => !ig.ignores(file.path));
}