#! /usr/bin/bash #set -x #DEBUG=echo upstream_branch=upstream our_name=suse die() { local retval=$1 rm -rf $tmpdir exit $retval } usage() { echo -e "$0 [ -r ] ...\n" \ " Convert repo to sha256 and run git command.\n" \ " ie. \`push {|${our_name}...\`\n" \ " -r : provide remote repository.\n" \ " Alternatively set environment variable SUSE_GIT.\n" \ " Local or remote branches from 'origin' are set up to be\n" \ " pushed to remotes/upstream/*\n" exit 0 } [ "$1" = "-h" ] && usage git_url=$(git config remote.origin.url) repo=$(basename $git_url) target_git=$our_name [ "$1" = "-r" ] && { shift; target_git=$1; shift; } cmdargs="${1+$@}" [ "$target_git" = "$our_name" ] && [[ $cmdargs =~ push\ ([^\ ]+)\\* ]] \ && target_git=${BASH_REMATCH[1]} [ "$target_git" = "$our_name" ] && target_git=$SUSE_GIT [ -n "$target_git" -a "$target_git" != "$our_name" ] || \ { echo -e "Target repo cannot be determined\n"\ " set environment variable SUSE_GIT\n"\ " to target repository or use the -r option"; exit 1; } [[ $target_git =~ .*/${repo} ]] || [[ $target_git =~ .*/${repo%.git} ]] || target_git+="/${repo}" cmdargs=${cmdargs//${target_git}/${our_name}} tmpdir=$(mktemp -d /tmp/tmp-XXXXXXXXX) mkdir -p $tmpdir/$repo git init --object-format=sha256 $tmpdir/$repo git fast-export --all | env GIT_DIR=$tmpdir/$repo/.git git fast-import export GIT_DIR=$tmpdir/$repo/.git git remote add origin ${git_url} git remote add ${our_name} ${target_git} git config set remote.${our_name}.push refs/heads/\*:refs/remotes/${upstream_branch}/\* git config set --append remote.${our_name}.push refs/remotes/origin/\*:refs/remotes/${upstream_branch}/\* #git remote rename origin ${upstream_branch} [ -n "$DEBUG" ] && $DEBUG $tmpdir ${DEBUG} git ${cmdargs} || die $? [ -n "$DEBUG" ] && echo $tmpdir || rm -rf $tmpdir