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
36 changes: 36 additions & 0 deletions src/dcc-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,36 @@ async function openPr() {
await open(pr.openUrl)
}

async function close() {
await gitOps.notOnMainBranch()
await gitOps.noUncommittedChanges()

const pr = await graphqlOps.getCurrentPr()
if (pr) {
print(`Cannot close: there is an open PR (#${pr.number}: ${pr.title})`)
return
}

const mainBranch = await gitOps.mainBranch()
const baselineCommit = await gitOps.findBaselineCommit(`origin/${mainBranch}`)
const changedFiles = await gitOps.getChangedFiles(baselineCommit)
if (changedFiles.length > 0) {
print(`Cannot close: there are ${changedFiles.length} changed file(s)`)
return
}

const branch = await gitOps.getBranch()
const branchName = branch.name

await gitOps.checkout(mainBranch)
await gitOps.deleteBranch(branchName)
print(`Deleted branch ${branchName}`)

await gitOps.fetch('origin', mainBranch)
await gitOps.merge('origin', mainBranch)
print(`Synced ${mainBranch}`)
}

async function checkoutPr(a: { prNumber: number }) {
await gitOps.noUncommittedChanges()

Expand Down Expand Up @@ -413,6 +443,12 @@ yargs(hideBin(process.argv))
launch((a: { files: string[] }) => restore(a)),
)
.command(['open', 'o'], 'Open the current PR files page in your browser', a => a, launch(openPr))
.command(
['close', 'cl'],
'Delete the current branch and switch to main (only if no open PR and no diff)',
a => a,
launch(close),
)
.command(
['checkout <pr-number>', 'co <pr-number>'],
'Checkout the branch of a PR by its number',
Expand Down
4 changes: 4 additions & 0 deletions src/git-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,8 @@ export class GitOps {
async commitAll(message: string): Promise<void> {
await this.git.raw(['commit', '-m', message])
}

async deleteBranch(branchName: string): Promise<void> {
await this.git.branch(['-D', branchName])
}
}