Skip to content

Commit

Permalink
feat: allow deleting branches with 'd' (with a confirmation prompt)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulk94 committed Jul 26, 2023
1 parent eb298e5 commit 1ca7cd4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ See a git log for the highlighted ref by pressing <kbd>SPACE</kbd>
| <kbd>enter</kbd> | Select highlighted item |
| <kbd>y</kbd> | Copy highlighted item |
| <kbd>space</kbd> | Git log |
| <kbd>d</kbd> | Prompt to delete highlighted item |
| <kbd>&</kbd> | Filter lines - enter blank search to show all lines |
| <kbd>/</kbd> | Search Lines |
| <kbd>n</kbd> | Jump to next search result |
Expand Down
34 changes: 34 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as config from "./utils/config.js";
import {
closeGitResponse,
doCheckoutBranch,
forceDeleteBranch,
doFetchBranches,
getRefData,
} from "./utils/git.js";
Expand Down Expand Up @@ -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();

/**
Expand Down
12 changes: 12 additions & 0 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ export const doFetchBranches = () => {
return execGit(args);
};

/**
* Force delete the given branch
*
* @param selectedBranch
* @returns {Promise<string>}
*/
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.
Expand Down
1 change: 1 addition & 0 deletions src/utils/helpText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const helpText = (): Array<Array<string>> => {
[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"],
Expand Down

0 comments on commit 1ca7cd4

Please sign in to comment.