Add --depth to submodule update/add

Add the --depth option to the add and update commands of "git submodule", which is then passed on to the clone command. This is useful when the submodule(s) are huge and you're not really interested in anything but the latest commit. Tests are added and some indention adjustments were made to conform to the rest of the testfile on "submodule update can handle symbolic links in pwd". Signed-off-by: Fredrik Gustafsson <iveqy@iveqy.com> Acked-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Fredrik Gustafsson committed Jul 2, 2013 at 23:42 UTC 275cd184d52b5b81cb89e4ec33e540fb2ae61c1f
4 files changed +61 -12
Documentation/git-submodule.txt
+8 -2
@@ -10,12 +10,12 @@ SYNOPSIS
10 --------
11 [verse]
12 'git submodule' [--quiet] add [-b <branch>] [-f|--force] [--name <name>]
13 - [--reference <repository>] [--] <repository> [<path>]
13 + [--reference <repository>] [--depth <depth>] [--] <repository> [<path>]
14 'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
15 'git submodule' [--quiet] init [--] [<path>...]
16 'git submodule' [--quiet] deinit [-f|--force] [--] <path>...
17 'git submodule' [--quiet] update [--init] [--remote] [-N|--no-fetch]
18 - [-f|--force] [--rebase] [--reference <repository>]
18 + [-f|--force] [--rebase] [--reference <repository>] [--depth <depth>]
19 [--merge] [--recursive] [--] [<path>...]
20 'git submodule' [--quiet] summary [--cached|--files] [(-n|--summary-limit) <n>]
21 [commit] [--] [<path>...]
@@ -328,6 +328,12 @@ for linkgit:git-clone[1]'s `--reference` and `--shared` options carefully.
328 only in the submodules of the current repo, but also
329 in any nested submodules inside those submodules (and so on).
330
331 +--depth::
332 + This option is valid for add and update commands. Create a 'shallow'
333 + clone with a history truncated to the specified number of revisions.
334 + See linkgit:git-clone[1]
335 +
336 +
337 <path>...::
338 Paths to submodule(s). When specified this will restrict the command
339 to only operate on the submodules found at the specified paths.
git-submodule.sh
+21 -3
@@ -32,6 +32,7 @@ nofetch=
32 update=
33 prefix=
34 custom_name=
35 +depth=
36
37 # The function takes at most 2 arguments. The first argument is the
38 # URL that navigates to the submodule origin repo. When relative, this URL
@@ -211,6 +212,7 @@ module_clone()
212 name=$2
213 url=$3
214 reference="$4"
215 + depth="$5"
216 quiet=
217 if test -n "$GIT_QUIET"
218 then
@@ -233,7 +235,7 @@ module_clone()
235 mkdir -p "$gitdir_base"
236 (
237 clear_local_git_env
236 - git clone $quiet -n ${reference:+"$reference"} \
238 + git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
239 --separate-git-dir "$gitdir" "$url" "$sm_path"
240 ) ||
241 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
@@ -309,6 +311,14 @@ cmd_add()
311 custom_name=$2
312 shift
313 ;;
314 + --depth)
315 + case "$2" in '') usage ;; esac
316 + depth="--depth=$2"
317 + shift
318 + ;;
319 + --depth=*)
320 + depth=$1
321 + ;;
322 --)
323 shift
324 break
@@ -405,7 +415,7 @@ Use -f if you really want to add it." >&2
415 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
416 fi
417 fi
408 - module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
418 + module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
419 (
420 clear_local_git_env
421 cd "$sm_path" &&
@@ -676,6 +686,14 @@ cmd_update()
686 --checkout)
687 update="checkout"
688 ;;
689 + --depth)
690 + case "$2" in '') usage ;; esac
691 + depth="--depth=$2"
692 + shift
693 + ;;
694 + --depth=*)
695 + depth=$1
696 + ;;
697 --)
698 shift
699 break
@@ -735,7 +753,7 @@ Maybe you want to use 'update --init'?")"
753
754 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
755 then
738 - module_clone "$sm_path" "$name" "$url" "$reference" || exit
756 + module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
757 cloned_modules="$cloned_modules;$name"
758 subsha1=
759 else
t/t7400-submodule-basic.sh
+15
@@ -868,4 +868,19 @@ test_expect_success 'submodule deinit fails when submodule has a .git directory
868 test -n "$(git config --get-regexp "submodule\.example\.")"
869 '
870
871 +test_expect_success 'submodule add clone shallow submodule' '
872 + mkdir super &&
873 + pwd=$(pwd)
874 + (
875 + cd super &&
876 + git init &&
877 + git submodule add --depth=1 file://"$pwd"/example2 submodule &&
878 + (
879 + cd submodule &&
880 + test 1 = $(git log --oneline | wc -l)
881 + )
882 + )
883 +'
884 +
885 +
886 test_done
t/t7406-submodule-update.sh
+17 -7
@@ -685,14 +685,24 @@ test_expect_success 'submodule update properly revives a moved submodule' '
685 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
686 mkdir -p linked/dir &&
687 ln -s linked/dir linkto &&
688 - (
689 - cd linkto &&
690 - git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
691 - (
692 - cd super &&
693 - git submodule update --init --recursive
694 - )
688 + (cd linkto &&
689 + git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
690 + (cd super &&
691 + git submodule update --init --recursive
692 + )
693 )
694 '
695
696 +test_expect_success 'submodule update clone shallow submodule' '
697 + git clone cloned super3 &&
698 + pwd=$(pwd)
699 + (cd super3 &&
700 + sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
701 + mv -f .gitmodules.tmp .gitmodules &&
702 + git submodule update --init --depth=3
703 + (cd submodule &&
704 + test 1 = $(git log --oneline | wc -l)
705 + )
706 + )
707 +'
708 test_done