Debian Codegraph @

Utilities/GitSetup/setup-hooks

raw
1 #!/usr/bin/env bash
2 #=============================================================================
3 # Copyright 2010-2012 Kitware, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # https://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #=============================================================================
17
18 # Run this script to set up local Git hooks for this project.
19
20 # Project configuration instructions:
21 #
22 # - Publish a "hooks" branch in the project repository such that
23 # clones will have "refs/remotes/origin/hooks".
24 #
25 # - Populate adjacent "config" file with:
26 # hooks.url = Repository URL publishing "hooks" branch
27 # hooks.branch = Repository branch instead of "hooks"
28
29 egrep-q() {
30 egrep "$@" >/dev/null 2>/dev/null
31 }
32
33 die() {
34 echo 1>&2 "$@" ; exit 1
35 }
36
37 # Make sure we are inside the repository.
38 cd "${BASH_SOURCE%/*}" &&
39
40 # Select a hooks branch.
41 if url=$(git config --get hooks.url); then
42 # Fetch hooks from locally configured repository.
43 branch=$(git config hooks.branch || echo hooks)
44 elif git for-each-ref refs/remotes/origin/hooks 2>/dev/null |
45 egrep-q 'refs/remotes/origin/hooks$'; then
46 # Use hooks cloned from origin.
47 url=.. && branch=remotes/origin/hooks
48 elif url=$(git config -f config --get hooks.url); then
49 # Fetch hooks from project-configured repository.
50 branch=$(git config -f config hooks.branch || echo hooks)
51 else
52 die 'This project is not configured to install local hooks.'
53 fi &&
54
55 # Populate ".git/hooks".
56 echo 'Setting up git hooks...' &&
57 git_dir=$(git rev-parse --git-dir) &&
58 mkdir -p "$git_dir/hooks" &&
59 cd "$git_dir/hooks" &&
60 if ! test -e .git; then
61 git init -q || die 'Could not run git init for hooks.'
62 fi &&
63 git fetch -q "$url" "$branch" &&
64 git reset -q --hard FETCH_HEAD || die 'Failed to install hooks'