Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow deleting branches with 'd' (with a confirmation prompt) #541

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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;
}
Comment on lines +313 to +316
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah there's a bug here as heads might not be index 0 always. So IF we decide to go ahead with this approach, I'll fix this up


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