f637971
#!/bin/sh
f637971
f637971
# Generic function to print an error message and bail out.
f637971
die ()
f637971
{
f637971
    echo $1 > /dev/stderr
f637971
    exit 1
f637971
}
f637971
f637971
# Print usage
f637971
usage ()
f637971
{
f637971
    cat <
f637971
$0 -- Generate .patch files for a RPM package from a git repository
f637971
f637971
Usage:
82f299d
  $0 <REPOSITORY> [<COMMIT_OR_TAG_OR_BRANCH>]
f637971
f637971
<REPOSITORY> is the directory where the rebase was performed.
f637971
82f299d
<COMMIT_OR_TAG_OR_BRANCH> is the commit or tag or branch against which
82f299d
the rebase was performed.  It generally just needs to be provided if
82f299d
the file "_git_upstream_commit" doesn't exist, or if you are doing a
82f299d
rebase.  This script will then use 'git merge-base' to find the most
82f299d
recent common ancestor between HEAD and COMMIT_OR_TAG_OR_BRANCH.
f637971
f637971
Options are:
f637971
f637971
  -h: Print this message
f637971
EOF
f637971
    exit 0
f637971
}
f637971
f637971
test -f gdb.spec || die "This script needs to run from the same directory as gdb.spec."
f637971
f637971
test -z $1 && die "You need to specify the repository."
f637971
test "$1" = "-h" && usage
f637971
82f299d
commit_or_tag="`cat _git_upstream_commit`"
f637971
if test ! -z "$2" ; then
f637971
    commit_or_tag="$2"
f637971
fi
f637971
82f299d
test -z $commit_or_tag && die "Because the '_git_upstream_commit' file doesn't exist, you need to specify a commit/tag/branch."
82f299d
f637971
test -d $1 || die "$1 is not a directory."
f637971
f637971
# Remove all the current patches
f637971
for f in `cat _patch_order` ; do
f637971
    git rm -f $f
f637971
done
f637971
f637971
cd $1
2e649f7
2e649f7
git name-rev $commit_or_tag
2e649f7
test $? -eq 0 || die "Could not find $commit_or_tag in the repository.  Did you run 'git fetch'?"
2e649f7
f637971
idx=1
f637971
common_ancestor=`git merge-base HEAD $commit_or_tag`
f637971
f637971
test -z "$common_ancestor" && die "Could not find common ancestor between HEAD and $commit_or_tag."
f637971
f637971
temp_PATCH_file=/tmp/_gdb.spec.Patch.include
f637971
temp_patch_file=/tmp/_gdb.spec.patch.include
f637971
temp_patch_order_file=/tmp/_patch_order
f637971
f637971
rm -f $temp_PATCH_file $temp_patch_file $temp_patch_order_file
f637971
f637971
for c in `git rev-list --reverse ${common_ancestor}..HEAD` ; do
fd7e5d7
    fname=`git log -1 --pretty='format:%s' $c`
fd7e5d7
    test -z $fname && die "Could not determine filename for commit $c."
a8767b3
    # Because git-format-patch generates patches with the first line
a8767b3
    # containing the commit hash, every time we do a git-format-patch
a8767b3
    # here we will have a different .patch file from what we had
a8767b3
    # before, even if nothing has changed.  This is bad, so we replace
a8767b3
    # the commit hash by something constant (the string
a8767b3
    # "FEDORA_PATCHES").
56d681a
    git format-patch --no-signature --no-stat --keep -1 --stdout $c | sed -e '1 s/^From [0-9a-f]\+ \(.*\)/From FEDORA_PATCHES \1/' -e '/^index [0-9a-f]\+\.\.[0-9a-f]\+.*$/d' > ../$fname
f637971
    (cd .. && git add $fname)
f637971
f637971
    cat >> $temp_PATCH_file <
f637971
`git log -1 --pretty='format:%b' $c | sed -n 's/^;;/#/p'`
f637971
EOF
f637971
    printf "Patch%03d: %s\n\n" $idx $fname >> $temp_PATCH_file
f637971
    printf "%%patch%03d -p1\n" $idx >> $temp_patch_file
f637971
    echo $fname >> $temp_patch_order_file
f637971
    idx=`expr $idx + 1`
f637971
done
f637971
f637971
cd ..
f637971
mv $temp_PATCH_file _gdb.spec.Patch.include
f637971
mv $temp_patch_file _gdb.spec.patch.include
f637971
mv $temp_patch_order_file _patch_order
f637971
echo "$common_ancestor" > _git_upstream_commit