git.fiddlerwoaroof.com
zsh/zsh_plugins/01-git.zsh
13cc20cf
 #:depends-on:git-get
9dc16eeb
 autoload -U regexp-replace
70e8006a
 GIT_DEBUG=0
 
7d0d88b2
 GIT_CMD="`which -p git 2>/dev/null`"
 GTI_CMD="`which -p gti 2>/dev/null`"
fe429cbf
 
8ef8fa75
 GIT_DIR=("$HOME"/git*repos)
 if ! [[ -d "$HOME/git*_repos" ]]; then
   mkdir -p "$HOME"/git_repos
 fi
 
1eb57280
 git-pwdurl () {
   set -x
   local -a parts
   parts=(${(s:/:)PWD})
   repo_root=${parts[(I)git_repos]}
   site_idx=$((repo_root + 1))
   repo_idx=$((repo_root + 2))
   repo=${(j:/:)parts[$repo_idx,-1]}
   site=${parts[$site_idx]}
   case "$1" in
     git) echo "git://$site:$repo.git"
          ;;
     ssh) echo git@$site:$repo.git
          ;;
     *) echo https://$site/$repo.git
        ;;
   esac
 }
 
 git-cpwd () {
   git clone "$(git-pwdurl "$1")" .
 }
 
f48ed522
 git-fp() {
   $(pbpaste | grep git\ push)
 }
 
fe429cbf
 git-bump() {
   git commit -m "${SITE_PREFIX:-}(bump)" --allow-empty
 }
 
4ae7c492
 git-update-repos() {
   find . -name .git | (
     while read -r repo; do
       printf "---\n$repo\n";
       git --work-tree="${repo%/*}" --git-dir="$repo" pull --ff-only ;
       printf "\n\n"
     done
   )
 }
 
64b97809
 git-ub() {
   git stash || exit 1
   git pull --rebase
   git stash pop
 }
 
70e8006a
 git-ufgrep() {
   PATTERN="$1"
   if [[ ${PATTERN}x == ""x ]]; then
     PATTERN='.*'
   fi
   PATTERN="${PATTERN//\//\\/}"
 
   if [[ "$GIT_DEBUG" == 1 ]]; then
     echo "$PATTERN"
   fi
 
   git status --porcelain | awk '$1 == "??" && $2 ~ /[.]'"$PATTERN"'$/'
 }
 
 git-run() {
   (
     DIR="$1"
     shift
     git cd "$DIR"
     exec "$@"
   )
 }
 
 git-back() {
   git checkout -
 }
 
 git-ff() {
64b97809
   git merge --ff-only
 }
 
 git-pff() {
70e8006a
   git pull --ff-only
 }
 
cd103bde
 git-root() {
eb65d556
   rootpath="$(git rev-parse --show-toplevel)"
   echo "$rootpath/$1"
70e8006a
 }
 
 git-ag() {
64b97809
   ag "$@" -- "$(git-root)"
cd103bde
 }
70e8006a
 
 git-cd() {
   cd "$(git-root)/$1"
 }
 alias groot=git-cd
cd103bde
 
06f3ccaf
 git-graph() {
   git log --graph --format=oneline --decorate "$@"
 }
 alias gl=git-graph
eb65d556
 
 git-find-tag() {
   git log --format=oneline --all |
     gawk -vtofind="$*" -vFS=$'[ ]+|:[ ]*' \
          'tolower($2) == "code" {$2=$2" "$3; for (i=3;i<NF;i++) $i=$(i+1); $NF=""} \
           {code=$2; hash=$1; for (i=1;i<=NF-2;i++) $i=$(i+2); NF=NF-2} \
           code ~ tofind {print code": "hash" "$0}'
 }
64b97809
 
 git-messages() {
   if [[ -d .git ]]; then
     echo "Git log messages:"
     git log -n 5 | egrep --color=yes -Io '(TODO|NOTE|FIXME|BUG|DONE):.*$'
   fi
06f3ccaf
 
64b97809
   echo "Messages from files:"
   egrep --color=yes -IHnro '(TODO|NOTE|FIXME|BUG):.*$' . |
     psc '
 for line in sys.stdin:
       line = line.strip().split(":", 2)
       print("%s\n\t%s" % (":".join(line[2:]), ":".join(line[:2])))'
cd103bde
 }
 
9dc16eeb
 git-gh-create() {
41339c2f
   local organization_name=${1?need a repo name or org/repo pair}
   local repo_name=${2:-$organization_name}
 
   local api_url=https://api.github.com/user/repos
   if (( $# == 2 )) {
        api_url="https://api.github.com/orgs/$organization_name/repos"
1eb57280
      }
41339c2f
 
1eb57280
      regexp-replace repo_name '[" ]' '-'
      local GH_TOKEN
      source "$HOME/.github-token"
      jq '{full_name, clone_url, ssh_url}' <( (
                                              curl -XPOST -v \
                                                   -u "fiddlerwoaroof:$GH_TOKEN" \
                                                   "$api_url" \
                                                   -H 'Content-Type: application/json' \
                                                   --data-binary @- <<-EOF
9dc16eeb
 {
   "name": "${repo_name}"
 }
 EOF
1eb57280
                                            ) )
9dc16eeb
 }
 
64b97809
 alias git-msg=git-messages
13cc20cf
 alias git-cj="git-get cj"
 alias git-hub="git-get github"
 alias git-lab="git-get gitlab"
 alias gh="git-hub"
06f3ccaf
 
7d0d88b2
 git-remote() {
   [[ "$GIT_DEBUG" == 1 ]] && set -x
   base_cmd=("$GIT_CMD" remote)
   opts=()
   args=()
8ef8fa75
   for x in "$@"; do
7d0d88b2
     if [[ "${x[1]}" == "-" ]]; then
       opts=("${opts[@]}" "$x")
     else
       args=("${args[@]}" "$x")
     fi
   done
   if (( $#args >= 2 )) {
        a=${args[1]};
        b=${args[2]};
        shift 2 args;
        args=("$a" "${args[@]}")
8ef8fa75
        if (( $#opts > 0 )); then
7d0d88b2
          "${base_cmd[@]}" "$b" "${opts[@]}" "${args[@]}"
8ef8fa75
        else
7d0d88b2
          "${base_cmd[@]}" "$b" "${args[@]}"
        fi
      } else {
        "${base_cmd[@]}" "$@"
      }
1eb57280
      [[ "$GIT_DEBUG" == 1 ]] && set +x
7d0d88b2
 }
cd103bde
 
64b97809
 if [[ ! -z "$GIT_CMD" ]]; then
cd103bde
   # git wrapper that mimics the functionality of git for commandlets but also
   # searches shell functions.
70e8006a
   git() {
13cc20cf
     local possible_cmd
     local cmdlets
64b97809
 
13cc20cf
     possible_cmd="${${$(expand-alias "git-$1")#'}%'}"
7d0d88b2
     printf "%s" "$possible_cmd" | read -A cmdlets
13cc20cf
 
     if [[ "$GIT_DEBUG" != "0" ]]; then
       echo "git: looking for: \"$possible_cmd\" command: \"${cmdlets[1]}\""
     fi
 
     if is-function "${cmdlets[1]}"; then
       "${cmdlets[@]}" "${@[2,-1]}"
cd103bde
     else
64b97809
       "$GIT_CMD" "$@"
cd103bde
     fi
   }
 fi