1
0
mirror of https://github.com/git/git.git synced 2024-10-20 13:28:13 +02:00
git/builtin
Eric Sunshine a4bf1e3c2e worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.

Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.

Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.

Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.

While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).

Implementation Notes:

Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.

The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.

Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 13:44:00 -08:00
..
add.c Merge branch 'tb/add-renormalize' 2017-11-27 11:06:37 +09:00
am.c Merge branch 'mh/tidy-ref-update-flags' 2017-11-15 12:14:29 +09:00
annotate.c
apply.c apply: move lockfile into apply_state 2017-10-06 10:07:18 +09:00
archive.c
bisect--helper.c bisect: mention "view" as an alternative to "visualize" 2017-11-13 10:51:14 +09:00
blame.c diff: make struct diff_flags members lowercase 2017-11-01 11:51:40 +09:00
branch.c Merge branch 'ma/branch-list-paginate' 2017-11-28 13:41:50 +09:00
bundle.c
cat-file.c Merge branch 'jk/diff-blob' into maint 2017-10-18 14:19:01 +09:00
check-attr.c
check-ignore.c
check-mailmap.c
check-ref-format.c Merge branch 'jc/check-ref-format-oor' 2017-11-06 14:24:28 +09:00
checkout-index.c checkout-index: simplify locking logic 2017-10-06 10:07:18 +09:00
checkout.c Merge branch 'jc/branch-name-sanity' 2017-11-28 13:41:49 +09:00
clean.c Merge branch 'jk/ref-filter-colors-fix' 2017-10-18 10:19:08 +09:00
clone.c Merge branch 'mh/tidy-ref-update-flags' 2017-11-15 12:14:29 +09:00
column.c
commit-tree.c
commit.c Merge branch 'ma/reduce-heads-leakfix' 2017-11-15 12:14:32 +09:00
config.c config: add --expiry-date 2017-11-18 12:31:29 +09:00
count-objects.c
credential.c
describe.c Merge branch 'bc/object-id' 2017-11-06 14:24:27 +09:00
diff-files.c
diff-index.c Merge branch 'ma/builtin-unleak' 2017-10-07 16:27:55 +09:00
diff-tree.c
diff.c Merge branch 'bw/diff-opt-impl-to-bitfields' 2017-11-09 14:31:27 +09:00
difftool.c read-cache: leave lock in right state in write_locked_index() 2017-10-07 10:20:56 +09:00
fast-export.c Merge branch 'bw/diff-opt-impl-to-bitfields' 2017-11-09 14:31:27 +09:00
fetch-pack.c
fetch.c Convert check_connected to use struct object_id 2017-10-16 11:05:50 +09:00
fmt-merge-msg.c Merge branch 'ma/reduce-heads-leakfix' 2017-11-15 12:14:32 +09:00
for-each-ref.c provide --color option for all ref-filter users 2017-10-04 11:35:29 +09:00
fsck.c Merge branch 'bp/read-index-from-skip-verification' 2017-11-15 12:14:37 +09:00
gc.c Merge branch 'aw/gc-lockfile-fscanf-fix' into maint 2017-10-18 14:18:59 +09:00
get-tar-commit-id.c distinguish error versus short read from read_in_full() 2017-09-27 15:45:24 +09:00
grep.c Merge branch 'bw/grep-recurse-submodules' 2017-11-06 13:11:27 +09:00
hash-object.c
help.c Merge branch 'ks/help-alias-label' 2017-09-25 15:24:07 +09:00
index-pack.c cleanup: fix possible overflow errors in binary search 2017-10-10 08:57:24 +09:00
init-db.c
interpret-trailers.c
log.c Merge branch 'bw/diff-opt-impl-to-bitfields' 2017-11-09 14:31:27 +09:00
ls-files.c Merge branch 'bp/fsmonitor' 2017-11-21 14:07:50 +09:00
ls-remote.c
ls-tree.c
mailinfo.c
mailsplit.c
merge-base.c Merge branch 'ma/reduce-heads-leakfix' 2017-11-15 12:14:32 +09:00
merge-file.c
merge-index.c
merge-ours.c Merge branch 'bw/diff-opt-impl-to-bitfields' 2017-11-09 14:31:27 +09:00
merge-recursive.c
merge-tree.c
merge.c Merge branch 'ma/reduce-heads-leakfix' 2017-11-15 12:14:32 +09:00
mktag.c
mktree.c
mv.c
name-rev.c Merge branch 'mg/timestamp-t-fix' into maint 2017-10-18 14:19:09 +09:00
notes.c Merge branch 'rd/doc-notes-prune-fix' 2017-12-06 09:23:40 -08:00
pack-objects.c Merge branch 'rj/no-sign-compare' into maint 2017-10-23 14:20:18 +09:00
pack-redundant.c
pack-refs.c
patch-id.c
prune-packed.c
prune.c Merge branch 'rd/man-prune-progress' 2017-12-06 09:23:39 -08:00
pull.c Merge branch 'sw/pull-ipv46-passthru' 2017-11-27 11:06:40 +09:00
push.c builtin/push.c: add push.pushOption config 2017-10-24 09:57:54 +09:00
read-tree.c
rebase--helper.c
receive-pack.c Merge branch 'bw/protocol-v1' 2017-12-06 09:23:44 -08:00
reflog.c config: add --expiry-date 2017-11-18 12:31:29 +09:00
remote-ext.c consistently use "fallthrough" comments in switches 2017-09-22 12:49:57 +09:00
remote-fd.c
remote.c Merge branch 'mh/tidy-ref-update-flags' 2017-11-15 12:14:29 +09:00
repack.c
replace.c refs: convert read_ref and read_ref_full to object_id 2017-10-16 11:05:50 +09:00
rerere.c
reset.c Merge branch 'bw/diff-opt-impl-to-bitfields' 2017-11-09 14:31:27 +09:00
rev-list.c Merge branch 'ma/bisect-leakfix' 2017-11-15 12:14:28 +09:00
rev-parse.c Merge branch 'mh/packed-ref-store-prep' into maint 2017-10-18 14:18:58 +09:00
revert.c
rm.c
send-pack.c
shortlog.c
show-branch.c Merge branch 'bc/object-id' 2017-11-06 14:24:27 +09:00
show-ref.c refs: convert peel_ref to struct object_id 2017-10-16 11:05:51 +09:00
stripspace.c
submodule--helper.c Merge branch 'bc/object-id' 2017-11-06 14:24:27 +09:00
symbolic-ref.c refs: rename constant REF_NODEREF to REF_NO_DEREF 2017-11-06 10:31:08 +09:00
tag.c Merge branch 'bc/object-id' 2017-11-06 14:24:27 +09:00
unpack-file.c Merge branch 'jk/write-in-full-fix' 2017-09-25 15:24:06 +09:00
unpack-objects.c cleanup: fix possible overflow errors in binary search 2017-10-10 08:57:24 +09:00
update-index.c Merge branch 'bp/fsmonitor' 2017-11-21 14:07:50 +09:00
update-ref.c refs: rename constant REF_NODEREF to REF_NO_DEREF 2017-11-06 10:31:08 +09:00
update-server-info.c
upload-archive.c
var.c
verify-commit.c
verify-pack.c
verify-tag.c
worktree.c worktree: add: fix 'post-checkout' not knowing new worktree location 2018-02-15 13:44:00 -08:00
write-tree.c