From f0993eb7d45a414030b15091fe47a08110f71e7f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 9 Dec 2019 21:24:03 +0100 Subject: git-branch-list: Fix checkout and drop commands for >10 saved branches Previously there was a bug when you had 11 or more saved branches. Saved branches 1 and 11 would both get matched by the regex in `branch_by_id`, causing the two branches to be concatenated. This resulted in a checkout error, preventing checkout. When dropping a branch, both in the pair would be dropped, even if only branch #1 was specified. Surround the branch IDs with whitespace to prevent incorrect matches. --- git-branch-list | 4 +- ...ckout-works-with-more-than-ten-saved-branches.t | 100 +++++++++++++++++++++ ...-drop-works-with-more-than-ten-saved-branches.t | 100 +++++++++++++++++++++ 3 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 t/106-checkout-works-with-more-than-ten-saved-branches.t create mode 100644 t/107-drop-works-with-more-than-ten-saved-branches.t diff --git a/git-branch-list b/git-branch-list index 7cd1295..fca335e 100755 --- a/git-branch-list +++ b/git-branch-list @@ -130,8 +130,8 @@ function branch_by_id () { local id="$1" local branch=$(list_branches | - tr -d ' ' | - fgrep "$id " | + tr -s ' ' | + fgrep " $id " | cut -d ' ' -f 2) echo "$branch" diff --git a/t/106-checkout-works-with-more-than-ten-saved-branches.t b/t/106-checkout-works-with-more-than-ten-saved-branches.t new file mode 100644 index 0000000..b51c699 --- /dev/null +++ b/t/106-checkout-works-with-more-than-ten-saved-branches.t @@ -0,0 +1,100 @@ +#!/usr/bin/env perl -w + +use strict; + +use Test::More; + +use Bin qw($BIN); + +chdir 't-git-repo' or die $!; + +system('git branch one'); +ok !$?; + +system("$BIN save one"); +ok !$?; + +system('git branch two'); +ok !$?; + +system("$BIN save two"); +ok !$?; + +system('git branch three'); +ok !$?; + +system("$BIN save three"); +ok !$?; + +system('git branch four'); +ok !$?; + +system("$BIN save four"); +ok !$?; + +system('git branch five'); +ok !$?; + +system("$BIN save five"); +ok !$?; + +system('git branch six'); +ok !$?; + +system("$BIN save six"); +ok !$?; + +system('git branch seven'); +ok !$?; + +system("$BIN save seven"); +ok !$?; + +system('git branch eight'); +ok !$?; + +system("$BIN save eight"); +ok !$?; + +system('git branch nine'); +ok !$?; + +system("$BIN save nine"); +ok !$?; + +system('git branch ten'); +ok !$?; + +system("$BIN save ten"); +ok !$?; + +system('git branch eleven'); +ok !$?; + +system("$BIN save eleven"); +ok !$?; + + +system("$BIN 1"); +ok !$?; + +my $current_branch = qx(git rev-parse --abbrev-ref HEAD); +is $current_branch, 'eleven +'; + + +system("$BIN 11"); +ok !$?; + +$current_branch = qx(git rev-parse --abbrev-ref HEAD); +is $current_branch, 'one +'; + + +# Teardown +system('git checkout master'); +system("$BIN clear"); +system('git branch -d one two three four five six seven eight nine ten eleven'); + + +done_testing; diff --git a/t/107-drop-works-with-more-than-ten-saved-branches.t b/t/107-drop-works-with-more-than-ten-saved-branches.t new file mode 100644 index 0000000..374f49b --- /dev/null +++ b/t/107-drop-works-with-more-than-ten-saved-branches.t @@ -0,0 +1,100 @@ +#!/usr/bin/env perl -w + +use strict; + +use Test::More; + +use Bin qw($BIN); + +chdir 't-git-repo' or die $!; + +system('git branch one'); +ok !$?; + +system("$BIN save one"); +ok !$?; + +system('git branch two'); +ok !$?; + +system("$BIN save two"); +ok !$?; + +system('git branch three'); +ok !$?; + +system("$BIN save three"); +ok !$?; + +system('git branch four'); +ok !$?; + +system("$BIN save four"); +ok !$?; + +system('git branch five'); +ok !$?; + +system("$BIN save five"); +ok !$?; + +system('git branch six'); +ok !$?; + +system("$BIN save six"); +ok !$?; + +system('git branch seven'); +ok !$?; + +system("$BIN save seven"); +ok !$?; + +system('git branch eight'); +ok !$?; + +system("$BIN save eight"); +ok !$?; + +system('git branch nine'); +ok !$?; + +system("$BIN save nine"); +ok !$?; + +system('git branch ten'); +ok !$?; + +system("$BIN save ten"); +ok !$?; + +system('git branch eleven'); +ok !$?; + +system("$BIN save eleven"); +ok !$?; + + +system("$BIN drop 1"); +ok !$?; + +my $branch_list = qx($BIN); +is $branch_list, ' 1 ten + 2 nine + 3 eight + 4 seven + 5 six + 6 five + 7 four + 8 three + 9 two + 10 one +'; + + +# Teardown +system("$BIN clear"); +system('git branch -d one two three four five six seven eight nine ten eleven'); + + +done_testing; -- cgit v1.2.3