-
Notifications
You must be signed in to change notification settings - Fork 19
Create Branch and add to current train in XML #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6b79447
f6e0acd
99dd255
0da93a7
7d218b1
c52baf4
81a3aae
94a66d3
72556c5
2447873
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,30 @@ async function pushBranches(sg, branches, forcePush, remote = DEFAULT_REMOTE) { | |
| console.log('All changes pushed ' + emoji.get('white_check_mark')); | ||
| } | ||
|
|
||
| async function checkoutNewBranch(sg, 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, atIndex, trainKey, ymlConfig) { | ||
| const branches = await sg.branchLocal(); | ||
| const currentBranch = branches.current; | ||
| 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]; | ||
| branchConfigs.splice(atIndex, 0, currentBranch); | ||
| return newYmlConfig; | ||
| } | ||
| } | ||
|
|
||
| async function getUnmergedBranches(sg, branches) { | ||
| const mergedBranchesOutput = await sg.raw(['branch', '--merged', 'master']); | ||
| const mergedBranches = mergedBranchesOutput | ||
|
|
@@ -91,6 +115,15 @@ async function loadConfig(sg) { | |
| return yaml.safeLoad(fs.readFileSync(path, 'utf8')); | ||
| } | ||
|
|
||
| /** | ||
| * @param {simpleGit.SimpleGit} sg | ||
| * @param {trains: Array.<TrainCfg>} sg | ||
| */ | ||
| async function saveConfig(sg, ymlConfig) { | ||
| const path = await getConfigPath(sg); | ||
| fs.writeFileSync(path, yaml.safeDump(ymlConfig)) | ||
| } | ||
|
|
||
| /** | ||
| * @param {BranchCfg} branchCfg | ||
| */ | ||
|
|
@@ -122,7 +155,20 @@ async function getBranchesConfigInCurrentTrain(sg, config) { | |
| function getBranchesInCurrentTrain(branchConfig) { | ||
| return branchConfig.map(b => getBranchName(b)); | ||
| } | ||
| /** | ||
| * @param {Array.<BranchCfg>} 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; | ||
| return Object.keys(trains).find(trainKey => { | ||
| return trains[trainKey] === trainCgf; | ||
| }); | ||
| } | ||
| /** | ||
| * @param {Array.<BranchCfg>} branchConfig | ||
| */ | ||
|
|
@@ -171,7 +217,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 <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 <branch>', 'Create a new branch to the train and place it after the current branch'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this rather be
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wait, I misunderstood. So this actually does create a new branch and add it to the train, gotcha. |
||
|
|
||
| program.on('--help', () => { | ||
| console.log(''); | ||
|
|
@@ -281,6 +328,19 @@ async function main() { | |
| } | ||
| pushBranches(sg, branchesToPush, program.force, program.remote); | ||
| } | ||
|
|
||
| if (program.newBranch) { | ||
| 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, newBranchIndex, trainKey, ymlConfig); | ||
| if (newYmlConfig) { | ||
| await saveConfig(sg, newYmlConfig); | ||
| console.log(`${program.newBranch} added to the train after ${currentBranch}`) | ||
| } | ||
| 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gives me
fatal: A branch named 'new-branch-name' already exists.Not sure how to avoid it.