From 7bb9292f60c8889b2a3529748679025d73e9d645 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 19 Apr 2018 23:08:09 +0200 Subject: git-branch-list(drop): Support multiple branches with similar names Previously if you had multiple branches with similar names, they could all be deleted when deleting a single one. For example: $ git branch-list 1 a-branch-with-suffix 2 a-branch $ git branch-list drop 2 $ git branch-list $ Change the regex used to match branches to drop to match the whole line, so we don't accidentally delete a branch when it has a positive partial match. --- git-branch-list | 2 +- ...es-not-drop-other-branches-with-similar-names.t | 37 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 t/103-drop-does-not-drop-other-branches-with-similar-names.t diff --git a/git-branch-list b/git-branch-list index 0c6a74a..cde44f2 100755 --- a/git-branch-list +++ b/git-branch-list @@ -78,7 +78,7 @@ function drop_branch () { done for branch in "${branch_names[@]}"; do - sed -i '.bak' "/$branch/d" "$DATABASE" + sed -i '.bak' "/^$branch$/d" "$DATABASE" done rm "${DATABASE}.bak" diff --git a/t/103-drop-does-not-drop-other-branches-with-similar-names.t b/t/103-drop-does-not-drop-other-branches-with-similar-names.t new file mode 100644 index 0000000..05360cb --- /dev/null +++ b/t/103-drop-does-not-drop-other-branches-with-similar-names.t @@ -0,0 +1,37 @@ +#!/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-similar-name'); +ok !$?; + +system("$BIN save"); +ok !$?; + +system('git checkout -b first'); +ok !$?; + +system("$BIN save"); +ok !$?; + +system("$BIN drop"); +ok !$?; + +my $branch_list = qx($BIN); +is $branch_list, ' 1 first-similar-name +'; + + +# Teardown +system('git checkout master'); +system('git branch -d first first-similar-name'); +system("$BIN clear"); + + +done_testing; -- cgit v1.2.3 From 92b7edb0d84ac0109f28da57fccf4a9d87896b1e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 19 Apr 2018 23:17:22 +0200 Subject: git-branch-list(save): Allow saving branches with similar names It wasn't possible to save branches with similar names before: $ git branch-list save a-branch-with-suffix $ git branch-list save a-branch #=> Errors $ git branch-list 1 a-branch-with-suffix This is because I wasn't matching against the whole line, so partial matches would set off the condition and exit. Use grep's `--line-regexp` flag to match against whole lines. Thanks to John Kugelman for the flag: https://stackoverflow.com/questions/4709912/how-to-make-grep-only-match-if-the-entire-line-matches/4709925#4709925 --- git-branch-list | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-branch-list b/git-branch-list index cde44f2..75167a3 100755 --- a/git-branch-list +++ b/git-branch-list @@ -44,7 +44,7 @@ __EOF__ function save_branch () { local branch=$(branch_or_current_branch "$1") - if fgrep "$branch" "$DATABASE" > /dev/null; then + if fgrep --line-regexp "$branch" "$DATABASE" > /dev/null; then return 1 fi -- cgit v1.2.3