repomix
Version:
A tool to pack repository contents to single file for AI consumption
60 lines (59 loc) • 1.81 kB
JavaScript
import { RepomixError } from '../../shared/errorHandle.js';
import { logger } from '../../shared/logger.js';
import { execGitDiff } from './gitCommand.js';
import { isGitRepository } from './gitRepositoryHandle.js';
export const getWorkTreeDiff = async (directory, deps = {
execGitDiff,
isGitRepository,
}) => {
return getDiff(directory, [], deps);
};
export const getStagedDiff = async (directory, deps = {
execGitDiff,
isGitRepository,
}) => {
return getDiff(directory, ['--cached'], deps);
};
const getDiff = async (directory, options, deps = {
execGitDiff,
isGitRepository,
}) => {
try {
const isGitRepo = await deps.isGitRepository(directory);
if (!isGitRepo) {
logger.trace('Not a git repository, skipping diff generation');
return '';
}
const result = await deps.execGitDiff(directory, options);
return result;
}
catch (error) {
logger.trace('Failed to get git diff:', error.message);
return '';
}
};
export const getGitDiffs = async (rootDirs, config, deps = {
getWorkTreeDiff,
getStagedDiff,
}) => {
let gitDiffResult;
if (config.output.git?.includeDiffs) {
try {
const gitRoot = rootDirs[0] || config.cwd;
const [workTreeDiffContent, stagedDiffContent] = await Promise.all([
deps.getWorkTreeDiff(gitRoot),
deps.getStagedDiff(gitRoot),
]);
gitDiffResult = {
workTreeDiffContent,
stagedDiffContent,
};
}
catch (error) {
if (error instanceof Error) {
throw new RepomixError(`Failed to get git diffs: ${error.message}`);
}
}
}
return gitDiffResult;
};