Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/dcc-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,29 @@ async function catchUp(mode: 'SILENT' | 'CHATTY') {
return mainBranch
}

async function createNew(a: { branch: string }) {
function generateBranchName() {
const now = new Date()
const hh = String(now.getHours()).padStart(2, '0')
const mm = String(now.getMinutes()).padStart(2, '0')
const ddd = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'][now.getDay()]
const yyyy = now.getFullYear()
const mo = String(now.getMonth() + 1).padStart(2, '0')
const dd = String(now.getDate()).padStart(2, '0')
return `${hh}${mm}-${ddd}-${yyyy}-${mo}-${dd}`
}

async function createNew(a: { branch?: string }) {
await gitOps.noUncommittedChanges()
const branch = a.branch ?? generateBranchName()
const [currBranch, mainBranch] = await Promise.all([gitOps.getBranch(), gitOps.mainBranch()])
const newBranchName = currBranch.name === mainBranch ? a.branch : `${currBranch.name}.${a.branch}`
const prefix = currBranch.name === mainBranch ? '' : `${currBranch.name}.`
let newBranchName = `${prefix}${branch}`
if (await gitOps.branchExists(newBranchName)) {
const suffix = Math.random()
.toString(36)
.slice(2, 6)
newBranchName = `${prefix}${branch}-${suffix}`
}
await gitOps.createBranch(newBranchName, 'HEAD')
}

Expand Down Expand Up @@ -422,13 +441,12 @@ yargs(hideBin(process.argv))
launch(() => diff({ tool: true })),
)
.command(
['new <branch>', 'n <branch>'],
['new [branch]', 'n [branch]'],
'Create a new branch from current HEAD',
yargs =>
yargs.positional('branch', {
type: 'string',
describe: 'The name of the new branch',
demandOption: true,
describe: 'The name of the new branch (default: hhmm-ddd-yyyy-mm-dd)',
}),
launch(createNew),
)
Expand Down
5 changes: 5 additions & 0 deletions src/git-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class GitOps {
return bs.branches[bs.current]
}

async branchExists(name: string): Promise<boolean> {
const bs = await this.git.branch([])
return name in bs.branches
}

async getRemoteBranchName(): Promise<string | undefined> {
try {
const out = await this.git.raw(['rev-parse', '--abbrev-ref', '@{upstream}'])
Expand Down