From 6b79447bddf09a443e9d3ef13b1926897ee834f9 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 12:45:44 +1100 Subject: [PATCH 01/10] add new option --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d475573..f643cf8 100755 --- a/index.js +++ b/index.js @@ -171,7 +171,8 @@ async function main() { .option('-f, --force', 'Force push to remote') .option('--push-merged', 'Push all branches (inclusing those that have already been merged into master)') .option('--remote ', 'Set remote to push to. Defaults to "origin"') - .option('-c, --create-prs', 'Create GitHub PRs from your train branches'); + .option('-c, --create-prs', 'Create GitHub PRs from your train branches') + .option('-n , --new-branch ', 'Create a new branch in current train'); program.on('--help', () => { console.log(''); From f6e0acdd2c40a90b3ed6a6b05404e0587a133aa9 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 13:36:59 +1100 Subject: [PATCH 02/10] . --- index.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/index.js b/index.js index f643cf8..e760932 100755 --- a/index.js +++ b/index.js @@ -63,6 +63,27 @@ async function pushBranches(sg, branches, forcePush, remote = DEFAULT_REMOTE) { console.log('All changes pushed ' + emoji.get('white_check_mark')); } +async function checkoutNewBranch(sg, newBranch){ + await sg.raw(['checkout', '-b', newBranch]); +} + +async function addNewBranchToTrain(sg, ymlConfig, newBranch){ + const trainCfg = await getBranchesConfigInCurrentTrain(sg, ymlConfig); + if (!trainCfg) { + console.log(`Current branch ${currentBranch} is not a train branch.`); + process.exit(1); + } + + await sg.raw(['checkout', '-b', newBranch]); + const currentBranch = newBranch +} + +async function insertBranchNameIntoTrainConfig(sg, trainKey, branchName, ymlConfig) { + const branchNames = ymlConfig[trainKey] + branchNames.push(branchName) + saveConfig(sg, ymlConfig) +} + async function getUnmergedBranches(sg, branches) { const mergedBranchesOutput = await sg.raw(['branch', '--merged', 'master']); const mergedBranches = mergedBranchesOutput @@ -91,6 +112,15 @@ async function loadConfig(sg) { return yaml.safeLoad(fs.readFileSync(path, 'utf8')); } +/** + * @param {simpleGit.SimpleGit} sg + * @return {Promise.<{trains: Array.}>} + */ +async function saveConfig(sg, ymlConfig) { + const path = await getConfigPath(sg); + return fs.writeFileSync(path, yaml.safeDump(ymlConfig)) +} + /** * @param {BranchCfg} branchCfg */ @@ -116,6 +146,24 @@ async function getBranchesConfigInCurrentTrain(sg, config) { return key && trains[key]; } +/** + * @return {Promise.} + */ +async function getCurrentTrainKey(sg, config) { + const branches = await sg.branchLocal(); + const currentBranch = branches.current; + const { trains } = config; + if (!trains) { + return null; + } + const key = Object.keys(trains).find(trainKey => { + const branches = trains[trainKey]; + const branchNames = branches.map(b => getBranchName(b)); + return branchNames.indexOf(currentBranch) >= 0; + }); + return key; +} + /** * @param {Array.} branchConfig */ @@ -290,6 +338,13 @@ async function main() { await ensurePrsExist(sg, sortedTrainBranches, combinedTrainBranch, program.remote); return; } + + if (program.newBranch) { + const currentTrainKey = await getCurrentTrainKey(sg, ymlConfig); + await sg.raw(['checkout', '-b', program.newBranch]); + insertBranchNameIntoTrainConfig(sg, currentTrainKey, program.newBranch, ymlConfig); + return; + } for (let i = 0; i < sortedTrainBranches.length - 1; ++i) { const b1 = sortedTrainBranches[i]; From 99dd255d01a3f7613f69ddf415928b6f380869e0 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 14:42:59 +1100 Subject: [PATCH 03/10] . --- index.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index e760932..2140b55 100755 --- a/index.js +++ b/index.js @@ -67,21 +67,11 @@ async function checkoutNewBranch(sg, newBranch){ await sg.raw(['checkout', '-b', newBranch]); } -async function addNewBranchToTrain(sg, ymlConfig, newBranch){ - const trainCfg = await getBranchesConfigInCurrentTrain(sg, ymlConfig); - if (!trainCfg) { - console.log(`Current branch ${currentBranch} is not a train branch.`); - process.exit(1); - } - - await sg.raw(['checkout', '-b', newBranch]); - const currentBranch = newBranch -} - -async function insertBranchNameIntoTrainConfig(sg, trainKey, branchName, ymlConfig) { - const branchNames = ymlConfig[trainKey] +function addBranchToYmlConfig(trainKey, branchName, ymlConfig) { + const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)) + const branchNames = newYmlConfig[trainKey] branchNames.push(branchName) - saveConfig(sg, ymlConfig) + return newYmlConfig } async function getUnmergedBranches(sg, branches) { @@ -341,8 +331,9 @@ async function main() { if (program.newBranch) { const currentTrainKey = await getCurrentTrainKey(sg, ymlConfig); - await sg.raw(['checkout', '-b', program.newBranch]); - insertBranchNameIntoTrainConfig(sg, currentTrainKey, program.newBranch, ymlConfig); + await checkoutNewBranch(sg, program.newBranch) + const newYmlConfig = addBranchToYmlConfig(currentTrainKey, program.newBranch, ymlConfig); + await saveConfig(newYmlConfig) return; } From 0da93a70219dccfacbf90828a83d835f77ccc440 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 14:59:09 +1100 Subject: [PATCH 04/10] . --- index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2140b55..024380c 100755 --- a/index.js +++ b/index.js @@ -67,10 +67,12 @@ async function checkoutNewBranch(sg, newBranch){ await sg.raw(['checkout', '-b', newBranch]); } -function addBranchToYmlConfig(trainKey, branchName, ymlConfig) { +async function addCurrentBranchToYmlConfig(sg, trainKey, ymlConfig) { + const branches = await sg.branchLocal(); + const currentBranch = branches.current; const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)) - const branchNames = newYmlConfig[trainKey] - branchNames.push(branchName) + const branchConfigs = newYmlConfig.trains[trainKey] + branchConfigs.push(currentBranch) return newYmlConfig } @@ -104,11 +106,11 @@ async function loadConfig(sg) { /** * @param {simpleGit.SimpleGit} sg - * @return {Promise.<{trains: Array.}>} + * @param {trains: Array.} sg */ async function saveConfig(sg, ymlConfig) { const path = await getConfigPath(sg); - return fs.writeFileSync(path, yaml.safeDump(ymlConfig)) + fs.writeFileSync(path, yaml.safeDump(ymlConfig)) } /** @@ -270,7 +272,8 @@ async function main() { } const { current: currentBranch, all: allBranches } = await sg.branchLocal(); - const trainCfg = await getBranchesConfigInCurrentTrain(sg, ymlConfig); + const currentTrainKey = await getCurrentTrainKey(sg, ymlConfig); + const trainCfg = ymlConfig.trains[currentTrainKey]; if (!trainCfg) { console.log(`Current branch ${currentBranch} is not a train branch.`); process.exit(1); @@ -332,7 +335,7 @@ async function main() { if (program.newBranch) { const currentTrainKey = await getCurrentTrainKey(sg, ymlConfig); await checkoutNewBranch(sg, program.newBranch) - const newYmlConfig = addBranchToYmlConfig(currentTrainKey, program.newBranch, ymlConfig); + const newYmlConfig = await addCurrentBranchToYmlConfig(sg, currentTrainKey, ymlConfig); await saveConfig(newYmlConfig) return; } From 7d218b131ade264d6bb5cdc39e21547e0f392f1a Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 15:05:07 +1100 Subject: [PATCH 05/10] . --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 024380c..0462d9b 100755 --- a/index.js +++ b/index.js @@ -67,7 +67,8 @@ async function checkoutNewBranch(sg, newBranch){ await sg.raw(['checkout', '-b', newBranch]); } -async function addCurrentBranchToYmlConfig(sg, trainKey, ymlConfig) { +async function addCurrentBranchToYmlConfig(sg, trainCfg, ymlConfig) { + const trainKey = ymlConfig.trains.indexOf(trainCfg) const branches = await sg.branchLocal(); const currentBranch = branches.current; const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)) @@ -272,8 +273,7 @@ async function main() { } const { current: currentBranch, all: allBranches } = await sg.branchLocal(); - const currentTrainKey = await getCurrentTrainKey(sg, ymlConfig); - const trainCfg = ymlConfig.trains[currentTrainKey]; + const trainCfg = await getBranchesConfigInCurrentTrain(sg, ymlConfig); if (!trainCfg) { console.log(`Current branch ${currentBranch} is not a train branch.`); process.exit(1); @@ -333,9 +333,8 @@ async function main() { } if (program.newBranch) { - const currentTrainKey = await getCurrentTrainKey(sg, ymlConfig); await checkoutNewBranch(sg, program.newBranch) - const newYmlConfig = await addCurrentBranchToYmlConfig(sg, currentTrainKey, ymlConfig); + const newYmlConfig = await addCurrentBranchToYmlConfig(sg, trainCfg, ymlConfig); await saveConfig(newYmlConfig) return; } From c52baf4e8ef507d5886495cf86577623469a29dc Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 15:11:15 +1100 Subject: [PATCH 06/10] semi -colons --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0462d9b..a437ae4 100755 --- a/index.js +++ b/index.js @@ -68,13 +68,13 @@ async function checkoutNewBranch(sg, newBranch){ } async function addCurrentBranchToYmlConfig(sg, trainCfg, ymlConfig) { - const trainKey = ymlConfig.trains.indexOf(trainCfg) + const trainKey = ymlConfig.trains.indexOf(trainCfg); const branches = await sg.branchLocal(); const currentBranch = branches.current; - const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)) - const branchConfigs = newYmlConfig.trains[trainKey] - branchConfigs.push(currentBranch) - return newYmlConfig + const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)); + const branchConfigs = newYmlConfig.trains[trainKey]; + branchConfigs.push(currentBranch); + return newYmlConfig; } async function getUnmergedBranches(sg, branches) { @@ -333,9 +333,9 @@ async function main() { } if (program.newBranch) { - await checkoutNewBranch(sg, program.newBranch) + await checkoutNewBranch(sg, program.newBranch); const newYmlConfig = await addCurrentBranchToYmlConfig(sg, trainCfg, ymlConfig); - await saveConfig(newYmlConfig) + await saveConfig(newYmlConfig); return; } From 81a3aae2997e098e159b8f3e080ca8b57295c1e1 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 15:21:36 +1100 Subject: [PATCH 07/10] . --- index.js | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/index.js b/index.js index a437ae4..9bae70b 100755 --- a/index.js +++ b/index.js @@ -139,24 +139,6 @@ async function getBranchesConfigInCurrentTrain(sg, config) { return key && trains[key]; } -/** - * @return {Promise.} - */ -async function getCurrentTrainKey(sg, config) { - const branches = await sg.branchLocal(); - const currentBranch = branches.current; - const { trains } = config; - if (!trains) { - return null; - } - const key = Object.keys(trains).find(trainKey => { - const branches = trains[trainKey]; - const branchNames = branches.map(b => getBranchName(b)); - return branchNames.indexOf(currentBranch) >= 0; - }); - return key; -} - /** * @param {Array.} branchConfig */ From 94a66d3a8585c60c0715cd7aae1449af2b89e1c6 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Thu, 22 Oct 2020 17:56:18 +1100 Subject: [PATCH 08/10] . --- index.js | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 9bae70b..71eb233 100755 --- a/index.js +++ b/index.js @@ -64,17 +64,30 @@ async function pushBranches(sg, branches, forcePush, remote = DEFAULT_REMOTE) { } async function checkoutNewBranch(sg, newBranch){ - await sg.raw(['checkout', '-b', newBranch]); + try { + await sg.raw(['checkout', '-b', newBranch]); + } catch (e) { + console.log(`${newBranch} is an existing branch... Checking out`) + await sg.raw(['checkout', newBranch]); + } } -async function addCurrentBranchToYmlConfig(sg, trainCfg, ymlConfig) { - const trainKey = ymlConfig.trains.indexOf(trainCfg); +async function addCurrentBranchToYmlConfig(sg, branchCfg, trainCfg, ymlConfig) { + const trainKey = getKeyOfTrain(trainCfg, ymlConfig); + const branches = await sg.branchLocal(); const currentBranch = branches.current; - const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)); - const branchConfigs = newYmlConfig.trains[trainKey]; - branchConfigs.push(currentBranch); - return newYmlConfig; + const { trains } = ymlConfig; + if (trains[trainKey].indexOf(currentBranch) >=0) { + console.log(`${currentBranch} is already in this train`) + return null + } else { + const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)); + const branchConfigs = newYmlConfig.trains[trainKey]; + const newBranchIndex = branchConfigs.indexOf(branchCfg) + 1; + branchConfigs.splice(newBranchIndex, 0, currentBranch); + return newYmlConfig; + } } async function getUnmergedBranches(sg, branches) { @@ -146,6 +159,12 @@ function getBranchesInCurrentTrain(branchConfig) { return branchConfig.map(b => getBranchName(b)); } +function getKeyOfTrain(trainCgf, ymlConfig) { + const { trains } = ymlConfig; + return Object.keys(trains).find(trainKey => { + return trains[trainKey] === trainCgf; + }); +} /** * @param {Array.} branchConfig */ @@ -195,7 +214,7 @@ async function main() { .option('--push-merged', 'Push all branches (inclusing those that have already been merged into master)') .option('--remote ', 'Set remote to push to. Defaults to "origin"') .option('-c, --create-prs', 'Create GitHub PRs from your train branches') - .option('-n , --new-branch ', 'Create a new branch in current train'); + .option('-n, --new-branch ', 'Create a new branch in current train'); program.on('--help', () => { console.log(''); @@ -315,9 +334,13 @@ async function main() { } if (program.newBranch) { + const branchOnTrain = currentBranch await checkoutNewBranch(sg, program.newBranch); - const newYmlConfig = await addCurrentBranchToYmlConfig(sg, trainCfg, ymlConfig); - await saveConfig(newYmlConfig); + const newYmlConfig = await addCurrentBranchToYmlConfig(sg, branchOnTrain, trainCfg, ymlConfig); + if (newYmlConfig) { + await saveConfig(sg, newYmlConfig); + console.log(`${program.newBranch} added to the train after ${branchOnTrain}`) + } return; } From 72556c555098ca2cf34da940fe55303e767f288a Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Fri, 23 Oct 2020 00:13:17 +1100 Subject: [PATCH 09/10] . --- index.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 71eb233..48fb917 100755 --- a/index.js +++ b/index.js @@ -72,9 +72,7 @@ async function checkoutNewBranch(sg, newBranch){ } } -async function addCurrentBranchToYmlConfig(sg, branchCfg, trainCfg, ymlConfig) { - const trainKey = getKeyOfTrain(trainCfg, ymlConfig); - +async function addCurrentBranchToYmlConfig(sg, atIndex, trainKey, ymlConfig) { const branches = await sg.branchLocal(); const currentBranch = branches.current; const { trains } = ymlConfig; @@ -84,8 +82,7 @@ async function addCurrentBranchToYmlConfig(sg, branchCfg, trainCfg, ymlConfig) { } else { const newYmlConfig = JSON.parse(JSON.stringify(ymlConfig)); const branchConfigs = newYmlConfig.trains[trainKey]; - const newBranchIndex = branchConfigs.indexOf(branchCfg) + 1; - branchConfigs.splice(newBranchIndex, 0, currentBranch); + branchConfigs.splice(atIndex, 0, currentBranch); return newYmlConfig; } } @@ -158,6 +155,13 @@ async function getBranchesConfigInCurrentTrain(sg, config) { function getBranchesInCurrentTrain(branchConfig) { return branchConfig.map(b => getBranchName(b)); } +/** + * @param {Array.} branchConfig + */ +async function getCurrentBranchIndex(sg, trainCfg) { + const branches = await sg.branchLocal(); + return trainCfg.map(b => getBranchName(b)).indexOf(branches.current); +} function getKeyOfTrain(trainCgf, ymlConfig) { const { trains } = ymlConfig; @@ -334,12 +338,14 @@ async function main() { } if (program.newBranch) { - const branchOnTrain = currentBranch + const trainKey = getKeyOfTrain(trainCfg, ymlConfig); + const currentBranchIndex = await getCurrentBranchIndex(sg, trainCfg); + const newBranchIndex = currentBranchIndex + 1; await checkoutNewBranch(sg, program.newBranch); - const newYmlConfig = await addCurrentBranchToYmlConfig(sg, branchOnTrain, trainCfg, ymlConfig); + const newYmlConfig = await addCurrentBranchToYmlConfig(sg, newBranchIndex, trainKey, ymlConfig); if (newYmlConfig) { await saveConfig(sg, newYmlConfig); - console.log(`${program.newBranch} added to the train after ${branchOnTrain}`) + console.log(`${program.newBranch} added to the train after ${currentBranch}`) } return; } From 2447873aab478d230795f3ed4c72a61d3c8f9bc7 Mon Sep 17 00:00:00 2001 From: Davey Tran Date: Fri, 23 Oct 2020 00:18:07 +1100 Subject: [PATCH 10/10] fixed description --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 48fb917..39cb27b 100755 --- a/index.js +++ b/index.js @@ -218,7 +218,7 @@ async function main() { .option('--push-merged', 'Push all branches (inclusing those that have already been merged into master)') .option('--remote ', 'Set remote to push to. Defaults to "origin"') .option('-c, --create-prs', 'Create GitHub PRs from your train branches') - .option('-n, --new-branch ', 'Create a new branch in current train'); + .option('-n, --new-branch ', 'Create a new branch to the train and place it after the current branch'); program.on('--help', () => { console.log(''); @@ -328,14 +328,6 @@ async function main() { } pushBranches(sg, branchesToPush, program.force, program.remote); } - - // If we're creating PRs, don't combine branches (that might change branch HEADs and consequently - // the PR titles and descriptions). Just push and create the PRs. - if (program.createPrs) { - await findAndPushBranches(); - await ensurePrsExist(sg, sortedTrainBranches, combinedTrainBranch, program.remote); - return; - } if (program.newBranch) { const trainKey = getKeyOfTrain(trainCfg, ymlConfig); @@ -350,6 +342,14 @@ async function main() { return; } + // If we're creating PRs, don't combine branches (that might change branch HEADs and consequently + // the PR titles and descriptions). Just push and create the PRs. + if (program.createPrs) { + await findAndPushBranches(); + await ensurePrsExist(sg, sortedTrainBranches, combinedTrainBranch, program.remote); + return; + } + for (let i = 0; i < sortedTrainBranches.length - 1; ++i) { const b1 = sortedTrainBranches[i]; const b2 = sortedTrainBranches[i + 1];