submodule-update.sh: check git version before running submodule commands
authorAntonio Ospite <ao2@ao2.it>
Mon, 17 Dec 2018 09:03:10 +0000 (10:03 +0100)
committerAntonio Ospite <ao2@ao2.it>
Mon, 17 Dec 2018 09:37:45 +0000 (10:37 +0100)
Git submodules in vcsh can only be supported reliably with git >= 2.20.0
which supports reading the .gitmodules file even when it is not checked
out in the working tree.

To recap, the need for git >= 2.20 is imposed by the following facts:

  - vcsh repositories share their working trees in $HOME;

  - if files with the same name (e.g. README, LICENSE) are present in
    different repositories they would conflict when checked out in
    $HOME;

  - sparse-checkout can solve the issue by preventing colliding files
    between repositories from being checked out, this includes the
    .gitmodules file;

  - submodules command require the .gitmodules file;

  - from git 2.20 the .gitmodules file can be accessed from the
    repository object store when the file is not in the working tree,
    this enables submodules usage with "vcsh run".

Now that the version check is in place, re-enable the post-clone hook to
update submodules automatically after cloning a repository.

.config/vcsh/hooks-available/submodule-update.sh
.config/vcsh/hooks-enabled/post-clone.00-submodule-init [new symlink]

index 34793ed..3f68fbc 100755 (executable)
@@ -1,4 +1,15 @@
 #!/bin/sh
 
+set -e
+
+GIT_VERSION_MAJOR=$(git --version | sed -n 's/.* \([0-9]\{1,\}\)\..*/\1/p' )
+GIT_VERSION_MINOR=$(git --version | sed -n 's/.* \([0-9]\{1,\}\)\.\([0-9]\{1,\}\).*/\2/p' )
+
+if [ "$GIT_VERSION_MAJOR" -lt 2 ] || [ "$GIT_VERSION_MINOR" -lt 20 ];
+then
+  echo "Git >= 2.20 is required for submodules to work properly with vcsh" 1>&2
+  exit 1
+fi
+
 git submodule sync --recursive
 git submodule update --init --recursive
diff --git a/.config/vcsh/hooks-enabled/post-clone.00-submodule-init b/.config/vcsh/hooks-enabled/post-clone.00-submodule-init
new file mode 120000 (symlink)
index 0000000..fc302b7
--- /dev/null
@@ -0,0 +1 @@
+../hooks-available/submodule-update.sh
\ No newline at end of file