1
1
#! /bin/sh
2
2
#
3
- # git-submodules.sh: init, update or list git submodules
3
+ # git-submodules.sh: add, init, update or list git submodules
4
4
#
5
5
# Copyright (c) 2007 Lars Hjemli
6
6
7
- USAGE=' [--quiet] [--cached] [status|init|update] [--] [<path>...]'
7
+ USAGE=' [--quiet] [--cached] [add <repo> [-b branch]| status|init|update] [--] [<path>...]'
8
8
. git-sh-setup
9
9
require_work_tree
10
10
11
+ add=
12
+ branch=
11
13
init=
12
14
update=
13
15
status=
25
27
fi
26
28
}
27
29
30
+ # NEEDSWORK: identical function exists in get_repo_base in clone.sh
31
+ get_repo_base () {
32
+ (
33
+ cd " ` /bin/pwd` " &&
34
+ cd " $1 " || cd " $1 .git" &&
35
+ {
36
+ cd .git
37
+ pwd
38
+ }
39
+ ) 2> /dev/null
40
+ }
41
+
28
42
#
29
43
# Map submodule path to submodule name
30
44
#
@@ -42,6 +56,11 @@ module_name()
42
56
#
43
57
# Clone a submodule
44
58
#
59
+ # Prior to calling, modules_update checks that a possibly existing
60
+ # path is not a git repository.
61
+ # Likewise, module_add checks that path does not exist at all,
62
+ # since it is the location of a new submodule.
63
+ #
45
64
module_clone ()
46
65
{
47
66
path=$1
@@ -65,6 +84,53 @@ module_clone()
65
84
die " Clone of '$url ' into submodule path '$path ' failed"
66
85
}
67
86
87
+ #
88
+ # Add a new submodule to the working tree, .gitmodules and the index
89
+ #
90
+ # $@ = repo [path]
91
+ #
92
+ # optional branch is stored in global branch variable
93
+ #
94
+ module_add ()
95
+ {
96
+ repo=$1
97
+ path=$2
98
+
99
+ if test -z " $repo " ; then
100
+ usage
101
+ fi
102
+
103
+ # Turn the source into an absolute path if
104
+ # it is local
105
+ if base=$( get_repo_base " $repo " ) ; then
106
+ repo=" $base "
107
+ fi
108
+
109
+ # Guess path from repo if not specified or strip trailing slashes
110
+ if test -z " $path " ; then
111
+ path=$( echo " $repo " | sed -e ' s|/*$||' -e ' s|:*/*\.git$||' -e ' s|.*[/:]||g' )
112
+ else
113
+ path=$( echo " $path " | sed -e ' s|/*$||' )
114
+ fi
115
+
116
+ test -e " $path " &&
117
+ die " '$path ' already exists"
118
+
119
+ git-ls-files --error-unmatch " $path " > /dev/null 2>&1 &&
120
+ die " '$path ' already exists in the index"
121
+
122
+ module_clone " $path " " $repo " || exit
123
+ (unset GIT_DIR && cd " $path " && git checkout -q ${branch: +-b " $branch " " origin/$branch " } ) ||
124
+ die " Unable to checkout submodule '$path '"
125
+ git add " $path " ||
126
+ die " Failed to add submodule '$path '"
127
+
128
+ GIT_CONFIG=.gitmodules git config submodule." $path " .path " $path " &&
129
+ GIT_CONFIG=.gitmodules git config submodule." $path " .url " $repo " &&
130
+ git add .gitmodules ||
131
+ die " Failed to register submodule '$path '"
132
+ }
133
+
68
134
#
69
135
# Register submodules in .git/config
70
136
#
@@ -173,6 +239,9 @@ modules_list()
173
239
while case " $# " in 0) break ;; esac
174
240
do
175
241
case " $1 " in
242
+ add)
243
+ add=1
244
+ ;;
176
245
init)
177
246
init=1
178
247
;;
185
254
-q|--quiet)
186
255
quiet=1
187
256
;;
257
+ -b|--branch)
258
+ case " $2 " in
259
+ ' ' )
260
+ usage
261
+ ;;
262
+ esac
263
+ branch=" $2 " ; shift
264
+ ;;
188
265
--cached)
189
266
cached=1
190
267
;;
201
278
shift
202
279
done
203
280
204
- case " $init ,$update ,$status ,$cached " in
205
- 1,,,)
281
+ case " $add ,$branch " in
282
+ 1,* )
283
+ ;;
284
+ ,)
285
+ ;;
286
+ ,* )
287
+ usage
288
+ ;;
289
+ esac
290
+
291
+ case " $add ,$init ,$update ,$status ,$cached " in
292
+ 1,,,,)
293
+ module_add " $@ "
294
+ ;;
295
+ ,1,,,)
206
296
modules_init " $@ "
207
297
;;
208
- ,1,,)
298
+ ,, 1,,)
209
299
modules_update " $@ "
210
300
;;
211
- ,,* ,* )
301
+ ,,,1 ,* )
212
302
modules_list " $@ "
213
303
;;
214
304
* )
0 commit comments