Andir
Andir

Reputation: 2083

Mercurial - Automatically collapsing commits based on message?

If I submit two or more commits into Mercurial with the same message, is there a way to collapse these into one change-set automatically? I assume this would have to be done in a changeset hook but I have not been able to find an automated method for this. Most examples require knowing the changeset revision numbers to collapse it manually and I'd want a way to automatically compare the previous change with the one being submitted and "merge" the two into one changeset.

In other words:

hg commit -m "some change"
hg commit -m "some change"

| * 20 <- "some change"
| * 19 <- "some change"
| * ..

Could this automatically become one changeset and fold it with the last one with the same commit message?

| * 19 <- "some change"
| * ..

This would only collapse changes if the commit message is exactly the same and sequential.

Because I know someone will try to point to these again... I'm not looking for these extensions, but an automatic method that does not require knowing the IDs or revisions.

ConcatenatingChangesets

CollapseExtension

HisteditExtension


Would this be possible if I 'tag'ged the previous commit? Could you combine your next commit into a tagged commit?

Upvotes: 1

Views: 295

Answers (1)

Mark Drago
Mark Drago

Reputation: 2058

Git calls this "amend" and it is a feature of the regular 'git commit' tool. It doesn't do it based on commit message, you have to pass a '--amend' option to commit and it will combine the previous commit with the one you're creating. I haven't tried this with mercurial, but the GitConcepts page on the mercurial wiki says you could run hg rollback; hg commit -m "message" to have the same affect.

I should warn you that rollback will rollback the most recent commit in the repository (tip), so if you have pulled changes or merged things since your last regular commit, rollback will most likely not rollback the changeset you want it to. Additionally if you have pushed changes (or others have pulled from you) you will be breaking compatibility with those repositories because you're essentially editing history.

In theory you could write a hook which will see if the tip of the repository is on the same branch that you're on and if its commit message matches the one you're entering, and if so do a rollback and a commit in one go.

I'm curious to know what your goal is as that will help in recommending a solution for that goal. There may be better and more elegant ways to produce the result you desire than combining changesets. I know that you're not interested in extensions for some reason, but if you're trying to achieve a clean and logical changeset history, you should look in to the MqExtension.

Upvotes: 3

Related Questions