blob: 9d142ed649ef98adcbf7c6d158890c9f55370ef7 [file] [log] [blame]
Lars Hjemli88961ef2007-06-02 03:27:42 +02001#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='Basic porcelain support for submodules
7
8This test tries to verify basic sanity of the init, update and status
9subcommands of git-submodule.
10'
11
12. ./test-lib.sh
13
14#
15# Test setup:
16# -create a repository in directory lib
17# -add a couple of files
18# -add directory lib to 'superproject', this creates a DIRLINK entry
19# -add a couple of regular files to enable testing of submodule filtering
20# -mv lib subrepo
Lars Hjemli941987a2007-06-11 21:12:24 +020021# -add an entry to .gitmodules for submodule 'example'
Lars Hjemli88961ef2007-06-02 03:27:42 +020022#
23test_expect_success 'Prepare submodule testing' '
Sven Verdoolaege0cf73752007-07-17 20:28:28 +020024 : > t &&
25 git-add t &&
26 git-commit -m "initial commit" &&
27 git branch initial HEAD &&
Lars Hjemli88961ef2007-06-02 03:27:42 +020028 mkdir lib &&
29 cd lib &&
Junio C Hamano5be60072007-07-02 22:52:14 -070030 git init &&
Lars Hjemli88961ef2007-06-02 03:27:42 +020031 echo a >a &&
Junio C Hamano5be60072007-07-02 22:52:14 -070032 git add a &&
Lars Hjemli88961ef2007-06-02 03:27:42 +020033 git-commit -m "submodule commit 1" &&
34 git-tag -a -m "rev-1" rev-1 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070035 rev1=$(git rev-parse HEAD) &&
Lars Hjemli88961ef2007-06-02 03:27:42 +020036 if test -z "$rev1"
37 then
Junio C Hamano5be60072007-07-02 22:52:14 -070038 echo "[OOPS] submodule git rev-parse returned nothing"
Lars Hjemli88961ef2007-06-02 03:27:42 +020039 false
40 fi &&
41 cd .. &&
42 echo a >a &&
43 echo z >z &&
Junio C Hamano5be60072007-07-02 22:52:14 -070044 git add a lib z &&
Lars Hjemli88961ef2007-06-02 03:27:42 +020045 git-commit -m "super commit 1" &&
46 mv lib .subrepo &&
Junio C Hamano5be60072007-07-02 22:52:14 -070047 GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/lib.git
Lars Hjemli941987a2007-06-11 21:12:24 +020048'
49
50test_expect_success 'status should fail for unmapped paths' '
51 if git-submodule status
52 then
53 echo "[OOPS] submodule status succeeded"
54 false
Junio C Hamano5be60072007-07-02 22:52:14 -070055 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path lib
Lars Hjemli941987a2007-06-11 21:12:24 +020056 then
Junio C Hamano5be60072007-07-02 22:52:14 -070057 echo "[OOPS] git config failed to update .gitmodules"
Lars Hjemli941987a2007-06-11 21:12:24 +020058 false
59 fi
Lars Hjemli88961ef2007-06-02 03:27:42 +020060'
61
62test_expect_success 'status should only print one line' '
63 lines=$(git-submodule status | wc -l) &&
64 test $lines = 1
65'
66
67test_expect_success 'status should initially be "missing"' '
68 git-submodule status | grep "^-$rev1"
69'
70
Lars Hjemli211b7f12007-06-06 11:13:02 +020071test_expect_success 'init should register submodule url in .git/config' '
72 git-submodule init &&
Junio C Hamano5be60072007-07-02 22:52:14 -070073 url=$(git config submodule.example.url) &&
Lars Hjemli211b7f12007-06-06 11:13:02 +020074 if test "$url" != "git://example.com/lib.git"
Lars Hjemli88961ef2007-06-02 03:27:42 +020075 then
Lars Hjemli211b7f12007-06-06 11:13:02 +020076 echo "[OOPS] init succeeded but submodule url is wrong"
77 false
Junio C Hamano5be60072007-07-02 22:52:14 -070078 elif ! git config submodule.example.url ./.subrepo
Lars Hjemli211b7f12007-06-06 11:13:02 +020079 then
80 echo "[OOPS] init succeeded but update of url failed"
81 false
82 fi
83'
84
85test_expect_success 'update should fail when path is used by a file' '
86 echo "hello" >lib &&
87 if git-submodule update
88 then
89 echo "[OOPS] update should have failed"
Lars Hjemli88961ef2007-06-02 03:27:42 +020090 false
Lars Hjemlib10ee762007-06-11 21:12:21 +020091 elif test "$(cat lib)" != "hello"
Lars Hjemli88961ef2007-06-02 03:27:42 +020092 then
Lars Hjemli211b7f12007-06-06 11:13:02 +020093 echo "[OOPS] update failed but lib file was molested"
Lars Hjemli88961ef2007-06-02 03:27:42 +020094 false
95 else
96 rm lib
97 fi
98'
99
Lars Hjemli211b7f12007-06-06 11:13:02 +0200100test_expect_success 'update should fail when path is used by a nonempty directory' '
Lars Hjemli88961ef2007-06-02 03:27:42 +0200101 mkdir lib &&
102 echo "hello" >lib/a &&
Lars Hjemli211b7f12007-06-06 11:13:02 +0200103 if git-submodule update
Lars Hjemli88961ef2007-06-02 03:27:42 +0200104 then
Lars Hjemli211b7f12007-06-06 11:13:02 +0200105 echo "[OOPS] update should have failed"
Lars Hjemli88961ef2007-06-02 03:27:42 +0200106 false
107 elif test "$(cat lib/a)" != "hello"
108 then
Lars Hjemli211b7f12007-06-06 11:13:02 +0200109 echo "[OOPS] update failed but lib/a was molested"
Lars Hjemli88961ef2007-06-02 03:27:42 +0200110 false
111 else
112 rm lib/a
113 fi
114'
115
Lars Hjemli211b7f12007-06-06 11:13:02 +0200116test_expect_success 'update should work when path is an empty dir' '
Lars Hjemli88961ef2007-06-02 03:27:42 +0200117 rm -rf lib &&
118 mkdir lib &&
Lars Hjemli211b7f12007-06-06 11:13:02 +0200119 git-submodule update &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700120 head=$(cd lib && git rev-parse HEAD) &&
Lars Hjemli88961ef2007-06-02 03:27:42 +0200121 if test -z "$head"
122 then
123 echo "[OOPS] Failed to obtain submodule head"
124 false
125 elif test "$head" != "$rev1"
126 then
127 echo "[OOPS] Submodule head is $head but should have been $rev1"
128 false
129 fi
130'
131
Lars Hjemli211b7f12007-06-06 11:13:02 +0200132test_expect_success 'status should be "up-to-date" after update' '
Lars Hjemli88961ef2007-06-02 03:27:42 +0200133 git-submodule status | grep "^ $rev1"
134'
135
136test_expect_success 'status should be "modified" after submodule commit' '
137 cd lib &&
138 echo b >b &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700139 git add b &&
Lars Hjemli88961ef2007-06-02 03:27:42 +0200140 git-commit -m "submodule commit 2" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700141 rev2=$(git rev-parse HEAD) &&
Lars Hjemli88961ef2007-06-02 03:27:42 +0200142 cd .. &&
143 if test -z "$rev2"
144 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700145 echo "[OOPS] submodule git rev-parse returned nothing"
Lars Hjemli88961ef2007-06-02 03:27:42 +0200146 false
147 fi &&
148 git-submodule status | grep "^+$rev2"
149'
150
151test_expect_success 'the --cached sha1 should be rev1' '
152 git-submodule --cached status | grep "^+$rev1"
153'
154
155test_expect_success 'update should checkout rev1' '
156 git-submodule update &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700157 head=$(cd lib && git rev-parse HEAD) &&
Lars Hjemli88961ef2007-06-02 03:27:42 +0200158 if test -z "$head"
159 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700160 echo "[OOPS] submodule git rev-parse returned nothing"
Lars Hjemli88961ef2007-06-02 03:27:42 +0200161 false
162 elif test "$head" != "$rev1"
163 then
164 echo "[OOPS] init did not checkout correct head"
165 false
166 fi
167'
168
169test_expect_success 'status should be "up-to-date" after update' '
170 git-submodule status | grep "^ $rev1"
171'
172
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200173test_expect_success 'checkout superproject with subproject already present' '
174 git-checkout initial &&
175 git-checkout master
176'
177
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200178test_expect_success 'apply submodule diff' '
179 git branch second &&
180 (
181 cd lib &&
182 echo s >s &&
183 git add s &&
184 git commit -m "change subproject"
185 ) &&
186 git update-index --add lib &&
187 git-commit -m "change lib" &&
188 git-format-patch -1 --stdout >P.diff &&
189 git checkout second &&
190 git apply --index P.diff &&
191 D=$(git diff --cached master) &&
192 test -z "$D"
193'
194
Lars Hjemli88961ef2007-06-02 03:27:42 +0200195test_done