Easier backout scripts

Based on the useful article from Ehsan Akhgari and the comments to that article (especially the nice suggestion from Daniel Holbert) and in the need for easier ways to backout stuff from mozilla-inbound, I've put together a couple scripts I use to easily (and more safely) backout, I'm going to share these with you, in the hope they may be useful and hopefully improved!

Disclaimer

I'm not a good bash scripter, your hints on that will be really appreciated. I also know these may probably be coded as Mercurial extensions, but I needed some quickly ready stuff.

Note that these script may destroy your unsaved work.  For simplicity I usually work in a clean tree, using Mercurial queues, so I don't care much about uncommitted changes.  The scripts will try to bring tree to a clean state through an hg update -C and qpopping any applied patch. You can modify them to cope with your needs, but be aware of that.

You can just put these scripts in your .profile and invoke them as common commands, also on Windows you can create a .profile file in C:/Users/YourUserName/ (or whatever path goes to your home folder) and mozilla-build will pick it up!

Hope these may be useful for your tree maintenance needs, you're absolutely free to do whatever you want with them.

Backout a range of changesets without a merge

This is called backout_range_n2o, since I needed to remember the order of the changesets matters, from newer to older. The 2 passed into changesets are included in the backout (this slightly differs from Ehsan's suggestion indeed). It will ask to edit the commit message providing some default content. Once done you'll find a qpushed backout.diff patch, ready to be gfinished and pushed. Check error messages and final result, if something goes wrong you just have an uncommitted patch in your mercurial queue, so no worries.

################################################################################
# Backout multiple consecutive changesets.
# - first argument is the most recent changeset
# - second argument is the least recent changeset
# After this, just have to hg qfin -a && hg push

backout_range_n2o() {
  if [ $# -lt 2 ]
  then
    exit 1
  fi

  local MESSAGE="Backout"

  hg update -C || exit 1
  hg qpop -a || exit 1
  hg pull || exit 1

  hg update $1 || exit 1
  hg revert -a -r $(hg parents -r $2 --template '{node|short}') || exit 1

  MESSAGE="$MESSAGE $(hg log -r $2:$1 --template '{node|short}, ')"

  hg qdelete backout.diff > /dev/null 2>&1
  hg qnew backout.diff -f -m "$MESSAGE"
  hg qrefresh -e
  hg qpop
  hg update -r default
  hg qpush
  hg outgoing -v
}

Backout cherry-picked changesets without a merge

This is called backout_n2o, since I needed to remember the order of the changesets matters, from newer to older. Can also be used for a single changeset. All the changesets are included in the backout. It will ask to edit the commit message providing some default content. Once done you'll find a qpushed backout.diff patch, ready to be gfinished and pushed. Check error messages and final result, if something goes wrong you just have an uncommitted patch in your mercurial queue, so no worries.

################################################################################
# Backout multiple non-consecutive changesets.
# Pass all wanted changesets as arguments.
# After this, just have to hg qfinish qbase:qtip && hg push

backout_n2o() {
  if [ $# -lt 1 ]
  then
    exit 1
  fi

  local MESSAGE="Backout"

  hg update -C || exit 1
  hg qpop -a || exit 1
  hg pull || exit 1

  for CHANGESET in $*
  do
    hg update $CHANGESET || exit 1
    hg revert -a -r $(hg parents -r $CHANGESET --template '{node|short}') || exit 1
    hg qdelete $CHANGESET.diff > /dev/null 2>&1
    hg qnew -f $CHANGESET.diff
    hg qpop || exit 1
    MESSAGE="$MESSAGE $CHANGESET,"
  done

  hg update -r default || exit 1

  hg qdelete backout.diff > /dev/null 2>&1
  hg qnew backout.diff -m "$MESSAGE"

  for CHANGESET in $*
  do
    hg qfold $CHANGESET.diff
  done

  hg qrefresh -e
  hg outgoing -v
}

qdiff with 8 lines of context, author and commit message

This is a really simple script I use to export the Mercurial queue tip changeset with 8 lines of context (for review), preserving author and commit message.  Usually I have Mercurial queue patches set at 3 lines of context, and qdiff doesn't output the header.  Can be easily modified to export any changeset if needed.

qdiff() {
  local REV=qtip
  if [ $1 ]
  then
    REV=$1
  fi

  hg log -r "$REV" --template '# HG changeset patch\n# User {author}\n{desc}\n\n'
  hg qdiff
}
MaK Venerdì 05 Agosto 2011 at 2:59 pm | | Mozilla-EN