From 975b9d397076e26ba205a71cac4a5db190d873f9 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 18 Mar 2018 18:25:38 +0100 Subject: t/: Move `$BIN` variable to module In order to be able to easily reuse the `$BIN` variable we created in the test for the `clear` sub-command, move it to a Perl module that can be included in other tests. Add the `t/` directory to the include path when running `prove` to ensure that `bin.pm` can be found and included. Thanks to these resources for explaining Perl modules: https://perlmaven.com/how-to-create-a-perl-module-for-code-reuse https://stackoverflow.com/questions/23899121/perl-declare-and-export-variables-from-a-module/23900384#23900384 https://stackoverflow.com/questions/17931981/what-is-isa-in-perl/17932340#17932340 --- Makefile | 2 +- t/100-clear-clears-all-branches-from-list.t | 4 +--- t/bin.pm | 13 +++++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 t/bin.pm 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/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/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; -- cgit v1.2.3 From a33c35b0561162306169fae17452a5b521ece067 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 18 Mar 2018 20:36:21 +0100 Subject: git-branch-list(drop): Show error message when branch not found Instead of displaying the underlying errors: sed: first RE may not be empty rm: .git/info/git-branch-list.bak: No such file or directory display a meaningful error message from our program to let users know that the given branch couldn't be found in the branch-list database. --- git-branch-list | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/git-branch-list b/git-branch-list index 1468bfe..c37ef74 100755 --- a/git-branch-list +++ b/git-branch-list @@ -54,12 +54,18 @@ function save_branch () { function drop_branch () { local branch=$(branch_or_current_branch "$1") + 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 + sed -i '.bak' "/$branch/d" "$DATABASE" rm "${DATABASE}.bak" } -- cgit v1.2.3 From 565c054f57248409eddc7ebe20629b85093f7c55 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 21 Mar 2018 17:17:26 +0100 Subject: Add `drop` command tests --- t/101-drop-accepts-multiple-branch-arguments.t | 43 ++++++++++++++++++++++++++ t/102-drop-drops-current-branch.t | 29 +++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 t/101-drop-accepts-multiple-branch-arguments.t create mode 100644 t/102-drop-drops-current-branch.t 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..b7834df --- /dev/null +++ b/t/101-drop-accepts-multiple-branch-arguments.t @@ -0,0 +1,43 @@ +#!/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; -- cgit v1.2.3 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