From 23883102f04772136742d2280a8bde5fe1fb8eba Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 5 Aug 2019 17:47:50 +0300 Subject: [PATCH] .shellrc: Improve `cdremote` 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 | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/.shellrc b/.shellrc index d9a2085..9a484c1 100644 --- 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 -- 2.39.2