git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Add git-submodule command

From
LHLars Hjemli <hjemli@gmail.com>
Date
May 25, 2007, 13:46 UTC
Message-ID
<8c5c35580705250646h12f4f30bt301f4c4bdc2ad530@mail.gmail.com>
In-Reply-To
<Pine.LNX.4.64.0705251157450.4648@racer.site>
On 5/25/07, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
Show 9 quoted lines
> Hi,
>
> On Fri, 25 May 2007, Lars Hjemli wrote:
>
> > There is currently no way to override the mappings in the .gitmodules
> > file, except by manually creating the subproject repository.
>
> I think that is okay. We can add that easily at a later stage, and the
> script is much easier without that logic.

Yes. And it is sort of a feature: if you've cloned the submodule using a different (aka local) url, 'git submodule --init' will leave it alone.

>
> > diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
>
> Looks good here, I checked with asciidoc.
Thanks.
Show 43 quoted lines
>
> > diff --git a/git-submodule.sh b/git-submodule.sh
> > new file mode 100755
> > index 0000000..c4a1cc3
> > --- /dev/null
> > +++ b/git-submodule.sh
> > @@ -0,0 +1,163 @@
> > +#!/bin/sh
> > +#
> > +# git-submodule.sh: init, update or list git submodules
> > +#
> > +# Copyright (c) 2007 Lars Hjemli
> > +
> > +USAGE='[-i | --init | -u | --update] [-q | --quiet] [--cached] <path>...'
> > +. git-sh-setup
> > +require_work_tree
> > +
> > +init=
> > +update=
> > +quiet=
> > +cached=
> > +
> > +#
> > +# print stuff on stdout unless -q was specified
> > +#
> > +say()
> > +{
> > +     if test -z "$quiet"
> > +     then
> > +             echo -e "$@"
> > +     fi
> > +}
> > +
> > +#
> > +# Find all (requested) submodules, run clone + checkout on missing paths
> > +#
> > +# $@ = requested paths (default to all)
> > +#
> > +modules_init()
> > +{
> > +     git ls-files --stage -- $@ | grep -e '^160000 ' |
>
> Any reason you read in the stage? It does not seem that you use it.
Are there any other way to get the mode info?
Show 55 quoted lines
>
> > +     while read mode sha1 stage path
> > +     do
> > +             test -d "$path/.git" && continue
> > +
> > +             if test -d "$path"
> > +             then
> > +                     rmdir "$path" 2>/dev/null ||
> > +                     die "Directory '$path' exist, but not as a submodule"
> > +             fi
> > +
> > +             test -e "$path" &&
> > +             die "A file already exist at path '$path'"
> > +
> > +             url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
>
> I like that command ;-)
>
> > +             test -z "$url" &&
> > +             die "No url found for submodule '$path' in .gitmodules"
> > +
> > +             git-clone "$url" "$path" ||
> > +             die "Clone of submodule '$path' failed"
> > +
> > +             $(cd "$path" && git-checkout -q "$sha1") ||
> > +             die "Checkout of submodule '$path' failed"
> > +
> > +             say "Submodule '$path' initialized"
> > +     done
> > +}
> > +
> > +#
> > +# Checkout correct revision of each initialized submodule
> > +#
> > +# $@ = requested paths (default to all)
> > +#
> > +modules_update()
> > +{
> > +     git ls-files --stage -- $@ | grep -e '^160000 ' |
>
> Same here.
>
> > +     while read mode sha1 stage path
> > +     do
> > +             if ! test -d "$path/.git"
> > +             then
> > +                     say "Submodule '$path' not initialized"
> > +                     continue;
> > +             fi
> > +             subsha1=$(cd "$path" && git-rev-parse --verify HEAD) ||
>
> Maybe it would be a better idea to use "git --git-dir="$path" rev-parse
> ..."? Just in case somebody calls this with GIT_DIR overridden...
>
> Or, unset GIT_DIR explicitely.

Hmm, that's annoying (overridden GIT_DIR). I guess 'git --git-dir $path/.git' would be the easiest solution.

Show 10 quoted lines
>
> > +             die "Unable to find current revision of submodule '$path'"
> > +
> > +             if test "$subsha1" != "$sha1"
> > +             then
> > +                     $(cd "$path" && git-fetch && git-checkout -q "$sha1") ||
>
> This will make a detached HEAD, right? Do you want that? (I am not really
> interested in submodules myself, so I haven't thought about it, and I
> haven't followed that monster discussion.)

Well, we might want to be smarter about this, but on the other hand: if the user cares, he can always do 'cd $path && git checkout $branch', since 'git submodule -u' will skip submodules with the correct commit checked out.

Show 7 quoted lines
>
> > +                     die "Unable to checkout revision $sha1 of submodule '$path'"
> > +
> > +                     say "Submodule '$path' reset to revision $sha1"
>
> I'd rather not say "reset", since this has a different meaning in Git, but
> rather "set to revision $sha1".
Ok.
Show 81 quoted lines
>
> > +             fi
> > +     done
> > +}
> > +
> > +#
> > +# List all registered submodules, prefixed with:
> > +#  - submodule not initialized
> > +#  + different version checked out
> > +#
> > +# If --cached was specified the revision in the index will be printed
> > +# instead of the currently checked out revision.
> > +#
> > +# $@ = requested paths (default to all)
> > +#
> > +modules_list()
> > +{
> > +     git ls-files --stage -- $@ | grep -e '^160000 ' |
> > +     while read mode sha1 stage path
> > +     do
> > +             if ! test -d "$path/.git"
> > +             then
> > +                     say "-$sha1 $path"
> > +                     continue;
> > +             fi
> > +             revname=$(cd "$path" && git-describe $sha1)
> > +             if git diff-files --quiet -- "$path"
> > +             then
> > +                     say " $sha1 $path\t($revname)"
> > +             else
> > +                     if test -z "$cached"
> > +                     then
> > +                             sha1=$(cd "$path" && git-rev-parse --verify HEAD)
> > +                             revname=$(cd "$path" && git-describe $sha1)
> > +                     fi
> > +                     say "+$sha1 $path\t($revname)"
> > +             fi
> > +     done
> > +}
> > +
> > +
> > +while case "$#" in 0) break ;; esac
> > +do
> > +     case "$1" in
> > +     -i|--init)
> > +             init=1
> > +             ;;
> > +     -u|--update)
> > +             update=1
> > +             ;;
> > +     -q|--quiet)
> > +             quiet=1
> > +             ;;
> > +     --cached)
> > +             cached=1
> > +             ;;
> > +     --)
> > +             break
> > +             ;;
> > +     -*)
> > +             usage
> > +             ;;
> > +     *)
> > +             break
> > +             ;;
> > +     esac
> > +     shift
> > +done
> > +
> > +
> > +if test "$init" = "1"
> > +then
> > +     modules_init $@
> > +elif test "$update" = "1"
> > +then
> > +     modules_update $@
> > +else
> > +     modules_list $@
> > +fi
>
> I'll let Junio comment on that command line parsing...
Heh, I'm a shell illiterate...
>
> All in all, I like it: it is short, to the point, and it should do the job
> (maybe with a few enhancements like "--update" without arguments means
> _all_ submodules).

Well, it does (or should) update all initialized submodules, but maybe that's not what you meant?

Thanks for the review!

-- larsh

Previous: Johannes SchindelinNext: Johannes Schindelin
Message 4 of 26 in “Add git-submodule command”
  1. Add git-submodule commandLars Hjemli, May 25, 2007
  2. Johannes SchindelinMay 25, 2007
  3. Johannes SchindelinMay 25, 2007
  4. Lars HjemliMay 25, 2007
  5. Johannes SchindelinMay 25, 2007
  6. Lars HjemliMay 25, 2007
  7. Johannes SchindelinMay 25, 2007
  8. Lars HjemliMay 25, 2007
  9. Add git-submodule commandLars Hjemli, May 25, 2007
  10. Johannes SchindelinMay 25, 2007
  11. Junio C HamanoMay 25, 2007
  12. Lars HjemliMay 25, 2007
  13. Junio C HamanoMay 25, 2007
  14. Lars HjemliMay 25, 2007
  15. Add git-submodule commandLars Hjemli, May 26, 2007
  16. Johannes SchindelinMay 26, 2007
  17. Junio C HamanoMay 26, 2007
  18. Lars HjemliMay 26, 2007
  19. Johannes SchindelinMay 26, 2007
  20. Add git-submodule commandLars Hjemli, May 26, 2007
  21. Simon HausmannMay 26, 2007
  22. Lars HjemliMay 26, 2007
  23. Johannes SchindelinMay 25, 2007
  24. Linus TorvaldsMay 25, 2007
  25. Johannes SchindelinMay 25, 2007
  26. Lars HjemliMay 25, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.