Skip to content

Commit

Permalink
Merge pull request #15 from connected-web/#14.use-dots-instead-of-sla…
Browse files Browse the repository at this point in the history
…shes-as-separator-in-branch-paths

#14 Use dots instead of slashes as separator in branch paths
  • Loading branch information
johnbeech committed Jul 27, 2021
2 parents 8e00b91 + 36127e3 commit 9da52ee
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/steps/createBranch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,47 @@ const exec = require('../util/asyncExec')
const createBranchNameSlug = require('../util/createBranchNameSlug')
const report = (...messages) => console.log('[PR Now] [Create Branch]', ...messages)

const LEGACY_PATH_SEPARATOR = '/'
const BRANCH_PATH_SEPARATOR = '.'

async function createBranch (workingKnowledge) {
const { ticket, ticketTitle, ticketUrl, cwd } = workingKnowledge
// - Create a branch equivalent to the ticket name

const currentBranchName = (await exec('git branch', { cwd })).stdout.split('\n').filter(n => /\* /.test(n))[0].substr(2)
report('Current Branch Name', currentBranchName)

let branchName = ticketTitle ? `${ticket}/${createBranchNameSlug(ticketTitle)}` : ticket
const titleSlug = createBranchNameSlug(ticketTitle)
const legacyBranchName = [ticket, titleSlug].join(LEGACY_PATH_SEPARATOR)
const extendedBranchName = [ticket, titleSlug].join(BRANCH_PATH_SEPARATOR)
const branchName = ticketTitle ? extendedBranchName : ticket

const checkForExactMatch = branchName
const checkForLegacyTicket = legacyBranchName
const checkForExtendedTicket = extendedBranchName

const outcomes = {}
outcomes[checkForExactMatch] = alreadyOnBranch
outcomes[checkForLegacyTicket] = switchToNewBranchFormat
outcomes[checkForExtendedTicket] = reusingExistingBranch
outcomes.default = checkoutNewBranch

const action = outcomes[currentBranchName] || outcomes.default
await action({ branchName, report })

async function alreadyOnBranch ({ branchName, report }) {
report(`Already on branch ${branchName}`)
}

async function switchToNewBranchFormat ({ branchName, report }) {
report('Switching to new branch format:', branchName)
}

if (currentBranchName === branchName) {
report(`Already on branch ${currentBranchName}`)
} else if (branchName.split('/')[0] === currentBranchName.split('/')[0]) {
branchName = currentBranchName
async function reusingExistingBranch ({ branchName, report }) {
report('Reusing existing branch:', branchName)
} else {
}

async function checkoutNewBranch ({ branchName, report }) {
const { stdout, stderr } = await exec(`git checkout -b "${branchName}"`, { cwd })
report('git checkout:', stdout, stderr)
}
Expand Down

0 comments on commit 9da52ee

Please sign in to comment.