@litmers/cursorflow-orchestrator 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils/git.ts CHANGED
@@ -1081,6 +1081,96 @@ export function pushBranchSafe(branchName: string, options: { cwd?: string; forc
1081
1081
  return { success: false, error: result.stderr };
1082
1082
  }
1083
1083
 
1084
+ /**
1085
+ * Push branch to remote with fallback branch name if push is rejected.
1086
+ *
1087
+ * If the remote branch already exists and push is rejected (e.g., fetch first error),
1088
+ * this function will rename the local branch with a suffix and retry the push.
1089
+ *
1090
+ * @returns Object with success status, the final branch name used, and optional error
1091
+ */
1092
+ export function pushWithFallbackBranchName(
1093
+ branchName: string,
1094
+ options: {
1095
+ cwd?: string;
1096
+ setUpstream?: boolean;
1097
+ maxRetries?: number;
1098
+ } = {}
1099
+ ): { success: boolean; finalBranchName: string; error?: string; renamed?: boolean } {
1100
+ const { cwd, setUpstream = true, maxRetries = 3 } = options;
1101
+
1102
+ // Check if origin exists
1103
+ if (!remoteExists('origin', { cwd })) {
1104
+ // If no origin, just skip pushing (useful for local tests)
1105
+ return { success: true, finalBranchName: branchName };
1106
+ }
1107
+
1108
+ let currentBranchName = branchName;
1109
+ let retryCount = 0;
1110
+ let renamed = false;
1111
+
1112
+ while (retryCount < maxRetries) {
1113
+ const args = ['push'];
1114
+ if (setUpstream) {
1115
+ args.push('-u', 'origin', currentBranchName);
1116
+ } else {
1117
+ args.push('origin', currentBranchName);
1118
+ }
1119
+
1120
+ const result = runGitResult(args, { cwd });
1121
+
1122
+ if (result.success) {
1123
+ return { success: true, finalBranchName: currentBranchName, renamed };
1124
+ }
1125
+
1126
+ // Check if the error is "rejected" (remote has newer commits or branch exists with different history)
1127
+ const isRejected = result.stderr.includes('[rejected]') ||
1128
+ result.stderr.includes('fetch first') ||
1129
+ result.stderr.includes('non-fast-forward');
1130
+
1131
+ if (!isRejected) {
1132
+ // Other error, don't retry
1133
+ return { success: false, finalBranchName: currentBranchName, error: result.stderr };
1134
+ }
1135
+
1136
+ // Generate new branch name with suffix
1137
+ retryCount++;
1138
+ const timestamp = Date.now();
1139
+ const newBranchName = `${branchName}-merged-${timestamp}`;
1140
+
1141
+ logger.warn(`⚠️ Push rejected for '${currentBranchName}', renaming to '${newBranchName}'`);
1142
+
1143
+ // Rename local branch
1144
+ const renameResult = runGitResult(['branch', '-m', currentBranchName, newBranchName], { cwd });
1145
+ if (!renameResult.success) {
1146
+ return {
1147
+ success: false,
1148
+ finalBranchName: currentBranchName,
1149
+ error: `Failed to rename branch: ${renameResult.stderr}`
1150
+ };
1151
+ }
1152
+
1153
+ currentBranchName = newBranchName;
1154
+ renamed = true;
1155
+
1156
+ // Small delay to avoid timestamp collision
1157
+ if (retryCount < maxRetries) {
1158
+ // Synchronous delay
1159
+ const start = Date.now();
1160
+ while (Date.now() - start < 100) {
1161
+ // busy wait
1162
+ }
1163
+ }
1164
+ }
1165
+
1166
+ return {
1167
+ success: false,
1168
+ finalBranchName: currentBranchName,
1169
+ error: `Push failed after ${maxRetries} retries`,
1170
+ renamed,
1171
+ };
1172
+ }
1173
+
1084
1174
  /**
1085
1175
  * Auto-commit any uncommitted changes and push to remote
1086
1176
  * Used for checkpoint before destructive operations