From c61c1ca3917f2377553df26a5290e5dfb8780631 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Jun 12 2014 20:36:41 +0000 Subject: handle detached branch case handle alternate directory name case cmdline args to override branch and source url --- diff --git a/get_sources.sh b/get_sources.sh index ab816f2..6451bd0 100755 --- a/get_sources.sh +++ b/get_sources.sh @@ -11,31 +11,91 @@ surl="https://git.centos.org/sources/" -pn=$(basename `pwd`) -f=.${pn}.metadata - # for setting any overrides, such as surl or f if [ -f /etc/centos-git-common ]; then . /etc/centos-git-common fi -if [ ! -e ${f} ] || [ ! -d .git ] || [ ! -d SPECS ]; then +#parse command line args +BRANCH='' +while (($# > 0)) +do + case $1 in + --branch) + #specify branch instead of asking git + BRANCH=$2 + shift 2 + ;; + --surl) + #override sources url + surl=$2 + shift 2 + ;; + esac +done + +# check metadata file and extract package name +found=0 +shopt -s nullglob +for meta in .*.metadata +do + pn=${meta%.metadata} + pn=${pn#.} + if [ -e "SPECS/$pn.spec" ] + then + found=1 + break + fi +done +if (( $found != 1 )) +then + echo 'Missing metadata or spec. Please run from inside a sources git repo' + exit 1 +fi + +if [ ! -d .git ] || [ ! -d SPECS ]; then echo 'You need to run this from inside a sources git repo' exit 1 fi mkdir -p SOURCES -br=$(cat .git/HEAD |awk -F'/' '{print $3}' ) -while read a ; do - fsha=$( echo ${a} | cut -f1 -d\ ) - fname=$( echo ${a} | cut -f2 -d\ ) - if [ ${fsha} = "da39a3ee5e6b4b0d3255bfef95601890afd80709" ]; then + +# sort out our branch +if [ -n "$BRANCH" ] +then + branches=("$BRANCH") +else + # generate a list of all branches containing current HEAD + branches=() + while IFS='' read -r line + do + # input from: git branch --contains HEAD + branch="${line:2}" + if [[ "$branch" =~ "detached from" ]] + then + # ignore detached heads + continue + fi + if [ ".${line:0:1}" = ".*" ] + then + # current branch, put it first + branches=("$branch" "${branches[@]}") + else + branches=("${branches[@]}" "$branch") + fi + done <<< "$(git branch --contains HEAD)" +fi +while read -r fsha fname ; do + if [ ".${fsha}" = ".da39a3ee5e6b4b0d3255bfef95601890afd80709" ]; then # zero byte file touch ${fname} else - if [ ! -e ${fname} ]; then - curl ${surl}/${pn}/${br}/${fsha} -o ${fname} + if [ ! -e "${fname}" ]; then + for br in "${branches[@]}" + do + curl -f "${surl}/${pn}/${br}/${fsha}" -o "${fname}" && break + done else echo "${fname} exists. skipping" fi fi -done < ${f} +done < "${meta}"