From 3f6e62684b1fb1167435c6fa4f6c6f02aaff3937 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 19 Apr 2018 23:44:13 +0200 Subject: git-branch-list(drop): Fix deletion for branches with slashes Previously, if a branch name had slashes in it, the `drop` command would fail. This is because it used `sed` with `/` separators, and the unescaped slashes in the branch name would mess up sed's pattern. Change this to use grep instead of sed so we don't have to worry about escaping and special characters in branch names. --- git-branch-list | 5 ++-- t/104-drop-works-with-branch-names-with-slashes.t | 29 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 t/104-drop-works-with-branch-names-with-slashes.t diff --git a/git-branch-list b/git-branch-list index 75167a3..cce35e3 100755 --- a/git-branch-list +++ b/git-branch-list @@ -78,10 +78,9 @@ function drop_branch () { done for branch in "${branch_names[@]}"; do - sed -i '.bak' "/^$branch$/d" "$DATABASE" + fgrep --line-regexp --invert-match "$branch" "$DATABASE" > "$DATABASE.bak" + mv "${DATABASE}.bak" "$DATABASE" done - - rm "${DATABASE}.bak" } function clear_branches () { diff --git a/t/104-drop-works-with-branch-names-with-slashes.t b/t/104-drop-works-with-branch-names-with-slashes.t new file mode 100644 index 0000000..8219e3e --- /dev/null +++ b/t/104-drop-works-with-branch-names-with-slashes.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 feature/with-slashes'); +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 feature/with-slashes'); + + +done_testing; -- cgit v1.2.3