Quantcast
Channel: Finding the oldest commit in a GitHub repository via the API - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by FedFranz for Finding the oldest commit in a GitHub repository via the API

$
0
0

Posting my solution, since all others didn't work for me.

The following script retrieves the list of commits for a given REPO ("owner/repo"), traverses to the last page if necessary, and outputs the JSON object of the last (oldest) commit.

    REPO="owner/repo"    URL="https://api.github.com/repos/$REPO/commits"    H=" -H \"Accept: application/vnd.github+json\" \      -H \"X-GitHub-Api-Version: 2022-11-28\""    response=$(curl -s -L --include $H $URL | awk 'NR > 1')    # Split the output into header and json    header=$(echo "$response" | awk 'BEGIN{RS="\r\n";ORS="\r\n"} /^[a-zA-Z0-9-]+:/')    commits=$(echo "$response" | awk '!/^[a-zA-Z0-9-]+:/')    # If paginated, get last page    if [[ $header == *"link"* ]]; then      # Extract the last page value      link_line=$(echo "$header" | grep -i "^link:")      last_page=$(echo "$link_line" | sed -n 's/.*page=\([0-9]\+\)[^0-9].*rel="last".*/\1/p')      # Get last-page commits      commits=$(curl -s -L $H $URL?page=$last_page)    fi    # Print first commit    echo $commits | jq '.[-1].commit'

Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>