Improve clone by refusing to run if from branch is not found
authorHugo Villeneuve <hvilleneuve@addenergie.ca>
Fri, 5 Jul 2019 14:28:11 +0000 (10:28 -0400)
committerHugo Villeneuve <hvilleneuve@addenergie.ca>
Fri, 5 Jul 2019 14:28:11 +0000 (10:28 -0400)
scripts/git-project-clone.sh

index 7c3ed79..4da6c4f 100755 (executable)
@@ -50,15 +50,18 @@ clone_repo()
 
     pushd "${r}" 1> /dev/null
 
-    exists=$(git show-ref refs/heads/${branch_new})
+    b=${branch_new}
+    exists=$(git show-ref refs/heads/${b})
     if [ -n "$exists" ]; then
+        # Switch to existing branch
+        vco -q ${b} 1> /dev/null
         echo "${b} (already created)"
     else
         # Create branch only if it doesn't already exist
-        vco -q -b ${branch_new} 1> /dev/null
+        vco -q -b ${b} 1> /dev/null
 
         if [ ${?} -ne 0 ]; then
-            echo "Error creating new branch: ${branch_new}"
+            echo "Error creating new branch: ${b}"
             rc=1
         else
             echo "${b}"
@@ -68,10 +71,65 @@ clone_repo()
     popd 1> /dev/null
 }
 
+update_repo()
+{
+    local valid="0"
+
+    if [ ${#} -ne 1 ]; then
+        echo "Missing repository name"
+        exit 1
+    fi
+
+    local r=${1}
+
+    echo -n "Repo ${r}: "
+
+    pushd "${r}" 1> /dev/null
+    b=${branch_from}
+
+    exists=$(git show-ref refs/heads/${b})
+    if [ -n "$exists" ]; then
+        vco -q ${b} 1> /dev/null
+
+        if [ ${?} -ne 0 ]; then
+            rc=1
+            echo "${b} (error)"
+        else
+            echo "${b}"
+        fi
+    else
+        echo "${b} (not found)"
+        rc=1
+    fi
+
+    popd 1> /dev/null
+}
+
 # First, try to update all subrepos to BRANCH_FROM
-git-project-update.sh ${branch_from}
 
-if [ ${?} -ne 0 ]; then
+update_repo ./
+
+# Read list of repositories from file named .gitsubrepos
+while IFS=$'\n' read r ; do
+    [[ "${r}" =~ \#.* ]] && continue # Skip comment lines
+    [ -z ${r} ] && continue # Skip enmpty lines
+
+    # Make sure directory exists
+    if [ ! -d "${r}" ]; then
+        echo "Missing repos ${r}"
+        exit 1
+    fi
+
+    # Update only git repos
+    if [ ! -d "${r}/.git" ]; then
+        echo "Not a GIT repository"
+        exit 1
+    fi
+
+    update_repo ${r}
+done < ${SUBREPOS_LIST}
+
+if [ ${rc} -ne 0 ]; then
     echo "Error switching to branch"
     exit 1
 fi