From 64fe9fc7fa4e073d6bb97cdee0d2914abb943391 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 21 Mar 2018 17:38:54 +0100 Subject: git-branch-list(drop): Add support for multiple branch arguments Allow users to drop multiple branches in a single command. Instead of: $ git branch-list 1 $ git branch-list 3 $ git branch-list 4 you can now do: $ git branch-list 1 3 4 This makes it easier to drop a bunch of branches that you no longer need saved. drop_branch(): * No longer uses `branch_or_current_branch()`. Kept that function around because I didn't feel like touching the `save_branch()` code, and extracted the Git command to get the current branch to a new `current_branch()` function. * Used one loop to get branch names from IDs if necessary and feed these into a Bash array. A second loop just after deletes all branches in the branch array using the `sed` method from before. Didn't feel like changing the `sed` command so did it this way. That said, we're probably going to have to come back to it sooner or later because it's not going to be able to handle branch names containing slashes. main `case`: Change `drop_branch` call to pass it all command line arguments, except the 0th argument, which is the "drop" command (this is why we `shift` before calling the function). 101-drop-accepts-multiple-branch-arguments.t: Update test expectation which had an extra newline at the start of the string which isn't there in the real `git branch-list` output. --- git-branch-list | 43 +++++++++++++++++++------- t/101-drop-accepts-multiple-branch-arguments.t | 3 +- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/git-branch-list b/git-branch-list index c37ef74..4d75092 100755 --- a/git-branch-list +++ b/git-branch-list @@ -53,20 +53,34 @@ function save_branch () { } function drop_branch () { - local branch=$(branch_or_current_branch "$1") - local input="$branch" + local branches="$@" + local branch_names=() - # Assume `$branch` is an ID if it's not a branch name - if ! is_a_branch "$branch"; then - branch=$(branch_by_id "$branch") + # If no branch arguments were given, default to the current branch. + if [ $# -eq 0 ]; then + branches="$(current_branch)" fi - if [ -z "$branch" ]; then - echo "branch-list: Unrecognised branch '$input'" 1>&2 - exit 1 - fi + for branch in $branches; do + local input="$branch" + + # Assume `$branch` is an ID if it's not a branch name + if ! is_a_branch "$branch"; then + branch=$(branch_by_id "$branch") + fi + + if [ -z "$branch" ]; then + echo "branch-list: Unrecognised branch '$input'" 1>&2 + exit 1 + fi + + branch_names+=("$branch") + done + + for branch in "${branch_names[@]}"; do + sed -i '.bak' "/$branch/d" "$DATABASE" + done - sed -i '.bak' "/$branch/d" "$DATABASE" rm "${DATABASE}.bak" } @@ -94,12 +108,16 @@ function branch_or_current_branch () { local branch="$1" if [ "$branch" = '' ]; then - git rev-parse --abbrev-ref HEAD + current_branch else echo "$branch" fi } +function current_branch () { + git rev-parse --abbrev-ref HEAD +} + function is_a_branch () { local branch="$1" @@ -137,7 +155,8 @@ case "$command" in save_branch "$2" ;; drop) - drop_branch "$2" + shift + drop_branch "$@" ;; clear) clear_branches diff --git a/t/101-drop-accepts-multiple-branch-arguments.t b/t/101-drop-accepts-multiple-branch-arguments.t index b7834df..16767bf 100644 --- a/t/101-drop-accepts-multiple-branch-arguments.t +++ b/t/101-drop-accepts-multiple-branch-arguments.t @@ -30,8 +30,7 @@ system("$BIN drop 1 3"); ok !$?; my $branch_list = qx($BIN); -is $branch_list, ' - 1 second +is $branch_list, ' 1 second '; -- cgit v1.2.3