hooks-available: avoid some errors with empty repositories
[config/vcsh.git] / .config / vcsh / hooks-available / populate-sparsely.sh
1 #!/bin/sh
2
3 set -e
4
5 # Only the same instance of vcsh that had the working tree fully populated is
6 # allowed to repopulate it sparsely.
7 LOCKDIR=/run/lock/vcsh
8
9 # If LOCKDIR does not exist it means that the lock is not active so there's no
10 # need to do anything.
11 [ -d "$LOCKDIR" ] || exit 0
12
13 LOCKPID=$(cat "$LOCKDIR/pid")
14 # Use the parent pid because the hooks are launched as children of vcsh.
15 [ "$LOCKPID" = $PPID ] || { echo "Repository entered from another vcsh instance. Aborting." 1>&2; exit 1; }
16
17 : "${XDG_CONFIG_HOME:="$HOME/.config"}"
18 . "$XDG_CONFIG_HOME/vcsh/hooks-available/sparse-checkout.sh"
19
20 # Verify if the current branch is valid before updating the working tree.
21 # This avoids errors with empty repositories which would only confuse the
22 # user.
23 if git rev-parse --verify HEAD >/dev/null 2>&1;
24 then
25   git read-tree -mu HEAD
26 fi
27
28 # Unlock the working tree.
29 rm -rf "$LOCKDIR"