在 Mercurial 中,如何将反向补丁应用于特定文件?

发布于 2024-07-26 16:29:27 字数 693 浏览 2 评论 0原文

Mercurial:在一个存储库中的分支之间合并一个文件 ,我正在尝试对单个文件执行取消操作,即使该文件是被取消的修订版本的众多参与者之一。

HG 是面向变更集的工具,它不想对文件进行操作。

我能找到的最接近的是使用 hg export 创建差异,手动编辑差异,然后 hg import 以相反的顺序修补文件。

..但后来我遇到了这个恼人的情况 http:// hgbook.red-bean.com/read/finding-and-fixing-mistakes.html 声称有一个 hg patch 的 --reverse 选项,但实际上没有。

因此,我能想到的最接近的方法是生成如上所述的手动编辑补丁,然后使用 vanilla patch -R 应用反向补丁。

hg backout 命令在这里似乎很有用,但实际上是转移注意力。

一定有更好的方法,不是吗?

Related to Mercurial: Merging one file between branches in one repo , I'm trying to perform a backout operation on a single file, even though that file was one of many participants in the revision being backed out.

HG being the changeset-oriented tool that it is, it doesn't want to operate on files.

Closest I could find was to use hg export to create a diff, hand-edit the diff, and then hg import to patch the file in reverse order.

..but then I hit this annoying situation where http://hgbook.red-bean.com/read/finding-and-fixing-mistakes.html claims that there is a --reverse option to hg patch when there is not.

So the closest thing I can think of is to generate a hand-edited patch as above, and then using vanilla patch -R to apply a reverse patch.

The hg backout command would seem to be useful here, but is actually a red herring.

There has GOT to be a better way, no?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆で 2024-08-02 16:29:27

您可以仅使用 -I (包括与给定模式匹配的名称)参数通过一行进行回退:

hg backout --merge -I thefiletorevert -m 'message' OFFENDINGREVISIONID

示例脚本:

hg init testrepo
cd testrepo
echo -e "line1\n\nline3" > file1
echo -e "line1\n\nline3" > file2
hg commit -A -m 'changes to two files'
perl -pi -e 's/line1/line 1/' file1
perl -pi -e 's/line1/line 1/' file2
hg commit -m 'put spaces in line1'
perl -pi -e 's/line3/line 3/' file1
perl -pi -e 's/line3/line 3/' file2
hg commit -m 'put spaces in line3'
hg backout --merge -I file1 -m 'remove spaces from line1' 1

示例输出:

adding file1
adding file2
reverting file1
created new head
changeset 3:6d354f1ad4c5 backs out changeset 1:906bbeaca6a3
merging with changeset 3:6d354f1ad4c5
merging file1
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

结果文件内容:

file1:line1
file1:line 3
file2:line 1
file2:line 3

请注意,在取消中间变更集之后,file1 在第一行中缺少空格,并且详细日志仅显示取消中更改的一个文件:

$ hg log -v -r tip
changeset:   3:6d354f1ad4c5
tag:         tip
parent:      1:906bbeaca6a3
user:        Ry4an Brase <ry4an@mini>
date:        Mon Sep 14 12:17:23 2009 -0500
files:       file1
description:
remove spaces from line1

You can do it using just the -I (include names matching the given patterns) argument for backout with a single line:

hg backout --merge -I thefiletorevert -m 'message' OFFENDINGREVISIONID

Example Script:

hg init testrepo
cd testrepo
echo -e "line1\n\nline3" > file1
echo -e "line1\n\nline3" > file2
hg commit -A -m 'changes to two files'
perl -pi -e 's/line1/line 1/' file1
perl -pi -e 's/line1/line 1/' file2
hg commit -m 'put spaces in line1'
perl -pi -e 's/line3/line 3/' file1
perl -pi -e 's/line3/line 3/' file2
hg commit -m 'put spaces in line3'
hg backout --merge -I file1 -m 'remove spaces from line1' 1

Sample output:

adding file1
adding file2
reverting file1
created new head
changeset 3:6d354f1ad4c5 backs out changeset 1:906bbeaca6a3
merging with changeset 3:6d354f1ad4c5
merging file1
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

Resulting File Contents:

file1:line1
file1:line 3
file2:line 1
file2:line 3

notice that file1 is missing it's space in line one after the backout of the middle changeset, and the verbose log shows only one file changed in the backout:

$ hg log -v -r tip
changeset:   3:6d354f1ad4c5
tag:         tip
parent:      1:906bbeaca6a3
user:        Ry4an Brase <ry4an@mini>
date:        Mon Sep 14 12:17:23 2009 -0500
files:       file1
description:
remove spaces from line1
倾`听者〃 2024-08-02 16:29:27

这就是我要做的:使用尖端修订版的新克隆。

hg backout --merge -r revision_where_the_change_happened

将反转的更改合并到工作副本中。

现在将有问题的文件复制到常规工作副本并提交

hg commit -m "Reversed the changes to file.h made in revision bla"

并丢弃上面创建的克隆。

这样,mercurial 就不知道 revision_where_the_change_happened 与此提交之间存在联系。 如果您希望 Mercurial 记住这一点,请

hg revert {all files except the one in question}

在将回退提交合并到工作副本之后并在提交之前执行此操作。 对于第二种方式,您不需要处理克隆,因为您想保留回退提交。

我猜想您使用哪种方式的选择取决于特定文件更改占更改集的大小。

Here's what I would do: Use a fresh clone of the tip revision.

hg backout --merge -r revision_where_the_change_happened

to merge the reversed changes into the working copy.

Now copy the file in question to your regular working copy and commit

hg commit -m "Reversed the changes to file.h made in revision bla"

and throw away the clone you created above.

This way, mercurial doesn't know that there is a connection between revision_where_the_change_happened and this commit. If you want mercurial to remember this, instead do a

hg revert {all files except the one in question}

after merging the backout commit into the working copy and before commiting. For the second way, you don't need to work on a clone, because you want to keep the backout commit.

I would guess that the choice of which way you use depends on how big a part of the changeset the particular file change was.

眼波传意 2024-08-02 16:29:27

使用恢复命令。

hg revert -r1 file

这应该将文件内容恢复到修订版 1 中的版本。
然后您可以进一步编辑它并正常提交。

Use the revert command.

hg revert -r1 file

This should revert the contents of file to the version in revision 1.
You can then further edit it and commit as normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文