: nply/git-summary

commit 51eb04d2b948355ec7e12dd02a00b18ee13a4fb7
parent 78f9e82e27d2a3f577f42220f7b1bac19912ef79
Author: Mirko Ledda <mko.ledda@gmail.com>
Date: Sat, 6 Jan 2018 22:44:43 -0800

added deep git repo lookup
1 file changed, 35 insertions(+), 24 deletions(-)
M
git-summary
|
59
+++++++++++++++++++++++++++++++++++------------------------
diff --git a/git-summary b/git-summary
@@ -11,7 +11,7 @@ usage() {
     sed 's/^        //' <<EOF
         git-summary - summarise git repos at some path
 
-        Usage: git-summary [-h] [-l] [path]
+        Usage: git-summary [-h] [-l] [-d] [path]
 
         Given a path to a folder containing one or more git repos,
         print a status summary table showing, for each repo:

@@ -38,6 +38,9 @@ usage() {
                 unpulled commits.  As this can be time consuming, this
                 flag lets you skip that.
 
+          -d    Deep lookup. Will search within the entire tree of the
+                current folder.
+
           path  Path to folder containing git repos; if omitted, the
                 current working directory is used.
 

@@ -50,17 +53,19 @@ git_summary() {
 
     local local_only=0
     local opt
-    while getopts "hl" opt; do
+    local deeplookup=0
+    while getopts "hld" opt; do
         case "${opt}" in
             h) usage ; exit 1 ;;
             l) local_only=1 ;;    # Will skip "git fetch"
+            d) deeplookup=1 ;;
         esac
     done
     shift $((OPTIND-1))
 
     # Use provided path, or default to pwd
     local target=$(${readlink_cmd} -f ${1:-`pwd`})
-    local repos=$(list_repos $target)
+    local repos=$(list_repos $target $deeplookup)
 
     if [[ -z $repos ]]; then
     	exit

@@ -82,12 +87,9 @@ git_summary() {
     print_header "$template" $max_repo_len $max_branch_len
 
     local f
-    local here=`pwd`
     for f in $repos ; do
-        cd $target/$f
-        summarise_one_git_repo "$template" "$local_only" >&1
+        summarise_one_git_repo $f "$template" "$local_only" >&1
     done
-    cd $here
 }
 
 # Autodetect the OS

@@ -122,31 +124,32 @@ print_header () {
 
 summarise_one_git_repo () {
 
-    local template=$1
-    local local_only=$2
+    local f=$1
+    local template=$2
+    local local_only=$3
 
-    local app_name=`basename $(pwd)`
-    local branch_name=`git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"`
+    local app_name=$f
+    local branch_name=`git -C $f symbolic-ref HEAD | sed -e "s/^refs\/heads\///"`
     printf "$template" $app_name $branch_name >&1
 
     local state=""
-    local untracked=`git status | grep Untracked -c`
-    local new_files=`git status | grep "new file" -c`
-    local modified=`git status | grep modified -c`
+    local untracked=`git -C $f status | grep Untracked -c`
+    local new_files=`git -C $f status | grep "new file" -c`
+    local modified=`git -C $f status | grep modified -c`
     state+=$([ $untracked -ne 0 ] && echo "?" || echo " ")
     state+=$([ $new_files -ne 0 ] && echo "+" || echo " ")
     state+=$([ $modified  -ne 0 ] && echo "M" || echo " ")
     echo -n "$state" >&1
 
     local rstate=""
-    local has_upstream=`git rev-parse --abbrev-ref @{u} 2> /dev/null | wc -l`
+    local has_upstream=`git -C $f rev-parse --abbrev-ref @{u} 2> /dev/null | wc -l`
     if [ $has_upstream -ne 0 ] ; then
         if [ $local_only -eq 0 ] ; then
             git fetch -q &> /dev/null
         fi
         # Unpulled and unpushed on *current* branch
-        local unpulled=`git log --pretty=format:'%h' ..@{u} | wc -c`
-        local unpushed=`git log --pretty=format:'%h' @{u}.. | wc -c`
+        local unpulled=`git -C $f log --pretty=format:'%h' ..@{u} | wc -c`
+        local unpushed=`git -C $f log --pretty=format:'%h' @{u}.. | wc -c`
         rstate+=$([ $unpulled -ne 0 ] && echo "v" || echo " ")
         rstate+=$([ $unpushed -ne 0 ] && echo "^" || echo " ")
     else

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

@@ -168,25 +171,33 @@ repo_branch () {
 list_repos () {
 	# https://stackoverflow.com/questions/23356779/how-can-i-store-find-command-result-as-arrays-in-bash
 	git_directories=()
+
+  local find_cmd
+  if [ $deeplookup -eq 0 ]; then
+    find_cmd="find $1 -maxdepth 2 -type d -name .git -print0"
+  else
+    find_cmd="find $1 -type d -name .git -print0"
+  fi
+
 	while IFS=  read -r -d $'\0'; do
 		git_directories+=("$REPLY")
-	done < <(find $1 -maxdepth 2 -type 'd' -name ".git" -print0)
+	done < <($find_cmd)
 
 	for i in ${git_directories[*]}; do
 		if [[ ! -z $i ]]; then
-    		$dirname_cmd -z $i | xargs -0 -L1 basename
+    		$dirname_cmd -z $i | xargs -0 -L1
     	fi
-    done
+  done
 }
 
 
 # Given the path to a folder containing git some repos, compute the
 # names of the current branches in the repos.
 repo_branches () {
-    local root=$1
+    local path=$1
     local repo
-    for repo in $(list_repos $root) ; do
-        echo $(repo_branch $root/$repo)
+    for repo in $(list_repos $path) ; do
+        echo $(repo_branch $repo)
     done
 }