Undoing accidental hg amend on top of Mercurial queue - mercurial

The following sequence of commands requires that the Mercurial Queues exension be enabled, and also the Evolve extension (for hg amend, alias hg refresh).
This combination of commands messes up the existing Mercurial queue, making the commit corresponding to the pushed patch obsolete, and creating a new commit with exactly the same contents.
This error is in theory quite easy to make: just type hg ref instead of hg qref, but I just made it recently after using Mercurial Queues for years. Regardless, I haven't figured out a clean way to fix this and get back the original state.
Note that a simple hg rollback does work in my example, but I'm not sure it will work in my case, because I've tried other things trying to fix this. In any case, hg rollback isn't something one should rely on.
In summary, how do I undo the hg amend and get back my applied MQ patch?
#!/bin/sh
hg init test
cd test
echo "This is foo" >> foo
hg add
hg ci -m "Add foo"
hg init --mq
echo "Line 2 of foo" >> foo
hg qnew p
hg ci --mq -m "Add patch p"
hg ref
hg log -vG --hidden
# changeset: 2:f7f038d3aab5
| tag: tip
| parent: 0:9d3a95922194
| user: Faheem Mitha <faheem#faheem.info>
| date: Sun Mar 11 16:38:51 2018 +0530
| files: foo
| description:
| [mq]: p
|
|
| x changeset: 1:e467a2433c7f
|/ tag: p
| tag: qbase
| tag: qtip
| user: Faheem Mitha <faheem#faheem.info>
| date: Sun Mar 11 16:38:50 2018 +0530
| obsolete: rewritten using amend as 2:f7f038d3aab5 by Faheem Mitha <faheem#faheem.info> (at 2018-03-11 16:38 +0530)
| obsolete: rewritten by Faheem Mitha <faheem#faheem.info> as f7f038d3aab5 (at 2018-03-11 16:38 +0530)
| files: foo
| description:
| [mq]: p
|
|
o changeset: 0:9d3a95922194
tag: qparent
user: Faheem Mitha <faheem#faheem.info>
date: Sun Mar 11 16:38:50 2018 +0530
files: foo
description:
Add foo

That's not what I get with your test case. I need an additional
echo "Line 3" >> foo
before the hg ref. Also, versioning your mq seems irrelevant here; I think you can remove the hg init --mq and hg ci --mq lines.
hg amend really ought to block that. But if you wanted to manually fix things up, edit .hg/patches/series and replace the obsolete hash with the successor's hash. (Just make sure to use the 20-byte hash, as is given by eg hg log -T '{node}\n').
Warning: when I tried that with your test case and qpopped, something mysteriously un-obsoleted the old head, and it gives a strange warning about the current directory not being a head (despite hg log -r 'head()' listing it.) But at least you have your queue back in a working state.

Related

Generating merge logs with mercurial

We're using Mercurial for source control and would like to introduce automatic merging and changelog for our QA process.
For the purpose of this discussion, let's just assume that we use a simple 3-branch flow like the below:
\ = forward merge
| = back merge of fix
Default --O--A--*--B--*---
\ ^ \
\ | \
QA -----M--X--C--*--N---
^ \
| \
Master --------H--------*---
So the question is:
Is it possible to construct a revset query that, at merge N, preferably before but otherwise after actually performing it, will result in a log of all commits on Default branch, which aren't yet in QA branch (On the above drawing that would be A and B).
Alternatively formulation:
Is it possible to construct a revset query that will return all the changesets, which would get merged if we merged Default into QA.
I'm only interested in commits on Default branch, not individual commits in feature branches already merged into Default (Feature branches not included on the drawing). I am also not interested in commits on the QA branch.
The repository is pretty well maintained, but a couple of years old and contains about 13000 nodes.
I've been experimenting with ancestor and ancestors, but either I get X as the latest common ancestor, or some very very old node. I understand the logic of getting X, but I'm really after O.
The most luck I've had is with variations of this:
hg log --rev "((heads(branch('Default')) and !closed()) % heads(branch('QA'))) and branch('Default') and p2(!branch('QA'))"
hg log --rev "((heads(branch('Default')) and !closed()) % heads(branch('QA'))) and branch('Default') and !children(branch('QA'))"
Update
Using "hg merge --preview --rev XYZ", as suggested by Tom, generates output like this:
changeset: 13070:7e59fc16aa4e
branch: Default
parent: 13068:5b9409ad504f
parent: 13069:849bd43d2023
user: *******
date: Mon Dec 18 18:40:46 2017 +0200
summary: Merged Feature branch A
changeset: 13071:07470ff919ff
branch: Feature branch B
parent: 13061:540eda2c959b
parent: 13068:5b9409ad504f
user: *****
date: Mon Dec 18 18:49:42 2017 +0200
summary: Merge with Default
changeset: 13072:a53260ffabca
branch: Feature branch B
user: *****
date: Mon Dec 18 18:58:05 2017 +0200
summary: Some text
changeset: 13073:37c895f2abf0
branch: Default
parent: 13070:7e59fc16aa4e
parent: 13072:a53260ffabca
user: *******
date: Mon Dec 18 18:58:05 2017 +0200
summary: Merged Feature branch B
Not too bad, but not super fun either. At least it requires more logic than simple grep/sls to parse.
Thanks in advance,
John
I found a solution that mostly does what I want:
hg log --rev "branch('Default') & !ancestors(branch('QA')) & ancestors(heads(branch('Default')) & !closed()) & !children(branch('QA'))"
The only thing now missing is to exclude multiple head merges, since they're just noise in a changelog.
Update:
So I picked this project up again and found some great answers elsewhere on SO.
The following produces output similar to hg merge --preview, but can be templated:
hg log -r "ancestors('$sourceBranch') - ancestors(.)" --template $template
This filters out all commits not on source branch:
hg log -r "(ancestors('$sourceBranch') - ancestors(.)) and (branch('$sourceBranch'))" --template $template

Continue mercurial branch at new location

I have a branch dev that has been merged into a release branch to form a release commit.
I am hoping to reopen dev from after that merge commit so that my repository has a visual "fan-in fan-out" style. By that I mean that all commits and branches converge into my release commit and then work for the next release diverges out of it. This visually makes it very easy to see what is contained in each release as it keeps them isolated between our tagged release commits.
The effect is that dev is no longer a contiguous branch and I am wondering if this is possible in mercurial.
When I try to hg branch dev again after the release, mercurial complains that it already exists. Even if I commit with say --close-branch after I merge it.
So currently my workaround is to:
(1) either use a different dev-VERSION for each release which is more fiddly and harder to script or
(2) run dev in parallel to release and keep merging from dev into release which isn't so bad, just a little messier for our particular release process.
I know that this might seem like a non-standard mercurial release process. It would suit our particular project and team structure really well though.
Thanks for any help!
EDIT:
To make what I mean a bit clearer, imagine I had merged a feature-01 branch into dev and then merged dev into release in order to create release-1.1. Then I want to make a new feature-02 branch off that merges into dev with a visualisation like:
# changeset: 6:4bcc59fe6ded
|\ branch: dev
| | summary: Merge feature-02
| |
| o changeset: 5:8e117ffe64d0
| | branch: feature-02
| | summary: Add d
\ |
o changeset: 4:c39dae3ff2fa <---- all commits converge to here then diverge back out
/| branch: release
| | summary: Release v1.1
| |
o | changeset: 3:c89af2b3e8db
| | branch: dev
summary: Merge feature-01
This is an example bash script where I'm trying to get something like this:
# Create test
rm -rf test
mkdir test
cd test
# Set up v1.0
hg init
hg branch release
touch a
hg commit -m 'Release v1.0'
# Set up dev branch
hg branch dev
touch b
hg add b
hg commit -m 'Created dev'
# Feature branch
hg branch feature-01
touch c
hg add c
hg commit -m 'Add c'
hg update dev
hg merge feature-01
hg commit -m 'Merge feature-01'
# Release all the things
hg update release
hg merge dev
hg commit -m 'Release v1.1'
# Start a new feature branch
hg update dev # <------ crucial line
hg branch feature-02
touch d
hg add d
hg commit -m 'Add d'
hg update dev
hg merge feature-02
hg commit -m 'Merge feature-02'
If I try to continue feature-02 on the dev branch, it ends up having commit 3:e7ba9d1d9bfe as it's parent when I would like it to have the release commit 4:bbc951a4b339 as its parent but still be on the dev branch.
hg log -G gives roughly this output:
# changeset: 6:46539f99dadd
|\ branch: dev
| | tag: tip
| | summary: Merge feature-02
| |
| o changeset: 5:885b0b930fed
|/ branch: feature-02
| summary: Add d
|
| o changeset: 4:bbc951a4b339 <--- not the parent of the new changes
|/| branch: release
| | summary: Release v1.1
| |
...
I could change my process so that the first feature branch after a new release is branched directly off release, but that makes the process harder to script and just delays the problem until I have to merge it into dev:
So if I use the same script as above but change it slightly:
...
# Start a new feature branch
#hg update dev
hg update release # <--- changed to release
hg branch feature-02
...
Then my graph ends up being:
# changeset: 6:4bcc59fe6ded <---- has the previous `dev` as a parent
|\ branch: dev
| | summary: Merge feature-02
| |
| o changeset: 5:8e117ffe64d0
| | branch: feature-02
| | summary: Add d
| |
| o changeset: 4:c39dae3ff2fa
|/| branch: release
| | summary: Release v1.1
| |
o | changeset: 3:c89af2b3e8db
|\ \ branch: dev
|| | summary: Merge feature-01
...
If I try to rebranch dev:
...
# Start a new feature branch
#hg update dev
#hg update release
hg branch dev # <--- changed
hg branch feature-02
...
I get an error: abort: a branch of the same name already exists.
I hope that helps make it clearer! Thanks for any help!
After doing your merge, just do hg update dev and start making commits. You don't need to close and reopen the branch. You don't need to create a new branch.

Mercurial built-in template style description

I am trying to tune the output of hg log by defining my own style. In fact, just adding information to the default style would be perfect. Is there a place where I can get the description of the default styles (default, compact, ...) to write my own?
After some research, I finally found the location of the default styles for hg log, located on my ubuntu 14.04 at
/usr/share/mercurial/templates/
You can find there the following files:
map-cmdline.bisect
map-cmdline.changelog
map-cmdline.compact
map-cmdline.default
map-cmdline.phases
map-cmdline.xml
They do not contain any color scheme, but they are allowed me to build my 'patched' version of default.
From other comments elsewhere you can find out where the styles location is by:
hg debuginstall --config ui.editor=cat
Or using a bug if you choose a directory under the template it complains and tells you where they are (probably should for non matching styles), well it does for 1.9.2:
hg in --style paper
If you want to colour the output you could use the color extension now.
add to .hgrc:
[extensions]
color=
If you know how to use the various keywords that Mercurial templating feature understands (see hg help templating for a very complete usage doc), then you can either:
specify your choices by giving a template on the command line, e.g.,
$ hg tip --template "{rev} at {date|isodate}"\n
882 at 2014-12-17 08:50 +0100
or edit an ascii file with the same format and use it with the style option, e.g.,
$ cat ~/mystyle.txt
changeset = "{rev} at {date|isodate}\n"
$ hg tip --style ~/mystyle.txt
882 at 2014-12-17 08:50 +0100
It's strange, but the default templates are hardcoded in mercurial code.
The file map-cmdline.default is never read at runtime, but can be used to exactly replicate mercurial output (and thus to extend it).
Example of how to extend hg log output to include the git commit hash of a git repository cloned via hg-git.
Look for the location of map-cmdline.default:
$ hg debuginstall --config ui.editor=cat | grep "default template"
checking default template (/usr/lib/python2.7/dist-packages/mercurial/templates/map-cmdline.default)
Edit /etc/mercurial/hgrc, pasting a slightly modified version of map-cmdline.default:
...first part of your hgrc...
[templates]
# support "git-commit" line in hg log
changeset_git = '{cset}{cset_git}{branches}{bookmarks}{tags}{parents}{user}{ldate}{ltroubles}{lobsfate}{summary}\n'
cset_git = '{if(gitnode, "git-commit: {label("status.unknown", gitnode|short)}\n")}'
...the rest of 'templates' section of map-cmdline.default...
[templatealias]
...the whole 'templatealias' section of map-cmdline.default...
Example output:
~/git$ hg log --graph
# changeset: 54881:d420062ce3d6
|\ git-commit: 6326d2e53311 <-- new line, only appears in git clones
| | tag: default/pu
| | tag: tip
| | parent: 54879:31a4c8ccc461
| | parent: 54880:691b9814f119
| | user: Junio C Hamano <gitster#pobox.com>
| | date: Sun Mar 25 10:52:08 2018 -0700
| | summary: Merge branch 'jh/partial-clone' into pu

Mercurial undo merge that has more commits following it?

Have a scenario where we un-intentionally merged a named branch (ABC) into our default branch.
hg rollback is not an option because there have been a couple commits since.
Is there a way to undo this?
You're going to need the Mq extension. If you don't have it turned on, do so by adding this to your Mercurial.ini or .hgrc file.
[extensions]
hgext.mq=
If you're not familiar with it, the Mq extension let's you manipulate history. The good news is, this will allow us to fix your repo. The bad news is that anyone who has a clone of the messed up repo will have to clone it again, because we'll be changing history.
First, go make another clone of your repo to work in, so we don't mess anything up.
Now, find the revision id of the merge changeset (that merged default and your named branch). Write it down. We'll refer to it as changesetM. Now find the revision id of the next changeset. Write it down. We'll refer to it as changesetN.
Once you have those two revision ids, head over to your command prompt and cd into your repo. Then type out the following, replacing changeset[M|N] with the appropriate revision id.:
$ hg qimport -r changesetN:tip
# This will add all of your changes since the merge to the queue
$ hg qpop -a
# This pops them all out of your history.
$ hg strip changesetM
# This removes the merge changeset.
$ hg update -C default
# Make sure we're on the default branch
$ hg qpush -a
# Take the changesets in the queue and push them back onto your history.
$ hg qfinish -a
# Remove changesets from the queue and finalize them as normal changesets.
Essentially, you are rebasing the new changesets on top of the default branch, removing the merge changeset in the process. Once you're done, you'll need to push the changes to a new repository on the server, and have your colleagues clone fresh copies.
Lastly, if you have any other Mercurial questions, also check out kiln.stackexchange.com.
UPDATE
I forgot to mention: If someone has based changes on something that was only in the other branch, it is possible that hg qpush -a will fail. You'll see a foo.txt.rej and foo.txt.orig file laying around. Unfortunately, you'll have to fix this yourself. To fix it, open the original file, the .orig file, and the .rej file and choose the right changes to merge in, saving them in the original file. Once you've merged it in, use hg qrefresh to update that patch to the new, merged patch. From their, you should be able to run hg qpush -a again and continue. If you run into the same error again on another patch, just follow the same process.
If you haven't publish the repo publicly you can do this
hg clone -r (parent1 of bad merge) -r (parent2 of bad merge) old new
and delete the old repo.
I came across the following scenario today:
# changeset: 1728:5d703e1051d3
|\ parent: 1727:1a5f73b5edb4
| | parent: 1720:65ddd0bde225
| | user: nn
| | date: Wed Feb 27 10:35:00 2013 +0100
| | summary: Merge with SomeBranch
| |
| o changeset: 1727:1a5f73b5edb4
| | user: nn
| | date: Wed Feb 27 10:34:30 2013 +0100
| | summary: lorem ipsum
| |
[some more changesets]
| |
o | changeset: 1720:65ddd0bde225
| | branch: SomeBranch
| | user: nn
| | date: Wed Feb 27 07:44:46 2013 +0100
| | summary: lorem ipsum
Where SomeBranch should not have been merged into default. What we did to solve this, was to use the backout command with the parent option like so:
hg backout --rev=1728 --parent=1727
By this you don't undo the merge itself: Looking at a branch graph (either with graph log or in TortoiseHg) you'll still see SomeBranch going into default at r1728. The result of the merge is however undone, meaning that the changeset containing the backout (r1729 in my case) is identical to r1727.

Mercurial: How to merge with manual control over changes & files?

Note: The scenario I describe here is not answered in Stack Overflow: Completely manual Mercurial merge.
I will explain my query with an example. Assume I start off a Mercurial repository to design a car:
C:\Car$ dir
Car.cpp
Car.h
I work on the design of Car for quite a while and the repository looks like:
r0-r1-...-r100-(default)
At some point in time I branch default to SolarCarBranch to work on a solar powered car in parallel:
C:\SolarCar$ dir
Car.cpp
Car.h
Solar.cpp
Solar.h
After some more time, the repository looks like:
r0-r1-...-r100-...-r200-(default)
\--r101-...-r201-(SolarCarBranch)
How do I merge SolarCarBranch back to default?
Take note of the following complications in the merge I want:
I should be able to continue work on both default and SolarCarBranch after the merge.
There might be fuel efficiency fixes in Car.cpp and Car.h in SolarCarBranch that I want pulled into default, however I do not want all the changes in those files. So, I want to cherry pick the changes I want to be included in default during the merge (aka manual merge).
I do not want Solar.cpp and Solar.h appearing in default. The world may not yet be ready for a solar powered car. ;-)
What I have learned:
This is possible by a hg merge SolarCarBranch
This can be achieved by setting kdiff3.premerge=False in Mercurial.ini
I do not know how to achieve this since premerge=False still merges/copies Solar.cpp and Solar.h into default without asking me for permission.
You're really close and you don't need to resort to Transplant (yuck) or cherry picking in general. Turning off premerge is the first step, and then just remove the files you don't want in the main branch after merging but before committing. You'll only have to do it once.
Here's a setup:
o changeset: 3:343d531512a3
| branch: solar
| tag: tip
| parent: 1:cb26642f8db5
| user: Ry4an Brase <ry4an#msi.umn.edu>
| date: Wed Mar 10 11:16:48 2010 -0600
| files: afile
| description:
| solar-change
|
|
| # changeset: 2:c5d14e34db07
| | parent: 0:56465175b2fc
| | user: Ry4an Brase <ry4an#msi.umn.edu>
| | date: Wed Mar 10 11:05:44 2010 -0600
| | files: other-main-file
| | description:
| | moremain
| |
| |
o | changeset: 1:cb26642f8db5
|/ branch: solar
| user: Ry4an Brase <ry4an#msi.umn.edu>
| date: Wed Mar 10 11:04:32 2010 -0600
| files: solar-only
| description:
| solar-initial
|
|
o changeset: 0:56465175b2fc
user: Ry4an Brase <ry4an#msi.umn.edu>
date: Wed Mar 10 11:04:14 2010 -0600
files: afile
description:
initial
You can see that changeset 1 adds a file to the solar branch -- a file we don't want in default. While changeset 3 tweaks a file that also exists in main, afile, and we want to manually control whether or not that change happens.
So do hg update default ; hg merge -r solar. The merge tool will pop up for afile and we decide line-by-line or chunk-by-chunk if we want those changes. After saving do a hg stat:
% hg stat
M afile
M solar-only
And we see that solar-only is queued up for commiting in default. Just remove it (with if).
% hg rm -f solar-only
Now hg stat shows it as removed:
% hg stat
M afile
R solar-only
and when we commit we'll have what we want in the new changeset.
Your first condition, to be able to continue work on both branches, is satisfied by simply not closing the branch (which you do with the --close-branch option to hg commit).
You can cherry pick changesets to merge using the Transplant extension. This will let you choose only specific changesets to merge into default.
The third condition is satisfied by the the idea that provided you don't mix changes to both your regular car and your solar powered car in the same commit, you can pull changesets back from the solar branch with impunity, and not have to worry about mixing your car types.
You might want to look at the internal option flags in the merge configuration docs too.