Stijnus commited on
Commit
3dd3faf
·
1 Parent(s): 9171cf4

Update api.update.ts

Browse files
Files changed (1) hide show
  1. app/routes/api.update.ts +80 -23
app/routes/api.update.ts CHANGED
@@ -45,6 +45,27 @@ export const action: ActionFunction = async ({ request }) => {
45
  };
46
 
47
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  // Fetch stage
49
  sendProgress({
50
  stage: 'fetch',
@@ -52,31 +73,67 @@ export const action: ActionFunction = async ({ request }) => {
52
  progress: 0,
53
  });
54
 
 
 
 
 
 
 
 
 
 
 
55
  // Get current commit hash
56
  const { stdout: currentCommit } = await execAsync('git rev-parse HEAD');
57
 
58
- // Fetch changes
59
- await execAsync('git fetch origin');
 
 
60
 
61
  // Get list of changed files
62
- const { stdout: diffOutput } = await execAsync(`git diff --name-status origin/${branch}`);
63
- const changedFiles = diffOutput
64
- .split('\n')
65
- .filter(Boolean)
66
- .map((line) => {
67
- const [status, file] = line.split('\t');
68
- return `${status === 'M' ? 'Modified' : status === 'A' ? 'Added' : 'Deleted'}: ${file}`;
69
- });
 
 
 
 
70
 
71
  // Get commit messages
72
- const { stdout: logOutput } = await execAsync(`git log --oneline ${currentCommit.trim()}..origin/${branch}`);
73
- const commitMessages = logOutput.split('\n').filter(Boolean);
 
 
 
 
 
 
74
 
75
  // Get diff stats
76
- const { stdout: diffStats } = await execAsync(`git diff --shortstat origin/${branch}`);
77
- const stats = diffStats.match(
78
- /(\d+) files? changed(?:, (\d+) insertions?\\(\\+\\))?(?:, (\d+) deletions?\\(-\\))?/,
79
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  sendProgress({
82
  stage: 'fetch',
@@ -93,11 +150,11 @@ export const action: ActionFunction = async ({ request }) => {
93
  // Pull stage
94
  sendProgress({
95
  stage: 'pull',
96
- message: `Pulling changes from ${branch}...`,
97
  progress: 0,
98
  });
99
 
100
- await execAsync(`git pull origin ${branch}`);
101
 
102
  sendProgress({
103
  stage: 'pull',
@@ -141,11 +198,11 @@ export const action: ActionFunction = async ({ request }) => {
141
  message: 'Update completed successfully! Click Restart to apply changes.',
142
  progress: 100,
143
  });
144
- } catch (error) {
145
  sendProgress({
146
  stage: 'complete',
147
  message: 'Update failed',
148
- error: error instanceof Error ? error.message : 'Unknown error occurred',
149
  });
150
  } finally {
151
  controller.close();
@@ -160,12 +217,12 @@ export const action: ActionFunction = async ({ request }) => {
160
  Connection: 'keep-alive',
161
  },
162
  });
163
- } catch (error) {
164
- console.error('Update preparation failed:', error);
165
  return json(
166
  {
167
  success: false,
168
- error: error instanceof Error ? error.message : 'Unknown error occurred while preparing update',
169
  },
170
  { status: 500 },
171
  );
 
45
  };
46
 
47
  try {
48
+ // Check if remote exists
49
+ let defaultBranch = branch || 'main'; // Make branch mutable
50
+
51
+ try {
52
+ await execAsync('git remote get-url origin');
53
+ } catch {
54
+ throw new Error(
55
+ 'No remote repository found. Please set up the remote repository first by running:\ngit remote add origin https://github.com/stackblitz-labs/bolt.diy.git',
56
+ );
57
+ }
58
+
59
+ // Get default branch if not specified
60
+ if (!branch) {
61
+ try {
62
+ const { stdout } = await execAsync('git remote show origin | grep "HEAD branch" | cut -d" " -f5');
63
+ defaultBranch = stdout.trim() || 'main';
64
+ } catch {
65
+ defaultBranch = 'main'; // Fallback to main if we can't detect
66
+ }
67
+ }
68
+
69
  // Fetch stage
70
  sendProgress({
71
  stage: 'fetch',
 
73
  progress: 0,
74
  });
75
 
76
+ // Fetch all remotes
77
+ await execAsync('git fetch --all');
78
+
79
+ // Check if remote branch exists
80
+ try {
81
+ await execAsync(`git rev-parse --verify origin/${defaultBranch}`);
82
+ } catch {
83
+ throw new Error(`Remote branch 'origin/${defaultBranch}' not found. Please push your changes first.`);
84
+ }
85
+
86
  // Get current commit hash
87
  const { stdout: currentCommit } = await execAsync('git rev-parse HEAD');
88
 
89
+ // Initialize variables
90
+ let changedFiles: string[] = [];
91
+ let commitMessages: string[] = [];
92
+ let stats: RegExpMatchArray | null = null;
93
 
94
  // Get list of changed files
95
+ try {
96
+ const { stdout: diffOutput } = await execAsync(`git diff --name-status origin/${defaultBranch}`);
97
+ changedFiles = diffOutput
98
+ .split('\n')
99
+ .filter(Boolean)
100
+ .map((line) => {
101
+ const [status, file] = line.split('\t');
102
+ return `${status === 'M' ? 'Modified' : status === 'A' ? 'Added' : 'Deleted'}: ${file}`;
103
+ });
104
+ } catch {
105
+ // Handle silently - empty changedFiles array will be used
106
+ }
107
 
108
  // Get commit messages
109
+ try {
110
+ const { stdout: logOutput } = await execAsync(
111
+ `git log --oneline ${currentCommit.trim()}..origin/${defaultBranch}`,
112
+ );
113
+ commitMessages = logOutput.split('\n').filter(Boolean);
114
+ } catch {
115
+ // Handle silently - empty commitMessages array will be used
116
+ }
117
 
118
  // Get diff stats
119
+ try {
120
+ const { stdout: diffStats } = await execAsync(`git diff --shortstat origin/${defaultBranch}`);
121
+ stats = diffStats.match(
122
+ /(\d+) files? changed(?:, (\d+) insertions?\\(\\+\\))?(?:, (\d+) deletions?\\(-\\))?/,
123
+ );
124
+ } catch {
125
+ // Handle silently - null stats will be used
126
+ }
127
+
128
+ // If no changes detected
129
+ if (!stats && changedFiles.length === 0) {
130
+ sendProgress({
131
+ stage: 'complete',
132
+ message: 'No updates available. You are on the latest version.',
133
+ progress: 100,
134
+ });
135
+ return;
136
+ }
137
 
138
  sendProgress({
139
  stage: 'fetch',
 
150
  // Pull stage
151
  sendProgress({
152
  stage: 'pull',
153
+ message: `Pulling changes from ${defaultBranch}...`,
154
  progress: 0,
155
  });
156
 
157
+ await execAsync(`git pull origin ${defaultBranch}`);
158
 
159
  sendProgress({
160
  stage: 'pull',
 
198
  message: 'Update completed successfully! Click Restart to apply changes.',
199
  progress: 100,
200
  });
201
+ } catch (err) {
202
  sendProgress({
203
  stage: 'complete',
204
  message: 'Update failed',
205
+ error: err instanceof Error ? err.message : 'Unknown error occurred',
206
  });
207
  } finally {
208
  controller.close();
 
217
  Connection: 'keep-alive',
218
  },
219
  });
220
+ } catch (err) {
221
+ console.error('Update preparation failed:', err);
222
  return json(
223
  {
224
  success: false,
225
+ error: err instanceof Error ? err.message : 'Unknown error occurred while preparing update',
226
  },
227
  { status: 500 },
228
  );