From 62b23b31839fc5d3cec85daf3d8a671c456baec1 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 19 May 2026 18:36:18 +0900 Subject: [PATCH] Make git work in the dev container when opened from a worktree When the dev container is opened from a git worktree, the worktree's .git is a file whose `gitdir:` line points at a host path under the main repo's .git/worktrees/. That host path is not visible inside the container by default, so git inside the container fails with "fatal: not a git repository: (null)" -- VS Code's safe.directory probe, credential.helper setup, and any developer git command all hit this. Resolve the common git dir on the host in initializeCommand.sh via `git rev-parse --git-common-dir`, write the absolute path into .devcontainer/.env, and have docker-compose.yml bind-mount it at the same path inside the container. The worktree's `.git` file's gitdir reference then resolves cleanly. For a main checkout this is a no-op-shaped redundant mount of /.git at the same host path inside the container; nothing breaks. .devcontainer/.env is generated per-launch and is gitignored. --- .devcontainer/docker-compose.yml | 1 + .devcontainer/initializeCommand.sh | 14 ++++++++++++++ .gitignore | 1 + 3 files changed, 16 insertions(+) diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index e29e10314..766de8a72 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -6,6 +6,7 @@ services: volumes: - ..:/workspaces/oracle-enhanced:cached - ./tzdata:/opt/tzdata:ro + - ${GIT_COMMON_DIR}:${GIT_COMMON_DIR}:cached command: sleep infinity network_mode: service:oracle diff --git a/.devcontainer/initializeCommand.sh b/.devcontainer/initializeCommand.sh index 6159e0806..38acfc7f7 100755 --- a/.devcontainer/initializeCommand.sh +++ b/.devcontainer/initializeCommand.sh @@ -4,6 +4,7 @@ set -euo pipefail IMAGE=gvenzl/oracle-free:latest SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) +WORKSPACE_DIR=$(cd "$SCRIPT_DIR/.." && pwd) OUT_DIR="$SCRIPT_DIR/tzdata" docker pull "$IMAGE" @@ -17,3 +18,16 @@ docker run --rm --entrypoint sh \ -c 'cp "$ORACLE_HOME"/oracore/zoneinfo/timezlrg_*.dat /out/ && chmod a+r /out/*.dat' ls -1 "$OUT_DIR" + +# When opened from a git worktree, .git is a file whose `gitdir:` line points +# at the main repo's .git/worktrees/. That host path is not visible +# inside the dev container by default, so git commands inside the container +# fail with "fatal: not a git repository". Resolve the common git dir on the +# host and write it to .devcontainer/.env; docker-compose.yml bind-mounts it +# at the same path inside the container so the gitdir reference resolves. +GIT_COMMON_DIR=$(git -C "$WORKSPACE_DIR" rev-parse --git-common-dir) +case "$GIT_COMMON_DIR" in + /*) ;; + *) GIT_COMMON_DIR=$(cd "$WORKSPACE_DIR/$GIT_COMMON_DIR" && pwd) ;; +esac +printf 'GIT_COMMON_DIR=%s\n' "$GIT_COMMON_DIR" > "$SCRIPT_DIR/.env" diff --git a/.gitignore b/.gitignore index 074ec120b..a2dfd6eea 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ ojdbc*.jar .byebug_history debug.log .devcontainer/tzdata/ +.devcontainer/.env