Peter Westlake
Peter Westlake

Reputation: 5036

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?

Upvotes: 5

Views: 363

Answers (3)

Ry4an Brase
Ry4an Brase

Reputation: 78330

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.

Upvotes: 4

Martin Geisler
Martin Geisler

Reputation: 73758

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.

Upvotes: 3

Ringding
Ringding

Reputation: 2856

You could clone your repo prior to pulling, and afterwards push into the clone with --new-branch. If that complains about new heads, you need to merge.

Upvotes: 0

Related Questions