Skip to content

Commit 5e10b4a

Browse files
committed
Make the pre-commit hook work in a git worktree
The devcontainer only mounts the worktree directory, so git inside the container couldn't find the gitdir that the worktree's .git file points at. Bind-mount the main repo's git dir at the same path when we're in a worktree.
1 parent 23f4f68 commit 5e10b4a

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Justfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
_dce := "devcontainer exec --workspace-folder ."
2+
# When we're in a git worktree, the workspace's .git is a file pointing at a
3+
# gitdir outside the workspace, so git doesn't work in the container unless we
4+
# also mount the main repo's git dir at the same path. Note that `devcontainer
5+
# up` reuses an existing container without comparing mounts, so a container
6+
# created before this mount existed needs a `just rebuild` once.
7+
_git_common_dir := `test -f .git && realpath "$(git rev-parse --git-common-dir)" || true`
8+
_git_mount := if _git_common_dir != "" { "--mount 'type=bind,source=" + _git_common_dir + ",target=" + _git_common_dir + "'" } else { "" }
29
_github_token := `git config github.token 2>/dev/null || true`
310
_gitlab_token := `git config gitlab.token 2>/dev/null || true`
411
_codeberg_token := `git config codeberg.token 2>/dev/null || true`
512

613
_up:
7-
devcontainer up --workspace-folder .
14+
devcontainer up --workspace-folder . {{ _git_mount }}
815

916
rebuild:
10-
devcontainer up --workspace-folder . --remove-existing-container
17+
devcontainer up --workspace-folder . {{ _git_mount }} --remove-existing-container
1118

1219
shell: _up
1320
{{ _dce }} bash -i

0 commit comments

Comments
 (0)