UNPKG

@elsikora/commitizen-plugin-commitlint-ai

Version:
77 lines (75 loc) 2.55 kB
/** * Git implementation of the commit repository */ class GitCommitRepository { COMMAND_SERVICE; constructor(commandService) { this.COMMAND_SERVICE = commandService; } /** * Create a commit with the given message * @param {CommitMessage} message - The commit message * @returns {Promise<void>} Promise that resolves when the commit is created */ async commit(message) { // Escape the commit message for shell const escapedMessage = message.toString().replaceAll("'", String.raw `'\''`); // Execute git commit await this.COMMAND_SERVICE.execute(`git commit -m '${escapedMessage}'`); } /** * Get the current branch name * @returns {Promise<string>} Promise resolving to the current branch name */ async getCurrentBranch() { const branch = await this.COMMAND_SERVICE.executeWithOutput("git rev-parse --abbrev-ref HEAD"); return branch || "main"; } /** * Get the staged diff * @returns {Promise<string>} Promise resolving to the staged diff */ async getStagedDiff() { try { // Get a compact diff suitable for LLM context const diff = await this.COMMAND_SERVICE.executeWithOutput("git diff --cached --stat -p --no-color"); // Limit diff size to avoid token limits const maxLength = 3000; if (diff.length > maxLength) { return diff.slice(0, Math.max(0, maxLength)) + "\n... (truncated)"; } return diff; } catch { return ""; } } /** * Get the list of staged files * @returns {Promise<Array<string>>} Promise resolving to array of staged file paths */ async getStagedFiles() { try { const output = await this.COMMAND_SERVICE.executeWithOutput("git diff --cached --name-only"); return output.split("\n").filter((file) => file.trim().length > 0); } catch { return []; } } /** * Check if there are staged changes * @returns {Promise<boolean>} Promise resolving to true if there are staged changes */ async hasStagedChanges() { try { const output = await this.COMMAND_SERVICE.executeWithOutput("git diff --cached --name-only"); return output.length > 0; } catch { return false; } } } export { GitCommitRepository }; //# sourceMappingURL=git-commit.repository.js.map