|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Source the common utilities |
| 4 | +source "$(dirname "$0")/common.sh" |
| 5 | + |
| 6 | +check_github_actions |
| 7 | + |
| 8 | +TOTAL_STEPS=6 |
| 9 | +CURRENT_STEP=0 |
| 10 | + |
| 11 | +# 0. Check environment |
| 12 | +((CURRENT_STEP++)) |
| 13 | +progress "$CURRENT_STEP" "$TOTAL_STEPS" |
| 14 | + |
| 15 | +if ! command -v curl &>/dev/null; then |
| 16 | + throw "curl not installed." |
| 17 | +fi |
| 18 | + |
| 19 | +if ! command -v git &>/dev/null; then |
| 20 | + throw "git not installed." |
| 21 | +fi |
| 22 | + |
| 23 | +# 1. Sleep for a random number of milliseconds |
| 24 | +# The time interval is important to reduce unintended network traffic. |
| 25 | +((CURRENT_STEP++)) |
| 26 | +progress "$CURRENT_STEP" "$TOTAL_STEPS" |
| 27 | + |
| 28 | +# Generate a random integer in [0..999]. |
| 29 | +random_ms=$((RANDOM % 1000)) |
| 30 | + |
| 31 | +# Convert that to a decimal of the form 0.xxx so that 'sleep' interprets it as seconds. |
| 32 | +# e.g., if random_ms is 5, we convert that to 0.005 (i.e. 5 ms). |
| 33 | +sleep_time="0.$(printf "%03d" "$random_ms")" |
| 34 | + |
| 35 | +sleep "$sleep_time" |
| 36 | + |
| 37 | +# 2. Fetch latest commit from GitHub |
| 38 | +((CURRENT_STEP++)) |
| 39 | +progress "$CURRENT_STEP" "$TOTAL_STEPS" |
| 40 | + |
| 41 | +REPO_OWNER=$(git config -l | grep -w remote.origin.url | sed -E 's%^.*github.com[/:]([^/]+)/lab0-c.*%\1%') |
| 42 | +REPO_NAME="lab0-c" |
| 43 | + |
| 44 | +repo_html=$(curl -s "https://github.com/${REPO_OWNER}/${REPO_NAME}") |
| 45 | + |
| 46 | +# Extract the default branch name from data-default-branch="..." |
| 47 | +DEFAULT_BRANCH=$(echo "$repo_html" | grep -oP "/${REPO_OWNER}/${REPO_NAME}/blob/\K[^/]+(?=/LICENSE)" | head -n 1) |
| 48 | + |
| 49 | +if [ "$DEFAULT_BRANCH" != "master" ]; then |
| 50 | + echo "$DEFAULT_BRANCH" |
| 51 | + throw "The default branch for $REPO_OWNER/$REPO_NAME is not 'master'." |
| 52 | +fi |
| 53 | + |
| 54 | +# Construct the URL to the commits page for the default branch |
| 55 | +COMMITS_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/commits/${DEFAULT_BRANCH}" |
| 56 | + |
| 57 | +temp_file=$(mktemp) |
| 58 | +curl -sSL -o "$temp_file" "$COMMITS_URL" |
| 59 | + |
| 60 | +# general grep pattern that finds commit links |
| 61 | +upstream_hash=$( |
| 62 | + grep -Po 'href="[^"]*/commit/\K[0-9a-f]{40}' "$temp_file" \ |
| 63 | + | head -n 1 |
| 64 | +) |
| 65 | + |
| 66 | +rm -f "$temp_file" |
| 67 | + |
| 68 | +if [ -z "$upstream_hash" ]; then |
| 69 | + throw "Failed to retrieve upstream commit hash from GitHub.\n" |
| 70 | +fi |
| 71 | + |
| 72 | +# 3. Check local repository awareness |
| 73 | + |
| 74 | +((CURRENT_STEP++)) |
| 75 | +progress "$CURRENT_STEP" "$TOTAL_STEPS" |
| 76 | + |
| 77 | +# Check if the local workspace knows about $upstream_hash. |
| 78 | +if ! git cat-file -e "${upstream_hash}^{commit}" 2>/dev/null; then |
| 79 | + throw "Local repository does not recognize upstream commit %s.\n\ |
| 80 | + Please fetch or pull from remote to update your workspace.\n" "$upstream_hash" |
| 81 | +fi |
| 82 | + |
| 83 | +# 4. List non-merge commits between BASE_COMMIT and upstream_hash |
| 84 | + |
| 85 | +((CURRENT_STEP++)) |
| 86 | +progress "$CURRENT_STEP" "$TOTAL_STEPS" |
| 87 | + |
| 88 | +# Base commit from which to start checking. |
| 89 | +BASE_COMMIT="dac4fdfd97541b5872ab44615088acf603041d0c" |
| 90 | + |
| 91 | +# Get a list of non-merge commit hashes after BASE_COMMIT in the local workspace. |
| 92 | +commits=$(git rev-list --no-merges "${BASE_COMMIT}".."${upstream_hash}") |
| 93 | + |
| 94 | +if [ -z "$commits" ]; then |
| 95 | + throw "No new non-merge commits found after the check point." |
| 96 | +fi |
| 97 | + |
| 98 | +# 5. Validate each commit for Change-Id. |
| 99 | + |
| 100 | +((CURRENT_STEP++)) |
| 101 | +progress "$CURRENT_STEP" "$TOTAL_STEPS" |
| 102 | + |
| 103 | +failed=0 |
| 104 | + |
| 105 | +for commit in $commits; do |
| 106 | + # Retrieve the commit message for the given commit. |
| 107 | + commit_msg=$(git log -1 --format=%B "${commit}") |
| 108 | + |
| 109 | + # Extract the last non-empty line from the commit message. |
| 110 | + last_line=$(echo "$commit_msg" | awk 'NF {line=$0} END {print line}') |
| 111 | + |
| 112 | + # Check if the last line matches the expected Change-Id format. |
| 113 | + if [[ ! $last_line =~ ^Change-Id:\ I[0-9a-fA-F]+$ ]]; then |
| 114 | + subject=$(git log -1 --format=%s "${commit}") |
| 115 | + short_hash=$(git rev-parse --short "${commit}") |
| 116 | + printf "\n${RED}[!]${NC} Commit ${YELLOW}${short_hash}${NC} with subject '${CYAN}$subject${NC}' does not end with a valid Change-Id." |
| 117 | + failed=1 |
| 118 | + fi |
| 119 | +done |
| 120 | + |
| 121 | +if [ $failed -ne 0 ]; then |
| 122 | + printf "\n\nSome commits are missing a valid ${YELLOW}Change-Id${NC}. Amend the commit messages accordingly.\n" |
| 123 | + printf "Please review the lecture materials for the correct ${RED}Git hooks${NC} installation process,\n" |
| 124 | + printf "as there appears to be an issue with your current setup.\n" |
| 125 | + exit 1 |
| 126 | +fi |
| 127 | + |
| 128 | +echo "Fingerprint: $(make_random_string 24 "$REPO_OWNER")" |
| 129 | + |
| 130 | +exit 0 |
0 commit comments