Create a new commit not including some old - version-control

So, i have a repository in mercurial as described at the picture below. I need to create a new commit but without changes that was done by two commits. Lets name these 2 commits as "commit 4" and "commit 16". But I can't just "rewrite the history" and delete these commits.

The answer is simple: backout the changesets you do not need or want anymore:
hg backout -rXXX
where XXX is the revision of the changeset you want to see gone. Thus in your case first checkout your last changeset (e.g. 23), then:
hg backout -r4
hg backout -r16
and your task is done.

Here's what I understand you want to do:
Take the topmost (latest) changeset, let's call this changeset 23
"Undo" whatever changeset 4 and changeset 16 did on top of this
This gives you:
Changeset 23 is still intact, complete with whatever changeset 4 and 16 did
Your new changeset(s) would be like 23, except without whatever changeset 4 and 16 did
Here's how to do this, here's the history prior to the operations:
23
|
...
|
16
|
...
|
4
|
...
Update to the topmost changeset
Create a backout changeset for changeset 4 and commit it, this will be committed on top of changeset 23
Create a backount changeset for changeset 16 and commit it, this will be committed on top of changeset 24
Your history should now look like this:
25 (-16)
|
24 (-4)
|
23
|
...
|
16
|
...
|
4
|
...
Note that:
Changeset 23 still contains changeset 4 and 16
Changeset 25 contains changeset 23, but the changes from 4 and 16 have been reversed (backed out)
If changes in 5 and up rely on changes introduced in changeset 4, and/or changes in 17 and up rely on changes introduced in changeset 16, you will get merge conflicts, you will need to handle these when merging

Related

Why is Mercurial matching a nonexistent local revision number?

Quick intro: In Mercurial there are two different ways to numerically refer to a changeset.
First, there's the node ID hash. It is global and functions like a git commit hash. It consists of 40 hexadecimal digits.
Second, there's the local revision number. It is a decimal number that starts at 0 and counts up. Unlike the node hash, this is local, meaning the same changeset can have different local revision numbers in two different repos. This depends on what other changesets are present in each repo and depends even on the order each repo received their changesets.
A revision can be specified numerically to Mercurial as a local revision number, a full 40-digit hash, or "a short-form identifier". The latter gives a unique prefix of a hash; that is, if only one full hash starts with the given string then the string matches that changeset.
I found that in certain cases, Mercurial commands (such as hg log with an -r switch), given plain decimal numbers, will match some revision even though there aren't enough local revisions for the given number to match as a local revision number.
Here's an example I constructed after coming across such a case by chance:
test$ hg --version
Mercurial Distributed SCM (version 6.1)
(see https://mercurial-scm.org for more information)
Copyright (C) 2005-2022 Olivia Mackall and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
test$ hg init
test$ touch a
test$ hg add a
test$ hg ci -d "1970-01-01 00:00:00 +0000" -u testuser -m a
test$ touch b
test$ hg add b
test$ hg ci -d "1970-01-01 00:00:00 +0000" -u testuser -m b
test$ hg log
changeset: 1:952880b76ae5
tag: tip
user: testuser
date: Thu Jan 01 00:00:00 1970 +0000
summary: b
changeset: 0:d61f66df66f9
user: testuser
date: Thu Jan 01 00:00:00 1970 +0000
summary: a
test$ hg log -r 2
abort: unknown revision '2'
test$ hg log -r 9
changeset: 1:952880b76ae5
tag: tip
user: testuser
date: Thu Jan 01 00:00:00 1970 +0000
summary: b
test$
As is evident, hg log -r 9 matches a changeset even though there aren't that many changesets to match the 9 as a local revision number.
The question: Why is this? Additionally, how can we avoid matching a nonexistent local revision number?
This is due to how Mercurial parses revision specifiers. Here's how Olivia Mackall explains it in a mail from 2014:
Here is a hexadecimal identifier:
60912eb2667de45415eff601bfc045ae0fe8db42
See how it starts with 6? If you ask for revision 6, Mercurial will:
a) look for revision 6
b) if that fails, look for a hex identifier starting with "6"
c) if we find more than one match, complain
d) if we find no matches, complain
e) we found one match: success!
That is, if hg log -r 9 doesn't match any local revision number (because there are less than ten changesets in the repo), Mercurial next will match a node hash that happens to start with a 9.
To avoid this ambiguity, she responded that one should use hg log -r 'rev(9)' to match only local revision numbers, and hg log -r 'id(9)' to match only prefixes or full hashes.
In the documentation on revsets, these predicates are listed as:
"id(string)"
Revision non-ambiguously specified by the given hex string prefix.
And:
"rev(number)"
Revision with the given numeric identifier.
Unfortunately, both this page and the help page on revisions do not (as of version 6.1) explicitly point out the ambiguity between numbers that can match either as local revision numbers or node hash prefixes. The 2014 mailing list thread I quoted does contain suggestions to clarify this but it appears nothing came off it.
Additionally, here is a changeset message in which I explained the entire affair and how it came to affect the operation of a script of mine:
fix to use 'rev(x)' instead of just x to refer to local rev number
The revsets syntax to unambiguously refer to a local revision
number is to wrap the number in rev(). Without this, a number
that doesn't exist (eg -r 2) may be misinterpreted to refer to
a changeset that has a node hash starting with the requested
number.
In our case this bug happened to act up after the revision on at
2022-04-02 16:10:42 2022 Z "changed encoding from cp850 to utf8"
which the day after it was added was converted as the second
(local revision number 1) changeset from the svn repo. The
particular hg convert command was:
hg convert svn-mirror DEST \
--config hooks.pretxncommit.checkcommitmessage=true \
--config convert.svn.startrev=1152
This created a changeset known as 1:2ec9f101bc31 from that svn
revision. Lacking a local revision number 2, the -r 2 picked up
this changeset because its hash started with the digit "2".
Tnus the NEWNODE variable received the changeset hash for this
changeset. Because our hg rebase command is configured to keep
empty changesets, the changeset got added atop its already
existing copy in the destination repo.
Ever since the akt.sh script would pick up the wrong revision
number from the destination repo and abort its run with the
message indicating "Revisions differ!".

Undo a wrong merge with Bazaar

I have an issue while using Bazaar and I would like to get the best practices to fix it.
Here is the context:
In the trunk of our project, one wrongly merged a branch (BranchA) in the trunk but he used revert on most files while keeping the merge info (so from bazaar BranchA was effectively merged in trunk, preventing to REALLY merge it later).
The merged revision was committed as r4.
After this (undesired) merge, many devs committed right after (r5 & r6).
So my question is : how to undo this wrong merge ? (while keeping the commits after).
I tried to uncommit back to r3 and merge each rev from r4 to R6 (omitting r4)
I tried 'reverse cherry pick' r4 ... but the BranchA merge information may still memorized.
trunk
|
r6
|
r5
| branchA
| |
r4 ---+
| |
| r2.2
| |
r3 r2.1
| |
r2 ---+
|
r1
If you have a solution or some clue to fix it, please share it !
(for example can REBASE command help here?)
Since information was lost due to the reverts in the merge that has gone wrong, there won't be a 100% fix. Your best bet is to create a new branch off of r3 in the trunk and cherry pick r5 and r6, like this:
cd /path/to/repo
bzr branch trunk -r3 branchX
cd branchX
bzr merge -c5 ../trunk
bzr commit -m "cherry picked r5: $(bzr log --line -r5 ../trunk)"
bzr merge -c6 ../trunk
bzr commit -m "cherry picked r6: $(bzr log --line -r6 ../trunk)"
Or, before cherry picking r5, you might want to merge from branchA the "right way".
The rebase command won't help you here. The purpose of rebasing is basically to reorder revisions: whether you merge or rebase, you end up with the same content in the files, only the revision history graph will have a different shape. The purpose of rebase is usually to make a history graph look flat, which can be neat, but it's only cosmetics, in terms of content the end result is the same.

How to view a single Mercurial commit from the command line?

I would like to view from the commandline what was changed in given Mercurial commit similar to what one would see from hg status or from the TortoiseHg tool. The closest I can seem to get is hg log --stat but that prints extra symbols (i.e. pluses and minuses) and I cannot specify at which specific revision I want to look.
I need this because I have developers who have check-in comments like "." or ",". >:-(
It turns out that hg status has a --change argument where you can pass the revision number (e.g. 109), relative revision (ie -1 is last commit, -2 is second-last, etc), or the hash of the revision to it and it will print out the changes (i.e. additions, removals, and modification) that revision had.
--change isolates that revision and shows just from that revision, but replacing --change with --rev shows the cumulative effect since that revision to the current state.
hg log -v -r <changeset>
changeset: 563:af4d66e2bc6e
tag: tip
user: David M. Carr <****>
date: Fri Oct 26 22:46:02 2012 -0400
files: hggit/gitrepo.py tests/test-pull.t
description:
pull: don't pull tags as bookmarks
or, using templates, something like
hg log -r tip --template "{node|short} - files: {files}\n"
with output
af4d66e2bc6e - files: hggit/gitrepo.py tests/test-pull.t

How to edit Mercurial commit message after branching?

I have some old commit messages in a Mercurial repository that should be changed (to adjust for some new tools). I already understand that this hacking has to be done on the master repository and all local repositories would have to be re-cloned, because checksums of all subsequent changesets will also change.
I've tried following the recipes in "How to edit incorrect commit messages in Mercurial?", but with MQ extension I got stuck on error message
X:\project>hg qimport -r 2:tip
abort: revision 2 is the root of more than one branch
and with Histedit quite similarly
X:\project>hg histedit 2
abort: cannot edit history that would orphan nodes
The problem seems to be that there have been branches created after the changeset.
I can see how it would become messy if I'd want to change the contents of patch, but perhaps there's a workaround that I've missed for editing the commit message?
I would use a hacked version of the convert extension to do this. The extension can do hg → hg conversions which lets you alter author and branch names. There is not support for changing commit messages yet, but you can hack it.
Specifically, you should change the getcommit method from:
def getcommit(self, rev):
ctx = self.changectx(rev)
parents = [p.hex() for p in self.parents(ctx)]
if self.saverev:
crev = rev
else:
crev = None
return commit(author=ctx.user(), date=util.datestr(ctx.date()),
desc=ctx.description(), rev=crev, parents=parents,
branch=ctx.branch(), extra=ctx.extra(),
sortkey=ctx.rev())
which is responsible for reading the old commits. Change the
desc=ctx.description()
to
desc=adjust(ctx.description())
and then implement the adjust function at the top of the file:
def adjust(text):
return text.upper()
If these are accidental/duplicate branches due to using --amend and push --force then strip them first and try 'histedit' again then wipe the central repo on bitbucket; try the following which worked for me:
Inspect the repository log and look for branches, you can use the GraphlogExtension which you will have to enable first:
# hg log -G | more
...
o changeset: 43:c2fcca731aa5
| parent: 41:59669b9dfa4a
| user: Daniel Sokolowski (https://webdesign.danols.com)
| date: Tue Aug 27 20:14:38 2013 -0400
| summary: Progress snapshot: major content text and model instance ..
...
| o changeset: 42:c50724a6f1c6
|/ user: Daniel Sokolowski (https://webdesign.danols.com)
| date: Tue Aug 27 20:14:38 2013 -0400
| summary: Progress snapshot: major content text and model instance ...
|
o changeset: 41:59669b9dfa4a
| user: Daniel Sokolowski (https://webdesign.danols.com)
...
Enable the MqExtension and strip all branches.
# hg strip --no-backup 42:c50724a6f1c6
# hg strip --no-backup 45:3420dja12jsa
...
If needed change the commit to 'draft' (see Phases) and re-run 'histedit' and all should be good now.
# hg histedit 14:599dfa4a669b
abort: cannot edit immutable changeset: b7cfa2f28bde
# hg phase -f -d 14:599dfa4a669b
# hg hsitedit 14:599dfa4a669ba
I needed something similar so I filed a feature request.

Unknown revision numbers in bzr tags command

When I run the bzr tags command on a branch, I often get some tags that are displayed with no revision number. It appears as a question mark. For example, when I run this command:
bzr tags -d lp:~zaber/openobject-client/main
tag 5.0.7 doesn't have a revision number:
5.0.0 930
5.0.0-2 933
5.0.0-3 938
5.0.0-alpha 719
5.0.0-rc1 771
5.0.0-rc1.1 776
5.0.0-rc2 830
5.0.0-rc3 858
5.0.1 946.1.19
5.0.2 976
5.0.3 983
5.0.4 986
5.0.5 993
5.0.6 1000
5.0.7 ?
5.0.7rc1 1022
5.0.7rc2 1042
This may happen more often when I've got shared repositories for several local branches, but I'm not sure.
Those tags are known to bzr (fetched or merged from another branch in some pull or merge operation) but corresponding revision is not present in your history (not merged to your branch).
Strictly to say that's a bug, you can find it in the bzr bugtracker on Launchpad.net.
What you can do about such tags:
remove them from your branch only with bzr tag --delete XXX
use them to merge those revisions later with bzr merge -r tag:YYY lp:XXX
look at the corresponding revision ids with bzr tags --show-ids
As bialix suggested, deleting the tags using bzr tag --delete XXX works. Also, deleting a tag on a checkout also deletes the tag on the master branch. (I guess that's parallel to the way commits work, but it still surprised me.) Sometimes a merge will bring a bunch of broken tags across, so here's a gawk command to remove all unknown tags from the local branch:
bzr tags | gawk '/\?/ { system("bzr tag --delete " $1) }'