Use git hook to re-initialize a subdirectory

Chris Coutinho chrisbcoutinho at gmail.com
Fri Mar 16 11:59:12 CET 2018


Hi Radon,

Thanks for the tip, you set me on the right track. Instead of using an
environmental variable, I alternated between making my hook executable and
not based on whether the specified sub-directory needed to be
reinitialized. This is what I came up with:

```
#!/bin/sh
#
# This git hook makes it easy to re-initialize password-stores that are
# shared between multiple computers/users. After each commit, this hook
# re-encrypts passwords in the $SUBDIR sub-directory.
#
# Copy the hook to .git/hooks/post-commit

gitdir="$(git rev-parse --git-dir)"
hook="$gitdir/hooks/post-commit"

# Make hook executable by default
chmod +x $hook

# Loop over sub-directories
for SUBDIR in 'subdir1' 'subdir2'; do
    echo "=== Checking if last commit contained keys/commits in $SUBDIR"
    if git show --name-only | grep "^$SUBDIR"
    then
        echo "=== Found a key/commit in $SUBDIR"
        echo "=== Running pass..."

        # Disable post-commit temporarily, this trick is from:
        #
https://coderwall.com/p/dv-dgg/bypass-post-commit-hook-temporarily
        [ -x $hook ] && chmod -x $hook

        pass init -p $SUBDIR $(cat $HOME/.password-store/$SUBDIR/.gpg-id)
        git commit --all --amend #--no-verify
        echo "=== Done running pass..."

        # Re-enable post-commit
        chmod +x $hook
        exit 0
    fi
    echo "=== No keys/commits associated with $SUBDIR"
done
```


On Thu, 15 Mar 2018 at 22:13 Radon Rosborough <radon.neon at gmail.com> wrote:

> > This doesn't work because it goes into an infinite loop.
>
> I'm no expert on Git hooks, but can't you set an environment variable
> when you run pass-init, and then have your hook script no-op if that
> variable is set?
>
> Best,
> Radon
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.zx2c4.com/pipermail/password-store/attachments/20180316/bc1882e0/attachment-0001.html>


More information about the Password-Store mailing list