|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync, exec } = require('node:child_process'); |
| 4 | +const fs = require('node:fs'); |
| 5 | + |
| 6 | +/* This script should be deleted someday. It is for syncing commits from the |
| 7 | + * old HarperDB closed-source repository while the Harper devs were |
| 8 | + * transitioning the platform to open source. See CONTRIBUTING.md for more |
| 9 | + * details. - WSM 2026-01-20 |
| 10 | + */ |
| 11 | + |
| 12 | +function letsBail(exitCode, syncBranch = null) { |
| 13 | + execSync('git checkout main', { stdio: 'ignore' }); |
| 14 | + if (syncBranch) { |
| 15 | + execSync(`git branch -D ${syncBranch}`, { stdio: 'ignore' }); |
| 16 | + } |
| 17 | + process.exit(exitCode); |
| 18 | +} |
| 19 | + |
| 20 | +function gitRemotes() { |
| 21 | + let remotesList = execSync('git remote -v') |
| 22 | + .toString() |
| 23 | + .trim() |
| 24 | + .split('\n') |
| 25 | + .map((r) => r.split('\t')); |
| 26 | + let remotes = {}; |
| 27 | + remotesList.forEach(([name, urlAndType]) => { |
| 28 | + if (remotes[name] == null) { |
| 29 | + remotes[name] = {}; |
| 30 | + } |
| 31 | + let [url, type] = urlAndType.split(' '); |
| 32 | + type = type.replace('(', '').replace(')', ''); |
| 33 | + remotes[name][type] = url; |
| 34 | + }); |
| 35 | + return remotes; |
| 36 | +} |
| 37 | + |
| 38 | +function verifyRemote(remoteName, remoteUrl) { |
| 39 | + let remotes = gitRemotes(); |
| 40 | + if (!Object.hasOwn(remotes, remoteName)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + if (!(Object.hasOwn(remotes[remoteName], 'fetch') && Object.hasOwn(remotes[remoteName], 'push'))) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + return remotes[remoteName]['fetch'] === remoteUrl && remotes[remoteName]['push'] === remoteUrl; |
| 47 | +} |
| 48 | + |
| 49 | +function isOldRemoteConfigured() { |
| 50 | + return verifyRemote('old', 'git@github.com:HarperFast/harperdb.git'); |
| 51 | +} |
| 52 | + |
| 53 | +function isOriginRemoteConfigured() { |
| 54 | + return verifyRemote('origin', 'git@github.com:HarperFast/harper.git'); |
| 55 | +} |
| 56 | + |
| 57 | +function isBranchCheckedOut(branchName) { |
| 58 | + let branch = execSync(`git branch --show-current`).toString().trim(); |
| 59 | + return branch === branchName; |
| 60 | +} |
| 61 | + |
| 62 | +function fetchCommits(remoteName) { |
| 63 | + exec(`git fetch ${remoteName}`, (error, _stdout, _stderr) => { |
| 64 | + // Note that git outputs all kinds of non-errors on stderr, so we don't |
| 65 | + // want to assume something went wrong if there's anything written there. |
| 66 | + if (error) { |
| 67 | + console.error(`git exited with error '${error.message}' fetching ${remoteName} commits`); |
| 68 | + letsBail(error.code); |
| 69 | + } |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function pullRemoteBranch(remoteName, branchName) { |
| 74 | + fetchCommits(remoteName); |
| 75 | + exec(`git merge ${remoteName}/${branchName}`, (error, _stdout, stderr) => { |
| 76 | + if (error) { |
| 77 | + console.error(`git exited with error '${error.message}' merging origin/main`); |
| 78 | + letsBail(error.code); |
| 79 | + } |
| 80 | + if (stderr) { |
| 81 | + console.error(`git error merging origin/main: ${stderr}`); |
| 82 | + letsBail(6); |
| 83 | + } |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +function checkoutNewBranch(branchName) { |
| 88 | + exec(`git checkout -b ${branchName}`, (error, _stdout, stderr) => { |
| 89 | + if (error) { |
| 90 | + console.error(`git exited with error '${error.message}' creating branch ${branchName}`); |
| 91 | + letsBail(error.code, branchName); |
| 92 | + } |
| 93 | + if (stderr && !stderr.startsWith('Switched to a new branch')) { |
| 94 | + console.error(`git error creating branch ${branchName}: ${stderr}`); |
| 95 | + letsBail(7, branchName); |
| 96 | + } |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function ensureValidConfig() { |
| 101 | + process.stdout.write('Verifying git config... '); |
| 102 | + if (!isOldRemoteConfigured()) { |
| 103 | + process.stdout.write('❌'); |
| 104 | + console.error('old remote not configured correctly.'); |
| 105 | + console.error( |
| 106 | + 'Run `git remote add old git@github.com:HarperFast/harperdb.git` to configure it (you may have to remove the old remote first with `git remote rm old`).' |
| 107 | + ); |
| 108 | + process.exit(2); |
| 109 | + } |
| 110 | + if (!isOriginRemoteConfigured()) { |
| 111 | + console.log('❌'); |
| 112 | + console.error('origin remote not configured correctly.'); |
| 113 | + console.error( |
| 114 | + 'Run `git remote add origin git@github.com:HarperFast/harper.git` to configure it (you may have to remove the origin remote first with `git remote rm origin`).' |
| 115 | + ); |
| 116 | + process.exit(3); |
| 117 | + } |
| 118 | + if (!isBranchCheckedOut('main')) { |
| 119 | + console.log('❌'); |
| 120 | + console.error('main branch not checked out. Run `git checkout main` to check it out.'); |
| 121 | + process.exit(4); |
| 122 | + } |
| 123 | + console.log('✅'); |
| 124 | +} |
| 125 | + |
| 126 | +function generateCommitsToPick(startCommit) { |
| 127 | + const commits = execSync(`git rev-list --reverse --first-parent ${startCommit}..old/main`) |
| 128 | + .toString() |
| 129 | + .trim() |
| 130 | + .split('\n'); |
| 131 | + // write to file in case a human needs to take over |
| 132 | + fs.writeFileSync('commits-to-pick.txt', commits.join('\n') + '\n'); |
| 133 | + return commits; |
| 134 | +} |
| 135 | + |
| 136 | +function isMergeCommit(commit) { |
| 137 | + try { |
| 138 | + execSync(`git rev-parse ${commit}^2`, { stdio: 'ignore' }); |
| 139 | + } catch { |
| 140 | + return false; |
| 141 | + } |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +function doItRockapella(startCommit) { |
| 146 | + process.stdout.write('Finding commits to sync... '); |
| 147 | + fetchCommits('old'); |
| 148 | + pullRemoteBranch('origin', 'main'); |
| 149 | + const syncDate = new Date(); |
| 150 | + const month = String(syncDate.getMonth() + 1).padStart(2, '0'); |
| 151 | + const day = String(syncDate.getDate()).padStart(2, '0'); |
| 152 | + checkoutNewBranch(`sync-${month}${day}${syncDate.getFullYear()}`); |
| 153 | + const commits = generateCommitsToPick(startCommit); |
| 154 | + console.log('✅'); |
| 155 | + console.log(`\n${commits.length} commits found:`); |
| 156 | + for (const commit of commits) { |
| 157 | + if (isMergeCommit(commit)) { |
| 158 | + console.log(`${commit} (merge): git cherry-pick -m 1 ${commit}`); |
| 159 | + } else { |
| 160 | + console.log(`${commit}: git cherry-pick ${commit}`); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +function run(startCommit) { |
| 166 | + if (!startCommit) { |
| 167 | + console.error(`No start commit specified. Specify a commit hash or tag: sync-commits.js <commit hash or tag>`); |
| 168 | + letsBail(1); |
| 169 | + } |
| 170 | + ensureValidConfig(); |
| 171 | + doItRockapella(startCommit); |
| 172 | +} |
| 173 | + |
| 174 | +run(process.argv[2]); |
0 commit comments