#!/bin/sh
#
# This script is used rather than git-import-orig to import a new upstream
# tarball.  It does essentially the same work as git-import-orig -- take the
# contents of the tarball and commit it to the upstream branch and then tag it
# with a new upstream/* tag -- but it records that commit as a merge commit
# between the upstream branch and another tag.
#
# The purpose of this procedure is to have the imported tarball look to Git
# like a merge between upstream's tagged Git tree corresponding to that
# tarball and our upstream branch.  This lets things like git cherry-pick work
# properly against upstream's release branch.
#
# This script assumes that the upstream tarball has already had non-DFSG
# material removed.
#
# Written by Sam Hartman <hartmans@debian.org> for krb5
# Adopted for openafs by Russ Allbery <rra@debian.org>

set -e

if [ $# -ne 3 ] ; then
    echo "Usage: import-upstream <tarball> <upstream-tag> <local-tag>" >&2
    exit 2
fi
tarball="$1"
upstream="$2"
tag="$3"

# Unpack the tarball.
dir=$(basename $(tar tzf "$tarball" | head -1))
tar xzf "$tarball"

# Add the tarball to the current index and then use that to create a tree
# object corresponding to the contents of that directory.  Then, use
# commit-tree to commit that to the repository.
git add -f "$dir"
tree=$(git write-tree --prefix="$dir"/)
commit=$(echo "Imported upstream tag $upstream via tarball" | \
    git commit-tree "$tree" -p upstream -p $(git rev-list -n1 "$upstream"))

# Now that we have a commit, repoint upstream at that commit, tag it, and then
# remove the unpacked upstream tarball from our index.
git branch -f upstream "$commit"
git tag "$tag" "$commit"
git rm -q -r -f "$dir"
