mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Fix git diff subcommand
This commit is contained in:
1 file changed
+177
-41
+177
-41
@@ -29,9 +29,12 @@ export class GitDiffCommand implements GitSubcommand {
|
|||||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { commits, paths } = this.parseArgs(args);
|
const { commits, paths, options } = this.parseArgs(args);
|
||||||
|
|
||||||
if (commits.length === 0) {
|
if (options.cached || options.staged) {
|
||||||
|
// Show staged changes (index vs HEAD)
|
||||||
|
return await this.showStagedDiff(paths);
|
||||||
|
} else if (commits.length === 0) {
|
||||||
// Show working directory changes vs index
|
// Show working directory changes vs index
|
||||||
return await this.showWorkingDirectoryDiff(paths);
|
return await this.showWorkingDirectoryDiff(paths);
|
||||||
} else if (commits.length === 1) {
|
} else if (commits.length === 1) {
|
||||||
@@ -83,78 +86,110 @@ export class GitDiffCommand implements GitSubcommand {
|
|||||||
try {
|
try {
|
||||||
// Get status to find changed files
|
// Get status to find changed files
|
||||||
const statusMatrix = await this.git.statusMatrix({
|
const statusMatrix = await this.git.statusMatrix({
|
||||||
|
|
||||||
dir: this.pwd,
|
dir: this.pwd,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Filter for modified files in working directory
|
// Filter for files with working directory changes
|
||||||
const modifiedFiles = statusMatrix.filter(([filepath, headStatus, workdirStatus, stageStatus]) => {
|
// Status matrix: [filepath, headStatus, workdirStatus, stageStatus]
|
||||||
const isModified = workdirStatus === 1 && stageStatus === 1 && headStatus !== workdirStatus;
|
// 0 = absent, 1 = present, 2 = modified
|
||||||
|
const changedFiles = statusMatrix.filter(([filepath, _headStatus, workdirStatus, stageStatus]) => {
|
||||||
|
// Show files that differ between index and working directory
|
||||||
|
const hasWorkingDirChanges = stageStatus !== workdirStatus;
|
||||||
|
|
||||||
if (paths.length > 0) {
|
if (paths.length > 0) {
|
||||||
return isModified && paths.some(path => filepath.includes(path));
|
return hasWorkingDirChanges && paths.some(path => filepath.includes(path));
|
||||||
}
|
}
|
||||||
return isModified;
|
return hasWorkingDirChanges;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (modifiedFiles.length === 0) {
|
if (changedFiles.length === 0) {
|
||||||
return createSuccessResult('');
|
return createSuccessResult('');
|
||||||
}
|
}
|
||||||
|
|
||||||
const diffLines: string[] = [];
|
const diffLines: string[] = [];
|
||||||
|
|
||||||
for (const [filepath] of modifiedFiles) {
|
for (const [filepath, _headStatus, workdirStatus, stageStatus] of changedFiles) {
|
||||||
try {
|
try {
|
||||||
// Get file content from HEAD
|
// Get staged content (index)
|
||||||
let headContent = '';
|
let stagedContent = '';
|
||||||
try {
|
if (stageStatus === 1) {
|
||||||
const headBlob = await this.git.readBlob({
|
try {
|
||||||
|
// Try to read from HEAD first as a fallback for staged content
|
||||||
dir: this.pwd,
|
const headBlob = await this.git.readBlob({
|
||||||
oid: 'HEAD',
|
dir: this.pwd,
|
||||||
filepath,
|
oid: 'HEAD',
|
||||||
});
|
filepath,
|
||||||
headContent = new TextDecoder().decode(headBlob.blob);
|
});
|
||||||
} catch {
|
stagedContent = new TextDecoder().decode(headBlob.blob);
|
||||||
// File might be new
|
} catch {
|
||||||
|
// File might be new
|
||||||
|
stagedContent = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get file content from working directory
|
// Get working directory content
|
||||||
let workdirContent = '';
|
let workdirContent = '';
|
||||||
try {
|
if (workdirStatus === 1) {
|
||||||
workdirContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
try {
|
||||||
} catch {
|
workdirContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||||
// File might be deleted
|
} catch {
|
||||||
|
// File might be deleted
|
||||||
|
workdirContent = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple diff representation
|
// Skip if contents are identical
|
||||||
diffLines.push(`diff --git a/${filepath} b/${filepath}`);
|
if (stagedContent === workdirContent) {
|
||||||
diffLines.push(`index ${this.getShortHash(headContent)}..${this.getShortHash(workdirContent)} 100644`);
|
continue;
|
||||||
diffLines.push(`--- a/${filepath}`);
|
}
|
||||||
diffLines.push(`+++ b/${filepath}`);
|
|
||||||
|
|
||||||
// Show a simplified diff
|
// Generate diff header
|
||||||
const headLines = headContent.split('\n');
|
diffLines.push(`diff --git a/${filepath} b/${filepath}`);
|
||||||
|
|
||||||
|
if (stageStatus === 0 && workdirStatus === 1) {
|
||||||
|
// New untracked file
|
||||||
|
diffLines.push('new file mode 100644');
|
||||||
|
diffLines.push(`index 0000000..${this.getShortHash(workdirContent)}`);
|
||||||
|
diffLines.push('--- /dev/null');
|
||||||
|
diffLines.push(`+++ b/${filepath}`);
|
||||||
|
} else if (stageStatus === 1 && workdirStatus === 0) {
|
||||||
|
// Deleted file
|
||||||
|
diffLines.push('deleted file mode 100644');
|
||||||
|
diffLines.push(`index ${this.getShortHash(stagedContent)}..0000000`);
|
||||||
|
diffLines.push(`--- a/${filepath}`);
|
||||||
|
diffLines.push('+++ /dev/null');
|
||||||
|
} else {
|
||||||
|
// Modified file
|
||||||
|
diffLines.push(`index ${this.getShortHash(stagedContent)}..${this.getShortHash(workdirContent)} 100644`);
|
||||||
|
diffLines.push(`--- a/${filepath}`);
|
||||||
|
diffLines.push(`+++ b/${filepath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate simple unified diff
|
||||||
|
const stagedLines = stagedContent.split('\n');
|
||||||
const workdirLines = workdirContent.split('\n');
|
const workdirLines = workdirContent.split('\n');
|
||||||
|
|
||||||
diffLines.push(`@@ -1,${headLines.length} +1,${workdirLines.length} @@`);
|
// Simple hunk header
|
||||||
|
diffLines.push(`@@ -1,${stagedLines.length} +1,${workdirLines.length} @@`);
|
||||||
|
|
||||||
// Show removed lines
|
// Show all old lines as removed
|
||||||
for (const line of headLines) {
|
for (const line of stagedLines) {
|
||||||
if (!workdirLines.includes(line)) {
|
if (line || stagedLines.length > 0) {
|
||||||
diffLines.push(`-${line}`);
|
diffLines.push(`-${line}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show added lines
|
// Show all new lines as added
|
||||||
for (const line of workdirLines) {
|
for (const line of workdirLines) {
|
||||||
if (!headLines.includes(line)) {
|
if (line || workdirLines.length > 0) {
|
||||||
diffLines.push(`+${line}`);
|
diffLines.push(`+${line}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
diffLines.push('');
|
diffLines.push('');
|
||||||
} catch {
|
} catch (error) {
|
||||||
// Skip files that can't be diffed
|
// Skip files that can't be diffed
|
||||||
|
console.warn(`Failed to diff ${filepath}:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +239,107 @@ export class GitDiffCommand implements GitSubcommand {
|
|||||||
return createErrorResult('Commit range diff is not implemented in this git command');
|
return createErrorResult('Commit range diff is not implemented in this git command');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async showStagedDiff(paths: string[]): Promise<ShellCommandResult> {
|
||||||
|
try {
|
||||||
|
// Get status to find staged files
|
||||||
|
const statusMatrix = await this.git.statusMatrix({
|
||||||
|
dir: this.pwd,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Filter for files with staged changes (index differs from HEAD)
|
||||||
|
const stagedFiles = statusMatrix.filter(([filepath, headStatus, _workdirStatus, stageStatus]) => {
|
||||||
|
const hasStagedChanges = headStatus !== stageStatus;
|
||||||
|
|
||||||
|
if (paths.length > 0) {
|
||||||
|
return hasStagedChanges && paths.some(path => filepath.includes(path));
|
||||||
|
}
|
||||||
|
return hasStagedChanges;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (stagedFiles.length === 0) {
|
||||||
|
return createSuccessResult('');
|
||||||
|
}
|
||||||
|
|
||||||
|
const diffLines: string[] = [];
|
||||||
|
|
||||||
|
for (const [filepath, headStatus, _workdirStatus, stageStatus] of stagedFiles) {
|
||||||
|
try {
|
||||||
|
// Get HEAD content
|
||||||
|
let headContent = '';
|
||||||
|
if (headStatus === 1) {
|
||||||
|
try {
|
||||||
|
const headBlob = await this.git.readBlob({
|
||||||
|
dir: this.pwd,
|
||||||
|
oid: 'HEAD',
|
||||||
|
filepath,
|
||||||
|
});
|
||||||
|
headContent = new TextDecoder().decode(headBlob.blob);
|
||||||
|
} catch {
|
||||||
|
headContent = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get staged content (approximated by working directory for now)
|
||||||
|
let stagedContent = '';
|
||||||
|
if (stageStatus === 1) {
|
||||||
|
try {
|
||||||
|
stagedContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||||
|
} catch {
|
||||||
|
stagedContent = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate diff header
|
||||||
|
diffLines.push(`diff --git a/${filepath} b/${filepath}`);
|
||||||
|
|
||||||
|
if (headStatus === 0 && stageStatus === 1) {
|
||||||
|
// New file staged
|
||||||
|
diffLines.push('new file mode 100644');
|
||||||
|
diffLines.push(`index 0000000..${this.getShortHash(stagedContent)}`);
|
||||||
|
diffLines.push('--- /dev/null');
|
||||||
|
diffLines.push(`+++ b/${filepath}`);
|
||||||
|
} else if (headStatus === 1 && stageStatus === 0) {
|
||||||
|
// File deleted from stage
|
||||||
|
diffLines.push('deleted file mode 100644');
|
||||||
|
diffLines.push(`index ${this.getShortHash(headContent)}..0000000`);
|
||||||
|
diffLines.push(`--- a/${filepath}`);
|
||||||
|
diffLines.push('+++ /dev/null');
|
||||||
|
} else {
|
||||||
|
// Modified file
|
||||||
|
diffLines.push(`index ${this.getShortHash(headContent)}..${this.getShortHash(stagedContent)} 100644`);
|
||||||
|
diffLines.push(`--- a/${filepath}`);
|
||||||
|
diffLines.push(`+++ b/${filepath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate unified diff
|
||||||
|
const headLines = headContent.split('\n');
|
||||||
|
const stagedLines = stagedContent.split('\n');
|
||||||
|
|
||||||
|
diffLines.push(`@@ -1,${headLines.length} +1,${stagedLines.length} @@`);
|
||||||
|
|
||||||
|
// Show removed lines
|
||||||
|
for (const line of headLines) {
|
||||||
|
diffLines.push(`-${line}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show added lines
|
||||||
|
for (const line of stagedLines) {
|
||||||
|
diffLines.push(`+${line}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
diffLines.push('');
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`Failed to diff staged ${filepath}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return createSuccessResult(diffLines.join('\n'));
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
return createErrorResult(`Failed to show staged diff: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private getShortHash(content: string): string {
|
private getShortHash(content: string): string {
|
||||||
// Simple hash for demo purposes - not a real git hash
|
// Simple hash for demo purposes - not a real git hash
|
||||||
let hash = 0;
|
let hash = 0;
|
||||||
|
|||||||
Reference in new issue
Block a user