Skip to content

Commit 4e284ae

Browse files
committed
Restructure pre-push hook
This commit improves repository validation with existence checks before verification and adds build output capture to help users diagnose failures quickly. Change-Id: Ie038aa7bf3365754cd80652437091e9de1137b4b
1 parent 2cdce75 commit 4e284ae

File tree

1 file changed

+94
-28
lines changed

1 file changed

+94
-28
lines changed

scripts/pre-push.hook

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

3+
# Validates repository integrity and runs build checks before pushing to master
4+
35
# Ensure that the common script exists and is readable, then verify it has no
46
# syntax errors and defines the required function.
57
common_script="$(dirname "$0")/../../scripts/common.sh"
@@ -11,46 +13,110 @@ declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not d
1113
set_colors
1214

1315
protected_branch='master'
14-
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
16+
current_branch=$(git symbolic-ref --short HEAD 2>/dev/null || echo "detached")
1517

16-
# Validate repository
17-
# commit 50c5ac53d31adf6baac4f8d3db6b3ce2215fee40
18+
# Validate repository integrity
19+
# Ensures this is a proper fork of the original lab0-c repository
20+
# Expected commit: 50c5ac53d31adf6baac4f8d3db6b3ce2215fee40
1821
# Author: Jim Huang <[email protected]>
1922
# Date: Thu Feb 20 05:20:55 2025 +0800
2023
# Bump copyright year
21-
commit=$(git rev-list --skip 1 --grep '^Bump copyright' 0b8be2c15160c216e8b6ec82c99a000e81c0e429...HEAD)
22-
if [ x"$commit" != x"50c5ac53d31adf6baac4f8d3db6b3ce2215fee40" ] ; then
23-
echo -e "${RED}ERROR${NC}: This repository is insane."
24-
echo -e "Make sure you did fork from https://github.com/sysprog21/lab0-c recently."
25-
echo ""
24+
validate_repository() {
25+
local expected_commit="50c5ac53d31adf6baac4f8d3db6b3ce2215fee40"
26+
local base_commit="0b8be2c15160c216e8b6ec82c99a000e81c0e429"
27+
28+
# Check if the expected commit exists in history
29+
if ! git rev-parse --verify "$expected_commit^{commit}" >/dev/null 2>&1; then
30+
echo -e "${RED}ERROR${NC}: Repository validation failed."
31+
echo -e "${YELLOW}Expected commit not found:${NC} $expected_commit"
32+
echo -e "Make sure you forked from https://github.com/sysprog21/lab0-c recently."
33+
echo ""
34+
return 1
35+
fi
36+
37+
# Verify the commit matches expected pattern
38+
local commit=$(git rev-list --skip 1 --grep '^Bump copyright' "$base_commit"...HEAD 2>/dev/null || true)
39+
if [ "$commit" != "$expected_commit" ]; then
40+
echo -e "${RED}ERROR${NC}: Repository history verification failed."
41+
echo -e "Make sure you forked from https://github.com/sysprog21/lab0-c recently."
42+
echo ""
43+
return 1
44+
fi
45+
46+
return 0
47+
}
48+
49+
# Run repository validation
50+
if ! validate_repository; then
2651
exit 1
2752
fi
2853

29-
# Show hints
30-
echo -e "${YELLOW}Hint${NC}: You might want to know why Git is always ${GREEN}asking for my password${NC}."
31-
echo -e " https://docs.github.com/en/get-started/getting-started-with-git/why-is-git-always-asking-for-my-password"
32-
echo ""
54+
# Show helpful hints for common issues
55+
show_hints() {
56+
echo -e "${YELLOW}Hint${NC}: If Git keeps asking for your password, see:"
57+
echo -e " https://docs.github.com/en/get-started/getting-started-with-git/why-is-git-always-asking-for-my-password"
58+
echo ""
59+
}
3360

34-
# only run this if you are pushing to master
35-
if [[ $current_branch = $protected_branch ]] ; then
36-
echo -e "${YELLOW}Running pre push to master check...${NC}"
61+
# Run build checks for protected branch
62+
run_build_checks() {
63+
echo -e "${YELLOW}Running pre-push checks for $protected_branch branch...${NC}"
64+
echo ""
65+
66+
# Clean previous build artifacts for fresh check
67+
echo -e "${YELLOW}Cleaning previous build...${NC}"
68+
make clean >/dev/null 2>&1 || true
3769

38-
echo -e "${YELLOW}Trying to build tests project...${NC}"
70+
echo -e "${YELLOW}Building project...${NC}"
3971

40-
# build the project
41-
make
72+
# Capture build output for better error reporting
73+
build_output=$(make 2>&1)
74+
build_result=$?
4275

43-
# $? is a shell variable which stores the return code from what we just ran
44-
rc=$?
45-
if [[ $rc != 0 ]] ; then
46-
echo -e "${RED}Failed to build the project, please fix this and push again${NC}"
76+
if [ $build_result -ne 0 ]; then
77+
echo -e "${RED}Build failed!${NC}"
78+
echo ""
79+
echo "Build output:"
80+
echo "============="
81+
echo "$build_output" | tail -20
82+
echo "============="
4783
echo ""
48-
exit $rc
84+
echo -e "${RED}Please fix build errors before pushing to $protected_branch${NC}"
85+
return 1
4986
fi
5087

51-
# Everything went OK so we can exit with a zero
52-
echo -e "${GREEN}Pre-push check passed!${NC}"
53-
echo ""
54-
fi
88+
echo -e "${GREEN}✓ Build successful${NC}"
89+
90+
# Additional checks could be added here
91+
# For example: basic tests, format checks, etc.
92+
93+
return 0
94+
}
95+
96+
# Main execution
97+
main() {
98+
show_hints
99+
100+
# Check if pushing to protected branch
101+
if [ "$current_branch" = "$protected_branch" ]; then
102+
if ! run_build_checks; then
103+
exit 1
104+
fi
105+
106+
echo ""
107+
echo -e "${GREEN}All pre-push checks passed!${NC}"
108+
echo -e "${GREEN}Pushing to $protected_branch...${NC}"
109+
echo ""
110+
else
111+
echo -e "${CYAN}Pushing to branch: $current_branch${NC}"
112+
echo -e "${CYAN}(Pre-push checks are only run for $protected_branch)${NC}"
113+
echo ""
114+
fi
115+
116+
exit 0
117+
}
118+
119+
# Handle script interruption gracefully
120+
trap 'echo -e "\n${YELLOW}Pre-push hook interrupted${NC}"; exit 130' INT TERM
55121

56-
exit 0
122+
main "$@"

0 commit comments

Comments
 (0)