forked from square/spacecommander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-repo.sh
executable file
·66 lines (54 loc) · 2.02 KB
/
setup-repo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
# setup-repo.sh
# Used to configure a repo for formatting, and adds a precommit hook to check formatting.
# Copyright 2015 Square, Inc
set -ex
export CDPATH=""
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
pre_commit_file='.git/hooks/pre-commit';
function ensure_pre_commit_file_exists() {
if [ -e "$pre_commit_file" ]; then
return 0
fi
# It's a symlink
if [ -h "$pre_commit_file" ]; then
pre_commit_file=$(readlink "$pre_commit_file")
return 0
fi
if [ -d ".git" ]; then
$(mkdir -p ".git/hooks");
elif [ -e ".git" ]; then
# grab the git dir from our .git file, listed as 'gitdir: blah/blah/foo'
git_dir=$(grep gitdir .git | cut -d ' ' -f 2)
pre_commit_file="$git_dir/hooks/pre-commit"
# Even if our git dir is in an unusual place, we still need to create the hook directory
# if it does not already exist.
$(mkdir -p "$git_dir/hooks");
else
$(mkdir -p ".git/hooks");
fi
$(touch $pre_commit_file)
}
function ensure_pre_commit_file_is_executable() {
$(chmod +x "$pre_commit_file")
}
function ensure_hook_is_installed() {
# check if this repo is referenced in the precommit hook already
repo_path=$(git rev-parse --show-toplevel)
if ! grep -q "$repo_path" "$pre_commit_file"; then
echo "#!/usr/bin/env bash" >> $pre_commit_file
echo "current_repo_path=\$(git rev-parse --show-toplevel)" >> $pre_commit_file
echo "repo_to_format=\"$repo_path\"" >> $pre_commit_file
echo 'if [ "$current_repo_path" == "$repo_to_format" ]'" && [ -e \"$DIR\"/format-objc-hook ]; then \"$DIR\"/format-objc-hook || exit 1; fi" >> $pre_commit_file
fi
}
function ensure_git_ignores_clang_format_file() {
grep -q ".clang-format" ".gitignore"
if [ $? -gt 0 ]; then
echo ".clang-format" >> ".gitignore"
fi
}
function symlink_clang_format() {
$(ln -sf "$DIR/.clang-format" ".clang-format")
}
ensure_pre_commit_file_exists && ensure_pre_commit_file_is_executable && ensure_hook_is_installed && ensure_git_ignores_clang_format_file && symlink_clang_format