From e7109b26ca3742d660d77df5a94326297f00eaa0 Mon Sep 17 00:00:00 2001
From: Antonio Ospite <ao2@ao2.it>
Date: Tue, 8 May 2018 09:48:34 +0200
Subject: [PATCH] Initial import

---
 .config/vcsh/hooks-available/populate-fully.sh      | 21 +++++++++++++++++++++
 .config/vcsh/hooks-available/populate-sparsely.sh   | 17 +++++++++++++++++
 .config/vcsh/hooks-available/sparse-checkout.sh     |  7 +++++++
 .config/vcsh/hooks-available/submodule-update.sh    |  4 ++++
 .../vcsh/hooks-enabled/post-clone.00-submodule-init |  1 +
 .../hooks-enabled/post-enter.00-populate-sparsely   |  1 +
 .../vcsh/hooks-enabled/post-pull.00-submodule-init  |  1 +
 .../vcsh/hooks-enabled/pre-enter.00-populate-fully  |  1 +
 .../hooks-enabled/pre-upgrade.00-sparse-checkout    |  1 +
 .config/vcsh/sparse-checkout                        |  6 ++++++
 .gitignore                                          | 17 +++++++++++++++++
 README                                              | 14 ++++++++++++++
 12 files changed, 91 insertions(+)
 create mode 100755 .config/vcsh/hooks-available/populate-fully.sh
 create mode 100755 .config/vcsh/hooks-available/populate-sparsely.sh
 create mode 100755 .config/vcsh/hooks-available/sparse-checkout.sh
 create mode 100755 .config/vcsh/hooks-available/submodule-update.sh
 create mode 120000 .config/vcsh/hooks-enabled/post-clone.00-submodule-init
 create mode 120000 .config/vcsh/hooks-enabled/post-enter.00-populate-sparsely
 create mode 120000 .config/vcsh/hooks-enabled/post-pull.00-submodule-init
 create mode 120000 .config/vcsh/hooks-enabled/pre-enter.00-populate-fully
 create mode 120000 .config/vcsh/hooks-enabled/pre-upgrade.00-sparse-checkout
 create mode 100644 .config/vcsh/sparse-checkout
 create mode 100644 .gitignore
 create mode 100644 README

diff --git a/.config/vcsh/hooks-available/populate-fully.sh b/.config/vcsh/hooks-available/populate-fully.sh
new file mode 100755
index 0000000..46e155f
--- /dev/null
+++ b/.config/vcsh/hooks-available/populate-fully.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+set -e
+
+# Only one vcsh instance at a time can have the work dir fully populated.
+LOCKDIR=/run/lock/vcsh
+
+# Kill the parent process because vcsh does not catch the hook exit value.
+# See: https://github.com/RichiH/vcsh/issues/251
+mkdir "$LOCKDIR" 2>/dev/null || { echo "An instance of vcsh already entered a repository." 1>&2; kill -- -$PPID;}
+
+# Lock on the parent pid because the hooks are launched as children of vcsh.
+echo $PPID > "$LOCKDIR/pid"
+
+# git read-tree manual page says this is the proper way to fully repopulate
+# the working directory
+git config core.sparseCheckout true
+rm -f "$GIT_DIR/info/sparse-checkout"
+echo "/*" > "$GIT_DIR/info/sparse-checkout"
+git read-tree -mu HEAD
+git config core.sparseCheckout false
diff --git a/.config/vcsh/hooks-available/populate-sparsely.sh b/.config/vcsh/hooks-available/populate-sparsely.sh
new file mode 100755
index 0000000..a49e4b4
--- /dev/null
+++ b/.config/vcsh/hooks-available/populate-sparsely.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+set -e
+
+# Only the same instance of vcsh that had the work dir fully populated is
+# allowed to repopulate it sparsely.
+LOCKDIR=/run/lock/vcsh
+LOCKPID=$(cat "$LOCKDIR/pid")
+# Use the parent pid because the hooks are launched as children of vcsh.
+[ "$LOCKPID" = $PPID ] || { echo "Repository entered from another vcsh instance. Aborting." 1>&2; exit 1; }
+
+: "${XDG_CONFIG_HOME:="$HOME/.config"}"
+. "$XDG_CONFIG_HOME/vcsh/hooks-available/sparse-checkout.sh"
+git read-tree -mu HEAD
+
+# Unlock the work dir.
+rm -rf "$LOCKDIR"
diff --git a/.config/vcsh/hooks-available/sparse-checkout.sh b/.config/vcsh/hooks-available/sparse-checkout.sh
new file mode 100755
index 0000000..b23909d
--- /dev/null
+++ b/.config/vcsh/hooks-available/sparse-checkout.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+: "${XDG_CONFIG_HOME:="$HOME/.config"}"
+
+git config core.sparseCheckout true
+rm -f "$GIT_DIR/info/sparse-checkout"
+ln -s "$XDG_CONFIG_HOME/vcsh/sparse-checkout" "$GIT_DIR/info/sparse-checkout"
diff --git a/.config/vcsh/hooks-available/submodule-update.sh b/.config/vcsh/hooks-available/submodule-update.sh
new file mode 100755
index 0000000..34793ed
--- /dev/null
+++ b/.config/vcsh/hooks-available/submodule-update.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+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
index 0000000..fc302b7
--- /dev/null
+++ b/.config/vcsh/hooks-enabled/post-clone.00-submodule-init
@@ -0,0 +1 @@
+../hooks-available/submodule-update.sh
\ No newline at end of file
diff --git a/.config/vcsh/hooks-enabled/post-enter.00-populate-sparsely b/.config/vcsh/hooks-enabled/post-enter.00-populate-sparsely
new file mode 120000
index 0000000..20dd6f6
--- /dev/null
+++ b/.config/vcsh/hooks-enabled/post-enter.00-populate-sparsely
@@ -0,0 +1 @@
+../hooks-available/populate-sparsely.sh
\ No newline at end of file
diff --git a/.config/vcsh/hooks-enabled/post-pull.00-submodule-init b/.config/vcsh/hooks-enabled/post-pull.00-submodule-init
new file mode 120000
index 0000000..fc302b7
--- /dev/null
+++ b/.config/vcsh/hooks-enabled/post-pull.00-submodule-init
@@ -0,0 +1 @@
+../hooks-available/submodule-update.sh
\ No newline at end of file
diff --git a/.config/vcsh/hooks-enabled/pre-enter.00-populate-fully b/.config/vcsh/hooks-enabled/pre-enter.00-populate-fully
new file mode 120000
index 0000000..8bdf4fb
--- /dev/null
+++ b/.config/vcsh/hooks-enabled/pre-enter.00-populate-fully
@@ -0,0 +1 @@
+../hooks-available/populate-fully.sh
\ No newline at end of file
diff --git a/.config/vcsh/hooks-enabled/pre-upgrade.00-sparse-checkout b/.config/vcsh/hooks-enabled/pre-upgrade.00-sparse-checkout
new file mode 120000
index 0000000..2a2ca43
--- /dev/null
+++ b/.config/vcsh/hooks-enabled/pre-upgrade.00-sparse-checkout
@@ -0,0 +1 @@
+../hooks-available/sparse-checkout.sh
\ No newline at end of file
diff --git a/.config/vcsh/sparse-checkout b/.config/vcsh/sparse-checkout
new file mode 100644
index 0000000..485e4a5
--- /dev/null
+++ b/.config/vcsh/sparse-checkout
@@ -0,0 +1,6 @@
+/*
+!/.gitattributes
+!/.gitignore
+!/.gitmodules
+!/LICENSE*
+!/README*
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8235482
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+*
+!/.config
+!/.config/vcsh
+!/.config/vcsh/hooks-available
+!/.config/vcsh/hooks-available/populate-fully.sh
+!/.config/vcsh/hooks-available/populate-sparsely.sh
+!/.config/vcsh/hooks-available/sparse-checkout.sh
+!/.config/vcsh/hooks-available/submodule-update.sh
+!/.config/vcsh/hooks-enabled
+!/.config/vcsh/hooks-enabled/post-clone.00-submodule-init
+!/.config/vcsh/hooks-enabled/post-enter.00-populate-sparsely
+!/.config/vcsh/hooks-enabled/post-pull.00-submodule-init
+!/.config/vcsh/hooks-enabled/pre-enter.00-populate-fully
+!/.config/vcsh/hooks-enabled/pre-upgrade.00-sparse-checkout
+!/.config/vcsh/sparse-checkout
+!/.gitignore
+!/README
diff --git a/README b/README
new file mode 100644
index 0000000..bc44f6e
--- /dev/null
+++ b/README
@@ -0,0 +1,14 @@
+vcsh configuration to handle collisions of files common to distinct vcsh
+repositories using the sparse-checkout functionality of git.
+
+The patterns for the colliding files are in .config/vcsh/sparse-checkout and
+a pre-upgrade hook is used to make vcsh repositories use it.
+
+Many git commands (e.g. git-merge, git-checkout) will automatically use the
+information in $GIT_DIR/info/sparse-checkout when dealing with files in the
+git work directory, so generally a calling git-read-tree explicitly is not
+needed.
+
+However when the content of $GIT_DIR/info/sparse-checkout changes it is
+necessary to call "git read-tree -mu HEAD" to update the content of the
+working directory.
-- 
2.1.4