diff --git a/src/dcc-cli.ts b/src/dcc-cli.ts index 8cde2c2..b3ef89b 100644 --- a/src/dcc-cli.ts +++ b/src/dcc-cli.ts @@ -208,7 +208,9 @@ async function push(args: { title?: string; submit?: boolean }) { if (currentPr) { await githubOps.updatePrTitle(currentPr.number, args.title) } else { - await githubOps.createPr(args.title) + const prNumber = await githubOps.createPr(args.title) + await gitOps.renameBranch(String(prNumber)) + print(`Local branch renamed to ${prNumber}`) } if (!args.submit) { diff --git a/src/dcc-config.ts b/src/dcc-config.ts index 774a555..2fd404c 100644 --- a/src/dcc-config.ts +++ b/src/dcc-config.ts @@ -9,4 +9,5 @@ export const DccConfig = z.object({ openOn: z.enum(['github', 'graphite']).optional(), }) -export type DccConfig = z.infer // eslint-disable-line no-redeclare +// eslint-disable-next-line no-redeclare +export type DccConfig = z.infer diff --git a/src/git-ops.ts b/src/git-ops.ts index 6ac5578..3ea3cc7 100644 --- a/src/git-ops.ts +++ b/src/git-ops.ts @@ -85,6 +85,16 @@ export class GitOps { return bs.branches[bs.current] } + async getRemoteBranchName(): Promise { + try { + const out = await this.git.raw(['rev-parse', '--abbrev-ref', '@{upstream}']) + // origin/my-branch -> my-branch + return out.trim().replace(/^[^/]+\//, '') + } catch { + return undefined + } + } + async noUncommittedChanges(): Promise { const d = await this.git.diffSummary() if (d.files.length) { @@ -219,6 +229,14 @@ export class GitOps { await this.git.raw(['commit', '-m', message]) } + async renameBranch(newName: string): Promise { + const remoteBranch = await this.getRemoteBranchName() + await this.git.branch(['-m', newName]) + if (remoteBranch) { + await this.git.branch(['--set-upstream-to', `origin/${remoteBranch}`]) + } + } + async deleteBranch(branchName: string): Promise { await this.git.branch(['-D', branchName]) } diff --git a/src/github-ops.ts b/src/github-ops.ts index 06ff3bf..9df74da 100644 --- a/src/github-ops.ts +++ b/src/github-ops.ts @@ -63,7 +63,7 @@ export class GithubOps { const b = await this.gitOps.getRepo() await this.kit.pulls.update({ owner: b.owner, repo: b.name, pull_number: prNumber, title: newTitle }) } - async createPr(title: string): Promise { + async createPr(title: string): Promise { const b = await this.gitOps.getBranch() const r = await this.gitOps.getRepo() @@ -104,5 +104,7 @@ export class GithubOps { issue_number: issueNumber, labels: this.prLabels, }) + + return issueNumber } } diff --git a/src/gql.ts b/src/gql.ts index a0906fb..0dd3110 100644 --- a/src/gql.ts +++ b/src/gql.ts @@ -46,6 +46,10 @@ export class GraphqlOps { } async getCurrentPr(): Promise { + const remoteBranch = await this.gitOps.getRemoteBranchName() + if (remoteBranch) { + return this.getPrOfBranch(remoteBranch) + } const b = await this.gitOps.getBranch() return this.getPrOfBranch(b.name) }