#!/usr/bin/env sh

# It looks at the commits just brought in, and if
# one of them has the subject "lazyvim: " in the commit message,
# it runs the headless Neovim plugin restore command.

# figure out the old HEAD before the merge
old_head="$(git rev-parse HEAD@\{1\})"
new_head="$(git rev-parse HEAD)"

# scan every new commit for our magic string
git log --format='%H' "$old_head..$new_head" | while read -r sha; do
  subject=$(git log -1 --format='%s' "$sha")
  if echo "$subject" | grep -q -i "lazyvim: "; then
    echo "Restoring LazyVim plugins to lockfile state"
    git restore ./dot-config/nvim-lazyvim/lazy-lock.json
    nvim --headless "+Lazy! restore" +qa >/dev/null
    break
  fi
done
