Skip to content

Instantly share code, notes, and snippets.

@JanX2
Forked from joeytwiddle/github_get_all_forks.sh
Last active December 12, 2023 14:04
Show Gist options
  • Save JanX2/f7fd01bbd37a863cbd0c6c3db6c37ca2 to your computer and use it in GitHub Desktop.
Save JanX2/f7fd01bbd37a863cbd0c6c3db6c37ca2 to your computer and use it in GitHub Desktop.
Add all forks of the current repo as remotes
#!/usr/bin/env bash
set -e
# See also: https://github.com/frost-nzcr4/find_forks (same thing but in python)
origin_url="$(git config --get remote.origin.url | sed 's+.*: ++')"
full_repo_name="$(echo "$origin_url" | sed 's+.*github.com/++ ; s+\.git$++')"
forks_url="https://api.github.com/repos/${full_repo_name}/forks"
#[ -e "forks.json" ] ||
curl -s "${forks_url}" -o forks.json
node -e "
var forks = JSON.parse(fs.readFileSync('forks.json', 'utf-8'));
forks.forEach(forkData => {
console.log('git remote add \"' + forkData.owner.login + '\" \"' + forkData.git_url + '\"');
});
console.log('git fetch --all');
" |
if [ "$1" = -do ]
then bash
else
cat
echo
echo "Pass -do to execute above commands"
fi
@johndpope
Copy link

new error

unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
error: Could not fetch andrewkuo
Fetching eridgd
fatal: remote error:
The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
error: Could not fetch eridgd
Fetching trajeshbe
fatal: remote error:
The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
error: Could not fetch trajeshbe
Fetching recepbalibey
fatal: remote error:
The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
error: Could not fetch recepbalibey
Fetching tahercoolguy
fatal: remote error:
The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
error: Could not fetch tahercoolguy
Fetching jsenapati
fatal: remote error:
The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
error: Could not fetch jsenapati
Fetching gerialkot
fatal: remote error:

https://github.blog/2021-09-01-improving-git-protocol-security-github/

@johndpope
Copy link

johndpope commented Dec 10, 2023

with pagination

sudo apt-get install jq

#!/usr/bin/env bash
# export GITHUB_TOKEN=" " https://github.com/settings/tokens

set -e

# See also: https://github.com/frost-nzcr4/find_forks (same thing but in python)

origin_url="$(git config --get remote.origin.url | sed 's+.*: ++')"

full_repo_name="$(echo "$origin_url" | sed 's+.*github.com/++ ; s+\.git$++')"

perPage=100  # Maximum items per page
page=1       # Start from page 1
forks=()


echo "export GITHUB_TOKEN=" " https://github.com/settings/tokens"
# Provide your GitHub token here
github_token="${GITHUB_TOKEN}"

# Function to check for API rate limit message
check_rate_limit() {
    if echo "$1" | grep -q "API rate limit exceeded"; then
        echo "API rate limit exceeded. Please wait or authenticate with a GitHub token."
        exit 1
    fi
}

# Fetch pages until no more forks are returned
while : ; do
    echo "Fetching page $page of forks..."
    forks_url="https://api.github.com/repos/${full_repo_name}/forks?per_page=${perPage}&page=${page}"

    # Use authenticated request if a token is provided
    if [ -n "$github_token" ]; then
        response=$(curl -s -H "Authorization: token $github_token" "${forks_url}")
    else
        response=$(curl -s "${forks_url}")
    fi

    # Check for rate limit message
    check_rate_limit "$response"

    # Break the loop if no forks are returned
    if [ -z "$response" ] || [ "$response" == "[]" ]; then
        break
    fi
    
    # Process and print each fork's owner login and git url
    fork_info=$(echo "$response" | jq -r '.[] | .owner.login + " " + .git_url')
    
    # If fork_info is empty, break the loop
    if [ -z "$fork_info" ]; then
        echo "No more valid fork data found."
        break
    fi

    echo "$fork_info"
    # Append current page's forks to the array
    forks+=("$response")

    # Increment page number
    ((page++))
done

# Process all fetched forks
for fork_data in "${forks[@]}"; do
    echo "$fork_data" | jq -r '.[] | "git remote add \"" + .owner.login + "\" \"" + .git_url + "\""' | bash
done
echo 'git fetch --all' | bash

if [ "$1" = -do ]
then bash
else
    cat
    echo
    echo "Pass -do to execute above commands"
fi

@JanX2
Copy link
Author

JanX2 commented Dec 12, 2023

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment