Leon4gr45 commited on
Commit
eb97644
·
verified ·
1 Parent(s): 3dfd512

Robust branch handling in GitHub sync

Browse files
server/src/services/githubSyncService.ts CHANGED
@@ -22,7 +22,17 @@ export async function runGithubSync(): Promise<void> {
22
  try {
23
  if (!fs.existsSync(syncDir)) {
24
  logInfo(`GitHub Sync: Cloning repository into ${syncDir}`);
25
- execSync(`git clone -b ${BRANCH} ${authenticatedRepoUrl} ${syncDir}`, { stdio: 'ignore' });
 
 
 
 
 
 
 
 
 
 
26
  } else {
27
  logInfo('GitHub Sync: Pulling latest changes');
28
  execSync(`git -C ${syncDir} pull origin ${BRANCH}`, { stdio: 'ignore' });
 
22
  try {
23
  if (!fs.existsSync(syncDir)) {
24
  logInfo(`GitHub Sync: Cloning repository into ${syncDir}`);
25
+ execSync(`git clone ${authenticatedRepoUrl} ${syncDir}`, { stdio: 'ignore' });
26
+
27
+ // Check if branch exists, otherwise create it
28
+ const branches = execSync(`git -C ${syncDir} branch -a`, { encoding: 'utf8' });
29
+ if (branches.includes(`remotes/origin/${BRANCH}`)) {
30
+ execSync(`git -C ${syncDir} checkout ${BRANCH}`, { stdio: 'ignore' });
31
+ } else {
32
+ logInfo(`GitHub Sync: Creating new branch ${BRANCH}`);
33
+ execSync(`git -C ${syncDir} checkout -b ${BRANCH}`, { stdio: 'ignore' });
34
+ execSync(`git -C ${syncDir} push -u origin ${BRANCH}`, { stdio: 'ignore' });
35
+ }
36
  } else {
37
  logInfo('GitHub Sync: Pulling latest changes');
38
  execSync(`git -C ${syncDir} pull origin ${BRANCH}`, { stdio: 'ignore' });