Skip to content

Commit e67f0f6

Browse files
committed
Utilize common script when possible
To consolidate the bash scripts, the commonly used functions were moved to the common script and all bash scripts should do 'source' before performing. Change-Id: Ic9a72b9a8d6fd4cd74b4e81a31d312a085a8426b
1 parent 7db0d54 commit e67f0f6

File tree

8 files changed

+109
-71
lines changed

8 files changed

+109
-71
lines changed

scripts/check-commitlog.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# Check that every non-merge commit after the specified base commit has a commit
44
# message ending with a valid Change-Id line. A valid Change-Id line must be the
@@ -8,6 +8,16 @@
88
#
99
# Merge commits are excluded from this check.
1010

11+
# Ensure that the common script exists and is readable, then verify it has no
12+
# syntax errors and defines the required function.
13+
common_script="$(dirname "$0")/common.sh"
14+
[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; }
15+
bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; }
16+
source "$common_script"
17+
declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; }
18+
19+
set_colors
20+
1121
# Base commit from which to start checking.
1222
BASE_COMMIT="0b8be2c15160c216e8b6ec82c99a000e81c0e429"
1323

@@ -33,10 +43,7 @@ for commit in $commits; do
3343
done
3444

3545
if [ $failed -ne 0 ]; then
36-
echo
37-
echo "Some commits are missing a valid Change-Id. Please amend the commit messages accordingly."
38-
echo
39-
exit 1
46+
throw "Some commits are missing a valid Change-Id. Please amend the commit messages accordingly."
4047
fi
4148

4249
exit 0

scripts/check-repo.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/usr/bin/env bash
22

3-
# Source the common utilities
4-
source "$(dirname "$0")/common.sh"
3+
# Ensure that the common script exists and is readable, then verify it has no
4+
# syntax errors and defines the required function.
5+
common_script="$(dirname "$0")/common.sh"
6+
[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; }
7+
bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; }
8+
source "$common_script"
9+
declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; }
10+
11+
set_colors
512

613
check_github_actions
714

scripts/checksums

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
398b59c23f699ff1bf1e73a94503a9a91caa9207 queue.h
22
9be9666430f392924f5d27caa71a412527bf9267 list.h
3-
3bb0192cee08d165fd597a9f6fbb404533e28fcf scripts/check-commitlog.sh
3+
1029c2784b4cae3909190c64f53a06cba12ea38e scripts/check-commitlog.sh

scripts/commit-msg.hook

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,20 @@ HOOK_EDITOR=
1111
SKIP_DISPLAY_WARNINGS=0
1212
WARNINGS=
1313

14-
RED=
15-
YELLOW=
16-
BLUE=
17-
WHITE=
18-
CYAN=
19-
NC=
20-
21-
# Set colour variables if the output should be coloured.
22-
set_colors() {
23-
local default_color=$(git config --get hooks.goodcommit.color || git config --get color.ui || echo 'auto')
24-
if [[ $default_color == 'always' ]] || [[ $default_color == 'auto' && -t 1 ]]; then
25-
RED='\033[1;31m'
26-
YELLOW='\033[1;33m'
27-
BLUE='\033[1;34m'
28-
WHITE='\033[1;37m'
29-
CYAN='\033[1;36m'
30-
NC='\033[0m' # No Color
31-
fi
32-
}
14+
# Ensure that the common script exists and is readable, then verify it has no
15+
# syntax errors and defines the required function.
16+
common_script="$(dirname "$0")/../../scripts/common.sh"
17+
[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; }
18+
bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; }
19+
source "$common_script"
20+
declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; }
3321

3422
# Set the hook editor, using the same approach as git.
3523
set_editor() {
36-
# $GIT_EDITOR appears to always be set to `:` when the hook is executed by Git?
37-
# ref: http://stackoverflow.com/q/41468839/885540
38-
# ref: https://github.com/tommarshall/git-good-commit/issues/11
24+
# $GIT_EDITOR appears to always be set to ':' when the hook is executed by Git?
25+
# Reference:
26+
# - http://stackoverflow.com/q/41468839/885540
27+
# - https://github.com/tommarshall/git-good-commit/issues/11
3928
# HOOK_EDITOR=$GIT_EDITOR
4029
test -z "${HOOK_EDITOR}" && HOOK_EDITOR=$(git config --get core.editor)
4130
test -z "${HOOK_EDITOR}" && HOOK_EDITOR=$VISUAL

scripts/common.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,3 @@ make_random_string() {
102102
echo "${raw_rand:0:pos}${sub_str}${raw_rand:pos}"
103103
fi
104104
}
105-
106-
set_colors

scripts/install-git-hooks

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
#!/usr/bin/env bash
22

3+
# Ensure that the common script exists and is readable, then verify it has no
4+
# syntax errors and defines the required function.
5+
common_script="$(dirname "$0")/common.sh"
6+
[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; }
7+
bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; }
8+
source "$common_script"
9+
declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; }
10+
11+
set_colors
12+
13+
TOTAL_STEPS=5
14+
CURRENT_STEP=0
15+
16+
# 1. Validate the workspace
17+
((CURRENT_STEP++))
18+
progress "$CURRENT_STEP" "$TOTAL_STEPS"
19+
320
if ! test -d .git; then
4-
echo "Execute scripts/install-git-hooks in the top-level directory."
5-
exit 1
21+
throw "Execute scripts/install-git-hooks in the top-level directory."
22+
fi
23+
24+
workspace=$(git rev-parse --show-toplevel)
25+
if [ ! -d "$workspace" ]; then
26+
throw "The workspace path '$workspace' contains non-ASCII characters."
627
fi
728

29+
# 2. Check GitHub account
30+
((CURRENT_STEP++))
31+
progress "$CURRENT_STEP" "$TOTAL_STEPS"
32+
833
ACCOUNT=$(git config -l | grep -w remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/lab0-c.*/\1/')
934

1035
CURL=$(which curl)
1136
if [ $? -ne 0 ]; then
12-
echo "[!] curl not installed." >&2
13-
exit 1
37+
throw "curl not installed."
1438
fi
1539

1640
CURL_RES=$(${CURL} -s \
@@ -19,44 +43,51 @@ https://api.github.com/repos/${ACCOUNT}/lab0-c/actions/workflows)
1943

2044
TOTAL_COUNT=$(echo ${CURL_RES} | sed -e 's/.*"total_count": \([^,"]*\).*/\1/')
2145
case ${TOTAL_COUNT} in
22-
*"Not Found"*)
23-
echo "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c"
24-
exit 1
46+
*"Not Found"*)
47+
throw "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c"
2548
esac
2649

50+
# 3. Ensure this repository is frok from sysprog21/lab0-c'.
51+
((CURRENT_STEP++))
52+
progress "$CURRENT_STEP" "$TOTAL_STEPS"
53+
2754
if [[ "${ACCOUNT}" != "sysprog21" ]]; then
28-
REPO_FORKED=$(${CURL} -s \
29-
-H "Accept: application/vnd.github.v3+json" \
30-
https://api.github.com/repos/${ACCOUNT}/lab0-c | grep -m 1 fork)
31-
case ${REPO_FORKED} in
32-
*true*)
33-
;;
34-
*)
35-
echo "Your repository MUST be forked from sysprog21/lab0-c"
36-
exit 1
37-
esac
55+
REPO_FORKED=$(${CURL} -s \
56+
-H "Accept: application/vnd.github.v3+json" \
57+
https://api.github.com/repos/${ACCOUNT}/lab0-c | grep -m 1 fork)
58+
case ${REPO_FORKED} in
59+
*true*)
60+
;;
61+
*)
62+
throw "Your repository MUST be forked from 'sysprog21/lab0-c'."
63+
esac
3864
fi
3965

66+
# 4. Check GitHub Actions
67+
((CURRENT_STEP++))
68+
progress "$CURRENT_STEP" "$TOTAL_STEPS"
69+
4070
if [ ${TOTAL_COUNT} -eq "0" ]; then
41-
echo "Fatal: GitHub Actions MUST be activated."
42-
case ${OSTYPE} in
43-
"linux"*)
44-
xdg-open https://github.com/${ACCOUNT}/lab0-c/actions > /dev/null 2>&1
45-
;;
46-
"darwin"*)
47-
open https://github.com/${ACCOUNT}/lab0-c/actions
48-
;;
49-
*)
50-
echo "Please activate at https://github.com/${ACCOUNT}/lab0-c/actions"
51-
;;
52-
esac
53-
echo "Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow"
54-
echo "Then, execute 'make' again."
55-
exit 1
56-
else
57-
echo "GitHub Actions has been activated"
71+
printf "\n[!] GitHub Actions MUST be activated."
72+
case ${OSTYPE} in
73+
"linux"*)
74+
xdg-open https://github.com/${ACCOUNT}/lab0-c/actions > /dev/null 2>&1
75+
;;
76+
"darwin"*)
77+
open https://github.com/${ACCOUNT}/lab0-c/actions
78+
;;
79+
*)
80+
echo "Please activate at https://github.com/${ACCOUNT}/lab0-c/actions"
81+
;;
82+
esac
83+
throw "Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow\n\
84+
Then, execute 'make' again."
5885
fi
5986

87+
# 5. Install Git hooks
88+
((CURRENT_STEP++))
89+
progress "$CURRENT_STEP" "$TOTAL_STEPS"
90+
6091
mkdir -p .git/hooks
6192

6293
ln -sf ../../scripts/pre-commit.hook .git/hooks/pre-commit || exit 1

scripts/pre-push.hook

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env bash
22

3+
# Ensure that the common script exists and is readable, then verify it has no
4+
# syntax errors and defines the required function.
5+
common_script="$(dirname "$0")/../../scripts/common.sh"
6+
[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; }
7+
bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; }
8+
source "$common_script"
9+
declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; }
10+
11+
set_colors
12+
313
protected_branch='master'
414
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
5-
RED='\033[0;31m'
6-
GREEN='\033[1;32m'
7-
YELLOW='\033[1;33m'
8-
NC='\033[0m' # No Color
915

1016
# Validate repository
1117
# commit 50c5ac53d31adf6baac4f8d3db6b3ce2215fee40

scripts/prepare-commit-msg.hook

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
COMMIT_MSG_FILE="$1"
44

0 commit comments

Comments
 (0)