universal_dependencies / tools /01_fetch_ud_repos.sh
iiegn's picture
Fix: Add git checkout UD_REV bc git fetch UD_REV sometimes misses
e41e43a verified
#!/usr/bin/env bash
#
# This script is based on:
# https://github.com/UniversalDependencies/docs-automation
#
# ./fetch_ud_repos.sh -h
#
# Set default vars and names directories and output files
[ -e .env ] && . .env
ONLINE_MODE=false
UD_VER=${UD_VER:-"2.15"}
UD_REV="r${UD_VER}"
UDS_SUBDIR="UD_repos"; mkdir -p ${UDS_SUBDIR}
GIT_API_OUTPUT=".UniversalDependencies.repos"
GIT_SUBMODULES_ADD=".UD_submodules_add.commands"
# Additional arguments for the `git submodule add` command(s)
###################
#############
#
# USAGE
#
usage() { OUTPUT=${1:-"verbose"}
echo "Usage: $0 [-o] [-r REV]" >&2
[ $OUTPUT = "short" ] && exit 0
>&2 cat << EOF
Identify all relevant UD repositories from GitHub:
* https://github.com/UniversalDependencies/
and generate a list of submodules to add to a (newly created) repository in the
subdirectory ${UDS_SUBDIR}/.
"${GIT_API_OUTPUT}" contains the (saved) list of GitHub repositories; the
file can be updated by running this script with the '-o|--online' option.
"${GIT_SUBMODULES_ADD}" contains the list of submodules to add to the main
repository.
Options:
-h Print this help message
-o|--online Enable online mode: (re-fetch UD repos from GitHub instead
of using ${GIT_API_OUTPUT})
-r|--rev VER The UD GitHub tagged version to checkout (default: ${UD_VER})
EOF
}
#
######
############
##################
VALID_ARGS=$(getopt -o hor: --long help,online,rev: -- "$@")
if [[ $? -ne 0 ]]; then
usage "short"
exit 1;
fi
eval set -- "$VALID_ARGS"
while [ : ]
do
case "$1" in
-o | --online) ONLINE_MODE=true; shift ;;
-r | --rev) UD_VER="$2"; shift 2 ;;
-h | --help) usage; shift ; exit 0;;
--) shift ; break ;;
*) >&2 echo Unsupported option: $1; usage; exit 1;;
esac
done
set -uo pipefail
# Function to get github API content
get_github_api_content() {
# We are interested in these keys:
#
# "git_url": "git://github.com/UniversalDependencies/docs.git",
# "ssh_url": "git@github.com:UniversalDependencies/docs.git",
# "clone_url": "https://github.com/UniversalDependencies/docs.git",
# "svn_url": "https://github.com/UniversalDependencies/docs",
# "homepage": "http://universaldependencies.org/",
#
# -> "clone_url"
#This will last us to 300 repositories
(for pg in 1 2 3; do wget "https://api.github.com/orgs/UniversalDependencies/repos?page=$pg&per_page=100" -O - ; done ) \
| grep clone_url \
| grep -Po 'https://.*?(?=")' \
> ${UDS_SUBDIR}/${GIT_API_OUTPUT}
}
# Check if the online argument is present
if [[ $ONLINE_MODE == true ]] || [[ ! -e ${UDS_SUBDIR}/${GIT_API_OUTPUT} ]]; then
# Download github API content
get_github_api_content
fi
MSG=""
echo "" > ${UDS_SUBDIR}/${GIT_SUBMODULES_ADD}
for r in $(cat ${UDS_SUBDIR}/${GIT_API_OUTPUT} | sort)
do
l=$(echo $r | perl -pe 's/.*\///' | cut -f 1 -d.)
# Process all potential UD_ repos
if [[ $l == UD_* ]] && [[ $l != UD_v2 ]]
then
# Create a file for the full set
echo "git submodule add --depth 1 -- $r $l" \
>> ${UDS_SUBDIR}/${GIT_SUBMODULES_ADD}
# Missing subdir
if [[ ! -e ${UDS_SUBDIR}/$l ]]
then
# git submodule add
# [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>]
# [--ref-format <format>] [--depth <depth>] [--] <repository> [<path>]
MSG="$MSG\n${UDS_SUBDIR}/$l"
fi
fi
done
# git show-ref refs/tags/${UD_REV}
# echo "git submodule --quiet foreach 'export name; bash -c \"(git fetch --depth 1 origin refs/tags/${UD_REV}:refs/tags/${UD_REV} ) || (echo \$name; pushd ../..; git submodule deinit -f \$name; popd)\" '" >> ${GIT_SUBMODULES_ADD}
echo "git submodule --quiet foreach 'export name; bash -c \"( REF=\$(git ls-remote --exit-code origin refs/tags/${UD_REV} | cut -f1) && [ ! -z \\\$REF ] && git fetch --depth=1 origin \\\$REF && git checkout \\\$REF && echo \\\$REF > .tag-${UD_REV}) || ( echo No rev:${UD_REV} \\\$name )\" ' " >> ${UDS_SUBDIR}/${GIT_SUBMODULES_ADD}
echo "git submodule --quiet foreach git status -s | grep -v .tag-" >> ${UDS_SUBDIR}/${GIT_SUBMODULES_ADD}
echo "Missing repos:"
echo -e "$MSG"
if [[ ! -e ${UDS_SUBDIR}/.git ]]
then
echo "${UDS_SUBDIR}: "'missing .git: run `git init` in subdirectory!'
fi