repo-forall-functions: Reduce requests on upstream

Don't always do a `git ls-remote` before `git fetch`; only do it in case
of errors to check for details. Especially AOSP is quick in rejecting
connections if we issue too many requests.

Change-Id: I2efe12387a3f9ac3d9f3120f7c0eb7234cc91724
diff --git a/shell-libs/repo-forall-functions.sh b/shell-libs/repo-forall-functions.sh
index abf6cd2..2defa03 100644
--- a/shell-libs/repo-forall-functions.sh
+++ b/shell-libs/repo-forall-functions.sh
@@ -309,12 +309,18 @@
 {
     local from_ref=$1
     local to_ref=$2
-
-    local result=0
-    _remote_ref_exists "${from_ref}" || result=$?
-
-    if [ $result -eq 0 ] ; then
+    # Try fetching first. In case of failures, use `git ls-remote` to check for
+    # details. Don't always call it; otherwise upstream will block connections
+    # because of too many requests.
+    (
+        # _git_fetch exits in case of errors, so run it in a sub shell.
         _git_fetch "${from_ref}:${to_ref}"
+    )
+
+    local result=$?
+    if [ "${result}" -ne 0 ]; then
+        # Try getting some details on what went wrong:
+        _remote_ref_exists "${from_ref}" || true
     fi
 }