Last active
July 16, 2024 14:03
-
-
Save KevinWang15/d5126cc01d0a5529fa67d9ecb692ad19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to print usage | |
print_usage() { | |
echo "Usage: $0 <github-repo> [branch]" | |
echo "Example: $0 go-gorm/gorm main" | |
echo "If branch is not specified, 'master' will be used as default." | |
} | |
# Check if at least a repository is provided | |
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then | |
print_usage | |
exit 1 | |
fi | |
REPO=$1 | |
BRANCH=${2:-master} # Use the second argument if provided, otherwise default to 'master' | |
# Fetch the latest commit information | |
RESPONSE=$(curl -s -w "%{http_code}" "https://api.github.com/repos/$REPO/commits/$BRANCH") | |
HTTP_STATUS="${RESPONSE: -3}" | |
BODY="${RESPONSE:0:${#RESPONSE}-3}" | |
# Check if the request was successful | |
if [[ ! "$HTTP_STATUS" =~ ^2 ]]; then | |
echo "Error: HTTP status $HTTP_STATUS" | |
echo "$BODY" | |
exit 1 | |
fi | |
# Extract the commit hash and date | |
COMMIT_HASH=$(echo "$BODY" | jq -r .sha | cut -c1-12) | |
COMMIT_DATE=$(echo "$BODY" | jq -r '.commit.committer.date') | |
# Convert the date to the required format | |
FORMATTED_DATE=$(date -u -d "$COMMIT_DATE" +"%Y%m%d%H%M%S") | |
# Construct the pseudo-version | |
PSEUDO_VERSION="v0.0.0-${FORMATTED_DATE}-${COMMIT_HASH}" | |
echo $PSEUDO_VERSION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment