From 8061c43bb08b253f81283bce1bd83d2be6a77263 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 13 Mar 2020 00:33:27 +0100 Subject: 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. --- git-branch-list | 4 ++-- t/108-works-with-git-worktrees.t | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 t/108-works-with-git-worktrees.t 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 . -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; -- cgit v1.2.3 From 19729fb26102b1de8e5c5b7aa18ea002e5fecdbb Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 13 Mar 2020 01:19:10 +0100 Subject: Increase version v0.1.3 -> v0.1.4 --- CHANGELOG | 5 +++++ git-branch-list | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 000c460..3c62714 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,11 @@ CHANGELOG ========= +v0.1.4 (2020-03-13): + Fixes: + + * The database file can now be correctly located when in a worktree. + v0.1.3 (2019-12-09): Fixes: diff --git a/git-branch-list b/git-branch-list index 95946f3..8bc3462 100755 --- a/git-branch-list +++ b/git-branch-list @@ -20,7 +20,7 @@ GIT_ROOT="$(git rev-parse --git-common-dir)" DATABASE="${GIT_ROOT}/info/git-branch-list" -VERSION=0.1.3 +VERSION=0.1.4 function initialise_database () { if [ -d .git ]; then -- cgit v1.2.3 From 39ab4b571f331d42a8a3485e811e17dfaf72a930 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 13 Mar 2020 01:23:19 +0100 Subject: git-branch-list: Update copyright year --- README.md | 2 +- git-branch-list | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a472433..81bb231 100644 --- a/README.md +++ b/README.md @@ -48,5 +48,5 @@ On other platforms, grab the code and put the `git-branch-list` script in your ## License -Copyright © 2018–2019 Teddy Wing. Licensed under the GNU GPLv3+ (see the +Copyright © 2018–2020 Teddy Wing. Licensed under the GNU GPLv3+ (see the included COPYING file). diff --git a/git-branch-list b/git-branch-list index 8bc3462..24eb5b7 100755 --- a/git-branch-list +++ b/git-branch-list @@ -3,7 +3,7 @@ # git-branch-list # Maintains a list of Git branches that can be checked out quickly. -# Copyright (c) 2018–2019 Teddy Wing +# Copyright (c) 2018–2020 Teddy Wing # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -- cgit v1.2.3