Skip to content

Commit 2bb7afa

Browse files
committed
Merge branch 'fg/submodule-clone-depth'
Allow shallow-cloning of submodules with "git submodule update". * fg/submodule-clone-depth: Add --depth to submodule update/add
2 parents 3bb6149 + 275cd18 commit 2bb7afa

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

Documentation/git-submodule.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git submodule' [--quiet] add [-b <branch>] [-f|--force] [--name <name>]
13-
[--reference <repository>] [--] <repository> [<path>]
13+
[--reference <repository>] [--depth <depth>] [--] <repository> [<path>]
1414
'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
1515
'git submodule' [--quiet] init [--] [<path>...]
1616
'git submodule' [--quiet] deinit [-f|--force] [--] <path>...
1717
'git submodule' [--quiet] update [--init] [--remote] [-N|--no-fetch]
18-
[-f|--force] [--rebase] [--reference <repository>]
18+
[-f|--force] [--rebase] [--reference <repository>] [--depth <depth>]
1919
[--merge] [--recursive] [--] [<path>...]
2020
'git submodule' [--quiet] summary [--cached|--files] [(-n|--summary-limit) <n>]
2121
[commit] [--] [<path>...]
@@ -330,6 +330,12 @@ for linkgit:git-clone[1]'s `--reference` and `--shared` options carefully.
330330
only in the submodules of the current repo, but also
331331
in any nested submodules inside those submodules (and so on).
332332

333+
--depth::
334+
This option is valid for add and update commands. Create a 'shallow'
335+
clone with a history truncated to the specified number of revisions.
336+
See linkgit:git-clone[1]
337+
338+
333339
<path>...::
334340
Paths to submodule(s). When specified this will restrict the command
335341
to only operate on the submodules found at the specified paths.

git-submodule.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ nofetch=
3535
update=
3636
prefix=
3737
custom_name=
38+
depth=
3839

3940
# The function takes at most 2 arguments. The first argument is the
4041
# URL that navigates to the submodule origin repo. When relative, this URL
@@ -251,6 +252,7 @@ module_clone()
251252
name=$2
252253
url=$3
253254
reference="$4"
255+
depth="$5"
254256
quiet=
255257
if test -n "$GIT_QUIET"
256258
then
@@ -273,7 +275,7 @@ module_clone()
273275
mkdir -p "$gitdir_base"
274276
(
275277
clear_local_git_env
276-
git clone $quiet -n ${reference:+"$reference"} \
278+
git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
277279
--separate-git-dir "$gitdir" "$url" "$sm_path"
278280
) ||
279281
die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
@@ -350,6 +352,14 @@ cmd_add()
350352
custom_name=$2
351353
shift
352354
;;
355+
--depth)
356+
case "$2" in '') usage ;; esac
357+
depth="--depth=$2"
358+
shift
359+
;;
360+
--depth=*)
361+
depth=$1
362+
;;
353363
--)
354364
shift
355365
break
@@ -459,7 +469,7 @@ Use -f if you really want to add it." >&2
459469
echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
460470
fi
461471
fi
462-
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
472+
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
463473
(
464474
clear_local_git_env
465475
cd "$sm_path" &&
@@ -736,6 +746,14 @@ cmd_update()
736746
--checkout)
737747
update="checkout"
738748
;;
749+
--depth)
750+
case "$2" in '') usage ;; esac
751+
depth="--depth=$2"
752+
shift
753+
;;
754+
--depth=*)
755+
depth=$1
756+
;;
739757
--)
740758
shift
741759
break
@@ -797,7 +815,7 @@ Maybe you want to use 'update --init'?")"
797815

798816
if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
799817
then
800-
module_clone "$sm_path" "$name" "$url" "$reference" || exit
818+
module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
801819
cloned_modules="$cloned_modules;$name"
802820
subsha1=
803821
else

t/t7400-submodule-basic.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,4 +963,20 @@ test_expect_success 'submodule with UTF-8 name' '
963963
git submodule >&2 &&
964964
test -n "$(git submodule | grep "$svname")"
965965
'
966+
967+
test_expect_success 'submodule add clone shallow submodule' '
968+
mkdir super &&
969+
pwd=$(pwd)
970+
(
971+
cd super &&
972+
git init &&
973+
git submodule add --depth=1 file://"$pwd"/example2 submodule &&
974+
(
975+
cd submodule &&
976+
test 1 = $(git log --oneline | wc -l)
977+
)
978+
)
979+
'
980+
981+
966982
test_done

t/t7406-submodule-update.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -729,14 +729,24 @@ test_expect_success 'submodule update properly revives a moved submodule' '
729729
test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
730730
mkdir -p linked/dir &&
731731
ln -s linked/dir linkto &&
732-
(
733-
cd linkto &&
734-
git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
735-
(
736-
cd super &&
737-
git submodule update --init --recursive
738-
)
732+
(cd linkto &&
733+
git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
734+
(cd super &&
735+
git submodule update --init --recursive
736+
)
739737
)
740738
'
741739

740+
test_expect_success 'submodule update clone shallow submodule' '
741+
git clone cloned super3 &&
742+
pwd=$(pwd)
743+
(cd super3 &&
744+
sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
745+
mv -f .gitmodules.tmp .gitmodules &&
746+
git submodule update --init --depth=3
747+
(cd submodule &&
748+
test 1 = $(git log --oneline | wc -l)
749+
)
750+
)
751+
'
742752
test_done

0 commit comments

Comments
 (0)