Add git-project-clone.sh
authorHugo Villeneuve <hvilleneuve@addenergie.ca>
Fri, 5 Jul 2019 14:01:22 +0000 (10:01 -0400)
committerHugo Villeneuve <hvilleneuve@addenergie.ca>
Fri, 5 Jul 2019 14:20:48 +0000 (10:20 -0400)
scripts/Makefile.am
scripts/git-project-clone.sh [new file with mode: 0755]

index 48eefa6..56cd27c 100644 (file)
@@ -27,6 +27,7 @@ dist_bin_SCRIPTS = \
     git-project-fetch.sh \
     git-project-list.sh \
     git-project-update.sh \
+    git-project-clone.sh \
     source-code-stats \
     flac2ogg flac2mp3 \
     hv-scan \
diff --git a/scripts/git-project-clone.sh b/scripts/git-project-clone.sh
new file mode 100755 (executable)
index 0000000..7c3ed79
--- /dev/null
@@ -0,0 +1,101 @@
+#!/bin/bash
+
+SCRIPT="`readlink -e $0`"
+SCRIPTPATH=$(dirname ${SCRIPT})
+
+SUBREPOS_LIST=.gitsubrepos
+
+# Uncomment to have verbose debug output
+##debug=1
+
+print_usage()
+{
+    echo "$(basename $0) -- GIT clone branch for multiple subrepositories"
+    echo "Usage: $(basename $0) [OPTIONS...] BRANCH_FROM BRANCH_NEW"
+}
+
+if [ "x${1}" = "x--help" ]; then
+    print_usage
+    exit 1
+fi
+
+if [ ${#} -ne 2 ]; then
+    echo "Missing arguments"
+    print_usage
+    exit 1
+fi
+
+if [ ! -f ${SUBREPOS_LIST} ]; then
+    echo "Missing file for list of subrepos: ${SUBREPOS_LIST}"
+    print_usage
+    exit 1
+fi
+
+branch_from=${1}
+branch_new=${2}
+rc=0
+
+clone_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
+
+    exists=$(git show-ref refs/heads/${branch_new})
+    if [ -n "$exists" ]; then
+        echo "${b} (already created)"
+    else
+        # Create branch only if it doesn't already exist
+        vco -q -b ${branch_new} 1> /dev/null
+
+        if [ ${?} -ne 0 ]; then
+            echo "Error creating new branch: ${branch_new}"
+            rc=1
+        else
+            echo "${b}"
+        fi
+    fi
+
+    popd 1> /dev/null
+}
+
+# First, try to update all subrepos to BRANCH_FROM
+git-project-update.sh ${branch_from}
+
+if [ ${?} -ne 0 ]; then
+    echo "Error switching to branch"
+    exit 1
+fi
+
+clone_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
+
+    clone_repo ${r}
+done < ${SUBREPOS_LIST}
+
+exit ${rc}