|
1 |
| -/* eslint-disable no-console */ |
2 |
| -import shell from 'shelljs'; |
| 1 | +import simpleGit from 'simple-git'; |
3 | 2 |
|
4 |
| -const upstream = '@{u}'; |
| 3 | +const requiredBranch = 'main'; |
| 4 | +const git = simpleGit(); |
5 | 5 |
|
6 |
| -const isUpstreamConfigured = |
7 |
| - shell.exec(`git rev-parse --abbrev-ref --symbolic-full-name ${upstream}`).code === 0; |
8 |
| - |
9 |
| -if (!isUpstreamConfigured) { |
10 |
| - console.error('You must configure upstream for the branch.'); |
11 |
| - shell.exit(); |
| 6 | +const activeBranch = (await git.branch()).current; |
| 7 | +// Check if the active branch match the required one. |
| 8 | +if (requiredBranch !== activeBranch) { |
| 9 | + throw new Error('You must be on main branch to create a release branch'); |
12 | 10 | }
|
13 | 11 |
|
14 |
| -const local = shell.exec('git rev-parse @'); |
15 |
| -const remote = shell.exec(`git rev-parse ${upstream}`); |
16 |
| -const base = shell.exec(`git merge-base @ ${upstream}`); |
| 12 | +await git.fetch(); |
| 13 | +const gitStatus = await git.status(); |
| 14 | + |
| 15 | +// Check if git directory is clean. |
| 16 | +if (!gitStatus.isClean()) { |
| 17 | + throw new Error('Git working directory must be in a clean state'); |
| 18 | +} |
17 | 19 |
|
18 |
| -// Branch is up to date with or ahead to remote. |
19 |
| -if (local === remote || remote === base) { |
20 |
| - shell.exit(0); |
| 20 | +// Check if branch is tracking a remote one. |
| 21 | +if (!gitStatus.tracking) { |
| 22 | + throw new Error('Local branch is not tracking a remote one'); |
21 | 23 | }
|
22 | 24 |
|
23 |
| -// The local branch is behind the remote version, a pull is needed. |
24 |
| -if (local === base) { |
25 |
| - console.error('The local branch is behind the remote version'); |
26 |
| - shell.exit(); |
| 25 | +// Check if main branch is up to date with remote version. |
| 26 | +if (gitStatus.ahead > 0) { |
| 27 | + throw new Error('Local branch is ahead of the remote version'); |
27 | 28 | }
|
| 29 | +if (gitStatus.behind > 0) { |
| 30 | + throw new Error('Local branch is behind of the remote version'); |
| 31 | +} |
| 32 | + |
| 33 | +// /* eslint-disable no-console */ |
| 34 | +// import shell from 'shelljs'; |
| 35 | + |
| 36 | +// const upstream = '@{u}'; |
| 37 | + |
| 38 | +// const isUpstreamConfigured = |
| 39 | +// shell.exec(`git rev-parse --abbrev-ref --symbolic-full-name ${upstream}`).code === 0; |
| 40 | + |
| 41 | +// if (!isUpstreamConfigured) { |
| 42 | +// console.error('You must configure upstream for the branch.'); |
| 43 | +// shell.exit(); |
| 44 | +// } |
| 45 | + |
| 46 | +// const local = shell.exec('git rev-parse @'); |
| 47 | +// const remote = shell.exec(`git rev-parse ${upstream}`); |
| 48 | +// const base = shell.exec(`git merge-base @ ${upstream}`); |
| 49 | + |
| 50 | +// // Branch is up to date with or ahead to remote. |
| 51 | +// if (local === remote || remote === base) { |
| 52 | +// shell.exit(0); |
| 53 | +// } |
| 54 | + |
| 55 | +// // The local branch is behind the remote version, a pull is needed. |
| 56 | +// if (local === base) { |
| 57 | +// console.error('The local branch is behind the remote version'); |
| 58 | +// shell.exit(); |
| 59 | +// } |
28 | 60 |
|
29 |
| -// Local and remote diverged. |
30 |
| -console.log('Local branch and remote branch diverged!'); |
31 |
| -shell.exit(); |
| 61 | +// // Local and remote diverged. |
| 62 | +// console.log('Local branch and remote branch diverged!'); |
| 63 | +// shell.exit(); |
0 commit comments