new tool: args2env
[hband-tools.git] / user-tools / git-submodule-auto-add
blob3214157bbeef8b71e044aa806b0910155f6fb01b
1 #!/bin/bash
3 true <<'EOF'
4 =pod
6 =head1 NAME
8 git-submodule-auto-add - Automatically add submodules to a git repo according to .gitmodules file
10 =head1 SYNOPSIS
12 git submodule-auto-add [I<OPTIONS>]
14 =head1 OPTIONS
16 Those which git-submodule(1) B<add> accepts.
18 =head1 DESCRIPTION
20 Call as many C<git submodule add ...> commands as many submodules are defined in
21 F<.gitmodules> file in the current repo's root.
22 Automatically adding submodules this way.
24 An extra feature is to able to define on which name the submodule's remote should
25 be called ("origin" or the tracking remote of superproject's current branch,
26 see git-submodule(1) for details). Add B<remotename> option to the submodule's section
27 in F<.gitmodules> to achieve this.
29 =head1 CAVEATS
31 Does not fail if a submodule can not be added, but continues with the next one.
33 =cut
35 EOF
38 set -e -o pipefail
39 set -u
42 reporoot=`git rev-parse --show-toplevel`
44 _gitconf()
46 git config -f "$reporoot"/.gitmodules --get "$1" || true
49 git config -f "$reporoot"/.gitmodules --get-regexp '^submodule\..*\.path$' |\
50 while read -r path_key local_path
52 set +e +o pipefail
54 set -e -o pipefail
56 url_key=${path_key/.path/.url}
57 branch_key=${path_key/.path/.branch}
59 url=`_gitconf "$url_key"`
60 branch=`_gitconf "$branch_key"`
62 gitparams=(submodule add ${branch:+-b "$branch"} "$@" "$url" "$local_path")
63 echo "+ git ${gitparams[*]}" >&2
64 git "${gitparams[@]}"
66 # set the submodule's origin remote name to the one configured
67 # in submodule.<SM_NAME>.remotename - if any
69 remotename_key=${path_key/.path/.remotename}
70 remotename=`_gitconf "$remotename_key"`
72 if [ -n "$remotename" ]
73 then
74 local_path=$local_path remotename=$remotename git submodule foreach '
75 [ "$sm_path" != "$local_path" ] && exit;
76 git remote | grep -qFx "$remotename" && exit;
77 git remote rename origin "$remotename";
81 done