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