diff options
author | Teddy Wing | 2020-03-13 00:33:27 +0100 |
---|---|---|
committer | Teddy Wing | 2020-03-13 00:33:27 +0100 |
commit | 8061c43bb08b253f81283bce1bd83d2be6a77263 (patch) | |
tree | 23042b1f6a72b9b685637e311a96f946045fe44e | |
parent | 73b82527df51dc093455ae4149799c278c1dcffc (diff) | |
download | git-branch-list-8061c43bb08b253f81283bce1bd83d2be6a77263.tar.bz2 |
git-branch-list: Allow commands to work in Git worktrees
Previously, `git-branch-list` didn't work inside worktree directories.
It expected the current directory to be a descendant of the main Git
repository directory, which isn't the case for worktrees.
Given a Git repository `test-repo`:
$ git rev-parse --show-toplevel
/tmp/test-repo
$ git rev-parse --git-common-dir
.git
$ mkdir subdir && cd subdir
$ git rev-parse --git-common-dir
../.git
$ cd -
$ git worktree add ../test-repo-worktree
$ cd ../test-repo-worktree
$ git rev-parse --show-toplevel
/tmp/test-repo-worktree
$ git rev-parse --git-common-dir
/tmp/test-repo/.git
The `git rev-parse --show-toplevel` command outputs the top-level path
of the worktree, not the main repository directory. Using `git rev-parse
--git-common-dir`, we can get the correct path whether we're in a
subdirectory or a worktree.
-rwxr-xr-x | git-branch-list | 4 | ||||
-rw-r--r-- | t/108-works-with-git-worktrees.t | 36 |
2 files changed, 38 insertions, 2 deletions
diff --git a/git-branch-list b/git-branch-list index b70b106..95946f3 100755 --- a/git-branch-list +++ b/git-branch-list @@ -18,8 +18,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. -GIT_ROOT=$(git rev-parse --show-toplevel) -DATABASE="${GIT_ROOT}/.git/info/git-branch-list" +GIT_ROOT="$(git rev-parse --git-common-dir)" +DATABASE="${GIT_ROOT}/info/git-branch-list" VERSION=0.1.3 function initialise_database () { diff --git a/t/108-works-with-git-worktrees.t b/t/108-works-with-git-worktrees.t new file mode 100644 index 0000000..b2901c0 --- /dev/null +++ b/t/108-works-with-git-worktrees.t @@ -0,0 +1,36 @@ +#!/usr/bin/env perl -w + +use strict; + +use Test::More; +use File::Path qw(remove_tree); + +use Bin qw($BIN); + +chdir 't-git-repo' or die $!; + +system('git branch a-branch'); +ok !$?; + +system("$BIN save a-branch"); +ok !$?; + +system('git worktree add ../t-git-repo-worktree'); +ok !$?; + +chdir '../t-git-repo-worktree' or die $!; + +my $branch_list = qx($BIN); +is $branch_list, ' 1 a-branch +'; + + +# Teardown +system("$BIN clear"); +chdir '../t-git-repo' or die $!; +remove_tree('../t-git-repo-worktree') or die $!; +system('git checkout master'); +system('git branch -d a-branch'); + + +done_testing; |