mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +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');
|
||||
}
|
||||
|
||||
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
|
||||
return await this.showWorkingDirectoryDiff(paths);
|
||||
} else if (commits.length === 1) {
|
||||
@@ -83,78 +86,110 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
try {
|
||||
// Get status to find changed files
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
|
||||
dir: this.pwd,
|
||||
});
|
||||
|
||||
// Filter for modified files in working directory
|
||||
const modifiedFiles = statusMatrix.filter(([filepath, headStatus, workdirStatus, stageStatus]) => {
|
||||
const isModified = workdirStatus === 1 && stageStatus === 1 && headStatus !== workdirStatus;
|
||||
// Filter for files with working directory changes
|
||||
// Status matrix: [filepath, headStatus, workdirStatus, stageStatus]
|
||||
// 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) {
|
||||
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('');
|
||||
}
|
||||
|
||||
const diffLines: string[] = [];
|
||||
|
||||
for (const [filepath] of modifiedFiles) {
|
||||
for (const [filepath, _headStatus, workdirStatus, stageStatus] of changedFiles) {
|
||||
try {
|
||||
// Get file content from HEAD
|
||||
let headContent = '';
|
||||
try {
|
||||
const headBlob = await this.git.readBlob({
|
||||
|
||||
dir: this.pwd,
|
||||
oid: 'HEAD',
|
||||
filepath,
|
||||
});
|
||||
headContent = new TextDecoder().decode(headBlob.blob);
|
||||
} catch {
|
||||
// File might be new
|
||||
// Get staged content (index)
|
||||
let stagedContent = '';
|
||||
if (stageStatus === 1) {
|
||||
try {
|
||||
// Try to read from HEAD first as a fallback for staged content
|
||||
const headBlob = await this.git.readBlob({
|
||||
dir: this.pwd,
|
||||
oid: 'HEAD',
|
||||
filepath,
|
||||
});
|
||||
stagedContent = new TextDecoder().decode(headBlob.blob);
|
||||
} catch {
|
||||
// File might be new
|
||||
stagedContent = '';
|
||||
}
|
||||
}
|
||||
|
||||
// Get file content from working directory
|
||||
// Get working directory content
|
||||
let workdirContent = '';
|
||||
try {
|
||||
workdirContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||
} catch {
|
||||
// File might be deleted
|
||||
if (workdirStatus === 1) {
|
||||
try {
|
||||
workdirContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||
} catch {
|
||||
// File might be deleted
|
||||
workdirContent = '';
|
||||
}
|
||||
}
|
||||
|
||||
// Simple diff representation
|
||||
diffLines.push(`diff --git a/${filepath} b/${filepath}`);
|
||||
diffLines.push(`index ${this.getShortHash(headContent)}..${this.getShortHash(workdirContent)} 100644`);
|
||||
diffLines.push(`--- a/${filepath}`);
|
||||
diffLines.push(`+++ b/${filepath}`);
|
||||
// Skip if contents are identical
|
||||
if (stagedContent === workdirContent) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Show a simplified diff
|
||||
const headLines = headContent.split('\n');
|
||||
// Generate diff header
|
||||
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');
|
||||
|
||||
diffLines.push(`@@ -1,${headLines.length} +1,${workdirLines.length} @@`);
|
||||
// Simple hunk header
|
||||
diffLines.push(`@@ -1,${stagedLines.length} +1,${workdirLines.length} @@`);
|
||||
|
||||
// Show removed lines
|
||||
for (const line of headLines) {
|
||||
if (!workdirLines.includes(line)) {
|
||||
// Show all old lines as removed
|
||||
for (const line of stagedLines) {
|
||||
if (line || stagedLines.length > 0) {
|
||||
diffLines.push(`-${line}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Show added lines
|
||||
// Show all new lines as added
|
||||
for (const line of workdirLines) {
|
||||
if (!headLines.includes(line)) {
|
||||
if (line || workdirLines.length > 0) {
|
||||
diffLines.push(`+${line}`);
|
||||
}
|
||||
}
|
||||
|
||||
diffLines.push('');
|
||||
} catch {
|
||||
} catch (error) {
|
||||
// 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');
|
||||
}
|
||||
|
||||
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 {
|
||||
// Simple hash for demo purposes - not a real git hash
|
||||
let hash = 0;
|
||||
|
||||
Reference in new issue
Block a user