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
55 changes: 48 additions & 7 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export class GitOperations {
*/
async getConflictedFiles(): Promise<string[]> {
try {
await this.git.status();

const diffOutput = await this.git.diff(['--name-only', '--diff-filter=U']);
const files = diffOutput
.split('\n')
Expand Down Expand Up @@ -156,14 +154,57 @@ export class GitOperations {
merging?: string;
mergeMessage?: string;
}> {
const files = await this.getConflictedFiles();
const info = await this.getMergeInfo();
// Reuse a single status call instead of 3 separate git invocations
const status = await this.git.status();
const current = status.current || 'HEAD';

const diffOutput = await this.git.diff(['--name-only', '--diff-filter=U']);
const files = diffOutput
.split('\n')
.map((f: string) => f.trim())
.filter((f: string) => f.length > 0);

// Get merge info (branches) using shared context
let merging: string | undefined;
let mergeMessage: string | undefined;
const gitDir = (await this.git.revparse(['--git-dir'])).trim();
const absGitDir = resolve(this.workingDir, gitDir);

// Try MERGE_MSG for merge context
try {
const mergeMsgPath = resolve(absGitDir, 'MERGE_MSG');
if (existsSync(mergeMsgPath)) {
const msg = await readFile(mergeMsgPath, 'utf-8');
const match = msg.match(/Merge branch ['"]([^'"]+)['"]/);
if (match) {
merging = match[1];
}
mergeMessage = msg.split('\n')[0].trim();
}
} catch { /* ignore */ }

// Try MERGE_HEAD if no merging branch found
if (!merging) {
try {
const mergeHeadPath = resolve(absGitDir, 'MERGE_HEAD');
if (existsSync(mergeHeadPath)) {
const sha = (await readFile(mergeHeadPath, 'utf-8')).trim();
try {
const name = await this.git.raw(['name-rev', '--name-only', sha]);
merging = name.trim() || sha.substring(0, 7);
} catch {
merging = sha.substring(0, 7);
}
}
} catch { /* ignore */ }
}

return {
hasConflicts: files.length > 0,
files,
branch: info.current,
merging: info.merging,
mergeMessage: info.mergeMessage,
branch: current,
merging,
mergeMessage,
};
}
}
12 changes: 7 additions & 5 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { GitOperations } from './git';

export class ConflictResolver {
private gitOps: GitOperations;
private workingDir: string;

constructor() {
this.gitOps = new GitOperations();
constructor(cwd?: string) {
this.workingDir = cwd ? resolve(cwd) : process.cwd();
this.gitOps = new GitOperations(cwd);
}

/**
* Open file in user's preferred editor
*/
async openInEditor(filePath: string): Promise<void> {
const editor = process.env.EDITOR || process.env.VISUAL || this.getDefaultEditor();
const fullPath = resolve(filePath);
const fullPath = resolve(this.workingDir, filePath);

console.log(` Opening in ${editor}...`);

Expand Down Expand Up @@ -56,7 +58,7 @@ export class ConflictResolver {
*/
async validateResolution(filePath: string): Promise<{ valid: boolean; reason?: string }> {
try {
const content = await readFile(resolve(filePath), 'utf-8');
const content = await readFile(resolve(this.workingDir, filePath), 'utf-8');

if (this.gitOps.hasConflictMarkers(content)) {
return {
Expand All @@ -79,7 +81,7 @@ export class ConflictResolver {
*/
async getConflictCount(filePath: string): Promise<number> {
try {
const content = await readFile(resolve(filePath), 'utf-8');
const content = await readFile(resolve(this.workingDir, filePath), 'utf-8');
return this.gitOps.countConflicts(content);
} catch {
return 0;
Expand Down