Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SoftCreatR/e5d3771a56c840db63340d7981f16609 to your computer and use it in GitHub Desktop.

Select an option

Save SoftCreatR/e5d3771a56c840db63340d7981f16609 to your computer and use it in GitHub Desktop.

Revisions

  1. SoftCreatR revised this gist Jun 28, 2025. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions check_for_new_github_commit.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/bin/bash
    #!/usr/bin/env bash
    set -euo pipefail

    # === Configuration ===
    GITHUB_USER="USER"
    @@ -7,14 +8,13 @@ BRANCH="main"
    LAST_COMMIT_FILE=".last_commit"

    # GitHub API URL for the branch
    API_URL="https://api.github.com/repos/${GITHUB_USER}/${REPO_NAME}/commits/${BRANCH}"
    GIT_URL="https://github.com/${GITHUB_USER}/${REPO_NAME}.git"

    # NFetch latest commit SHA from the remote repository
    LATEST_COMMIT_SHA=$(curl -s "$API_URL" | grep '"sha":' | head -n 1 | cut -d '"' -f 4)
    # Fetch the latest remote SHA
    LATEST_COMMIT_SHA=$(git ls-remote "$GIT_URL" "$BRANCH" | awk '{print $1}')

    # Abort if no SHA was received
    if [[ -z "$LATEST_COMMIT_SHA" ]]; then
    echo "Error: Could not retrieve the latest commit."
    echo "Error: couldn't retrieve SHA"
    exit 1
    fi

    @@ -26,7 +26,7 @@ if [[ ! -f "$LAST_COMMIT_FILE" ]]; then
    fi

    # Load previously known SHA
    LAST_KNOWN_COMMIT=$(cat "$LAST_COMMIT_FILE")
    LAST_KNOWN_COMMIT=$(<"$LAST_COMMIT_FILE")

    # Compare
    if [[ "$LATEST_COMMIT_SHA" != "$LAST_KNOWN_COMMIT" ]]; then
  2. @xopez xopez revised this gist Jun 27, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions check_for_new_github_commit.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    #!/bin/bash

    # === Configuration ===
    GITHUB_USER="xRuffKez"
    REPO_NAME="NRD"
    GITHUB_USER="USER"
    REPO_NAME="REPO"
    BRANCH="main"
    LAST_COMMIT_FILE=".last_commit"

  3. @xopez xopez renamed this gist Jun 27, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @xopez xopez created this gist Jun 27, 2025.
    37 changes: 37 additions & 0 deletions check_for_new_commit.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/bash

    # === Configuration ===
    GITHUB_USER="xRuffKez"
    REPO_NAME="NRD"
    BRANCH="main"
    LAST_COMMIT_FILE=".last_commit"

    # GitHub API URL for the branch
    API_URL="https://api.github.com/repos/${GITHUB_USER}/${REPO_NAME}/commits/${BRANCH}"

    # NFetch latest commit SHA from the remote repository
    LATEST_COMMIT_SHA=$(curl -s "$API_URL" | grep '"sha":' | head -n 1 | cut -d '"' -f 4)

    # Abort if no SHA was received
    if [[ -z "$LATEST_COMMIT_SHA" ]]; then
    echo "Error: Could not retrieve the latest commit."
    exit 1
    fi

    # If no SHA is stored yet, store it and notify
    if [[ ! -f "$LAST_COMMIT_FILE" ]]; then
    echo "$LATEST_COMMIT_SHA" > "$LAST_COMMIT_FILE"
    echo "First run – Commit SHA stored: $LATEST_COMMIT_SHA"
    exit 0
    fi

    # Load previously known SHA
    LAST_KNOWN_COMMIT=$(cat "$LAST_COMMIT_FILE")

    # Compare
    if [[ "$LATEST_COMMIT_SHA" != "$LAST_KNOWN_COMMIT" ]]; then
    echo "New commit found: $LATEST_COMMIT_SHA"
    echo "$LATEST_COMMIT_SHA" > "$LAST_COMMIT_FILE"
    else
    echo "No new commit – everything is up to date."
    fi