jcpunk / centos-git-common

Forked from centos-git-common 5 years ago
Clone

Blame get_sources.sh

Karanbir Singh afa935
#!/bin/bash
Karanbir Singh afa935
#
Karanbir Singh afa935
# Script to parse the non-text sources metadata file and download
Karanbir Singh afa935
# the required files from the lookaside cache
Karanbir Singh afa935
#
Karanbir Singh afa935
# Please note: this script is non-destructive, it wont replace
Karanbir Singh afa935
# files that already exist, regardless of their state, allowing you
Karanbir Singh afa935
# to have work-in-progress content that wont get overwritten.
Karanbir Singh afa935
#
Karanbir Singh afa935
# Might want to drop this in ~/bin/ and chmod u+x it
Karanbir Singh afa935
c19d26
surl="https://git.centos.org/sources/"
Karanbir Singh afa935
1842fe
# for setting any overrides, such as surl or f
1842fe
if [ -f /etc/centos-git-common ]; then
1842fe
  . /etc/centos-git-common
1842fe
fi
1842fe
c61c1c
#parse command line args
c61c1c
BRANCH=''
Pat Riehecky 425807
QUIET=''
Pat Riehecky 061172
CHECK=0
c61c1c
while (($# > 0))
c61c1c
do
c61c1c
  case $1 in
c61c1c
    --branch)
c61c1c
        #specify branch instead of asking git
c61c1c
        BRANCH=$2
c61c1c
        shift 2
c61c1c
        ;;
c61c1c
    --surl)
c61c1c
        #override sources url
c61c1c
        surl=$2
c61c1c
        shift 2
c61c1c
        ;;
Pat Riehecky 061172
    --check)
Pat Riehecky 061172
        #verify the sha1sum of the downloaded file
Pat Riehecky 061172
        CHECK=1
Pat Riehecky 061172
        shift
Pat Riehecky 061172
        ;;
Pat Riehecky 425807
     -q)
Pat Riehecky 425807
        # Be less chatty
Pat Riehecky 425807
        QUIET='--silent'
Pat Riehecky 425807
        shift
Pat Riehecky 425807
        ;;
c61c1c
  esac
c61c1c
done
c61c1c
c61c1c
# check metadata file and extract package name
c61c1c
found=0
c61c1c
shopt -s nullglob
c61c1c
for meta in .*.metadata
c61c1c
do
c61c1c
  pn=${meta%.metadata}
c61c1c
  pn=${pn#.}
c61c1c
  if [ -e "SPECS/$pn.spec" ]
c61c1c
  then
c61c1c
      found=1
c61c1c
      break
c61c1c
  fi
c61c1c
done
c61c1c
if (( $found != 1 ))
c61c1c
then
c61c1c
    echo 'Missing metadata or spec. Please run from inside a sources git repo'
c61c1c
    exit 1
c61c1c
fi
c61c1c
c61c1c
if [ ! -d .git ] || [ ! -d SPECS ]; then
Karanbir Singh afa935
  echo 'You need to run this from inside a sources git repo'
Karanbir Singh afa935
  exit 1
Karanbir Singh afa935
fi
f3a920
mkdir -p SOURCES
c61c1c
c61c1c
# sort out our branch
c61c1c
if [ -n "$BRANCH" ]
c61c1c
then
c61c1c
  branches=("$BRANCH")
c61c1c
else
c61c1c
  # generate a list of all branches containing current HEAD
c61c1c
  branches=()
c61c1c
  while IFS='' read -r line
c61c1c
  do
c61c1c
    # input from: git branch --contains HEAD
c61c1c
    branch="${line:2}"
c61c1c
    if [[ "$branch" =~ "detached from" ]]
c61c1c
    then
c61c1c
      # ignore detached heads
c61c1c
      continue
c61c1c
    fi
c61c1c
    if [ ".${line:0:1}" = ".*" ]
c61c1c
    then
c61c1c
      # current branch, put it first
c61c1c
      branches=("$branch" "${branches[@]}")
c61c1c
    else
c61c1c
      branches=("${branches[@]}" "$branch")
c61c1c
    fi
c61c1c
  done <<< "$(git branch --contains HEAD)"
c61c1c
fi
c61c1c
while read -r fsha fname ; do
c61c1c
  if [ ".${fsha}" = ".da39a3ee5e6b4b0d3255bfef95601890afd80709" ]; then
Karanbir Singh afa935
    # zero byte file
a4a7af
    touch ${fname}
Karanbir Singh afa935
  else
c61c1c
    if [ ! -e "${fname}" ]; then
c61c1c
      for br in "${branches[@]}"
c61c1c
      do
Pat Riehecky 425807
        curl ${QUIET} -f "${surl}/${pn}/${br}/${fsha}" -o "${fname}" && break
c61c1c
      done
Karanbir Singh afa935
    else
Karanbir Singh afa935
      echo "${fname} exists. skipping"
Karanbir Singh afa935
    fi
Pat Riehecky 061172
    if [ ${CHECK} -eq 1 ]; then
Pat Riehecky 061172
        downsum=$(sha1sum ${fname} | awk '{print $1}')
Pat Riehecky 061172
        if [ ${fsha} != ${downsum} ]; then
Pat Riehecky 061172
            rm -f ${fname}
Pat Riehecky 061172
            echo "failed to download ${fname}" >&2
Pat Riehecky 061172
        fi
Pat Riehecky 061172
    fi
Karanbir Singh afa935
  fi
c61c1c
done < "${meta}"