hooks: set up sparse checkout after cloning the vcsh repository
[config/vcsh.git] / .config / vcsh / hooks-available / populate-fully.sh
1 #!/bin/sh
2
3 set -e
4
5 # If the vcsh repository does not exist, exit without doing anything.
6 [ -d "$GIT_DIR" ] || exit 0
7
8 # Only one vcsh instance at a time can have the working tree fully populated.
9 LOCKDIR=/run/lock/vcsh
10
11 # If mkdir fails it means that the lock dir already exists and the lock is
12 # already active.
13 if ! mkdir "$LOCKDIR" 2>/dev/null;
14 then
15   echo "An instance of vcsh already entered a repository." 1>&2
16
17   # Exit vcsh if the process which keeps the lock is still alive.
18   if kill -0 "$(cat "$LOCKDIR/pid")" 2>/dev/null;
19   then
20     # Kill the parent process instead of just bailing out because vcsh does
21     # not catch the hook exit value.
22     # See: https://github.com/RichiH/vcsh/issues/251
23     kill -- -$PPID
24   else
25     echo "The instance which entered the repository earlier does not exist anymore." 1>&2
26     echo "Continuing anyway." 1>&2
27   fi
28 fi
29
30 # Lock on the parent pid because the hooks are launched as children of vcsh.
31 echo $PPID > "$LOCKDIR/pid"
32
33 # Verify if the current branch is valid before updating the working tree.
34 # This avoids errors with empty repositories which would only confuse the
35 # user.
36 if git rev-parse --verify HEAD >/dev/null 2>&1;
37 then
38   # git read-tree manual page says this is the proper way to fully repopulate
39   # the working tree.
40   git config core.sparseCheckout true
41   rm -f "$GIT_DIR/info/sparse-checkout"
42   echo "/*" > "$GIT_DIR/info/sparse-checkout"
43   RET=0
44   git read-tree -mu HEAD || RET=$?
45   git config core.sparseCheckout false
46
47   # if updating the working tree failed exit the whole vcsh process to prevent
48   # entering the repository.
49   if [ $RET -ne 0 ];
50   then
51     echo "Fix the problems before entering the repository." 1>&2
52     kill -- -$PPID
53   fi
54 fi