/**
* pgh commit - Stage and commit changes
*
* Stages files and commits with a message.
* Supports -a (all tracked), -m (message), --amend.
*/
import { log, error, jsonOut } from '../output.js'
import { gitExec, gitSpawn } from '../git.js'
export async function commitCommand (client, sub, args, flags) {
const cwd = await gitExec(['rev-parse', '--show-toplevel'], '.')
const message = flags.m || flags.message
if (!message && !flags.amend) {
error('Commit message required. Use -m "message"')
}
// Collect files to stage from positional args (sub + args)
const files = []
if (sub) files.push(sub)
files.push(...args)
// Stage files if specified, or all with -a
if (files.length > 0) {
await gitSpawn(['add', ...files], cwd)
} else if (flags.a || flags.all) {
await gitSpawn(['add', '-A'], cwd)
}
// Build commit args
const gitArgs = ['commit']
if (flags.amend) gitArgs.push('--amend')
if (message) gitArgs.push('-m', message)
if (flags.amend && !message) gitArgs.push('--no-edit')
try {
if (flags.json) {
const output = await gitExec(gitArgs, cwd)
jsonOut({ status: 'ok', output })
} else {
await gitSpawn(gitArgs, cwd)
}
} catch (err) {
if (flags.json) {
jsonOut({ status: 'error', error: err.message })
process.exit(1)
}
throw err
}
}
export function commitHelp () {
log(`Usage: pgh commit [files...] -m "message" [flags]
Stage and commit changes.
Arguments:
[files...] Files to stage before committing
Flags:
-m, --message MSG Commit message (required unless --amend --no-edit)
-a, --all Stage all tracked changes before committing
--amend Amend the previous commit
--json Output as JSON
Examples:
pgh commit -a -m "Add push/pull commands"
pgh commit src/app.js -m "Fix bug"
pgh commit -m "Update readme"
pgh commit --amend -m "Better message"
pgh commit --amend Amend with no message change`)
}