Skip to content

Commit 275cd18

Browse files
iveqygitster
authored andcommitted
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 <[email protected]> Acked-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edca415 commit 275cd18

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-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>...]
@@ -328,6 +328,12 @@ for linkgit:git-clone[1]'s `--reference` and `--shared` options carefully.
328328
only in the submodules of the current repo, but also
329329
in any nested submodules inside those submodules (and so on).
330330

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+
331337
<path>...::
332338
Paths to submodule(s). When specified this will restrict the command
333339
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
@@ -32,6 +32,7 @@ nofetch=
3232
update=
3333
prefix=
3434
custom_name=
35+
depth=
3536

3637
# The function takes at most 2 arguments. The first argument is the
3738
# URL that navigates to the submodule origin repo. When relative, this URL
@@ -211,6 +212,7 @@ module_clone()
211212
name=$2
212213
url=$3
213214
reference="$4"
215+
depth="$5"
214216
quiet=
215217
if test -n "$GIT_QUIET"
216218
then
@@ -233,7 +235,7 @@ module_clone()
233235
mkdir -p "$gitdir_base"
234236
(
235237
clear_local_git_env
236-
git clone $quiet -n ${reference:+"$reference"} \
238+
git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
237239
--separate-git-dir "$gitdir" "$url" "$sm_path"
238240
) ||
239241
die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
@@ -309,6 +311,14 @@ cmd_add()
309311
custom_name=$2
310312
shift
311313
;;
314+
--depth)
315+
case "$2" in '') usage ;; esac
316+
depth="--depth=$2"
317+
shift
318+
;;
319+
--depth=*)
320+
depth=$1
321+
;;
312322
--)
313323
shift
314324
break
@@ -405,7 +415,7 @@ Use -f if you really want to add it." >&2
405415
echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
406416
fi
407417
fi
408-
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
418+
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
409419
(
410420
clear_local_git_env
411421
cd "$sm_path" &&
@@ -676,6 +686,14 @@ cmd_update()
676686
--checkout)
677687
update="checkout"
678688
;;
689+
--depth)
690+
case "$2" in '') usage ;; esac
691+
depth="--depth=$2"
692+
shift
693+
;;
694+
--depth=*)
695+
depth=$1
696+
;;
679697
--)
680698
shift
681699
break
@@ -735,7 +753,7 @@ Maybe you want to use 'update --init'?")"
735753

736754
if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
737755
then
738-
module_clone "$sm_path" "$name" "$url" "$reference" || exit
756+
module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
739757
cloned_modules="$cloned_modules;$name"
740758
subsha1=
741759
else

t/t7400-submodule-basic.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,4 +868,19 @@ test_expect_success 'submodule deinit fails when submodule has a .git directory
868868
test -n "$(git config --get-regexp "submodule\.example\.")"
869869
'
870870

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+
871886
test_done

t/t7406-submodule-update.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,24 @@ test_expect_success 'submodule update properly revives a moved submodule' '
685685
test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
686686
mkdir -p linked/dir &&
687687
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+
)
695693
)
696694
'
697695

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+
'
698708
test_done

0 commit comments

Comments
 (0)