git-submodule: work with GIT_DIR/GIT_WORK_TREE

The combination of GIT_DIR and GIT_WORK_TREE can be used to manage files in one directory hierarchy while keeping the repository that keeps track of them outside the directory hierarchy. For example: git init --bare /path/to/there alias dotfiles="GIT_DIR=/path/to/there GIT_WORK_TREE=/path/to/here git" cd /path/to/here dotfiles add file dotfiles commit -a -m "add /path/to/here/file" ... lets you manage files under /path/to/here/ in the repository located at /path/to/there. git-submodule however fails to add submodules, as it is confused by GIT_DIR and GIT_WORK_TREE environment variables when it tries to work in the submodule, like so: dotfiles submodule add http://path.to/submodule fatal: working tree '/path/to/here' already exists. Simply unsetting the environment where the command works on the submodule is sufficient to fix this, as it has set things up so that GIT_DIR and GIT_WORK_TREE do not even have to point at the repository and the working tree of the submodule. Signed-off-by: Daniel Graña <dangra@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Graña committed Jul 22, 2012 at 11:49 UTC be8779f7ac9a3be9aa783df008d59082f4054f67
2 files changed +66 -2
git-submodule.sh
+5 -2
@@ -181,8 +181,11 @@ module_clone()
181 rm -f "$gitdir/index"
182 else
183 mkdir -p "$gitdir_base"
184 - git clone $quiet -n ${reference:+"$reference"} \
185 - --separate-git-dir "$gitdir" "$url" "$sm_path" ||
184 + (
185 + clear_local_git_env
186 + git clone $quiet -n ${reference:+"$reference"} \
187 + --separate-git-dir "$gitdir" "$url" "$sm_path"
188 + ) ||
189 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
190 fi
191
t/t7409-submodule-detached-worktree.sh new
+61
@@ -0,0 +1,61 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2012 Daniel Graña
4 +#
5 +
6 +test_description='Test submodules on detached working tree
7 +
8 +This test verifies that "git submodule" initialization, update and addition works
9 +on detahced working trees
10 +'
11 +
12 +TEST_NO_CREATE_REPO=1
13 +. ./test-lib.sh
14 +
15 +test_expect_success 'submodule on detached working tree' '
16 + git init --bare remote &&
17 + test_create_repo bundle1 &&
18 + (cd bundle1 && test_commit "shoot") &&
19 + mkdir home &&
20 + (
21 + cd home &&
22 + export GIT_WORK_TREE="$(pwd)" GIT_DIR="$(pwd)/.dotfiles" &&
23 + git clone --bare ../remote .dotfiles &&
24 + git submodule add ../bundle1 .vim/bundle/sogood &&
25 + test_commit "sogood" &&
26 + git push origin master
27 + ) &&
28 + mkdir home2 &&
29 + (
30 + cd home2 &&
31 + export GIT_WORK_TREE="$(pwd)" GIT_DIR="$(pwd)/.dotfiles" &&
32 + git clone --bare ../remote .dotfiles &&
33 + git submodule update --init
34 + )
35 +'
36 +
37 +test_expect_success 'submodule on detached working pointed by core.worktree' '
38 + mkdir home3 &&
39 + (
40 + cd home3 &&
41 + export GIT_DIR="$(pwd)/.dotfiles" &&
42 + git clone --bare ../remote "$GIT_DIR" &&
43 + git config core.bare false &&
44 + git config core.worktree .. &&
45 + git submodule add ../bundle1 .vim/bundle/dupe &&
46 + test_commit "dupe" &&
47 + git push origin master
48 + ) &&
49 + (
50 + cd home &&
51 + export GIT_DIR="$(pwd)/.dotfiles" &&
52 + git config core.bare false &&
53 + git config core.worktree .. &&
54 + git pull &&
55 + git submodule update &&
56 + git submodule status &&
57 + test -d .vim/bundle/dupe
58 + )
59 +'
60 +
61 +test_done