From 17221ee06c14d4e39dd45e9c5b779b0f915f2f4e Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Sat, 28 Mar 2020 15:11:59 -0400 Subject: [PATCH] Group common git-project functions in script --- scripts/Makefile.am | 2 + scripts/git-project-clone.sh | 85 +++---------------- scripts/git-project-list.sh | 34 ++------ scripts/git-project-tag.sh | 37 +++++++++ scripts/git-project-update.sh | 68 ++-------------- scripts/hv-git-functions.sh | 149 ++++++++++++++++++++++++++++++++++ 6 files changed, 213 insertions(+), 162 deletions(-) create mode 100755 scripts/git-project-tag.sh create mode 100644 scripts/hv-git-functions.sh diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 8d32e91..91a359e 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -24,10 +24,12 @@ dist_bin_SCRIPTS = \ hg-format-patch hg-update-subrepos \ git-hg-sub-import git-hg-sub-update \ git-repos-update-clean \ + hv-git-functions.sh \ git-project-fetch.sh \ git-project-list.sh \ git-project-update.sh \ git-project-clone.sh \ + git-project-tag.sh \ source-code-stats \ flac2ogg flac2mp3 \ hv-scan \ diff --git a/scripts/git-project-clone.sh b/scripts/git-project-clone.sh index 49a40b6..44d0eba 100755 --- a/scripts/git-project-clone.sh +++ b/scripts/git-project-clone.sh @@ -2,8 +2,7 @@ SCRIPT="`readlink -e $0`" SCRIPTPATH=$(dirname ${SCRIPT}) - -SUBREPOS_LIST=.gitsubrepos +source ${SCRIPTPATH}/hv-git-functions.sh # Uncomment to have verbose debug output ##debug=1 @@ -25,11 +24,7 @@ if [ ${#} -ne 2 ]; then exit 1 fi -if [ ! -f ${SUBREPOS_LIST} ]; then - echo "Missing file for list of subrepos: ${SUBREPOS_LIST}" - print_usage - exit 1 -fi +hv_git_validate_subrepos_list branch_from=${1} branch_new=${2} @@ -37,8 +32,6 @@ rc=0 clone_repo() { - local valid="0" - if [ ${#} -ne 1 ]; then echo "Missing repository name" exit 1 @@ -71,89 +64,33 @@ 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 -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 - +for r in ./ $(hv_git_get_subrepos_list); do # Make sure directory exists if [ ! -d "${r}" ]; then echo "Missing repos ${r}, skipping" continue fi - # Update only git repos - if [ ! -d "${r}/.git" ]; then - echo "Not a GIT repository" + hv_git_checkout ${r} ${branch_from} + if [ ${?} -ne 0 ]; then + echo "Error switching to ${branch_from} in repos ${r}" exit 1 fi +done - update_repo ${r} -done < ${SUBREPOS_LIST} - -if [ ${rc} -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 +# Then clone repo +for r in ./ $(hv_git_get_subrepos_list); do # Make sure directory exists if [ ! -d "${r}" ]; then echo "Missing repos ${r}, skipping" continue fi - # Update only git repos - if [ ! -d "${r}/.git" ]; then - echo "Not a GIT repository" - exit 1 - fi - clone_repo ${r} -done < ${SUBREPOS_LIST} + rc=${?} +done exit ${rc} diff --git a/scripts/git-project-list.sh b/scripts/git-project-list.sh index 0ca38de..7480026 100755 --- a/scripts/git-project-list.sh +++ b/scripts/git-project-list.sh @@ -2,11 +2,7 @@ SCRIPT="`readlink -e $0`" SCRIPTPATH=$(dirname ${SCRIPT}) - -SUBREPOS_LIST=.gitsubrepos - -# Uncomment to have verbose debug output -##debug=1 +source ${SCRIPTPATH}/hv-git-functions.sh print_usage() { @@ -19,11 +15,7 @@ if [ "x${1}" = "x--help" ]; then exit 1 fi -if [ ! -f ${SUBREPOS_LIST} ]; then - echo "Missing file for list of subrepos: ${SUBREPOS_LIST}" - print_usage - exit 1 -fi +hv_git_validate_subrepos_list list_revisions() { @@ -35,29 +27,19 @@ list_revisions() local r=${1} pushd "${r}" 1> /dev/null - local sha=$(git log --oneline HEAD~1..HEAD) - echo "${r}: ${sha}" + local sha=$(git log --abbrev=10 --pretty='format:%h %s' HEAD~1..HEAD) + + local line=' ' + printf "%s %s %s\n" ${r} "${line:${#r}}" "${sha}" popd 1> /dev/null } -list_revisions ./ - -# 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 - +for r in ./ $(hv_git_get_subrepos_list); do # Make sure directory exists if [ ! -d "${r}" ]; then echo "Missing repos ${r}, skipping" continue fi - # Update only git repos - if [ ! -d "${r}/.git" ]; then - echo "Not a GIT repository" - exit 1 - fi - list_revisions ${r} -done < ${SUBREPOS_LIST} +done diff --git a/scripts/git-project-tag.sh b/scripts/git-project-tag.sh new file mode 100755 index 0000000..3cf7673 --- /dev/null +++ b/scripts/git-project-tag.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +SCRIPT="`readlink -e $0`" +SCRIPTPATH=$(dirname ${SCRIPT}) +source ${SCRIPTPATH}/hv-git-functions.sh + +print_usage() +{ + echo "$(basename $0) -- Create tag from branch" + echo "Usage: $(basename $0) [OPTIONS...] BRANCH TAG" +} + +if [ "x${1}" = "x--help" ]; then + print_usage + exit 1 +fi + +if [ ${#} -ne 2 ]; then + echo "Missing arguments" + print_usage + exit 1 +fi + +hv_git_validate_subrepos_list + +branch_name=${1} +tag_name=${2} + +for r in ./ $(hv_git_get_subrepos_list); do + # Make sure directory exists + if [ ! -d "${r}" ]; then + echo "${SCRIPT}: missing repos ${r}, skipping" + continue + fi + + hv_git_tag_from_branch ${r} ${branch_name} ${tag_name} +done diff --git a/scripts/git-project-update.sh b/scripts/git-project-update.sh index b47dcbc..6b6895f 100755 --- a/scripts/git-project-update.sh +++ b/scripts/git-project-update.sh @@ -2,11 +2,7 @@ SCRIPT="`readlink -e $0`" SCRIPTPATH=$(dirname ${SCRIPT}) - -SUBREPOS_LIST=.gitsubrepos - -# Uncomment to have verbose debug output -##debug=1 +source ${SCRIPTPATH}/hv-git-functions.sh print_usage() { @@ -25,72 +21,20 @@ if [ ${#} -ne 1 ]; then exit 1 fi -if [ ! -f ${SUBREPOS_LIST} ]; then - echo "Missing file for list of subrepos: ${SUBREPOS_LIST}" - print_usage - exit 1 -fi +hv_git_validate_subrepos_list branch=${1} rc=0 -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 - - for b in ${branch} latest master; do - exists=$(git show-ref refs/heads/${b}) - if [ -n "$exists" ]; then - vco -q ${b} 1> /dev/null - - if [ ${?} -ne 0 ]; then - rc=1 - fi - echo "${b}" - valid="1" - break - fi - done - - if [ x"${valid}" = x"0" ]; then - echo "No valid branch found" - rc=1 - fi - - popd 1> /dev/null -} - -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 - +for r in ./ $(hv_git_get_subrepos_list); do # Make sure directory exists if [ ! -d "${r}" ]; then echo "Skipping missing repos ${r}" continue fi - # Update only git repos - if [ ! -d "${r}/.git" ]; then - echo "Not a GIT repository" - exit 1 - fi - - update_repo ${r} -done < ${SUBREPOS_LIST} + hv_git_checkout ${r} ${branch} + rc=${?} +done exit ${rc} diff --git a/scripts/hv-git-functions.sh b/scripts/hv-git-functions.sh new file mode 100644 index 0000000..4ab8fe0 --- /dev/null +++ b/scripts/hv-git-functions.sh @@ -0,0 +1,149 @@ +#!/bin/bash + +# Uncomment to have verbose debug output +#hv_git_debug=1 + +SUBREPOS_LIST=.gitsubrepos + +hv_git_validate_subrepos_list() +{ + if [ ! -f ${SUBREPOS_LIST} ]; then + echo "${FUNCNAME}: missing subrepos file list: ${SUBREPOS_LIST}" + print_usage + exit 1 + fi + + # 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 + + # If repo directory is absent, it is ok. + # If repo directory is present, make sure it is a GIT repo + if [ -d "${r}" ]; then + if [ ! -d "${r}/.git" ]; then + echo "${FUNCNAME}: not a GIT repository: ${r}" + echo "BOZO=<${r}/.git>" + exit 1 + fi + fi + done < ${SUBREPOS_LIST} + + return 0 +} + +hv_git_get_subrepos_list() +{ + local list="" + local r="" + local IFS="" + + # 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 + + # Add to list only if directory is present, and if it is a valid + # GIT repo + if [ -d "${r}/.git" ]; then + list="${list} ${r}" + fi + done < ${SUBREPOS_LIST} + + echo "${list}" +} + +# Arg 1: repository +# Arg 2: branch to checkout (or tag) +hv_git_checkout() +{ + if [ ${#} -ne 2 ]; then + echo "${FUNCNAME}: missing arguments" + exit 1 + fi + + local rc=0 + local r=${1} + local b=${2} + + echo -n "Repo ${r}: " + + pushd "${r}" 1> /dev/null + + exists=$(git rev-parse -q --verify ${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 + + return ${rc} +} + +# Create a new tag in repo +# Arg 1: repository +# Arg 2: branch from which to create tag +# Arg 3: new tag name +hv_git_tag_from_branch() +{ + if [ ${#} -ne 3 ]; then + echo "${FUNCNAME}: missing arguments" + exit 1 + fi + + local rc=0 + local sha="" + local r=${1} + local b=${2} + local tag=${3} + + echo -n "Repo ${r}: " + + pushd "${r}" 1> /dev/null + + sha=$(git show-ref refs/heads/${b} | awk '{print $1}') + if [ -n "${sha}" ]; then + git tag ${tag} ${sha} 1> /dev/null + rc=${?} + + if [ ${rc} -ne 0 ]; then + echo "${tag} (error)" + else + echo "${tag}" + fi + else + echo "${FUNCNAME}: branch \"${b}\" not found" + rc=1 + fi + + popd 1> /dev/null + + return ${rc} +} + +# Encore utile? +# Arg1: repository +hv_git_get_head_sha() +{ + if [ ${#} -ne 1 ]; then + echo "Missing repository name" + exit 1 + fi + + local r=${1} + + pushd "${r}" 1> /dev/null + local sha=$(git log --pretty='format:%H' HEAD~1..HEAD) + echo "${sha}" + popd 1> /dev/null +} -- 2.20.1