#!/bin/bash
# Example: Git shortcut functions
#
# Handy git aliases that pair well with wa's branch management.
# Source this in your shell profile if you want these shortcuts.

# Show recent branches (sorted by last commit)
recent() {
  git for-each-ref --sort=-committerdate --count=15 --format='%(refname:short)' refs/heads/
}

# One-line git log
log() {
  git log --oneline || return
}

status() {
  git status || return
}

show() {
  git show $1 || return
}

show_files() {
  git show --pretty="" --name-only $1 || return
}

# Stash shortcuts
stash() {
  git stash $1 || return
}

stash_list() {
  git stash list || return
}

stash_show() {
  git stash show $1 || return
}

stash_pop() {
  git stash pop || return
}

pull() {
  git pull || return
}

push() {
  git push || return
}

# Sync current branch with a target branch
gitsync() {
  if [ -z "$1" ]; then
    echo "Usage: gitsync <branch-to-sync-from>"
    return 1
  fi

  TARGET_BRANCH="$1"
  CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

  echo "Stashing changes..."
  git stash push -u -m "auto-stash before syncing with $TARGET_BRANCH"

  echo "Rebasing $TARGET_BRANCH into $CURRENT_BRANCH..."
  git rebase -i "$TARGET_BRANCH"
  pull
  push

  echo "Done. Don't forget to apply your stash if needed:"
  echo "  stash list"
  echo "  stash pop"
}
