Skip to content

Instantly share code, notes, and snippets.

@phargogh
Created February 13, 2020 18:21
Show Gist options
  • Save phargogh/013ede75f9ffbc700dafcd7bc64a48d6 to your computer and use it in GitHub Desktop.
Save phargogh/013ede75f9ffbc700dafcd7bc64a48d6 to your computer and use it in GitHub Desktop.
Script to merge multiple heads on closed mercurial branches
#!/bin/bash
# NOTE: This version of this script was adapted from
# https://gist.github.com/FernFerret/3178035, with a few minor revisions
# for efficiency and clarity of output.
# This simple script will look at all named branches in a given repository
# and print out any branch that contains multiple open heads. This is useful
# to see if your repository is clean or if someone has pushed multiple
# anonymous branches.
foundone=""
# Retrieve the branch list. The format will contain other garbage, so only
# grab the name of the branch using gawk. You could use awk if required.
#branchlist=`hg branches | awk '{print $1}'`
branchlist=`hg branches -c | gawk '{print $1}'`
# Iterate through the branches and find the one(s) that contain multiple heads.
for branch in ${branchlist[@]}
do
count=`hg log -r "head() and branch('$branch')" --template "{node}\n" | wc -l`
if [ "$count" -ne "1" ] ; then
echo -e "$branch"
foundone="yes"
fi
done
echo -e "" # extra newline
# If no branches contained multiple heads, state that life is good!
if [ -z "$foundone" ] ; then
echo -e "---[ No named branches contained multiple heads. Yay! ]---"
fi
#!/usr/bin/env -ex
REPO=./invest
# NOTE: the textfile branches-with-multiple-heads.txt was created from a modifed
# version of the script at https://gist.github.com/FernFerret/3178035. See
# branchfind.sh in this gist for the script.
for branch in `cat branches-with-multiple-heads.txt`
do
heads=`hg heads -R $REPO -c $branch -T "{node} "`
if [ `echo ${heads} | awk '{ print NF }'` -eq 1 ]
then
echo "Branch $branch only has one head. continuing."
else
echo "Merging heads on branch $branch"
# Update to the first rev
hg up -r `echo ${heads} | awk '{ print $1 }'` -R $REPO
hg merge -r `echo ${heads} | awk '{ print $2 }'` -R $REPO
hg commit -m "Merging heads on branch $branch" -R $REPO
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment