Skip to content
Merged
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
39 changes: 34 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,36 @@ import {
debugResponse,
debugValidation,
} from "./debug.js";
import type { Config, BackendType } from "./types.js";
import type { Config, BackendType, DiffResult } from "./types.js";

function buildDiffContext(diffResult: DiffResult): string {
const parts: string[] = [];

if (diffResult.filesAdded.length > 0) {
parts.push(`Files added:\n${diffResult.filesAdded.slice(0, 5).join("\n")}`);
if (diffResult.filesAdded.length > 5) {
parts.push(` ... and ${diffResult.filesAdded.length - 5} more added`);
}
}

if (diffResult.filesDeleted.length > 0) {
parts.push(`Files deleted:\n${diffResult.filesDeleted.slice(0, 5).join("\n")}`);
if (diffResult.filesDeleted.length > 5) {
parts.push(` ... and ${diffResult.filesDeleted.length - 5} more deleted`);
}
}

if (diffResult.filesModified.length > 0) {
parts.push(`Files modified:\n${diffResult.filesModified.slice(0, 5).join("\n")}`);
if (diffResult.filesModified.length > 5) {
parts.push(` ... and ${diffResult.filesModified.length - 5} more modified`);
}
}

parts.push(`Stats: ${diffResult.stats}`);

return parts.join("\n");
}

async function promptUser(question: string, choices: string[]): Promise<string> {
const rl = createInterface({
Expand Down Expand Up @@ -235,7 +264,7 @@ async function handleSingleCommit(
}

debugDiff(diff, diffResult.files);
const context = `Files changed:\n${diffResult.files.slice(0, 5).join("\n")}\nStats: ${diffResult.stats}`;
const context = buildDiffContext(diffResult);

let message = await runCommitFlow(backend, cfg, diff, context, options.skipConfirm, options.constraints);

Expand Down Expand Up @@ -322,7 +351,7 @@ async function handleIndividualCommits(

console.log(chalk.bold(`\nProcessing: ${filePath}`));

const context = `File: ${filePath}\nStats: ${diffResult.stats}`;
const context = buildDiffContext(diffResult);
let message = await runCommitFlow(backend, cfg, diffResult.diff, context, options.skipConfirm, options.constraints);

if (message === null) {
Expand Down Expand Up @@ -487,7 +516,7 @@ export function createProgram(): Command {
diff = filterDiffByPatterns(diff, cfg.ignore_patterns);
}

const context = `Files changed:\n${diffResult.files.slice(0, 5).join("\n")}\nStats: ${diffResult.stats}`;
const context = buildDiffContext(diffResult);
const temperatures = [cfg.temperature, ...cfg.retry_temperatures];
const message = await generateMessage(backend, diff, context, temperatures, constraints);

Expand Down Expand Up @@ -658,7 +687,7 @@ export function createProgram(): Command {
console.log(` ... and ${diffResult.files.length - 10} more`);
}

const context = `Files changed: ${diffResult.files.slice(0, 5).join(", ")}\nStats: ${diffResult.stats}`;
const context = buildDiffContext(diffResult);
const prompt = buildSummarizePrompt(diffResult.diff, context);
debugPrompt(prompt);

Expand Down
62 changes: 62 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,31 @@ export function getStagedDiff(): DiffResult {
const filesOutput = runGitSafe("diff", "--cached", "--name-only");
const files = filesOutput.split("\n").filter((f) => f);

// Get file status (A=added, D=deleted, M=modified, R=renamed)
const statusOutput = runGitSafe("diff", "--cached", "--name-status");
const filesAdded: string[] = [];
const filesDeleted: string[] = [];
const filesModified: string[] = [];

statusOutput.split("\n").filter((f) => f).forEach((line) => {
const [status, ...pathParts] = line.split("\t");
const filePath = pathParts.join("\t"); // Handle filenames with tabs
if (status.startsWith("A")) {
filesAdded.push(filePath);
} else if (status.startsWith("D")) {
filesDeleted.push(filePath);
} else if (status.startsWith("M") || status.startsWith("R")) {
filesModified.push(filePath);
}
});

return {
diff,
stats,
files,
filesAdded,
filesDeleted,
filesModified,
isEmpty: !diff.trim(),
};
}
Expand All @@ -99,10 +120,30 @@ export function getFileDiff(filePath: string): DiffResult {
const stats = runGitSafe("diff", "--cached", "--stat", "--", filePath);
const files = diff ? [filePath] : [];

// Get file status for this specific file
const statusOutput = runGitSafe("diff", "--cached", "--name-status", "--", filePath);
const filesAdded: string[] = [];
const filesDeleted: string[] = [];
const filesModified: string[] = [];

if (statusOutput) {
const [status] = statusOutput.split("\t");
if (status.startsWith("A")) {
filesAdded.push(filePath);
} else if (status.startsWith("D")) {
filesDeleted.push(filePath);
} else if (status.startsWith("M") || status.startsWith("R")) {
filesModified.push(filePath);
}
}

return {
diff,
stats,
files,
filesAdded,
filesDeleted,
filesModified,
isEmpty: !diff.trim(),
};
}
Expand Down Expand Up @@ -185,10 +226,31 @@ export function getLastCommitDiff(): DiffResult {
const filesOutput = runGitSafe("diff", "HEAD~1", "HEAD", "--name-only");
const files = filesOutput.split("\n").filter((f) => f);

// Get file status for the last commit
const statusOutput = runGitSafe("diff", "HEAD~1", "HEAD", "--name-status");
const filesAdded: string[] = [];
const filesDeleted: string[] = [];
const filesModified: string[] = [];

statusOutput.split("\n").filter((f) => f).forEach((line) => {
const [status, ...pathParts] = line.split("\t");
const filePath = pathParts.join("\t");
if (status.startsWith("A")) {
filesAdded.push(filePath);
} else if (status.startsWith("D")) {
filesDeleted.push(filePath);
} else if (status.startsWith("M") || status.startsWith("R")) {
filesModified.push(filePath);
}
});

return {
diff,
stats,
files,
filesAdded,
filesDeleted,
filesModified,
isEmpty: !diff.trim(),
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export interface DiffResult {
diff: string;
stats: string;
files: string[];
filesAdded: string[];
filesDeleted: string[];
filesModified: string[];
isEmpty: boolean;
}

Expand Down