Skip to content

Instantly share code, notes, and snippets.

@jajm
Last active March 1, 2021 12:42
Show Gist options
  • Save jajm/f0b77da1a41b6b1c5ab1 to your computer and use it in GitHub Desktop.
Save jajm/f0b77da1a41b6b1c5ab1 to your computer and use it in GitHub Desktop.
git-bz-rebase
#!/bin/sh
usage="Usage: git bz-rebase BUG_NUMBER"
if [ -z "$1" ]; then
echo "$usage" && exit 1
fi
if [ ! -x "$(which pup)" ]; then
echo "pup is required for this to work"
echo "To install it, run: go get github.com/ericchiang/pup"
echo "You must have go installed and GOPATH set"
exit 1
fi
run() {
echo "$@" && eval "$@"
}
silent() {
"$@" >/dev/null
}
first_patch_date() {
wget -q -O- "http://bugs.koha-community.org/bugzilla3/attachment.cgi?bugid=$1&action=viewall&hide_obsolete=1" \
| pup 'table.viewall_desc tr:nth-child(2) json{}' \
| perl -MJSON -MList::Util=first -E '
my $trs = JSON->new->decode(do { local $/; <STDIN> });
$trs = ref $trs eq "ARRAY" ? $trs : [ $trs ];
say ( ( first {
defined $_->{children}[1]{children} and $_->{children}[1]{children}[0]{text} eq "patch"
} @$trs )->{children}[2]{text} );
'
}
branch_exists() {
git show-ref --verify --quiet "refs/heads/$1"
}
current_branch() {
git symbolic-ref --short HEAD
}
bugnumber=$1
date=$(first_patch_date $bugnumber)
if [ -z "$date" ]; then
echo "Failed to find the date of the first patch for bug $bugnumber. Are you sure there are patches ?"
exit 1
fi
if [ "$(current_branch)" != "rebase/bug/$bugnumber" ]; then
branch_exists "rebase/bug/$bugnumber" && run silent git branch -D "rebase/bug/$bugnumber"
run silent git checkout -b "rebase/bug/$bugnumber"
fi
commit=$(git rev-list -n 1 --before="'$date'" origin/master)
echo "Resetting HEAD to where origin/master was at $date"
run silent git reset --hard $commit
run git bz apply $bugnumber
echo ""
echo "If all patches were applied successfully, you can run: git rebase origin/master"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment