-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Improve fork check in Git hooks installation #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The github api, https://api.github.com/repos/${ACCOUNT}/${REPO_NAME}, return with a JSON object like the one below: {
"id": 944494149,
"node_id": "R_kgDOOEvWRQ",
"name": "lab0-c_dev",
"full_name": "willy-liu/lab0-c_dev",
"fork": true,
"parent": {
"id": 149674932,
"node_id": "MDEwOlJlcG9zaXRvcnkxNDk2NzQ5MzI=",
"name": "lab0-c",
"full_name": "sysprog21/lab0-c",
}
} The "fork" value indicates whether the repository is forked from another repository. If "fork" is true, the API will also return the "parent" field, which contains details of the original repository. We can check However, since grep does not handle JSON format well, I added a |
scripts/install-git-hooks
Outdated
JQ=$(which jq) | ||
if [ $? -ne 0 ]; then | ||
echo "jq not installed." | ||
exit 1 | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do that. The jq
is not required for this case. Use sed/awk instead.
25ef28c
to
eb59173
Compare
|
@@ -31,7 +31,8 @@ fi | |||
((CURRENT_STEP++)) | |||
progress "$CURRENT_STEP" "$TOTAL_STEPS" | |||
|
|||
ACCOUNT=$(git config -l | grep -w remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/lab0-c.*/\1/') | |||
ACCOUNT=$(git config --get remote.origin.url | awk -F'[:/]' '{print $(NF-1)}') | |||
REPO_NAME=$(git config --get remote.origin.url | awk -F'[:/]' '{gsub(/\.git$/, "", $NF); print $NF}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The homework assignment requires that the repository must always be named lab0-c
. This naming convention serves as a strict identification mechanism to determine who is participating in the assignment.
*) | ||
throw "Your repository MUST be forked from 'sysprog21/lab0-c'." | ||
esac | ||
RESPONSE=$(${CURL} -s -H "Accept: application/vnd.github.v3+json" \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should ensure the content retrieved from the curl
command is not empty.
eb59173
to
b22bdf5
Compare
This change enforces that the repository name is "lab0-c" both locally Modifications:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refine the commit messages to avoid unnecessary bullets for modification items. Always use plain English sentences to describe.
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.
b22bdf5
to
f370589
Compare
Thank @willy-liu for contributing! |
This change allows users to modify the repository name when working with a fork.
Modifications:
Note: