]> git.phdru.name Git - dotfiles.git/commitdiff
.shellrc: Improve `cdremote`
authorOleg Broytman <phd@phdru.name>
Mon, 5 Aug 2019 14:47:50 +0000 (17:47 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 5 Aug 2019 14:47:50 +0000 (17:47 +0300)
If the remote URL for the current branch is not a filesystem path
try to find any remote with path as the URL;
if one is found `cd` to the path, else report error.

.shellrc

index d9a20856b7dd7d342a2f1e8110c7c169cdc8cb6a..9a484c15d96f836e0362a4004b71128e7e81dfda 100644 (file)
--- a/.shellrc
+++ b/.shellrc
@@ -221,7 +221,27 @@ if test -x /usr/bin/git >/dev/null 2>&1; then
          return 1
       fi
 
-      cdgitpath remote.$remote.url
+      if git config --get remote.$remote.url | grep -q '^\(/\|\.\./\)'; then
+         cdgitpath remote.$remote.url
+      else
+
+         local GIT_REMOTES remote
+         GIT_REMOTES=""
+         for remote in `git remote`; do
+            if git config --get remote.$remote.url | grep -q '^\(/\|\.\./\)'; then
+               GIT_REMOTES="$GIT_REMOTES $remote"
+            fi
+         done
+         GIT_REMOTES=($GIT_REMOTES)
+         if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
+            remote=${GIT_REMOTES[0]}
+            cdgitpath remote.$remote.url
+         else
+            echo "Cannot find directory for any remote" >&2
+            echo "Usage: cdremote [remote_name]" >&2
+            return 1
+         fi
+      fi
    }
 
    if test -n "$BASH_VERSION"; then