|
9 | 9 | // |
10 | 10 | // Usage: node stable-sync.js [branch-name] |
11 | 11 | // If no branch name is provided, defaults to 'stable-sync' |
| 12 | +// |
| 13 | +// Environment variables: |
| 14 | +// CREATE_BRANCH - if set to 'true', will push the branch at the end |
12 | 15 |
|
13 | 16 | const { promisify } = require('util'); |
14 | 17 | const exec = promisify(require('child_process').exec); |
15 | 18 |
|
16 | 19 | async function runGitCommands() { |
17 | 20 | // Get branch name from command line arguments or use default |
18 | 21 | const branchName = process.argv[2] || 'stable-main'; |
| 22 | + |
| 23 | + // Check if CREATE_BRANCH environment variable exists and is set to true |
| 24 | + const shouldPushBranch = (process.env.CREATE_BRANCH || 'false').toLowerCase() === 'true'; |
19 | 25 |
|
20 | 26 | try { |
21 | 27 | try { |
@@ -111,11 +117,36 @@ async function runGitCommands() { |
111 | 117 | await exec('git add .'); |
112 | 118 | console.log('Executed: git add .'); |
113 | 119 |
|
114 | | - await exec(`git commit -m "Merge origin/main into ${branchName}" --no-verify`); |
115 | | - console.log('Executed: git commit'); |
| 120 | + try { |
| 121 | + // Check if there are any changes to commit |
| 122 | + const { stdout: status } = await exec('git status --porcelain'); |
| 123 | + if (!status.trim()) { |
| 124 | + console.log('No changes to commit, skipping commit step'); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + await exec(`git commit -m "Merge origin/main into ${branchName}" --no-verify`); |
| 129 | + console.log('Executed: git commit'); |
| 130 | + } catch (error) { |
| 131 | + console.error(`Error: ${error.message}`); |
| 132 | + process.exit(1); |
| 133 | + } |
116 | 134 |
|
117 | 135 | console.log(`Your local ${branchName} branch is now ready to become a PR.`); |
118 | | - console.log('You likely now need to do `git push --force`'); |
| 136 | + |
| 137 | + // Push the branch if CREATE_BRANCH is true |
| 138 | + if (shouldPushBranch) { |
| 139 | + try { |
| 140 | + console.log(`Pushing branch ${branchName} to remote...`); |
| 141 | + await exec(`git push --set-upstream origin ${branchName}`); |
| 142 | + console.log(`Successfully pushed branch ${branchName} to remote`); |
| 143 | + } catch (error) { |
| 144 | + console.error(`Error pushing branch: ${error.message}`); |
| 145 | + process.exit(1); |
| 146 | + } |
| 147 | + } else { |
| 148 | + console.log('You likely now need to do `git push --force`'); |
| 149 | + } |
119 | 150 | } catch (error) { |
120 | 151 | console.error(`Error: ${error.message}`); |
121 | 152 | process.exit(1); |
|
0 commit comments