White scenery @showyou, hatena

If you have any comments, you may also send twitter @showyou.

git sparse-checkoutで一部以下のディレクトリしか見えなくする

http://vmiklos.hu/blog/sparse-checkout-example-in-git-1-7

http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html#_sparse_checkout

git sparse-checkoutはgit 1.7から備わった機能で、現在のワーキングコピー中で一部のディレクトリ以下のみを管理するように変えます。(見えてないディレクトリもダウンロードはされてるみたいです)

やり方としてはまず core.sparsecheckoutをtrueにして、.git/info/sparese/checkoutに見たいディレクトリパスを記述してgit read-tree -m -u HEADを実行すれば良い模様です。git read-treeについてはこちら。 http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html

具体的には下の様なコマンドを実行します。

% git config --global core.sparsecheckout true

% echo trunk/target > .git/info/sparse-checkout

% git read-tree -m -u HEAD

以下、実際に試した例です。

ここではディレクトリ構造を以下の通りと仮定しています。

+/trunk

++hoo.txt

++/target

+++bar.txt

+/branch

++baz.txt

この中でtrunk/target以下を見たい時の手順です。

% mkdir test-sparse-checkout

% cd test-sparse-checkout

% ls

% git init

Initialized empty Git repository in /*/test-sparse-checkout/.git/

% mkdir trunk

% mkdir trunk/target

% mkdir branch

% touch trunk/hoge.txt

% vim trunk/hoge.txt

% vim trunk/target/foo.txt

% vim branch/branch.txt

% git add trunk/hoge.txt

% git add trunk/target/foo.txt

% git add branch/branch.txt

% git commit -a

[master (root-commit) 3c72cf7] append sample text

3 files changed, 14 insertions(+)

create mode 100644 branch/branch.txt

create mode 100644 trunk/hoge.txt

create mode 100644 trunk/target/foo.txt

% git remote add origin git@####/test-sparse-checkout.git

% git push origin master Counting objects: 8, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (8/8), 482 bytes, done.

Total 8 (delta 0), reused 0 (delta 0)

To git@git@####/test-sparse-checkout.git

* [new branch] master -> master

% cd ../

% git clone git@git@####/test-sparse-checkout.git test-sparse-checkout2

Cloning into 'test-sparse-checkout2'...

remote: Counting objects: 8, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 8 (delta 0), reused 8 (delta 0)

Receiving objects: 100% (8/8), done.

% cd test-sparse-checkout2

% ls

branch trunk

% cd test-sparse-checkout2

% ls

branch trunk

% git config --global core.sparsecheckout true

% echo trunk/target > .git/info/sparse-checkout

% git read-tree -m -u HEAD

% ls

trunk

念の為sparse-checkoutしたときとそうでない時でサイズを確認します。

% du ../test-sparse-checkout2

72 ../test-sparse-checkout2/.git/hooks

16 ../test-sparse-checkout2/.git/info

8 ../test-sparse-checkout2/.git/logs/refs/heads

8 ../test-sparse-checkout2/.git/logs/refs

16 ../test-sparse-checkout2/.git/logs

0 ../test-sparse-checkout2/.git/objects/info

16 ../test-sparse-checkout2/.git/objects/pack

16 ../test-sparse-checkout2/.git/objects

8 ../test-sparse-checkout2/.git/refs/heads

8 ../test-sparse-checkout2/.git/refs/remotes/origin

8 ../test-sparse-checkout2/.git/refs/remotes

0 ../test-sparse-checkout2/.git/refs/tags

16 ../test-sparse-checkout2/.git/refs

176 ../test-sparse-checkout2/.git

8 ../test-sparse-checkout2/trunk/target

8 ../test-sparse-checkout2/trunk

184 ../test-sparse-checkout2

% git clone git@git@####/test-sparse-checkout.git test-sparse-checkout3

Cloning into 'test-sparse-checkout2'...

remote: Counting objects: 8, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 8 (delta 0), reused 8 (delta 0)

Receiving objects: 100% (8/8), done.

% du ./test-sparse-checkout3

72 test-sparse-checkout3/.git/hooks

8 test-sparse-checkout3/.git/info

8 test-sparse-checkout3/.git/logs/refs/heads

8 test-sparse-checkout3/.git/logs/refs

16 test-sparse-checkout3/.git/logs

0 test-sparse-checkout3/.git/objects/info

16 test-sparse-checkout3/.git/objects/pack

16 test-sparse-checkout3/.git/objects

8 test-sparse-checkout3/.git/refs/heads

8 test-sparse-checkout3/.git/refs/remotes/origin

8 test-sparse-checkout3/.git/refs/remotes

0 test-sparse-checkout3/.git/refs/tags

16 test-sparse-checkout3/.git/refs

168 test-sparse-checkout3/.git

8 test-sparse-checkout3/branch

8 test-sparse-checkout3/trunk/target

16 test-sparse-checkout3/trunk

192 test-sparse-checkout3