diff --git a/README.md b/README.md index e645f88..81b2a81 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ See a git log for the highlighted ref by pressing SPACE | enter | Select highlighted item | | y | Copy highlighted item | | space | Git log | +| d | Prompt to delete highlighted item | | & | Filter lines - enter blank search to show all lines | | / | Search Lines | | n | Jump to next search result | diff --git a/src/app.ts b/src/app.ts index 0bc8b25..9d33d24 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,6 +8,7 @@ import * as config from "./utils/config.js"; import { closeGitResponse, doCheckoutBranch, + forceDeleteBranch, doFetchBranches, getRefData, } from "./utils/git.js"; @@ -303,6 +304,39 @@ export const start = async (args: string[]) => { } }); + branchTable.key("d", function () { + const selection = parseSelection( + branchTable.items[branchTable.selected].content, + ); + const branchName = selection[2]; + + if (state.currentRemoteIndex !== 0) { + logger.error("Branch deletion is only available on heads"); + return; + } + + getPrompt( + `Are you sure you want to force delete ${branchName}? (only y is accepted)`, + async (value: string) => { + if (value === "y") { + try { + await forceDeleteBranch(branchName); + + process.stdout.write( + `Successfully deleted branch ${chalk.bold(branchName)}\n`, + ); + + process.exit(0); + } catch (error) { + logger.error(`Failed to delete branch ${branchName}`); + } + } else { + logger.log("Skipping deletion"); + } + }, + ); + }); + branchTable.focus(); /** diff --git a/src/utils/git.ts b/src/utils/git.ts index ecec5bd..ca5b33d 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -127,6 +127,18 @@ export const doFetchBranches = () => { return execGit(args); }; +/** + * Force delete the given branch + * + * @param selectedBranch + * @returns {Promise} + */ +export const forceDeleteBranch = (selectedBranch: string) => { + const args = ["branch", "-D", selectedBranch]; + + return execGit(args); +}; + /** * Format output from getBranchesFrom() and return an array of arrays containing * formatted lines for the data table. diff --git a/src/utils/helpText.ts b/src/utils/helpText.ts index 0febecb..d9585bd 100644 --- a/src/utils/helpText.ts +++ b/src/utils/helpText.ts @@ -13,6 +13,7 @@ export const helpText = (): Array> => { [chalk.bold("enter"), "Select highlighted item"], [chalk.bold("y"), "Copy highlighted item"], [chalk.bold("space"), "Git log"], + [chalk.bold("d"), "Prompt to delete highlighted item"], [chalk.bold("&"), "Filter lines - enter blank search to show all lines"], [chalk.bold("/"), "Search lines"], [chalk.bold("n"), "Jump to next search result"],