|
1 | | -export const CommitManager = {} |
| 1 | +/** |
| 2 | + * 📝 CommitManager.ts |
| 3 | + * ------------------------------------------------------------- |
| 4 | + * Handles all commit-related operations such as adding, |
| 5 | + * committing, viewing logs, diffs, and undoing commits. |
| 6 | + * ------------------------------------------------------------- |
| 7 | + */ |
| 8 | + |
| 9 | +import inquirer from 'inquirer'; |
| 10 | +import chalk from 'chalk'; |
| 11 | +import { execSync } from 'child_process'; |
| 12 | +import { GitExecutor } from '../core/GitExecutor'; |
| 13 | +import { Logger } from '../utils/Logger'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Helper: get list of modified/untracked files |
| 17 | + */ |
| 18 | +function getModifiedFiles(): string[] { |
| 19 | + try { |
| 20 | + const output = execSync('git status --porcelain', { encoding: 'utf-8' }); |
| 21 | + return output |
| 22 | + .split('\n') |
| 23 | + .map((l) => l.trim()) |
| 24 | + .filter(Boolean) |
| 25 | + .map((l) => l.replace(/^.. /, '')); |
| 26 | + } catch { |
| 27 | + return []; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const CommitManager = { |
| 32 | + /** |
| 33 | + * Stage all files (git add .) |
| 34 | + */ |
| 35 | + async stageAll() { |
| 36 | + Logger.info('🧩 Staging all modified and new files...'); |
| 37 | + await GitExecutor.run('git add .'); |
| 38 | + Logger.success('✅ All files staged successfully!'); |
| 39 | + }, |
| 40 | + |
| 41 | + /** |
| 42 | + * Stage specific files interactively |
| 43 | + */ |
| 44 | + async stageFiles() { |
| 45 | + const files = getModifiedFiles(); |
| 46 | + if (!files.length) { |
| 47 | + Logger.info('⚠️ No modified or untracked files found.'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const { selected } = await inquirer.prompt([ |
| 52 | + { |
| 53 | + type: 'checkbox', |
| 54 | + name: 'selected', |
| 55 | + message: 'Select files to stage:', |
| 56 | + choices: files, |
| 57 | + }, |
| 58 | + ]); |
| 59 | + |
| 60 | + if (selected.length === 0) { |
| 61 | + Logger.info('No files selected.'); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const cmd = `git add ${selected.join(' ')}`; |
| 66 | + Logger.info(`🧩 Staging ${selected.length} file(s)...`); |
| 67 | + await GitExecutor.run(cmd); |
| 68 | + Logger.success(`✅ Staged ${selected.length} file(s).`); |
| 69 | + }, |
| 70 | + |
| 71 | + /** |
| 72 | + * Unstage files (git restore --staged) |
| 73 | + */ |
| 74 | + async unstageFiles() { |
| 75 | + const files = getModifiedFiles(); |
| 76 | + if (!files.length) { |
| 77 | + Logger.info('⚠️ No files to unstage.'); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const { selected } = await inquirer.prompt([ |
| 82 | + { |
| 83 | + type: 'checkbox', |
| 84 | + name: 'selected', |
| 85 | + message: 'Select files to unstage:', |
| 86 | + choices: files, |
| 87 | + }, |
| 88 | + ]); |
| 89 | + |
| 90 | + if (selected.length === 0) { |
| 91 | + Logger.info('No files selected.'); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const cmd = `git restore --staged ${selected.join(' ')}`; |
| 96 | + Logger.info(`🗑️ Unstaging ${selected.length} file(s)...`); |
| 97 | + await GitExecutor.run(cmd); |
| 98 | + Logger.success(`✅ Unstaged ${selected.length} file(s).`); |
| 99 | + }, |
| 100 | + |
| 101 | + /** |
| 102 | + * Commit staged files with message |
| 103 | + */ |
| 104 | + async commitChanges() { |
| 105 | + const { message } = await inquirer.prompt([ |
| 106 | + { type: 'input', name: 'message', message: '📝 Enter commit message:' }, |
| 107 | + ]); |
| 108 | + |
| 109 | + if (!message.trim()) { |
| 110 | + Logger.info('⚠️ Commit message cannot be empty.'); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const cmd = `git commit -m "${message}"`; |
| 115 | + Logger.info('💾 Committing changes...'); |
| 116 | + await GitExecutor.run(cmd); |
| 117 | + Logger.success('✅ Commit completed!'); |
| 118 | + }, |
| 119 | + |
| 120 | + /** |
| 121 | + * Amend last commit message (fix last commit) |
| 122 | + */ |
| 123 | + async amendLastCommit() { |
| 124 | + const { message } = await inquirer.prompt([ |
| 125 | + { |
| 126 | + type: 'input', |
| 127 | + name: 'message', |
| 128 | + message: 'Enter new commit message for the last commit:', |
| 129 | + }, |
| 130 | + ]); |
| 131 | + |
| 132 | + const cmd = `git commit --amend -m "${message}"`; |
| 133 | + Logger.info('✏️ Amending last commit...'); |
| 134 | + await GitExecutor.run(cmd); |
| 135 | + Logger.success('✅ Last commit amended successfully!'); |
| 136 | + }, |
| 137 | + |
| 138 | + /** |
| 139 | + * Undo last commit (keep changes staged) |
| 140 | + */ |
| 141 | + async undoLastCommit() { |
| 142 | + Logger.info('↩️ Undoing last commit (keeping changes)...'); |
| 143 | + await GitExecutor.run('git reset --soft HEAD~1'); |
| 144 | + Logger.success('✅ Last commit undone. Changes remain staged.'); |
| 145 | + }, |
| 146 | + |
| 147 | + /** |
| 148 | + * Show last commit details |
| 149 | + */ |
| 150 | + async showLastCommit() { |
| 151 | + Logger.info('📜 Showing last commit details...'); |
| 152 | + await GitExecutor.run('git show HEAD --stat --pretty=medium'); |
| 153 | + }, |
| 154 | + |
| 155 | + /** |
| 156 | + * View commit history (graph) |
| 157 | + */ |
| 158 | + async showLog() { |
| 159 | + const { format } = await inquirer.prompt([ |
| 160 | + { |
| 161 | + type: 'list', |
| 162 | + name: 'format', |
| 163 | + message: 'Select log format:', |
| 164 | + choices: [ |
| 165 | + { name: 'Compact (oneline)', value: '--oneline' }, |
| 166 | + { name: 'Detailed (default)', value: '' }, |
| 167 | + { name: 'Graph view', value: '--oneline --graph --decorate' }, |
| 168 | + ], |
| 169 | + }, |
| 170 | + ]); |
| 171 | + |
| 172 | + const cmd = `git log ${format}`; |
| 173 | + Logger.info('📜 Viewing commit history...'); |
| 174 | + await GitExecutor.run(cmd); |
| 175 | + }, |
| 176 | + |
| 177 | + /** |
| 178 | + * Show unstaged diff |
| 179 | + */ |
| 180 | + async showDiff() { |
| 181 | + Logger.info('🔍 Showing unstaged changes...'); |
| 182 | + await GitExecutor.run('git diff'); |
| 183 | + }, |
| 184 | + |
| 185 | + /** |
| 186 | + * Show staged diff |
| 187 | + */ |
| 188 | + async showStagedDiff() { |
| 189 | + Logger.info('🔍 Showing staged (cached) changes...'); |
| 190 | + await GitExecutor.run('git diff --cached'); |
| 191 | + }, |
| 192 | +}; |
0 commit comments