-
Notifications
You must be signed in to change notification settings - Fork 1
feat: extract merge operation to core/operations/merge.ts #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2witstudios
wants to merge
3
commits into
main
Choose a base branch
from
ppg/issue-59-merge-op
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,138 +1,57 @@ | ||
| import { execa } from 'execa'; | ||
| import { requireManifest, updateManifest, resolveWorktree } from '../core/manifest.js'; | ||
| import { refreshAllAgentStatuses } from '../core/agent.js'; | ||
| import { getRepoRoot, getCurrentBranch } from '../core/worktree.js'; | ||
| import { cleanupWorktree } from '../core/cleanup.js'; | ||
| import { getCurrentPaneId } from '../core/self.js'; | ||
| import { listSessionPanes, type PaneInfo } from '../core/tmux.js'; | ||
| import { PpgError, WorktreeNotFoundError, MergeFailedError } from '../lib/errors.js'; | ||
| import { performMerge } from '../core/operations/merge.js'; | ||
| import { getRepoRoot } from '../core/worktree.js'; | ||
| import { output, success, info, warn } from '../lib/output.js'; | ||
| import { execaEnv } from '../lib/env.js'; | ||
|
|
||
| export interface MergeOptions { | ||
| export interface MergeCommandOptions { | ||
| strategy?: 'squash' | 'no-ff'; | ||
| cleanup?: boolean; | ||
| dryRun?: boolean; | ||
| force?: boolean; | ||
| json?: boolean; | ||
| } | ||
|
|
||
| export async function mergeCommand(worktreeId: string, options: MergeOptions): Promise<void> { | ||
| export async function mergeCommand(worktreeId: string, options: MergeCommandOptions): Promise<void> { | ||
| const projectRoot = await getRepoRoot(); | ||
|
|
||
| await requireManifest(projectRoot); | ||
| const manifest = await updateManifest(projectRoot, async (m) => { | ||
| return refreshAllAgentStatuses(m, projectRoot); | ||
| }); | ||
|
|
||
| const wt = resolveWorktree(manifest, worktreeId); | ||
|
|
||
| if (!wt) throw new WorktreeNotFoundError(worktreeId); | ||
|
|
||
| // Check all agents finished | ||
| const agents = Object.values(wt.agents); | ||
| const incomplete = agents.filter((a) => a.status === 'running'); | ||
|
|
||
| if (incomplete.length > 0 && !options.force) { | ||
| const ids = incomplete.map((a) => a.id).join(', '); | ||
| throw new PpgError( | ||
| `${incomplete.length} agent(s) still running: ${ids}. Use --force to merge anyway.`, | ||
| 'AGENTS_RUNNING', | ||
| ); | ||
| } | ||
|
|
||
| if (options.dryRun) { | ||
| info('Dry run — no changes will be made'); | ||
| info(`Would merge branch ${wt.branch} into ${wt.baseBranch} using ${options.strategy ?? 'squash'} strategy`); | ||
| if (options.cleanup !== false) { | ||
| info(`Would remove worktree ${wt.id} and delete branch ${wt.branch}`); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Set worktree status to merging | ||
| await updateManifest(projectRoot, (m) => { | ||
| if (m.worktrees[wt.id]) { | ||
| m.worktrees[wt.id].status = 'merging'; | ||
| } | ||
| return m; | ||
| const result = await performMerge({ | ||
| projectRoot, | ||
| worktreeRef: worktreeId, | ||
| strategy: options.strategy, | ||
| cleanup: options.cleanup, | ||
| dryRun: options.dryRun, | ||
| force: options.force, | ||
| }); | ||
|
|
||
| const strategy = options.strategy ?? 'squash'; | ||
|
|
||
| try { | ||
| const currentBranch = await getCurrentBranch(projectRoot); | ||
| if (currentBranch !== wt.baseBranch) { | ||
| info(`Switching to base branch ${wt.baseBranch}`); | ||
| await execa('git', ['checkout', wt.baseBranch], { ...execaEnv, cwd: projectRoot }); | ||
| } | ||
|
|
||
| info(`Merging ${wt.branch} into ${wt.baseBranch} (${strategy})`); | ||
|
|
||
| if (strategy === 'squash') { | ||
| await execa('git', ['merge', '--squash', wt.branch], { ...execaEnv, cwd: projectRoot }); | ||
| await execa('git', ['commit', '-m', `ppg: merge ${wt.name} (${wt.branch})`], { | ||
| ...execaEnv, | ||
| cwd: projectRoot, | ||
| }); | ||
| } else { | ||
| await execa('git', ['merge', '--no-ff', wt.branch, '-m', `ppg: merge ${wt.name} (${wt.branch})`], { | ||
| ...execaEnv, | ||
| cwd: projectRoot, | ||
| }); | ||
| if (result.dryRun) { | ||
| info(`Would merge branch ${result.branch} into ${result.baseBranch} using ${result.strategy} strategy`); | ||
| if (options.cleanup !== false) { | ||
| info(`Would remove worktree ${result.worktreeId} and delete branch ${result.branch}`); | ||
| } | ||
|
|
||
| success(`Merged ${wt.branch} into ${wt.baseBranch}`); | ||
| } catch (err) { | ||
| await updateManifest(projectRoot, (m) => { | ||
| if (m.worktrees[wt.id]) { | ||
| m.worktrees[wt.id].status = 'failed'; | ||
| } | ||
| return m; | ||
| }); | ||
| throw new MergeFailedError( | ||
| `Merge failed: ${err instanceof Error ? err.message : err}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Mark as merged | ||
| await updateManifest(projectRoot, (m) => { | ||
| if (m.worktrees[wt.id]) { | ||
| m.worktrees[wt.id].status = 'merged'; | ||
| m.worktrees[wt.id].mergedAt = new Date().toISOString(); | ||
| } | ||
| return m; | ||
| }); | ||
|
|
||
| // Cleanup with self-protection | ||
| let selfProtected = false; | ||
| if (options.cleanup !== false) { | ||
| info('Cleaning up...'); | ||
|
|
||
| const selfPaneId = getCurrentPaneId(); | ||
| let paneMap: Map<string, PaneInfo> | undefined; | ||
| if (selfPaneId) { | ||
| paneMap = await listSessionPanes(manifest.sessionName); | ||
| } | ||
|
|
||
| const cleanupResult = await cleanupWorktree(projectRoot, wt, { selfPaneId, paneMap }); | ||
| selfProtected = cleanupResult.selfProtected; | ||
| success(`Merged ${result.branch} into ${result.baseBranch}`); | ||
|
|
||
| if (selfProtected) { | ||
| warn(`Some tmux targets skipped during cleanup — contains current ppg process`); | ||
| if (result.cleaned) { | ||
| if (result.selfProtected) { | ||
| warn('Some tmux targets skipped during cleanup — contains current ppg process'); | ||
| } | ||
| success(`Cleaned up worktree ${wt.id}`); | ||
| success(`Cleaned up worktree ${result.worktreeId}`); | ||
| } | ||
|
|
||
| if (options.json) { | ||
| output({ | ||
| success: true, | ||
| worktreeId: wt.id, | ||
| branch: wt.branch, | ||
| baseBranch: wt.baseBranch, | ||
| strategy, | ||
| cleaned: options.cleanup !== false, | ||
| selfProtected: selfProtected || undefined, | ||
| worktreeId: result.worktreeId, | ||
| branch: result.branch, | ||
| baseBranch: result.baseBranch, | ||
| strategy: result.strategy, | ||
| cleaned: result.cleaned, | ||
| selfProtected: result.selfProtected || undefined, | ||
| }, true); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--jsonmode currently emits human logs and skips dry-run JSON output.info/success/warnare emitted even whenoptions.jsonis true, and the dry-run branch returns before the JSON payload is written. This breaks machine-readable command output.✅ Suggested flow fix
export async function mergeCommand(worktreeId: string, options: MergeCommandOptions): Promise<void> { const projectRoot = await getRepoRoot(); - const strategy = options.strategy ?? 'squash'; - if (options.dryRun) { + if (options.dryRun && !options.json) { info('Dry run — no changes will be made'); } const result = await performMerge({ projectRoot, worktreeRef: worktreeId, strategy: options.strategy, cleanup: options.cleanup, dryRun: options.dryRun, force: options.force, }); + if (options.json) { + output({ + success: true, + worktreeId: result.worktreeId, + branch: result.branch, + baseBranch: result.baseBranch, + strategy: result.strategy, + dryRun: result.dryRun, + merged: result.merged, + cleaned: result.cleaned, + selfProtected: result.selfProtected || undefined, + }, true); + return; + } + if (result.dryRun) { info(`Would merge branch ${result.branch} into ${result.baseBranch} using ${result.strategy} strategy`); if (options.cleanup !== false) { info(`Would remove worktree ${result.worktreeId} and delete branch ${result.branch}`); } return; } success(`Merged ${result.branch} into ${result.baseBranch}`); if (result.cleaned) { if (result.selfProtected) { warn('Some tmux targets skipped during cleanup — contains current ppg process'); } success(`Cleaned up worktree ${result.worktreeId}`); } - - if (options.json) { - output({ - success: true, - worktreeId: result.worktreeId, - branch: result.branch, - baseBranch: result.baseBranch, - strategy: result.strategy, - cleaned: result.cleaned, - selfProtected: result.selfProtected || undefined, - }, true); - } }As per coding guidelines
src/commands/**/*.ts: "Support--jsonflag on every command and useoutput(data, json)andoutputError(error, json)for dual output".🤖 Prompt for AI Agents