paulsd / centos-git-common

Forked from centos-git-common 4 years ago
Clone
Blob Blame History Raw
#!/bin/bash
#
# Script to parse the non-text sources metadata file and download
# the required files from the lookaside cache
#
# Please note: this script is non-destructive, it wont replace
# files that already exist, regardless of their state, allowing you
# to have work-in-progress content that wont get overwritten.
#
# Might want to drop this in ~/bin/ and chmod u+x it

surl="https://git.centos.org/sources/"

# for setting any overrides, such as surl or f
if [ -f /etc/centos-git-common ]; then
  . /etc/centos-git-common
fi

#parse command line args
BRANCH=''
QUIET=''
CHECK=0
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
        ;;
    --check)
        #verify the sha1sum of the downloaded file
        CHECK=1
        shift
        ;;
     -q)
        # Be less chatty
        QUIET='--silent'
        shift
        ;;
  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

# 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
      for br in "${branches[@]}"
      do
        curl ${QUIET} -f "${surl}/${pn}/${br}/${fsha}" -o "${fname}" && break
      done
    else
      echo "${fname} exists. skipping"
    fi
    if [ ${CHECK} -eq 1 ]; then
        downsum=$(sha1sum ${fname} | awk '{print $1}')
        if [ ${fsha} != ${downsum} ]; then
            rm -f ${fname}
            echo "failed to download ${fname}" >&2
        fi
    fi
  fi
done < "${meta}"