]> git.nbdom.net Git - nb.git/commitdiff
gitcd
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 18 Aug 2016 21:51:58 +0000 (22:51 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 18 Aug 2016 21:51:58 +0000 (22:51 +0100)
etc/profile.d/git.sh

index bac0a023714add0621b8308577fd3bd8b6c532e3..bf517098352df0e42663ad60c677787add85c4eb 100644 (file)
@@ -24,27 +24,43 @@ git_cache_rm() {
 }
 
 gitcd() {
-  shell_help_noarg "Change directory to file's dirname\nUsage: $FUNCNAME GIT_ARGUMENTS" "$@" && return 1
+
+  shell_help_noarg "Change directory to run git commands\nUsage: $FUNCNAME FILE|DIR GIT_ARGUMENTS" "$@" && return 1
   local i d dir args
   local pwd; pwd=$PWD
   #for i in $@; do
 
   while [ $# -gt 0 ]; do
+
     i="$1"
+
     # Replace file with realpath
     if [ -e "$i" ]; then
       i=$(realpath "$i")
+
+      # if dir stay there
+      [ -d "$i" ] && i="$i/.git"
+
       d=$(dirname "$i")
-      [ -z "$dir" ] && cd "$d" || return
+
       dir="$d"
     fi
+
     shell_push_arg "$i" args
     shift
+
   done
+
   [ -z "$dir" ] && echo "Can't find any file in $@" && return 1
+
+  cd "$dir" || return
+  #echo PWD=$(pwd)
+
   eval git ${args}
+
   cd "$pwd"
-}
+
+} # < gitcd()
 
 git_commit_push() {
   git commit -a -m "$*" && git push #|| echo "Try: git push --set-upstream origin master"