Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a merge is needed after a pull in Mercurial?

I have a clone of a repository with one head, nice and simple. After pulling in someone else's changes, I have a script that counts the heads to see if a merge is required. But if the other person has made a branch and merged it, "hg heads" shows two heads, and the script thinks it has to merge. What should the test really be?

Before:

0 - 1

After:

0 - 1 - 2 - 3
    \  /
     4 (branch)

This doesn't need merging. But a simple comparison of the number of heads before and after would suggest that it does. Why does Mercurial even show more than one head in this case?

like image 904
Peter Westlake Avatar asked Apr 24 '12 12:04

Peter Westlake


People also ask

How does hg merge work?

The merge process is simple. Usually you will want to merge the tip into your working directory. Thus you run hg merge and Mercurial will incorporate the changes from the tip into your local changes; Tip will then become the second parent revision of your working directory.

How do you merge Heads in Mercurial?

To start a merge between the two heads, we use the hg merge command. We resolve the contents of hello. c This updates the working directory so that it contains changes from both heads, which is reflected in both the output of hg parents and the contents of hello.

What does hg update do?

Description. Update the repository's working directory to the specified changeset. If no changeset is specified, update to the tip of the current named branch and move the active bookmark (see hg help bookmarks). Update sets the working directory's parent revision to the specified changeset (see hg help parents).


2 Answers

Instead of calling hg heads call hg heads --topo which shows only topological heads -- those with no kids. You're see the head of their merged branch, but since it was merged in it's not a topological head and --topo will suppress it.

like image 153
Ry4an Brase Avatar answered Nov 14 '22 16:11

Ry4an Brase


If you don't have spaces in your branch names, then you can use this script:

#!/bin/sh

for b in $(hg branches -q); do
    h=$(hg heads --template "." $b)
    if test ${#h} -gt 1; then
        echo "Branch $b needs merging, it has ${#h} heads"
    fi
done

It iterates over each open branch and counts the number of heads on it.

like image 39
Martin Geisler Avatar answered Nov 14 '22 15:11

Martin Geisler