From c67a978a0c2b5165b361224a2d9d8bba5b0473bd Mon Sep 17 00:00:00 2001 From: Itay Maman <94941+imaman@users.noreply.github.com> Date: Sun, 29 Mar 2026 13:03:47 +0300 Subject: [PATCH] defaultname --- src/dcc-cli.ts | 28 +++++++++++++++++++++++----- src/git-ops.ts | 5 +++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/dcc-cli.ts b/src/dcc-cli.ts index b3ef89b..60011e4 100644 --- a/src/dcc-cli.ts +++ b/src/dcc-cli.ts @@ -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') } @@ -422,13 +441,12 @@ yargs(hideBin(process.argv)) launch(() => diff({ tool: true })), ) .command( - ['new ', 'n '], + ['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), ) diff --git a/src/git-ops.ts b/src/git-ops.ts index 3ea3cc7..0a2fcf9 100644 --- a/src/git-ops.ts +++ b/src/git-ops.ts @@ -85,6 +85,11 @@ export class GitOps { return bs.branches[bs.current] } + async branchExists(name: string): Promise { + const bs = await this.git.branch([]) + return name in bs.branches + } + async getRemoteBranchName(): Promise { try { const out = await this.git.raw(['rev-parse', '--abbrev-ref', '@{upstream}'])