Skip to content

Commit b22bdf5

Browse files
committed
Improve fork check in Git hooks installation
This change enforces that the repository name is "lab0-c" both locally and on GitHub while ensuring it is correctly forked from "sysprog21/lab0-c." It validates both the fork status and the parent repository name using the GitHub API. Modifications: - Replaced grep and sed parsing with Git commands and awk for extracting the repository owner and name. - Added validation to ensure the repository name is "lab0-c." - Improved fork verification by checking the GitHub API response for both fork status and parent repository name.
1 parent e83f2fb commit b22bdf5

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

scripts/install-git-hooks

+14-14
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)