diff --git a/scripts/create-github-repo-function.sh b/scripts/create-github-repo-function.sh new file mode 100644 index 0000000..05401a7 --- /dev/null +++ b/scripts/create-github-repo-function.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +# Shell function to create a new directory, initialize git, and create a GitHub repository +# This function keeps you in the newly created directory after completion +# +# To use this function, add to your .zshrc or .bashrc: +# source ~/dotfiles/scripts/create-github-repo-function.sh +# +# Usage: create-github-repo [description] + +create-github-repo() { + # Check if gh CLI is installed + if ! command -v gh &> /dev/null; then + echo "Error: GitHub CLI (gh) is not installed" + echo "Install it from: https://cli.github.com/" + return 1 + fi + + # Check if repo name is provided + if [ -z "$1" ]; then + echo "Usage: create-github-repo [description]" + echo "Example: create-github-repo my-new-project 'A cool new project'" + return 1 + fi + + local REPO_NAME="$1" + local REPO_DESC="${2:-}" + + # Check if directory already exists + if [ -d "$REPO_NAME" ]; then + echo "Error: Directory '$REPO_NAME' already exists" + return 1 + fi + + echo "Creating new repository: $REPO_NAME" + + # Create directory and cd into it + mkdir "$REPO_NAME" || return 1 + cd "$REPO_NAME" || return 1 + + # Initialize git repository + git init -b main || { cd ..; rm -rf "$REPO_NAME"; return 1; } + echo "# $REPO_NAME" > README.md + + # Create initial commit + git add README.md + git commit -m "Initial commit" || { cd ..; rm -rf "$REPO_NAME"; return 1; } + + # Create GitHub repository (public) + echo "Creating public GitHub repository..." + if [ -n "$REPO_DESC" ]; then + gh repo create "$REPO_NAME" --public --source=. --remote=origin --description "$REPO_DESC" || { cd ..; rm -rf "$REPO_NAME"; return 1; } + else + gh repo create "$REPO_NAME" --public --source=. --remote=origin || { cd ..; rm -rf "$REPO_NAME"; return 1; } + fi + + # Push to GitHub (this also sets up tracking) + git push -u origin main || { cd ..; rm -rf "$REPO_NAME"; return 1; } + + echo "" + echo "✓ Repository created successfully!" + echo " Local path: $(pwd)" + echo " Remote URL: $(git remote get-url origin)" + echo "" +} diff --git a/scripts/create-github-repo.sh b/scripts/create-github-repo.sh new file mode 100755 index 0000000..49b15f2 --- /dev/null +++ b/scripts/create-github-repo.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +# Script to create a new directory, initialize git, and create a GitHub repository +# Usage: create-github-repo.sh [description] + +set -e # Exit on error + +# Check if gh CLI is installed +if ! command -v gh &> /dev/null; then + echo "Error: GitHub CLI (gh) is not installed" + echo "Install it from: https://cli.github.com/" + exit 1 +fi + +# Check if repo name is provided +if [ -z "$1" ]; then + echo "Usage: $0 [description]" + echo "Example: $0 my-new-project 'A cool new project'" + exit 1 +fi + +REPO_NAME="$1" +REPO_DESC="${2:-}" + +# Check if directory already exists +if [ -d "$REPO_NAME" ]; then + echo "Error: Directory '$REPO_NAME' already exists" + exit 1 +fi + +echo "Creating new repository: $REPO_NAME" + +# Create directory and cd into it +mkdir "$REPO_NAME" +cd "$REPO_NAME" + +# Initialize git repository +# Check git version and use appropriate init syntax +GIT_VERSION=$(git --version | awk '{print $3}') +# Function to compare versions: returns 0 if $1 >= $2 +version_ge() { + [ "$(printf '%s\n' "$2" "$1" | sort -V | head -n1)" = "$2" ] +} +if version_ge "$GIT_VERSION" "2.28.0"; then + git init -b main +else + git init + git branch -m main +fi +echo "# $REPO_NAME" > README.md + +# Create initial commit +git add README.md +git commit -m "Initial commit" + +# Create GitHub repository (public) +echo "Creating public GitHub repository..." +if [ -n "$REPO_DESC" ]; then + gh repo create "$REPO_NAME" --public --source=. --remote=origin --description "$REPO_DESC" +else + gh repo create "$REPO_NAME" --public --source=. --remote=origin +fi + +# Push to GitHub (this also sets up tracking) +git push -u origin main + +echo "" +echo "✓ Repository created successfully!" +echo " Local path: $(pwd)" +echo " Remote URL: $(git remote get-url origin)" +echo "" diff --git a/zsh/.zshrc b/zsh/.zshrc index 3537157..1731f0d 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -186,4 +186,7 @@ alias pylist='pyenv versions' # Show current Python version alias pyversion='python --version' +# GitHub repository creation function +[ -f "$HOME/dotfiles/scripts/create-github-repo-function.sh" ] && source "$HOME/dotfiles/scripts/create-github-repo-function.sh" + . "$HOME/.turso/env"