blob: 7de0d3fbd919d5950f1e801f527ae1f76290e4d9 [file] [log] [blame]
#!/bin/bash
#
# Copyright 2022 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Installs git hooks for this repo into .git/hooks.
#
# Usage:
# install_hooks.sh
set -e # Fail on any error.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/helpers.sh || exit $?
ensure-embedder-dir
# Ensure the hooks were marked executable before copying them over,
# or git will fail to execute them.
chmod +x $FUCHSIA_EMBEDDER_DIR/hooks/*
# Install dependency for the hooks.
cp $FUCHSIA_EMBEDDER_DIR/scripts/lib/helpers.sh $FUCHSIA_EMBEDDER_DIR/.git/hooks/helpers.sh
# Install the post-checkout hook for checkout, merge and rebase.
cp $FUCHSIA_EMBEDDER_DIR/hooks/post-checkout $FUCHSIA_EMBEDDER_DIR/.git/hooks/post-checkout
cp $FUCHSIA_EMBEDDER_DIR/hooks/post-checkout $FUCHSIA_EMBEDDER_DIR/.git/hooks/post-merge
cp $FUCHSIA_EMBEDDER_DIR/hooks/post-checkout $FUCHSIA_EMBEDDER_DIR/.git/hooks/post-rewrite
# post-rewrite gets fired for `amend` as well, so add a line that
# exits the post-rewrite hook immediately for anything but 'rebase'.
sed -i -e '6iif [[ "$1" != "rebase" ]]; then exit 0; fi;\' $FUCHSIA_EMBEDDER_DIR/.git/hooks/post-rewrite
# Install the pre-commit hook.
cp $FUCHSIA_EMBEDDER_DIR/hooks/pre-commit $FUCHSIA_EMBEDDER_DIR/.git/hooks/pre-commit
# Install Gerrit's commit-msg hook for code reviews.
if [[ ! -f `git -C $FUCHSIA_EMBEDDER_DIR rev-parse --git-dir`/hooks/commit-msg ]]
then
f=`git -C $FUCHSIA_EMBEDDER_DIR rev-parse --git-dir`/hooks/commit-msg ; mkdir -p $(dirname $f) ; curl -Lo $f https://gerrit-review.googlesource.com/tools/hooks/commit-msg ; chmod +x $f
fi