: nply/git-summary

commit cfd22b783bed6e72dc3d51f111fbfa1e1bf1f186
parent d892f513894e05b38dd9c1b7588fe1039af969e7
Author: Mirko Ledda <mko.ledda@gmail.com>
Date: Mon, 2 Jul 2018 22:40:57 -0700

Merge pull request #4 from ruricolist/master

Merge request
1 file changed, 60 insertions(+), 13 deletions(-)
M
git-summary
|
73
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
diff --git a/git-summary b/git-summary
@@ -6,6 +6,8 @@
 #
 # Freely distributed under the MIT license. 2018@MirkoLedda
 
+set -eu
+
 # Colorcode
 GREEN='\e[0;32m'
 ORANGE='\e[0;33m'

@@ -17,7 +19,7 @@ usage() {
     sed 's/^        //' <<EOF
         git-summary - summarize git repos at some path
 
-        Usage: git-summary.sh [-h] [-l] [-d] [path]
+        Usage: git-summary.sh [-h] [-l] [-d] [-q] [path]
 
         Given a path to a folder containing one or more git repos,
         print a status summary table showing, for each repo:

@@ -47,6 +49,9 @@ usage() {
           -d    Deep lookup. Will search within the entire tree of the
                 current folder.
 
+          -q    Print nothing for repos that are up to date. Also print
+                a final tally.
+
           path  Path to folder containing git repos; if omitted, the
                 current working directory is used.
 

@@ -57,15 +62,18 @@ EOF
 git_summary() {
 
     detect_OS
+    detect_Git4Windows
 
     local local_only=0
     local opt
     local deeplookup=0
-    while getopts "hld" opt; do
+    local quiet=0
+    while getopts "hldq" opt; do
         case "${opt}" in
             h) usage ; exit 1 ;;
             l) local_only=1 ;;    # Will skip "git fetch"
             d) deeplookup=1 ;;
+            q) quiet=1 ;;
         esac
     done
     shift $((OPTIND-1))

@@ -92,25 +100,61 @@ git_summary() {
     local template=$(printf "%%b%%-%ds  %%-%ds  %%-5s" $max_repo_len $max_branch_len)
     print_header "$template" $max_repo_len $max_branch_len
 
+    local repo_count=0
+
     local f
     for f in $repos ; do
-        summarize_one_git_repo $f "$template" "$local_only" >&1
+        summarize_one_git_repo $f "$template" "$local_only" "$quiet" >&1 &
+        (( repo_count+=1 ))
     done
+    wait
+
+    if [ $quiet -eq 1 ]; then
+        echo "Checked ${repo_count} repositories."
+    fi
+
 }
 
 # Autodetect the OS
 detect_OS() {
   if [ "$(uname)" == "Darwin" ]; then  # macOS
+    OS=Darwin
     readlink_cmd="greadlink"
     dirname_cmd="gdirname"
     gawk_cmd="awk"
   elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then  # Linux
+    OS=Linux
     readlink_cmd="readlink"
     dirname_cmd="dirname"
     gawk_cmd="gawk"
+  elif [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then  # Cygwin
+    OS=CYGWIN
+    readlink_cmd="readlink"
+    dirname_cmd="dirname"
+    gawk_cmd="gawk"
+  else
+    echo "Cannot identify OS."
+    exit 1
   fi
 }
 
+GIT4WINDOWS=1
+
+detect_Git4Windows() {
+    if [[ "$OS" == "CYGWIN" && "$(git --version)" == *"windows"* ]]; then
+       GIT4WINDOWS=0
+  fi
+}
+
+gitC() {
+    local ldir=$1; shift;
+    if [ $GIT4WINDOWS -eq 0 ]; then
+       git -C "$(cygpath -w $ldir)" "$@"
+    else
+       git -C "$ldir" "$@"
+    fi
+}
+
 
 print_header () {
     local template="$1"

@@ -136,21 +180,22 @@ summarize_one_git_repo () {
     local f=$1
     local template=$2
     local local_only=$3
+    local quiet=$4
 
     local app_name=$f
-    local branch_name=`git -C $f symbolic-ref HEAD | sed -e "s/^refs\/heads\///"`
+    local branch_name=`gitC $f symbolic-ref HEAD | sed -e "s/^refs\/heads\///"`
     local numState=0
 
     ### Check remote state
     local rstate=""
-    local has_upstream=`git -C $f rev-parse --abbrev-ref @{u} 2> /dev/null | wc -l`
+    local has_upstream=`gitC $f rev-parse --abbrev-ref @{u} 2> /dev/null | wc -l`
     if [ $has_upstream -ne 0 ] ; then
         if [ $local_only -eq 0 ] ; then
-            git -C $f fetch -q &> /dev/null
+            gitC $f fetch -q &> /dev/null
         fi
         # Unpulled and unpushed on *current* branch
-        local unpulled=`git -C $f log --pretty=format:'%h' ..@{u} | wc -c`
-        local unpushed=`git -C $f log --pretty=format:'%h' @{u}.. | wc -c`
+        local unpulled=`gitC $f log --pretty=format:'%h' ..@{u} | wc -c`
+        local unpushed=`gitC $f log --pretty=format:'%h' @{u}.. | wc -c`
 
         if [ $unpulled -ne 0 ]; then
           rstate="${rstate}v"

@@ -172,9 +217,9 @@ summarize_one_git_repo () {
 
     ### Check local state
     local state=""
-    local untracked=`LC_ALL=C git -C $f status | grep Untracked -c`
-    local new_files=`LC_ALL=C git -C $f status | grep "new file" -c`
-    local modified=`LC_ALL=C git -C $f status | grep modified -c`
+    local untracked=`LC_ALL=C gitC $f status | grep Untracked -c`
+    local new_files=`LC_ALL=C gitC $f status | grep "new file" -c`
+    local modified=`LC_ALL=C gitC $f status | grep modified -c`
 
     if [ $untracked -ne 0 ]; then
       state="${state}?"

@@ -199,7 +244,9 @@ summarize_one_git_repo () {
 
     ### Print to stdout
     if [ $numState -eq 0 ]; then
-      printf "$template\n" $GREEN $app_name $branch_name "$state$rstate" >&1
+        if [ $quiet -eq 0 ]; then
+            printf "$template\n" $GREEN $app_name $branch_name "$state$rstate" >&1
+        fi
     elif [ $numState -eq 1 ]; then
       printf "$template\n" $ORANGE $app_name $branch_name "$state$rstate" >&1
     elif [ $numState -eq 2 ]; then

@@ -210,7 +257,7 @@ summarize_one_git_repo () {
 
 # Given the path to a git repo, compute its current branch name.
 repo_branch () {
-    git -C $1 symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
+    gitC "$1" symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
 }