-
Notifications
You must be signed in to change notification settings - Fork 578
Feature: Git sync, set-tracking, and push divergence handling #796
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
Merged
gsxdsm
merged 2 commits into
AutoMaker-Org:v0.15.0rc
from
gsxdsm:feature/git-sync-set-tracking-divergence-handling
Feb 22, 2026
Merged
Changes from all commits
Commits
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
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
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * POST /set-tracking endpoint - Set the upstream tracking branch for a worktree | ||
| * | ||
| * Sets `git branch --set-upstream-to=<remote>/<branch>` for the current branch. | ||
| * | ||
| * Note: Git repository validation (isGitRepo, hasCommits) is handled by | ||
| * the requireValidWorktree middleware in index.ts | ||
| */ | ||
|
|
||
| import type { Request, Response } from 'express'; | ||
| import { execGitCommand } from '@automaker/git-utils'; | ||
| import { getErrorMessage, logError } from '../common.js'; | ||
| import { getCurrentBranch } from '../../../lib/git.js'; | ||
|
|
||
| export function createSetTrackingHandler() { | ||
| return async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { worktreePath, remote, branch } = req.body as { | ||
| worktreePath: string; | ||
| remote: string; | ||
| branch?: string; | ||
| }; | ||
|
|
||
| if (!worktreePath) { | ||
| res.status(400).json({ success: false, error: 'worktreePath required' }); | ||
| return; | ||
| } | ||
|
|
||
| if (!remote) { | ||
| res.status(400).json({ success: false, error: 'remote required' }); | ||
| return; | ||
| } | ||
|
|
||
| // Get current branch if not provided | ||
| let targetBranch = branch; | ||
| if (!targetBranch) { | ||
| try { | ||
| targetBranch = await getCurrentBranch(worktreePath); | ||
| } catch (err) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: `Failed to get current branch: ${getErrorMessage(err)}`, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (targetBranch === 'HEAD') { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: 'Cannot set tracking in detached HEAD state.', | ||
| }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Set upstream tracking (pass local branch name as final arg to be explicit) | ||
| await execGitCommand( | ||
| ['branch', '--set-upstream-to', `${remote}/${targetBranch}`, targetBranch], | ||
| worktreePath | ||
| ); | ||
|
|
||
| res.json({ | ||
| success: true, | ||
| result: { | ||
| branch: targetBranch, | ||
| remote, | ||
| upstream: `${remote}/${targetBranch}`, | ||
| message: `Set tracking branch to ${remote}/${targetBranch}`, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| logError(error, 'Set tracking branch failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /** | ||
| * POST /sync endpoint - Pull then push a worktree branch | ||
| * | ||
| * Performs a full sync operation: pull latest from remote, then push | ||
| * local commits. Handles divergence automatically. | ||
| * | ||
| * Git business logic is delegated to sync-service.ts. | ||
| * | ||
| * Note: Git repository validation (isGitRepo, hasCommits) is handled by | ||
| * the requireValidWorktree middleware in index.ts | ||
| */ | ||
|
|
||
| import type { Request, Response } from 'express'; | ||
| import { getErrorMessage, logError } from '../common.js'; | ||
| import { performSync } from '../../../services/sync-service.js'; | ||
|
|
||
| export function createSyncHandler() { | ||
| return async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { worktreePath, remote } = req.body as { | ||
| worktreePath: string; | ||
| remote?: string; | ||
| }; | ||
|
|
||
| if (!worktreePath) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: 'worktreePath required', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const result = await performSync(worktreePath, { remote }); | ||
|
|
||
| if (!result.success) { | ||
| const statusCode = result.hasConflicts ? 409 : 500; | ||
| res.status(statusCode).json({ | ||
| success: false, | ||
| error: result.error, | ||
| hasConflicts: result.hasConflicts, | ||
| conflictFiles: result.conflictFiles, | ||
| conflictSource: result.conflictSource, | ||
| pulled: result.pulled, | ||
| pushed: result.pushed, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| res.json({ | ||
| success: true, | ||
| result: { | ||
| branch: result.branch, | ||
| pulled: result.pulled, | ||
| pushed: result.pushed, | ||
| isFastForward: result.isFastForward, | ||
| isMerge: result.isMerge, | ||
| autoResolved: result.autoResolved, | ||
| message: result.message, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| logError(error, 'Sync worktree failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.