zshrc file (with git user switcher)
.zshrcShell

.zshrc

export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="half-life"
plugins=(git)
source $ZSH/oh-my-zsh.sh

# pnpm
export PNPM_HOME="/Users/milind/Library/pnpm"
case ":$PATH:" in
  *":$PNPM_HOME:"*) ;;
  *) export PATH="$PNPM_HOME:$PATH" ;;
esac

# Git user configurations
declare -A GH_USERS=(
  ["personal"]="thatbeautifuldream:Milind Mishra:milind.mishra4@gmail.com"
  ["work"]="milind-foyer:Milind Mishra:milind@foyer.work"
)

# Fixed guser function
guser() {
    # Use array to access arguments instead of \$1
    local args=("$@")
    local first_arg="${args[1]}"
    
    if [[ $# -eq 0 ]]; then
        echo "Usage: guser [personal|work|current]"
        return 1
    fi

    case "$first_arg" in
        "personal")
            local config=(${(s/:/)GH_USERS[personal]})
            ;;
        "work")
            local config=(${(s/:/)GH_USERS[work]})
            ;;
        "current")
            echo "Current Git Config:"
            echo "Name: $(git config user.name)"
            echo "Email: $(git config user.email)"
            echo "GitHub: $(git config user.github)"
            return 0
            ;;
        *)
            echo "Error: Unknown account '$first_arg'"
            echo "Available: personal, work, current"
            return 1
            ;;
    esac

    local gh_user="${config[1]}"
    local git_name="${config[2]}"
    local git_email="${config[3]}"

    echo "🔄 Switching to $first_arg account..."
    
    # Switch GitHub auth
    gh auth switch --user "$gh_user" 2>/dev/null || {
        echo "❌ Failed to switch GitHub auth. Run: gh auth login"
        return 1
    }
    
    # Update Git config
    git config --global user.name "$git_name"
    git config --global user.email "$git_email"
    git config --global user.github "$gh_user"
    
    echo "✅ Switched to $first_arg"
    echo "Git: $git_name <$git_email> [$gh_user]"
}

# Directory-based credential checking
check-directory-credentials() {
    local current_dir="$PWD"
    
    # Check if in personal directory
    if [[ "$current_dir" == "$HOME/code/self"* ]]; then
        local expected="personal"
    elif [[ "$current_dir" == "$HOME/code/work"* ]]; then
        local expected="work"
    else
        return 0  # No restriction outside managed directories
    fi
    
    local current_github=$(git config user.github 2>/dev/null)
    local expected_config=(${(s/:/)GH_USERS[$expected]})
    local expected_github="${expected_config[1]}"
    
    if [[ "$current_github" != "$expected_github" ]]; then
        echo "❌ Wrong credentials for $expected directory"
        echo "Current: $current_github"
        echo "Expected: $expected_github"
        echo "Run: guser $expected"
        return 1
    fi
    
    return 0
}

# Enhanced commit function
gcap() {
    if [[ $# -eq 0 ]]; then
        echo "❌ Commit message required"
        return 1
    fi
    
    if ! git rev-parse --git-dir >/dev/null 2>&1; then
        echo "❌ Not in a git repository"
        return 1
    fi
    
    # Check credentials before committing
    if ! check-directory-credentials; then
        echo "❌ COMMIT BLOCKED: Fix credentials first"
        return 1
    fi
    
    # do not stage all changes, just a safer approach
    # git add -A
    git commit -m "$*"
    
    if [[ $? -eq 0 ]]; then
        echo "✅ Commit successful"
    else
        echo "❌ Commit failed"
        return 1
    fi
}

# Auto-switch on directory change
chpwd() {
    if [[ ! -d ".git" ]]; then
        return 0
    fi
    
    local current_dir="$PWD"
    
    if [[ "$current_dir" == "$HOME/code/self"* ]]; then
        if ! check-directory-credentials; then
            echo "🔄 Auto-switching to personal credentials..."
            guser personal
        fi
    elif [[ "$current_dir" == "$HOME/code/work"* ]]; then
        if ! check-directory-credentials; then
            echo "🔄 Auto-switching to work credentials..."
            guser work
        fi
    fi
}

# Commit type helpers
gfeat() { gcap "feat: $@"; }
gnew() { gcap "feat: $@"; }
gfix() { gcap "fix: $@"; }
gdocs() { gcap "docs: $@"; }
gstyle() { gcap "style: $@"; }
grefactor() { gcap "refactor: $@"; }
gperf() { gcap "perf: $@"; }
gtest() { gcap "test: $@"; }
gbuild() { gcap "build: $@"; }
gci() { gcap "ci: $@"; }
gchore() { gcap "chore: $@"; }
gbrk() { gcap "feat!: $@"; }

# Help function
gtype() {
    echo ""
    echo "🔒 STRICT MODE: Credentials verified before commits"
    echo ""
    echo "Commands:"
    echo "  guser personal  - Switch to personal account"
    echo "  guser work      - Switch to work account"
    echo "  guser current   - Show current account"
    echo ""
    echo "Commit helpers:"
    echo "  gfeat <msg>    - feat: commit"
    echo "  gfix <msg>     - fix: commit"
    echo "  gdocs <msg>    - docs: commit"
    echo "  gstyle <msg>   - style: commit"
    echo "  grefactor <msg> - refactor: commit"
    echo "  gperf <msg>    - perf: commit"
    echo "  gtest <msg>    - test: commit"
    echo "  gbuild <msg>   - build: commit"
    echo "  gci <msg>      - ci: commit"
    echo "  gchore <msg>   - chore: commit"
    echo "  gbrk <msg>     - feat!: commit (breaking)"
    echo ""
}

# Package manager aliases
alias pn=pnpm
alias px="pnpm dlx"
alias pnd="pnpm run dev"
alias pns="pnpm run start"

# Editor aliases
alias clauded="claude --dangerously-skip-permissions"
alias c='open "\$1" -a "Cursor"'
alias v='open "\$1" -a "Visual Studio Code"'

# Initialize zoxide
eval "$(zoxide init zsh)"

# bun
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
[ -s "/Users/milind/.bun/_bun" ] && source "/Users/milind/.bun/_bun"
Updated: 7/16/2025