Skip to content

Commit ecda072

Browse files
Sven Verdoolaegegitster
authored andcommitted
git-submodule: provide easy way of adding new submodules
To make a submodule effectively usable, the path and a URL where the submodule can be cloned need to be stored in .gitmodules. This subcommand takes care of setting this information after cloning the new submodule. Only the index is updated, so, if needed, the user may still change the URL or switch to a different branch of the submodule before committing. Signed-off-by: Sven Verdoolaege <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 444649e commit ecda072

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

Documentation/git-submodule.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ git-submodule - Initialize, update or inspect submodules
88

99
SYNOPSIS
1010
--------
11+
'git-submodule' [--quiet] [-b branch] add <repository> [<path>]
1112
'git-submodule' [--quiet] [--cached] [status|init|update] [--] [<path>...]
1213

1314

1415
COMMANDS
1516
--------
17+
add::
18+
Add the given repository as a submodule at the given path
19+
to the changeset to be committed next. In particular, the
20+
repository is cloned at the specified path, added to the
21+
changeset and registered in .gitmodules. If no path is
22+
specified, the path is deduced from the repository specification.
23+
1624
status::
1725
Show the status of the submodules. This will print the SHA-1 of the
1826
currently checked out commit for each submodule, along with the
@@ -39,6 +47,9 @@ OPTIONS
3947
-q, --quiet::
4048
Only print error messages.
4149

50+
-b, --branch::
51+
Branch of repository to add as submodule.
52+
4253
--cached::
4354
Display the SHA-1 stored in the index, not the SHA-1 of the currently
4455
checked out submodule commit. This option is only valid for the

git-submodule.sh

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/bin/sh
22
#
3-
# git-submodules.sh: init, update or list git submodules
3+
# git-submodules.sh: add, init, update or list git submodules
44
#
55
# Copyright (c) 2007 Lars Hjemli
66

7-
USAGE='[--quiet] [--cached] [status|init|update] [--] [<path>...]'
7+
USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
88
. git-sh-setup
99
require_work_tree
1010

11+
add=
12+
branch=
1113
init=
1214
update=
1315
status=
@@ -25,6 +27,18 @@ say()
2527
fi
2628
}
2729

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+
2842
#
2943
# Map submodule path to submodule name
3044
#
@@ -42,6 +56,11 @@ module_name()
4256
#
4357
# Clone a submodule
4458
#
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+
#
4564
module_clone()
4665
{
4766
path=$1
@@ -65,6 +84,53 @@ module_clone()
6584
die "Clone of '$url' into submodule path '$path' failed"
6685
}
6786

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+
68134
#
69135
# Register submodules in .git/config
70136
#
@@ -173,6 +239,9 @@ modules_list()
173239
while case "$#" in 0) break ;; esac
174240
do
175241
case "$1" in
242+
add)
243+
add=1
244+
;;
176245
init)
177246
init=1
178247
;;
@@ -185,6 +254,14 @@ do
185254
-q|--quiet)
186255
quiet=1
187256
;;
257+
-b|--branch)
258+
case "$2" in
259+
'')
260+
usage
261+
;;
262+
esac
263+
branch="$2"; shift
264+
;;
188265
--cached)
189266
cached=1
190267
;;
@@ -201,14 +278,27 @@ do
201278
shift
202279
done
203280

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,,,)
206296
modules_init "$@"
207297
;;
208-
,1,,)
298+
,,1,,)
209299
modules_update "$@"
210300
;;
211-
,,*,*)
301+
,,,1,*)
212302
modules_list "$@"
213303
;;
214304
*)

0 commit comments

Comments
 (0)