Skip to content

Instantly share code, notes, and snippets.

@Raimo33
Last active May 2, 2024 21:09
Show Gist options
  • Save Raimo33/4bde1a6b872f04986409895c2dcdf98c to your computer and use it in GitHub Desktop.
Save Raimo33/4bde1a6b872f04986409895c2dcdf98c to your computer and use it in GitHub Desktop.
Open most recent repo
#!/bin/bash
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export DISPLAY=:0
# Ensure the X server is accessible
xhost local:root > /dev/null 2>&1
function check_internet()
{
## Check if Google's DNS can be reached
ping -c 1 8.8.8.8 > /dev/null 2>&1
return $?
}
# Wait for internet connection
while ! check_internet
do
echo "Waiting for internet connection..."
sleep 0.5
done
# Configure your GitHub username and the target directory for cloning
GITHUB_USER="Raimo33"
TARGET_DIR="$HOME/Desktop"
# Load GitHub token from a secure place
GITHUB_TOKEN=$(< ~/.github_token) # Assuming you store your token in this file
# Create target directory if it does not exist
mkdir -p "$TARGET_DIR"
cd "$TARGET_DIR"
# Fetch the latest updated repository from GitHub API
REPO_NAME=$(curl -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/users/$GITHUB_USER/repos?sort=updated" | \
jq -r '.[0].name')
# Clone the latest repository
if [ -d "$REPO_NAME" ]; then
cd "$REPO_NAME"
#open in VSCode
code .
git pull
cd ..
else
git clone "https://$GITHUB_TOKEN:x-oauth-basic@github.com/$GITHUB_USER/$REPO_NAME.git"
#open in VSCode
code "$REPO_NAME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment