11

git submodule update has supported the --depth option as described in this answer.

But still we can't easily determine the depth value, which would probably make git unable to find the intended revision of a submodule.

Is there a true solution for updating submodules shallowly?

Community
  • 1
  • 1
Bohr
  • 1,566
  • 1
  • 16
  • 21
  • 1
    It shouldn't prevent git to fetch the right depth. But if it was, then same comment as in http://stackoverflow.com/questions/24294361/make-a-shallow-git-repository-less-shallow#comment37554751_24294361: (for git 1.9.x+) Did you try a git fetch --update-shallow in it (through a custom command/update maybe: http://stackoverflow.com/a/17693008/6309). – VonC Jun 19 '14 at 10:02
  • I would first test the `git submodule update --depth` first: it should be able to update to the right depth, depending on recorded SHA1. – VonC Jun 19 '14 at 10:03
  • As of git 2.0.0, `--depth` must be followed by a value. So I think the exact depth would still be a myth, which is the only and must be resolved concern of the question. Thanks for help @VonC – Bohr Jun 19 '14 at 12:04

1 Answers1

6

So I think the exact depth would still be a myth, which is the only and must be resolved concern of the question

While there is indeed no "exact depth", you can still record a "depth recommendation" with git 2.9.x+ (Q3 2016).

See commit abed000, commit 37f52e9 (26 May 2016) by Stefan Beller (stefanbeller).
(Merged by Junio C Hamano -- gitster -- in commit 3807098, 20 Jun 2016)

submodule update: learn --[no-]recommend-shallow option

An upstream project can make a recommendation to shallowly clone some submodules in the .gitmodules file it ships.

Sometimes the history of a submodule is not considered important by the projects upstream.
To make it easier for downstream users, allow a boolean field 'submodule.<name>.shallow' in .gitmodules, which can be used to recommend whether upstream considers the history important.

This field is honored in the initial clone by default, it can be ignored by giving the --no-recommend-shallow option.

That way, a simple git submodule update (no additional parameters) will use the recommended depth value, if found.

See also "Git submodule without extra weight" with:

git config -f .gitmodules submodule.<name>.shallow true

g19fanatic proposes in the comments:

quick 1-liner to add shallow=true to all submodules:

git submodule | awk '{print $2}' | \
  xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true'

or, with git submodule foreach:

git submodule foreach 'git config -f .gitmodules submodule.$sm_path.shallow true'

However, akhan adds in the comments:

The foreach command is executed from the submodules and hence will not work as described here (try git submodule foreach 'echo $PWD' for instance).

However the xargs works just fine.

Working recipe:

git submodule | awk '{print $2}' | \
 xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true' 
git submodule update --depth=1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • quick 1-liner to add shallow=true to all submodules `git submodule | awk '{print $2}' | xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true'` – g19fanatic Sep 22 '20 at 11:45
  • @g19fanatic Thank you. That should come in handy indeed. I have included your comment in the answer for more visibility. – VonC Sep 22 '20 at 11:54
  • @g19fanatic But I believe using [`git submodule foreach`](https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-foreach--recursiveltcommandgt) is safer and does not need to rely on awk and other parsing tricks. – VonC Sep 22 '20 at 11:55
  • probably right. never really used foreach but awk/xargs are in my utility belt. for completeness `git submodule foreach 'git config -f .gitmodules submodule.$sm_path.shallow true'` – g19fanatic Sep 22 '20 at 12:29
  • @g19fanatic And yet, as they say: "[This is the way](https://www.magicalquote.com/seriesquotes/this-is-the-way/)". – VonC Sep 22 '20 at 12:34
  • @VonC The `foreach` command is executed from the submodules and hence will not work as described here (try `git submodule foreach 'echo $PWD'` for instance). However the xargs works just fine. Working recipe: `git submodule | awk '{print $2}' | xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true'` followed by `git submodule update --depth=1` – akhan Apr 11 '23 at 21:39
  • @akhan Good point, thank you. I have included your comment in the answer for more visibility. – VonC Apr 11 '23 at 22:15