aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-03-21 18:13:00 +0100
committerTeddy Wing2018-03-21 18:13:00 +0100
commit25991886eba08a3baa418d7b5f4e5e4eaa5df23b (patch)
tree3ebd3ecfce83ce5da79e6c442b16c0becfada4fd
parent9a4abcc9b0e1121c89ae8a12be8eeb291e7dd34e (diff)
parent64fe9fc7fa4e073d6bb97cdee0d2914abb943391 (diff)
downloadgit-branch-list-25991886eba08a3baa418d7b5f4e5e4eaa5df23b.tar.bz2
Merge branch 'drop-multiple-branches'
-rw-r--r--Makefile2
-rwxr-xr-xgit-branch-list39
-rw-r--r--t/100-clear-clears-all-branches-from-list.t4
-rw-r--r--t/101-drop-accepts-multiple-branch-arguments.t42
-rw-r--r--t/102-drop-drops-current-branch.t29
-rw-r--r--t/bin.pm13
6 files changed, 118 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 2cab678..e67850b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
.PHONY: test
test:
- prove -v
+ prove -v -I./t
diff --git a/git-branch-list b/git-branch-list
index 1468bfe..4d75092 100755
--- a/git-branch-list
+++ b/git-branch-list
@@ -53,14 +53,34 @@ function save_branch () {
}
function drop_branch () {
- local branch=$(branch_or_current_branch "$1")
+ 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
- sed -i '.bak' "/$branch/d" "$DATABASE"
+ 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
+
rm "${DATABASE}.bak"
}
@@ -88,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"
@@ -131,7 +155,8 @@ case "$command" in
save_branch "$2"
;;
drop)
- drop_branch "$2"
+ shift
+ drop_branch "$@"
;;
clear)
clear_branches
diff --git a/t/100-clear-clears-all-branches-from-list.t b/t/100-clear-clears-all-branches-from-list.t
index 952d68a..2e53350 100644
--- a/t/100-clear-clears-all-branches-from-list.t
+++ b/t/100-clear-clears-all-branches-from-list.t
@@ -4,9 +4,7 @@ use strict;
use Test::More;
-use File::Spec;
-
-my $BIN = File::Spec->rel2abs('git-branch-list');
+use Bin qw($BIN);
chdir 't-git-repo' or die $!;
diff --git a/t/101-drop-accepts-multiple-branch-arguments.t b/t/101-drop-accepts-multiple-branch-arguments.t
new file mode 100644
index 0000000..16767bf
--- /dev/null
+++ b/t/101-drop-accepts-multiple-branch-arguments.t
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl -w
+
+use strict;
+
+use Test::More;
+
+use Bin qw($BIN);
+
+chdir 't-git-repo' or die $!;
+
+system('git branch first');
+ok !$?;
+
+system("$BIN save first");
+ok !$?;
+
+system('git branch second');
+ok !$?;
+
+system("$BIN save second");
+ok !$?;
+
+system('git branch third');
+ok !$?;
+
+system("$BIN save third");
+ok !$?;
+
+system("$BIN drop 1 3");
+ok !$?;
+
+my $branch_list = qx($BIN);
+is $branch_list, ' 1 second
+';
+
+
+# Teardown
+system('git branch -d first second third');
+system("$BIN clear");
+
+
+done_testing;
diff --git a/t/102-drop-drops-current-branch.t b/t/102-drop-drops-current-branch.t
new file mode 100644
index 0000000..5534b6e
--- /dev/null
+++ b/t/102-drop-drops-current-branch.t
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl -w
+
+use strict;
+
+use Test::More;
+
+use Bin qw($BIN);
+
+chdir 't-git-repo' or die $!;
+
+system('git checkout -b first');
+ok !$?;
+
+system("$BIN save");
+ok !$?;
+
+system("$BIN drop");
+ok !$?;
+
+my $branch_list = qx($BIN);
+is $branch_list, '';
+
+
+# Teardown
+system('git checkout master');
+system('git branch -d first');
+
+
+done_testing;
diff --git a/t/bin.pm b/t/bin.pm
new file mode 100644
index 0000000..ad5b7f1
--- /dev/null
+++ b/t/bin.pm
@@ -0,0 +1,13 @@
+package Bin;
+
+use strict;
+use warnings;
+
+use Exporter qw(import);
+our @EXPORT = qw($BIN);
+
+use File::Spec;
+
+our $BIN = File::Spec->rel2abs('git-branch-list');
+
+1;