bluenote10
bluenote10

Reputation: 26640

Sort hg branches by modification time

When cleaning up branches in git, I have been using something like this to differentiate between old/inactive and new/active branches.

How can I sort all branches by their modification time in Mercurial?

Upvotes: 0

Views: 436

Answers (1)

torek
torek

Reputation: 488835

The concept of a branch is just different enough in Mercurial vs Git to make this a bit tricky. Depending on what you really want, the "trickiness" may be that it becomes completely trivial!

Start with hg heads, which shows all heads (see hg help heads for the definition of a head). Each head is a commit, and therefore:

  • has an ID;
  • is in one particular branch;1 and
  • has a date stamp.

It also has a local revision number within your repository, which won't necessarily match the revision number in some clone that has the same commit. That is:

$ hg heads
changeset:   5:5f5df3fc4f1c

Changeset 5f5df3fc4f1c in some clone might be 4:5f5df3fc4f1c or 100:5f5df3fc4f1c or some such. The number in front of the colon is the local revision number.

Now, the dates on commits are up to the machine that creates the commit, so they can be quite wildly wrong. The local revision numbers are assigned sequentially as you add commits to your Mercurial repository, though—so if a local revision number is higher, that means that you introduced the commit later. (Caveat: this could be "a lot later than actually made" if you just now brought new commits in from an old, inactive, but never-before-incorporated clone.)

In that sense, then, the output of hg heads, which is shown by default in reverse local-number order, is already in the order you probably want (or the reverse of it). Things printed later are "less active". So there's probably no work to do here other than to read through the output of hg heads (and check whether there are multiple heads within some branch).


1This differs from Git in that in Git, a tip commit is the commit to which some refs/heads/ name points. That tip defines the branch, but that commit can be in multiple branches! The tip is necessarily a head in the Mercurial sense,2 but there can only be one such head, because it's the branch's name that locates and thereby defines the head / tip commit, and each name identifies only one commit.

2Unless, that is, you mean hg heads --topo, in which case a Git commit identified by refs/heads/name might not be a head after all:

...--o--*   <-- refs/heads/midpoint
         \
          o--●   <-- refs/heads/tip

The commit identified by refs/heads/tip is a topological head while that identified by refs/heads/midpoint is not. In Git the midpoint commit is on both branches.


If you do want different sortings, it's harder: instead of hg heads, you must use hg log and use a revision specifier. Note that -r "head() and not closed()" produces the same output as hg heads, so you can start with that. The output from hg log is sorted in whatever order you choose, but defaults to the same local-numeric-revision sorting. See hg help revisions for more.

Upvotes: 1

Related Questions