File size: 6,158 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
/**
 * SyncService - Pull then push in a single operation
 *
 * Composes performPull() and performPush() to synchronize a branch
 * with its remote. Always uses stashIfNeeded for the pull step.
 * If push fails with divergence after pull, retries once.
 *
 * Follows the same pattern as pull-service.ts and push-service.ts.
 */

import { createLogger, getErrorMessage } from '@automaker/utils';
import { performPull } from './pull-service.js';
import { performPush } from './push-service.js';
import type { PullResult } from './pull-service.js';
import type { PushResult } from './push-service.js';

const logger = createLogger('SyncService');

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

export interface SyncOptions {
  /** Remote name (defaults to 'origin') */
  remote?: string;
}

export interface SyncResult {
  success: boolean;
  error?: string;
  branch?: string;
  /** Whether the pull step was performed */
  pulled?: boolean;
  /** Whether the push step was performed */
  pushed?: boolean;
  /** Pull resulted in conflicts */
  hasConflicts?: boolean;
  /** Files with merge conflicts */
  conflictFiles?: string[];
  /** Source of conflicts ('pull' | 'stash') */
  conflictSource?: 'pull' | 'stash';
  /** Whether the pull was a fast-forward */
  isFastForward?: boolean;
  /** Whether the pull resulted in a merge commit */
  isMerge?: boolean;
  /** Whether push divergence was auto-resolved */
  autoResolved?: boolean;
  message?: string;
}

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

/**
 * Perform a sync operation (pull then push) on the given worktree.
 *
 * The workflow:
 * 1. Pull from remote with stashIfNeeded: true
 * 2. If pull has conflicts, stop and return conflict info
 * 3. Push to remote
 * 4. If push fails with divergence after pull, retry once
 *
 * @param worktreePath - Path to the git worktree
 * @param options - Sync options (remote)
 * @returns SyncResult with detailed status information
 */
export async function performSync(
  worktreePath: string,
  options?: SyncOptions
): Promise<SyncResult> {
  const targetRemote = options?.remote || 'origin';

  // 1. Pull from remote
  logger.info('Sync: starting pull', { worktreePath, remote: targetRemote });

  let pullResult: PullResult;
  try {
    pullResult = await performPull(worktreePath, {
      remote: targetRemote,
      stashIfNeeded: true,
    });
  } catch (pullError) {
    return {
      success: false,
      error: `Sync pull failed: ${getErrorMessage(pullError)}`,
    };
  }

  if (!pullResult.success) {
    return {
      success: false,
      branch: pullResult.branch,
      pulled: false,
      pushed: false,
      error: `Sync pull failed: ${pullResult.error}`,
      hasConflicts: pullResult.hasConflicts,
      conflictFiles: pullResult.conflictFiles,
      conflictSource: pullResult.conflictSource,
    };
  }

  // 2. If pull had conflicts, stop and return conflict info
  if (pullResult.hasConflicts) {
    return {
      success: false,
      branch: pullResult.branch,
      pulled: true,
      pushed: false,
      hasConflicts: true,
      conflictFiles: pullResult.conflictFiles,
      conflictSource: pullResult.conflictSource,
      isFastForward: pullResult.isFastForward,
      isMerge: pullResult.isMerge,
      error: 'Sync stopped: pull resulted in merge conflicts. Resolve conflicts and try again.',
      message: pullResult.message,
    };
  }

  // 3. Push to remote
  logger.info('Sync: pull succeeded, starting push', { worktreePath, remote: targetRemote });

  let pushResult: PushResult;
  try {
    pushResult = await performPush(worktreePath, {
      remote: targetRemote,
    });
  } catch (pushError) {
    return {
      success: false,
      branch: pullResult.branch,
      pulled: true,
      pushed: false,
      isFastForward: pullResult.isFastForward,
      isMerge: pullResult.isMerge,
      error: `Sync push failed: ${getErrorMessage(pushError)}`,
    };
  }

  if (!pushResult.success) {
    // 4. If push diverged after pull, retry once with autoResolve
    if (pushResult.diverged) {
      logger.info('Sync: push diverged after pull, retrying with autoResolve', {
        worktreePath,
        remote: targetRemote,
      });

      try {
        const retryResult = await performPush(worktreePath, {
          remote: targetRemote,
          autoResolve: true,
        });

        if (retryResult.success) {
          return {
            success: true,
            branch: retryResult.branch,
            pulled: true,
            pushed: true,
            autoResolved: true,
            isFastForward: pullResult.isFastForward,
            isMerge: pullResult.isMerge,
            message: 'Sync completed (push required auto-resolve).',
          };
        }

        return {
          success: false,
          branch: retryResult.branch,
          pulled: true,
          pushed: false,
          hasConflicts: retryResult.hasConflicts,
          conflictFiles: retryResult.conflictFiles,
          error: retryResult.error,
        };
      } catch (retryError) {
        return {
          success: false,
          branch: pullResult.branch,
          pulled: true,
          pushed: false,
          error: `Sync push retry failed: ${getErrorMessage(retryError)}`,
        };
      }
    }

    return {
      success: false,
      branch: pushResult.branch,
      pulled: true,
      pushed: false,
      isFastForward: pullResult.isFastForward,
      isMerge: pullResult.isMerge,
      error: `Sync push failed: ${pushResult.error}`,
    };
  }

  return {
    success: true,
    branch: pushResult.branch,
    pulled: pullResult.pulled ?? true,
    pushed: true,
    isFastForward: pullResult.isFastForward,
    isMerge: pullResult.isMerge,
    message: pullResult.pulled
      ? 'Sync completed: pulled latest changes and pushed.'
      : 'Sync completed: already up to date, pushed local commits.',
  };
}