Skip to content

Commit 62b23b3

Browse files
committed
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/<branch>. 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 <repo>/.git at the same host path inside the container; nothing breaks. .devcontainer/.env is generated per-launch and is gitignored.
1 parent 0e2d13b commit 62b23b3

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

.devcontainer/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ services:
66
volumes:
77
- ..:/workspaces/oracle-enhanced:cached
88
- ./tzdata:/opt/tzdata:ro
9+
- ${GIT_COMMON_DIR}:${GIT_COMMON_DIR}:cached
910
command: sleep infinity
1011
network_mode: service:oracle
1112

.devcontainer/initializeCommand.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -euo pipefail
44

55
IMAGE=gvenzl/oracle-free:latest
66
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
7+
WORKSPACE_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
78
OUT_DIR="$SCRIPT_DIR/tzdata"
89

910
docker pull "$IMAGE"
@@ -17,3 +18,16 @@ docker run --rm --entrypoint sh \
1718
-c 'cp "$ORACLE_HOME"/oracore/zoneinfo/timezlrg_*.dat /out/ && chmod a+r /out/*.dat'
1819

1920
ls -1 "$OUT_DIR"
21+
22+
# When opened from a git worktree, .git is a file whose `gitdir:` line points
23+
# at the main repo's .git/worktrees/<branch>. That host path is not visible
24+
# inside the dev container by default, so git commands inside the container
25+
# fail with "fatal: not a git repository". Resolve the common git dir on the
26+
# host and write it to .devcontainer/.env; docker-compose.yml bind-mounts it
27+
# at the same path inside the container so the gitdir reference resolves.
28+
GIT_COMMON_DIR=$(git -C "$WORKSPACE_DIR" rev-parse --git-common-dir)
29+
case "$GIT_COMMON_DIR" in
30+
/*) ;;
31+
*) GIT_COMMON_DIR=$(cd "$WORKSPACE_DIR/$GIT_COMMON_DIR" && pwd) ;;
32+
esac
33+
printf 'GIT_COMMON_DIR=%s\n' "$GIT_COMMON_DIR" > "$SCRIPT_DIR/.env"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ ojdbc*.jar
1515
.byebug_history
1616
debug.log
1717
.devcontainer/tzdata/
18+
.devcontainer/.env

0 commit comments

Comments
 (0)