Skip to content

Instantly share code, notes, and snippets.

@smoser
Created June 12, 2019 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/730d5d12be0b4b4bdf630c839104ba0a to your computer and use it in GitHub Desktop.
Save smoser/730d5d12be0b4b4bdf630c839104ba0a to your computer and use it in GitHub Desktop.
add ubuntu git as a remote to current git repo

git-ubuntu is somewhat documented in ubuntu wiki here.

This script simply adds a git remote for a given package.

Then in the same git repo, you can have upstream and ubuntu package information easily with something like this:

$ git clone -o upstream https://git.launchpad.net/cloud-utils
$ cd cloud-utils
$ git remote-add-ubuntu -v cloud-utils
...

$ git remote -v
pkg	https://git.launchpad.net/~usd-import-team/ubuntu/+source/cloud-utils (fetch)
pkg	https://git.launchpad.net/~usd-import-team/ubuntu/+source/cloud-utils (push)
upstream	https://git.launchpad.net/cloud-utils (fetch)
upstream	https://git.launchpad.net/cloud-utils (push)

# show differences other than in debian/ dir
$ git diff pkg/ubuntu/devel..upstream/master  -- ':!debian/' | diffstat -p1
 bin/ec2metadata |    2 +-
 tools/build-deb |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
#!/bin/bash
VERBOSITY=0
REMOVE_REMOTE=""
error() { echo "$@" 1>&2; }
errorrc() { local r=$?; error "$@"; return $r; }
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 ] package-name
Add remotes for Ubuntu git package name.
options:
-r | --remote NAME remote name. default 'pkg'.
EOF
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
cleanup() {
[ -z "$REMOVE_REMOTE" ] || {
vrun git remote remove "$REMOTE_REMOTE"
}
}
debug() {
local level=${1}; shift;
[ "${level}" -gt "${VERBOSITY}" ] && return
error "${@}"
}
vrun() {
debug 2 "running:" "$@"
"$@"
}
main() {
local short_opts="hr:v"
local long_opts="help,remote:,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="pkg" pkg=""
while [ $# -ne 0 ]; do
cur="$1"; next="$2";
case "$cur" in
-h|--help) Usage ; exit 0;;
-r|--remote) rname=$next; shift;;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
--) shift; break;;
esac
shift;
done
[ $# -ne 0 ] || { bad_Usage "must provide package name"; return; }
pkg="$1"
local out="" bref="" fetchurl="" gitv=""
[ "$VERBOSITY" -gt 1 ] && gitv="-v"
bref="https://git.launchpad.net/~usd-import-team/ubuntu/+source/"
fetchurl="${bref}${pkg}"
out="$(git remote 2>&1)" ||
{ errorrc "Failed to run git remote"; return 1; }
if echo "$out" | grep -q "^$rname$"; then
errorrc "remote '$rname' already exists."
return 1
fi
# just check that it is imported.
debug 1 "Checking if $fetchurl looks good"
out=$(curl --silent --location "$fetchurl" 2>&1)
case "$out" in
*[Ii]nvalid*[Oo]pen[Ii][Dd]*)
error "Seems like package '$pkg' not imported";
return 1;;
esac
debug 1 "Adding remote $rname with fetchurl $fetchurl."
vrun git remote add "$rname" "$fetchurl" || {
errorrc "Failed to add remote $rname at $fetchurl"
return 1
}
REMOVE_REMOTE="$rname"
vrun git config "remote.$rname.fetch" \
"+refs/heads/*:refs/remotes/$rname/*" &&
vrun git config --add "remote.$rname.fetch" \
"+refs/tags/*:refs/tags/pkg/*" || {
errorrc "Failed to add fetch for $rname"
return 1
}
vrun git config "remote.$rname.tagOpt" --no-tags || {
errorrc "Failed to set --no-tags on $rname"
return 1
}
debug 1 "Fetching $rname"
vrun git fetch $gitv "$rname" || {
errorrc "Failed to fetch '$rname'. removing."
return 1
}
REMOVE_REMOTE=""
return 0
}
main "$@"
# vi: ts=4 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment