2015-10-08 20:01:05 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='test git worktree list'
|
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
test_expect_success 'setup' '
|
|
|
|
test_commit init
|
|
|
|
'
|
|
|
|
|
rev-parse: fix several options when running in a subdirectory
In addition to making git_path() aware of certain file names that need
to be handled differently e.g. when running in worktrees, the commit
557bd833bb (git_path(): be aware of file relocation in $GIT_DIR,
2014-11-30) also snuck in a new option for `git rev-parse`:
`--git-path`.
On the face of it, there is no obvious bug in that commit's diff: it
faithfully calls git_path() on the argument and prints it out, i.e. `git
rev-parse --git-path <filename>` has the same precise behavior as
calling `git_path("<filename>")` in C.
The problem lies deeper, much deeper. In hindsight (which is always
unfair), implementing the .git/ directory discovery in
`setup_git_directory()` by changing the working directory may have
allowed us to avoid passing around a struct that contains information
about the current repository, but it bought us many, many problems.
In this case, when being called in a subdirectory, `git rev-parse`
changes the working directory to the top-level directory before calling
`git_path()`. In the new working directory, the result is correct. But
in the working directory of the calling script, it is incorrect.
Example: when calling `git rev-parse --git-path HEAD` in, say, the
Documentation/ subdirectory of Git's own source code, the string
`.git/HEAD` is printed.
Side note: that bug is hidden when running in a subdirectory of a
worktree that was added by the `git worktree` command: in that case, the
(correct) absolute path of the `HEAD` file is printed.
In the interest of time, this patch does not go the "correct" route to
introduce a struct with repository information (and removing global
state in the process), instead this patch chooses to detect when the
command was called in a subdirectory and forces the result to be an
absolute path.
While at it, we are also fixing the output of --git-common-dir and
--shared-index-path.
Lastly, please note that we reuse the same strbuf for all of the
relative_path() calls; this avoids frequent allocation (and duplicated
code), and it does not risk memory leaks, for two reasons: 1) the
cmd_rev_parse() function does not return anywhere between the use of
the new strbuf instance and its final release, and 2) git-rev-parse is
one of these "one-shot" programs in Git, i.e. it exits after running
for a very short time, meaning that all allocated memory is released
with the exit() call anyway.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-02-17 19:59:06 +03:00
|
|
|
test_expect_success 'rev-parse --git-common-dir on main worktree' '
|
2016-02-12 07:31:45 +03:00
|
|
|
git rev-parse --git-common-dir >actual &&
|
|
|
|
echo .git >expected &&
|
|
|
|
test_cmp expected actual &&
|
|
|
|
mkdir sub &&
|
|
|
|
git -C sub rev-parse --git-common-dir >actual2 &&
|
2017-02-17 19:59:02 +03:00
|
|
|
echo ../.git >expected2 &&
|
2016-02-12 07:31:45 +03:00
|
|
|
test_cmp expected2 actual2
|
|
|
|
'
|
|
|
|
|
rev-parse: fix several options when running in a subdirectory
In addition to making git_path() aware of certain file names that need
to be handled differently e.g. when running in worktrees, the commit
557bd833bb (git_path(): be aware of file relocation in $GIT_DIR,
2014-11-30) also snuck in a new option for `git rev-parse`:
`--git-path`.
On the face of it, there is no obvious bug in that commit's diff: it
faithfully calls git_path() on the argument and prints it out, i.e. `git
rev-parse --git-path <filename>` has the same precise behavior as
calling `git_path("<filename>")` in C.
The problem lies deeper, much deeper. In hindsight (which is always
unfair), implementing the .git/ directory discovery in
`setup_git_directory()` by changing the working directory may have
allowed us to avoid passing around a struct that contains information
about the current repository, but it bought us many, many problems.
In this case, when being called in a subdirectory, `git rev-parse`
changes the working directory to the top-level directory before calling
`git_path()`. In the new working directory, the result is correct. But
in the working directory of the calling script, it is incorrect.
Example: when calling `git rev-parse --git-path HEAD` in, say, the
Documentation/ subdirectory of Git's own source code, the string
`.git/HEAD` is printed.
Side note: that bug is hidden when running in a subdirectory of a
worktree that was added by the `git worktree` command: in that case, the
(correct) absolute path of the `HEAD` file is printed.
In the interest of time, this patch does not go the "correct" route to
introduce a struct with repository information (and removing global
state in the process), instead this patch chooses to detect when the
command was called in a subdirectory and forces the result to be an
absolute path.
While at it, we are also fixing the output of --git-common-dir and
--shared-index-path.
Lastly, please note that we reuse the same strbuf for all of the
relative_path() calls; this avoids frequent allocation (and duplicated
code), and it does not risk memory leaks, for two reasons: 1) the
cmd_rev_parse() function does not return anywhere between the use of
the new strbuf instance and its final release, and 2) git-rev-parse is
one of these "one-shot" programs in Git, i.e. it exits after running
for a very short time, meaning that all allocated memory is released
with the exit() call anyway.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-02-17 19:59:06 +03:00
|
|
|
test_expect_success 'rev-parse --git-path objects linked worktree' '
|
2017-02-17 19:59:02 +03:00
|
|
|
echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
|
2017-02-17 19:59:02 +03:00
|
|
|
git worktree add --detach linked-tree master &&
|
|
|
|
git -C linked-tree rev-parse --git-path objects >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2015-10-08 20:01:05 +03:00
|
|
|
test_expect_success '"list" all worktrees from main' '
|
|
|
|
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf here out actual expect && git worktree prune" &&
|
2015-10-08 20:01:05 +03:00
|
|
|
git worktree add --detach here master &&
|
|
|
|
echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git worktree list >out &&
|
|
|
|
sed "s/ */ /g" <out >actual &&
|
2015-10-08 20:01:05 +03:00
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"list" all worktrees from linked' '
|
|
|
|
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf here out actual expect && git worktree prune" &&
|
2015-10-08 20:01:05 +03:00
|
|
|
git worktree add --detach here master &&
|
|
|
|
echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git -C here worktree list >out &&
|
|
|
|
sed "s/ */ /g" <out >actual &&
|
2015-10-08 20:01:05 +03:00
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"list" all worktrees --porcelain' '
|
|
|
|
echo "worktree $(git rev-parse --show-toplevel)" >expect &&
|
|
|
|
echo "HEAD $(git rev-parse HEAD)" >>expect &&
|
|
|
|
echo "branch $(git symbolic-ref HEAD)" >>expect &&
|
|
|
|
echo >>expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf here actual expect && git worktree prune" &&
|
2015-10-08 20:01:05 +03:00
|
|
|
git worktree add --detach here master &&
|
|
|
|
echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
|
|
|
|
echo "HEAD $(git rev-parse HEAD)" >>expect &&
|
|
|
|
echo "detached" >>expect &&
|
|
|
|
echo >>expect &&
|
|
|
|
git worktree list --porcelain >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2020-11-03 14:48:07 +03:00
|
|
|
test_expect_success '"list" all worktrees with locked annotation' '
|
2020-10-11 13:11:52 +03:00
|
|
|
test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
|
|
|
|
git worktree add --detach locked master &&
|
|
|
|
git worktree add --detach unlocked master &&
|
|
|
|
git worktree lock locked &&
|
|
|
|
git worktree list >out &&
|
|
|
|
grep "/locked *[0-9a-f].* locked$" out &&
|
|
|
|
! grep "/unlocked *[0-9a-f].* locked$" out
|
|
|
|
'
|
|
|
|
|
2015-10-08 20:01:05 +03:00
|
|
|
test_expect_success 'bare repo setup' '
|
|
|
|
git init --bare bare1 &&
|
|
|
|
echo "data" >file1 &&
|
|
|
|
git add file1 &&
|
|
|
|
git commit -m"File1: add data" &&
|
|
|
|
git push bare1 master &&
|
|
|
|
git reset --hard HEAD^
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"list" all worktrees from bare main' '
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
|
2015-10-08 20:01:05 +03:00
|
|
|
git -C bare1 worktree add --detach ../there master &&
|
|
|
|
echo "$(pwd)/bare1 (bare)" >expect &&
|
|
|
|
echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git -C bare1 worktree list >out &&
|
|
|
|
sed "s/ */ /g" <out >actual &&
|
2015-10-08 20:01:05 +03:00
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"list" all worktrees --porcelain from bare main' '
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
|
2015-10-08 20:01:05 +03:00
|
|
|
git -C bare1 worktree add --detach ../there master &&
|
|
|
|
echo "worktree $(pwd)/bare1" >expect &&
|
|
|
|
echo "bare" >>expect &&
|
|
|
|
echo >>expect &&
|
|
|
|
echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
|
|
|
|
echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
|
|
|
|
echo "detached" >>expect &&
|
|
|
|
echo >>expect &&
|
|
|
|
git -C bare1 worktree list --porcelain >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"list" all worktrees from linked with a bare main' '
|
2017-04-04 00:35:57 +03:00
|
|
|
test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
|
2015-10-08 20:01:05 +03:00
|
|
|
git -C bare1 worktree add --detach ../there master &&
|
|
|
|
echo "$(pwd)/bare1 (bare)" >expect &&
|
|
|
|
echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git -C there worktree list >out &&
|
|
|
|
sed "s/ */ /g" <out >actual &&
|
2015-10-08 20:01:05 +03:00
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'bare repo cleanup' '
|
|
|
|
rm -rf bare1
|
|
|
|
'
|
|
|
|
|
get_worktrees() must return main worktree as first item even on error
This is required by git-worktree.txt, stating that the main worktree is
the first line (especially in --porcelain mode when we can't just change
behavior at will).
There's only one case when get_worktrees() may skip main worktree, when
parse_ref() fails. Update the code so that we keep first item as main
worktree and return something sensible in this case:
- In user-friendly mode, since we're not constraint by anything,
returning "(error)" should do the job (we already show "(detached
HEAD)" which is not machine-friendly). Actually errors should be
printed on stderr by parse_ref() (*)
- In plumbing mode, we do not show neither 'bare', 'detached' or
'branch ...', which is possible by the format description if I read
it right.
Careful readers may realize that when the local variable "head_ref" in
get_main_worktree() is emptied, add_head_info() will do nothing to
wt->head_sha1. But that's ok because head_sha1 is zero-ized in the
previous patch.
(*) Well, it does not. But it's supposed to be a stop gap implementation
until we can reuse refs code to parse "ref: " stuff in HEAD, from
resolve_refs_unsafe(). Now may be the time since refs refactoring is
mostly done.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-11-28 12:36:54 +03:00
|
|
|
test_expect_success 'broken main worktree still at the top' '
|
|
|
|
git init broken-main &&
|
|
|
|
(
|
|
|
|
cd broken-main &&
|
|
|
|
test_commit new &&
|
|
|
|
git worktree add linked &&
|
|
|
|
cat >expected <<-EOF &&
|
|
|
|
worktree $(pwd)
|
2018-05-13 05:24:13 +03:00
|
|
|
HEAD $ZERO_OID
|
get_worktrees() must return main worktree as first item even on error
This is required by git-worktree.txt, stating that the main worktree is
the first line (especially in --porcelain mode when we can't just change
behavior at will).
There's only one case when get_worktrees() may skip main worktree, when
parse_ref() fails. Update the code so that we keep first item as main
worktree and return something sensible in this case:
- In user-friendly mode, since we're not constraint by anything,
returning "(error)" should do the job (we already show "(detached
HEAD)" which is not machine-friendly). Actually errors should be
printed on stderr by parse_ref() (*)
- In plumbing mode, we do not show neither 'bare', 'detached' or
'branch ...', which is possible by the format description if I read
it right.
Careful readers may realize that when the local variable "head_ref" in
get_main_worktree() is emptied, add_head_info() will do nothing to
wt->head_sha1. But that's ok because head_sha1 is zero-ized in the
previous patch.
(*) Well, it does not. But it's supposed to be a stop gap implementation
until we can reuse refs code to parse "ref: " stuff in HEAD, from
resolve_refs_unsafe(). Now may be the time since refs refactoring is
mostly done.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-11-28 12:36:54 +03:00
|
|
|
|
|
|
|
EOF
|
|
|
|
cd linked &&
|
|
|
|
echo "worktree $(pwd)" >expected &&
|
|
|
|
echo "ref: .broken" >../.git/HEAD &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git worktree list --porcelain >out &&
|
|
|
|
head -n 3 out >actual &&
|
get_worktrees() must return main worktree as first item even on error
This is required by git-worktree.txt, stating that the main worktree is
the first line (especially in --porcelain mode when we can't just change
behavior at will).
There's only one case when get_worktrees() may skip main worktree, when
parse_ref() fails. Update the code so that we keep first item as main
worktree and return something sensible in this case:
- In user-friendly mode, since we're not constraint by anything,
returning "(error)" should do the job (we already show "(detached
HEAD)" which is not machine-friendly). Actually errors should be
printed on stderr by parse_ref() (*)
- In plumbing mode, we do not show neither 'bare', 'detached' or
'branch ...', which is possible by the format description if I read
it right.
Careful readers may realize that when the local variable "head_ref" in
get_main_worktree() is emptied, add_head_info() will do nothing to
wt->head_sha1. But that's ok because head_sha1 is zero-ized in the
previous patch.
(*) Well, it does not. But it's supposed to be a stop gap implementation
until we can reuse refs code to parse "ref: " stuff in HEAD, from
resolve_refs_unsafe(). Now may be the time since refs refactoring is
mostly done.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-11-28 12:36:54 +03:00
|
|
|
test_cmp ../expected actual &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git worktree list >out &&
|
|
|
|
head -n 1 out >actual.2 &&
|
get_worktrees() must return main worktree as first item even on error
This is required by git-worktree.txt, stating that the main worktree is
the first line (especially in --porcelain mode when we can't just change
behavior at will).
There's only one case when get_worktrees() may skip main worktree, when
parse_ref() fails. Update the code so that we keep first item as main
worktree and return something sensible in this case:
- In user-friendly mode, since we're not constraint by anything,
returning "(error)" should do the job (we already show "(detached
HEAD)" which is not machine-friendly). Actually errors should be
printed on stderr by parse_ref() (*)
- In plumbing mode, we do not show neither 'bare', 'detached' or
'branch ...', which is possible by the format description if I read
it right.
Careful readers may realize that when the local variable "head_ref" in
get_main_worktree() is emptied, add_head_info() will do nothing to
wt->head_sha1. But that's ok because head_sha1 is zero-ized in the
previous patch.
(*) Well, it does not. But it's supposed to be a stop gap implementation
until we can reuse refs code to parse "ref: " stuff in HEAD, from
resolve_refs_unsafe(). Now may be the time since refs refactoring is
mostly done.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-11-28 12:36:54 +03:00
|
|
|
grep -F "(error)" actual.2
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2016-11-28 12:36:56 +03:00
|
|
|
test_expect_success 'linked worktrees are sorted' '
|
|
|
|
mkdir sorted &&
|
|
|
|
git init sorted/main &&
|
|
|
|
(
|
|
|
|
cd sorted/main &&
|
|
|
|
test_tick &&
|
|
|
|
test_commit new &&
|
|
|
|
git worktree add ../first &&
|
|
|
|
git worktree add ../second &&
|
2017-04-04 00:35:57 +03:00
|
|
|
git worktree list --porcelain >out &&
|
|
|
|
grep ^worktree out >actual
|
2016-11-28 12:36:56 +03:00
|
|
|
) &&
|
|
|
|
cat >expected <<-EOF &&
|
|
|
|
worktree $(pwd)/sorted/main
|
|
|
|
worktree $(pwd)/sorted/first
|
|
|
|
worktree $(pwd)/sorted/second
|
|
|
|
EOF
|
|
|
|
test_cmp expected sorted/main/actual
|
|
|
|
'
|
|
|
|
|
2020-03-04 10:00:00 +03:00
|
|
|
test_expect_success 'worktree path when called in .git directory' '
|
2020-03-23 00:14:22 +03:00
|
|
|
git worktree list >list1 &&
|
2020-03-04 10:00:00 +03:00
|
|
|
git -C .git worktree list >list2 &&
|
|
|
|
test_cmp list1 list2
|
|
|
|
'
|
|
|
|
|
2015-10-08 20:01:05 +03:00
|
|
|
test_done
|