|
#!/bin/sh |
|
# git-remote-add-centos |
|
# https://gist.github.com/smoser/319519224aac91d3057821e4898cec24 |
|
|
|
REMOVE_REMOTE="" |
|
VERBOSITY=0 |
|
TEMP_D="" |
|
|
|
error() { echo "$@" 1>&2; } |
|
fail() { local r=$?; [ $r -eq 0 ] && r=1; failrc "$r" "$@"; } |
|
failrc() { local r=$1; shift; [ $# -eq 0 ] || error "$@"; exit $r; } |
|
|
|
Usage() { |
|
cat <<EOF |
|
Usage: ${0##*/} [options] [source] package |
|
|
|
Add remote repo for packagke on git.centos.org. |
|
|
|
source can be 'centos' or 'fedora'. Default is centos. |
|
|
|
options: |
|
-o | --name NAME name the remote NAME (default = source) |
|
|
|
Basically writes a remote config stanza like below and does a fetch: |
|
|
|
[remote "NAME"] |
|
url = https://git.centos.org/rpms/PACKAGE.git |
|
fetch = +refs/heads/*:refs/remotes/SOURCE/* |
|
tagopt = --no-tags |
|
fetch = +refs/tags/*:refs/tags/SOURCE/* |
|
EOF |
|
} |
|
|
|
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; } |
|
cleanup() { |
|
if [ -n "$REMOVE_REMOTE" ]; then |
|
error "removing remote $REMOVE_REMOTE" |
|
git remote remove "$REMOVE_REMOTE" |
|
fi |
|
if [ -n "$TEMP_D" -a -d "$TEMP_D" ]; then |
|
rm -Rf "$TEMP_D" |
|
fi |
|
} |
|
|
|
debug() { |
|
local level=${1}; shift; |
|
[ "${level}" -gt "${VERBOSITY}" ] && return |
|
error "${@}" |
|
} |
|
|
|
shellquote() { |
|
# quote the arguments as appropriate for shell. |
|
local x="" wrap="" tickset=$'"!$' quoteset=" '*" tick="'" cmd="" |
|
for x in "$@"; do |
|
if [ "${x#*[${tickset}]}" != "$x" ]; then |
|
wrap="'" |
|
x=${x//$tick/$tick\\$tick$tick} |
|
elif [ "${x#*[$quoteset]}" != "$x" ]; then |
|
wrap='"' |
|
else |
|
wrap="" |
|
fi |
|
cmd="${cmd} $wrap$x$wrap" |
|
done |
|
_RET="${cmd# }" |
|
} |
|
|
|
run(){ |
|
# run(level=[warn,info,debug,trace], [quiet], cmd...) |
|
local rc="" m="" |
|
shellquote "$@" |
|
debug 1 "$_RET" |
|
"$@" |
|
} |
|
|
|
main() { |
|
local short_opts="ho:v" |
|
local long_opts="help,name:,verbose" |
|
local getopt_out="" |
|
getopt_out=$(getopt --name "${0##*/}" \ |
|
--options "${short_opts}" --long "${long_opts}" -- "$@") && |
|
eval set -- "${getopt_out}" || |
|
{ bad_Usage; return; } |
|
|
|
local cur="" next="" rname="" src=centos |
|
|
|
while [ $# -ne 0 ]; do |
|
cur="$1"; next="$2"; |
|
case "$cur" in |
|
-h|--help) Usage ; exit 0;; |
|
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));; |
|
-o|--name) rname=$2; shift;; |
|
--) shift; break;; |
|
esac |
|
shift; |
|
done |
|
|
|
[ $# -eq 1 -o $# -eq 2 ] || { |
|
bad_Usage "got $# args, expected 1 or 2"; |
|
reutrn 1 |
|
} |
|
|
|
[ $# -eq 2 ] && { src=$1; pkg=$2; } |
|
[ $# -eq 1 ] && { src="centos"; pkg=$1; } |
|
[ -z "$rname" ] && rname="$src" |
|
|
|
case "$src" in |
|
centos) url="https://git.centos.org/rpms/$pkg.git";; |
|
fedora) url="https://src.fedoraproject.org/rpms/$pkg.git";; |
|
*) error "Bad source '$src'. expected fedora or centos.";; |
|
esac |
|
|
|
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") || |
|
fail "failed to make tempdir" |
|
trap cleanup EXIT |
|
|
|
if [ ! -d .git ]; then |
|
[ -d "$pkg" ] && fail "no .git dir in $PWD and $pkg exists." |
|
debug 0 "Creating new repo for package $pkg in ./$pkg" |
|
git init "$pkg" || fail "failed git init" |
|
cd "$pkg" |
|
fi |
|
|
|
if git remote | grep -q "^$rname$"; then |
|
fail "$rname remote exists. remove first." |
|
fi |
|
|
|
REMOVE_REMOTE="$rname" |
|
if [ "${SLOW:-0}" != "0" ]; then |
|
# this is simply much slower. |
|
# https://github.com/repoSpanner/repoSpanner/issues/79 |
|
run git remote add --no-tags $rname "$url" || |
|
fail "Failed to add $rname at $url. removing." |
|
|
|
run git config --local --add \ |
|
"remote.$rname.fetch" "+refs/tags/*:refs/tags/$rname/*" || |
|
fail "failed to config remote.$rname.fetch for tags" |
|
|
|
run git fetch -v "$rname" || fail "Failed to fetch $rname." |
|
else |
|
cdir="${TEMP_D}/$rname" |
|
run git clone --bare "$url" "$cdir" || |
|
fail "failed clone $url to $cdir" |
|
run git remote add --no-tags "$rname" "$cdir" || |
|
fail "Failed to add remote $rname" |
|
run git config --local --add \ |
|
"remote.$rname.fetch" "+refs/tags/*:refs/tags/$rname/*" || |
|
fail "failed to config remote.$rname.fetch for tags" |
|
run git fetch -v "$rname" || fail "Failed to fetch $rname." |
|
run git remote set-url "$rname" "$url" || |
|
fail "failed to set url to actual $url" |
|
run git fetch "$rname" || fail "Failed second fetch" |
|
fi |
|
REMOVE_REMOTE="" |
|
exit 0 |
|
} |
|
|
|
main "$@" |
|
# vi: ts=4 expandtab |