Skip to content

Commit b9d5c9f

Browse files
willy-liujserv
authored andcommitted
Improve fork check in Git hooks installation
This change ensures that the repository is named "lab0-c" both locally and on GitHub while verifying that it is correctly forked from "sysprog21/lab0-c." The validation now relies on the GitHub API to check both the fork status and the parent repository name. The previous approach using grep and sed for parsing has been replaced with Git commands and awk to extract the repository owner and name. Additional validation ensures the repository name is "lab0-c", and fork verification has been improved by directly analyzing the GitHub API response. Change-Id: If5756d175c5bffeb9301de6bea304dd16dad9ab4
1 parent 1a8b5f4 commit b9d5c9f

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

scripts/install-git-hooks

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ fi
3131
((CURRENT_STEP++))
3232
progress "$CURRENT_STEP" "$TOTAL_STEPS"
3333

34-
ACCOUNT=$(git config -l | grep -w remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/lab0-c.*/\1/')
34+
ACCOUNT=$(git config --get remote.origin.url | awk -F'[:/]' '{print $(NF-1)}')
35+
REPO_NAME=$(git config --get remote.origin.url | awk -F'[:/]' '{gsub(/\.git$/, "", $NF); print $NF}')
3536

3637
CURL=$(which curl)
3738
if [ $? -ne 0 ]; then
@@ -43,25 +44,24 @@ CURL_RES=$(${CURL} -s \
4344
https://api.github.com/repos/${ACCOUNT}/lab0-c/actions/workflows)
4445

4546
TOTAL_COUNT=$(echo ${CURL_RES} | sed -e 's/.*"total_count": \([^,"]*\).*/\1/')
46-
case ${TOTAL_COUNT} in
47-
*"Not Found"*)
48-
throw "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c"
49-
esac
47+
if [[ "$REPO_NAME" != "lab0-c" || "$TOTAL_COUNT" == *"Not Found"* ]]; then
48+
throw "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c"
49+
fi
5050

5151
# 3. Ensure this repository is frok from sysprog21/lab0-c'.
5252
((CURRENT_STEP++))
5353
progress "$CURRENT_STEP" "$TOTAL_STEPS"
5454

5555
if [[ "${ACCOUNT}" != "sysprog21" ]]; then
56-
REPO_FORKED=$(${CURL} -s \
57-
-H "Accept: application/vnd.github.v3+json" \
58-
https://api.github.com/repos/${ACCOUNT}/lab0-c | grep -m 1 fork)
59-
case ${REPO_FORKED} in
60-
*true*)
61-
;;
62-
*)
63-
throw "Your repository MUST be forked from 'sysprog21/lab0-c'."
64-
esac
56+
RESPONSE=$(${CURL} -s -H "Accept: application/vnd.github.v3+json" \
57+
"https://api.github.com/repos/${ACCOUNT}/lab0-c")
58+
59+
IS_FORK=$(echo "$RESPONSE" | sed -n 's/.*"fork": \(true\|false\).*/\1/p' | head -n1)
60+
PARENT_NAME=$(echo "$RESPONSE" | awk -F'"' '/"parent": \{/{flag=1} flag && /"full_name":/{print $4; exit}')
61+
62+
if [[ "$IS_FORK" != "true" || "$PARENT_NAME" != "sysprog21/lab0-c" ]]; then
63+
throw "Your repository MUST be forked from 'sysprog21/lab0-c'."
64+
fi
6565
fi
6666

6767
# 4. Check GitHub Actions

0 commit comments

Comments
 (0)