From d8e96bf4c6a5354e97fa37b3b64197c0e1306831 Mon Sep 17 00:00:00 2001
From: crater2150 <crater@qwertyuiop.de>
Date: Fri, 24 Nov 2017 16:44:29 +0100
Subject: [PATCH] Refactor vcs aliases / functions

---
 aliases/git | 73 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 32 deletions(-)

diff --git a/aliases/git b/aliases/git
index 9bde9cb..1d5a15f 100644
--- a/aliases/git
+++ b/aliases/git
@@ -1,49 +1,58 @@
 #!/bin/zsh
 ginit() {
-	git init;
-	git add ${*:-"."};
-	git commit -a -m "Initial Commit"
+    git init;
+    git add ${*:-"."};
+    git commit -a -m "Initial Commit"
 }
 
-gst() {
-	if [[ "${vcs_info_msg_0_[2,3]}" == 'hg' ]]; then
-		hg status "$@"
-	else
-		git status "$@"
-	fi
+# define a function that calls different versioning systems depending on
+# detected vcs for current directory
+# usage: defvcsfun <command name> <parameters for git> <parameters for hg> <parameters for svn>
+# see below for examples
+defvcsfun() {
+    local name=$1; shift
+    local args=($@)
+    eval "$name() {
+        case \$VCS_DETECTED in
+            git)
+                git ${=args[1]} \"\$@\";;
+            hg)
+                hg ${=args[2]} \"\$@\";;
+            svn)
+                svn ${=args[3]} \"\$@\";;
+            *)
+                echo \"unknown vcs: \$VCS_DETECTED\";;
+        esac
+    }"
 }
 
-gpl() {
-	if [[ "${vcs_info_msg_0_[2,3]}" == 'hg' ]]; then
-		hg pull "$@"
-	else
-		git pull "$@"
-	fi
-}
+#VCS      command   git             hg              svn
+defvcsfun gst       "status -s"     "status"        "status"
+defvcsfun gpl       "pull"          "pull"          "update"
+defvcsfun gcm       "commit"        "commit"        "commit"
+defvcsfun ga        "add"           "add"           "add"
+defvcsfun gpu       "push"          "push"          "i_am_not_distributed_please_dont_push_me"
 
-alias gca="git commit -a"
-alias gcm="git commit"
 alias gco="git checkout"
-alias ga="git add"
+alias gca="git commit -a"
 alias gap="git add --patch"
-alias gpu="git push"
 
 
 gls() {
-	zmodload zsh/mapfile
-	gitignore="$(git rev-parse --show-toplevel 2>/dev/null)/.gitignore"
-	globalgitignore="$(git config core.excludesfile)"
+    zmodload zsh/mapfile
+    gitignore="$(git rev-parse --show-toplevel 2>/dev/null)/.gitignore"
+    globalgitignore="$(git config core.excludesfile)"
 
-	typeset -a ignores
-	for i in $gitignore $globalgitignore; do
-		if [[ -e $i ]]; then
-			for ignore in "${(f)mapfile[$i]}"; do
-				ignores+="--ignore=$ignore"
-			done
-		fi
-	done
+    typeset -a ignores
+    for i in $gitignore $globalgitignore; do
+        if [[ -e $i ]]; then
+            for ignore in "${(f)mapfile[$i]}"; do
+                ignores+="--ignore=$ignore"
+            done
+        fi
+    done
 
-	ls --color=auto -h $ignores "$@"
+    ls --color=auto -h $ignores "$@"
 }
 
 alias gll="gls -l"