UNPKG

repomix

Version:

A tool to pack repository contents to single file for AI consumption

69 lines (68 loc) 2.36 kB
import { RepomixError } from '../../shared/errorHandle.js'; import { logger } from '../../shared/logger.js'; import { execGitLog } from './gitCommand.js'; import { isGitRepository } from './gitRepositoryHandle.js'; export const GIT_LOG_RECORD_SEPARATOR = '\x00'; export const GIT_LOG_FORMAT_SEPARATOR = '%x00'; const parseGitLog = (rawLogOutput, recordSeparator = GIT_LOG_RECORD_SEPARATOR) => { if (!rawLogOutput.trim()) { return []; } const commits = []; const logEntries = rawLogOutput.split(recordSeparator).filter(Boolean); for (const entry of logEntries) { const lines = entry.split(/\r?\n/).filter((line) => line.trim() !== ''); if (lines.length === 0) continue; const firstLine = lines[0]; const separatorIndex = firstLine.indexOf('|'); if (separatorIndex === -1) continue; const date = firstLine.substring(0, separatorIndex); const message = firstLine.substring(separatorIndex + 1); const files = lines.slice(1).filter((line) => line.trim() !== ''); commits.push({ date, message, files, }); } return commits; }; export const getGitLog = async (directory, maxCommits, deps = { execGitLog, isGitRepository, }) => { if (!(await deps.isGitRepository(directory))) { logger.trace(`Directory ${directory} is not a git repository`); return ''; } try { return await deps.execGitLog(directory, maxCommits, GIT_LOG_FORMAT_SEPARATOR); } catch (error) { logger.trace('Failed to get git log:', error.message); throw error; } }; export const getGitLogs = async (rootDirs, config, deps = { getGitLog, }) => { let gitLogResult; if (config.output.git?.includeLogs) { try { const gitRoot = rootDirs[0] || config.cwd; const maxCommits = config.output.git?.includeLogsCount || 50; const logContent = await deps.getGitLog(gitRoot, maxCommits); const commits = parseGitLog(logContent); gitLogResult = { logContent, commits, }; } catch (error) { throw new RepomixError(`Failed to get git logs: ${error.message}`, { cause: error }); } } return gitLogResult; };