调试补丁导入到Mercurial和Git repos:从补丁导入失败,但差异似乎匹配

时间:2014-08-31 21:39:04

标签: git mercurial diff patch git-diff

我通过我的git repo创建了补丁

git format-patch --relative <sha1>...<sha2>

创建2个补丁:01.patch02.patch。我通过

将补丁导入了我的hg repo
hg import <patch file>

第一个补丁有效,这是workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/EdgeBase.java的整个文件添加。第二个补丁是文件workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java上的差异,我得到以下输出:

[lucas-ThinkPad-W520]/home/.../.Solstice_WS/7K_Manual$ hg import 0*
applying 01.patch
applying 02.patch
patching file workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java
Hunk #1 FAILED at 10
Hunk #2 FAILED at 21
Hunk #3 FAILED at 30
Hunk #4 FAILED at 39
Hunk #5 FAILED at 48
Hunk #6 FAILED at 57
6 out of 6 hunks FAILED -- saving rejects to file workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java.rej
transaction abort!
rollback completed
abort: patch failed to apply
[lucas-ThinkPad-W520]/home/.../.Solstice_WS/7K_Manual$ ls

有关如何调试此问题的任何建议?代码中的差异(“帅哥”)似乎完美匹配。有关在何处查看的建议,或在调试时需要考虑的其他因素?

以下是我的.hgrc的相关部分:

[extensions]
mq =
hgext.purge=
hgext.fetch=
shelve=

[diff]
git = 1
showfunc = 1
unified = 8

如果有兴趣,这里是帅哥和原始的UndirectedEdge.java文件:

帅哥:

 .../washington/cs/utils/graph/impl/UndirectedEdge.java  | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java b/workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java
index ef7e993..647f4f1 100644
--- a/workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java
+++ b/workspace/GraphUtilities/src/edu/washington/cs/utils/graph/impl/UndirectedEdge.java
@@ -11,7 +11,8 @@ import edu.washington.cs.utils.graph.IVertex;
  * 
  * @param <V> Vertex type for the edge
  */
-public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
+public class UndirectedEdge<V extends IVertex> extends EdgeBase<V> 
+   implements IUndirectedEdge<V>
 {
     /**
      * Constructs an undirected edge between <code>vertex1</code> and <code>vertex1</code>.
@@ -22,7 +23,7 @@ public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
      */
     public UndirectedEdge(V vertex1, V vertex2)
     {
-        throw new RuntimeException("Method not implemented.");
+       super(vertex1, vertex2);
     }

     /**
@@ -31,7 +32,7 @@ public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
     @Override
     public Set<V> getVertices()
     {
-        throw new RuntimeException("Method not implemented.");
+       return super.getVertices();
     }

     /**
@@ -40,7 +41,7 @@ public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
     @Override
     public boolean equals(IUndirectedEdge<?> other)
     {
-        throw new RuntimeException("Method not implemented.");
+       return getVertices().equals(other.getVertices());
     }

     /**
@@ -49,7 +50,9 @@ public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
     @Override
     public boolean equals(Object other)
     {
-        throw new RuntimeException("Method not implemented.");
+       if (other instanceof IUndirectedEdge<?>)
+           return equals((IUndirectedEdge<?>) other);
+       return false;
     }

     /**
@@ -58,6 +61,8 @@ public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
     @Override
     public int hashCode()
     {
-        throw new RuntimeException("Method not implemented.");
+       int result = 17;
+       result = result * 31 + getVertices().hashCode();
+       return result;
     }
 }
-- 
1.9.1

UndirectedEdge.java:

package edu.washington.cs.utils.graph.impl;

import java.util.Set;

import edu.washington.cs.utils.graph.IUndirectedEdge;
import edu.washington.cs.utils.graph.IVertex;


/**
 * UndirectedEdge is a implementation of {@link IUndirectedEdge}.
 * 
 * @param <V> Vertex type for the edge
 */
public class UndirectedEdge<V extends IVertex> implements IUndirectedEdge<V>
{
    /**
     * Constructs an undirected edge between <code>vertex1</code> and <code>vertex1</code>.
     * 
     * @param vertex1 vertex at one end of the edge.
     * @param vertex2 vertex at the other end of the edge.
     * @throws IllegalArgumentException if any of the arguments is <code>null</code>
     */
    public UndirectedEdge(V vertex1, V vertex2)
    {
        throw new RuntimeException("Method not implemented.");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Set<V> getVertices()
    {
        throw new RuntimeException("Method not implemented.");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(IUndirectedEdge<?> other)
    {
        throw new RuntimeException("Method not implemented.");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object other)
    {
        throw new RuntimeException("Method not implemented.");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        throw new RuntimeException("Method not implemented.");
    }
}

1 个答案:

答案 0 :(得分:2)

这对我来说是疏忽,但也许它可以帮助别人。我的Mercurial存储库采用DOS换行格式,而我的Git存储库采用Unix格式。格式差异导致帅哥失败。

我在git format-patch输出带有.patch个新行的\n文件时发现了此问题,而hg import仅为新行添加了\r\n个补丁。< / p>

可以使用:%s/\n/\r\n/gunix2dos或使用Linux / Unix命令行定义here在Vim中修复此问题,并将其应用于.patch文件。