File size: 8,660 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
 * PushService - Push git operations without HTTP
 *
 * Encapsulates the full git push workflow including:
 * - Branch name and detached HEAD detection
 * - Safe array-based command execution (no shell interpolation)
 * - Divergent branch detection and auto-resolution via pull-then-retry
 * - Structured result reporting
 *
 * Mirrors the pull-service.ts pattern for consistency.
 */

import { createLogger, getErrorMessage } from '@automaker/utils';
import { execGitCommand } from '@automaker/git-utils';
import { getCurrentBranch } from '../lib/git.js';
import { performPull } from './pull-service.js';

const logger = createLogger('PushService');

// ============================================================================
// Types
// ============================================================================

export interface PushOptions {
  /** Remote name to push to (defaults to 'origin') */
  remote?: string;
  /** Force push */
  force?: boolean;
  /** When true and push is rejected due to divergence, pull then retry push */
  autoResolve?: boolean;
}

export interface PushResult {
  success: boolean;
  error?: string;
  branch?: string;
  pushed?: boolean;
  /** Whether the push was initially rejected because the branches diverged */
  diverged?: boolean;
  /** Whether divergence was automatically resolved via pull-then-retry */
  autoResolved?: boolean;
  /** Whether the auto-resolve pull resulted in merge conflicts */
  hasConflicts?: boolean;
  /** Files with merge conflicts (only when hasConflicts is true) */
  conflictFiles?: string[];
  message?: string;
}

// ============================================================================
// Helper Functions
// ============================================================================

/**
 * Detect whether push error output indicates a diverged/non-fast-forward rejection.
 */
function isDivergenceError(errorOutput: string): boolean {
  const lower = errorOutput.toLowerCase();
  // Require specific divergence indicators rather than just 'rejected' alone,
  // which could match pre-receive hook rejections or protected branch errors.
  const hasNonFastForward = lower.includes('non-fast-forward');
  const hasFetchFirst = lower.includes('fetch first');
  const hasFailedToPush = lower.includes('failed to push some refs');
  const hasRejected = lower.includes('rejected');
  return hasNonFastForward || hasFetchFirst || (hasRejected && hasFailedToPush);
}

// ============================================================================
// Main Service Function
// ============================================================================

/**
 * Perform a git push on the given worktree.
 *
 * The workflow:
 * 1. Get current branch name (detect detached HEAD)
 * 2. Attempt `git push <remote> <branch>` with safe array args
 * 3. If push fails with divergence and autoResolve is true:
 *    a. Pull from the same remote (with stash support)
 *    b. If pull succeeds without conflicts, retry push
 * 4. If push fails with "no upstream" error, retry with --set-upstream
 * 5. Return structured result
 *
 * @param worktreePath - Path to the git worktree
 * @param options - Push options (remote, force, autoResolve)
 * @returns PushResult with detailed status information
 */
export async function performPush(
  worktreePath: string,
  options?: PushOptions
): Promise<PushResult> {
  const targetRemote = options?.remote || 'origin';
  const force = options?.force ?? false;
  const autoResolve = options?.autoResolve ?? false;

  // 1. Get current branch name
  let branchName: string;
  try {
    branchName = await getCurrentBranch(worktreePath);
  } catch (err) {
    return {
      success: false,
      error: `Failed to get current branch: ${getErrorMessage(err)}`,
    };
  }

  // 2. Check for detached HEAD state
  if (branchName === 'HEAD') {
    return {
      success: false,
      error: 'Cannot push in detached HEAD state. Please checkout a branch first.',
    };
  }

  // 3. Build push args (no -u flag; upstream is set in the fallback path only when needed)
  const pushArgs = ['push', targetRemote, branchName];
  if (force) {
    pushArgs.push('--force');
  }

  // 4. Attempt push
  try {
    await execGitCommand(pushArgs, worktreePath);

    return {
      success: true,
      branch: branchName,
      pushed: true,
      message: `Successfully pushed ${branchName} to ${targetRemote}`,
    };
  } catch (pushError: unknown) {
    const err = pushError as { stderr?: string; stdout?: string; message?: string };
    const errorOutput = `${err.stderr || ''} ${err.stdout || ''} ${err.message || ''}`;

    // 5. Check if the error is a divergence rejection
    if (isDivergenceError(errorOutput)) {
      if (!autoResolve) {
        return {
          success: false,
          branch: branchName,
          pushed: false,
          diverged: true,
          error: `Push rejected: remote has changes not present locally. Use sync or pull first, or enable auto-resolve.`,
          message: `Push to ${targetRemote} was rejected because the remote branch has diverged.`,
        };
      }

      // 6. Auto-resolve: pull then retry push
      logger.info('Push rejected due to divergence, attempting auto-resolve via pull', {
        worktreePath,
        remote: targetRemote,
        branch: branchName,
      });

      try {
        const pullResult = await performPull(worktreePath, {
          remote: targetRemote,
          stashIfNeeded: true,
        });

        if (!pullResult.success) {
          return {
            success: false,
            branch: branchName,
            pushed: false,
            diverged: true,
            autoResolved: false,
            error: `Auto-resolve failed during pull: ${pullResult.error}`,
          };
        }

        if (pullResult.hasConflicts) {
          return {
            success: false,
            branch: branchName,
            pushed: false,
            diverged: true,
            autoResolved: false,
            hasConflicts: true,
            conflictFiles: pullResult.conflictFiles,
            error:
              'Auto-resolve pull resulted in merge conflicts. Resolve conflicts and push again.',
          };
        }

        // 7. Retry push after successful pull
        try {
          await execGitCommand(pushArgs, worktreePath);

          return {
            success: true,
            branch: branchName,
            pushed: true,
            diverged: true,
            autoResolved: true,
            message: `Push succeeded after auto-resolving divergence (pulled from ${targetRemote} first).`,
          };
        } catch (retryError: unknown) {
          const retryErr = retryError as { stderr?: string; message?: string };
          return {
            success: false,
            branch: branchName,
            pushed: false,
            diverged: true,
            autoResolved: false,
            error: `Push failed after auto-resolve pull: ${retryErr.stderr || retryErr.message || 'Unknown error'}`,
          };
        }
      } catch (pullError) {
        return {
          success: false,
          branch: branchName,
          pushed: false,
          diverged: true,
          autoResolved: false,
          error: `Auto-resolve pull failed: ${getErrorMessage(pullError)}`,
        };
      }
    }

    // 6b. Non-divergence error (e.g. no upstream configured) - retry with --set-upstream
    const isNoUpstreamError =
      errorOutput.toLowerCase().includes('no upstream') ||
      errorOutput.toLowerCase().includes('has no upstream branch') ||
      errorOutput.toLowerCase().includes('set-upstream');
    if (isNoUpstreamError) {
      try {
        const setUpstreamArgs = ['push', '--set-upstream', targetRemote, branchName];
        if (force) {
          setUpstreamArgs.push('--force');
        }
        await execGitCommand(setUpstreamArgs, worktreePath);

        return {
          success: true,
          branch: branchName,
          pushed: true,
          message: `Successfully pushed ${branchName} to ${targetRemote} (set upstream)`,
        };
      } catch (upstreamError: unknown) {
        const upstreamErr = upstreamError as { stderr?: string; message?: string };
        return {
          success: false,
          branch: branchName,
          pushed: false,
          error: upstreamErr.stderr || upstreamErr.message || getErrorMessage(pushError),
        };
      }
    }

    // 6c. Other push error - return as-is
    return {
      success: false,
      branch: branchName,
      pushed: false,
      error: err.stderr || err.message || getErrorMessage(pushError),
    };
  }
}