我正在免费(开源)Java 差异库中挖掘 Google 的结果,似乎有很多这样的库(其中一些甚至可以使用通用对象,而不仅仅是字符串)。
在我浏览大量搜索结果却找不到我正在搜索的内容之前,我会先在这里问:
这些 diff 库是否支持诸如 cvs annotate 或 svn blame 之类的功能。我要
String[] 传递给一个函数String[] 传递给一个函数,直到我用完所有它们,或者库告诉我没有原始行未被注释(最后一件事是不是必须的,但非常有用,因为检索旧版本的 String[] 非常昂贵,所以我想尽早停止)ìnt[] 告诉我当前版本的每一行,最后一次更改的版本或者是否根本没有更改(即最后一次更改在第一个版本中)。支持不是 String 的对象很好,但不是必须的。如果 API 不完全是那样,我想我可以接受。
如果没有,任何人都可以建议一个可扩展的差异库,可以轻松添加该功能,最好是一个愿意将该功能作为贡献的人(并且不需要在他们接受贡献之前填写大量的文书工作) ,比如 GNU 项目)?那么我会自愿(至少尝试)将其添加到那里。
最佳答案
我决定自己为 Dmitry Naumenko 的 java-diff-utils 实现它图书馆:
/*
Copyright 2010 Michael Schierl (schierlm@gmx.de)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package difflib.annotate;
import java.util.*;
import difflib.*;
/**
* Generates an annotated version of a revision based on a list of older
* revisions, like <tt>cvs annotate</tt> or <tt>svn blame</tt>.
*
* @author <a href="schierlm@gmx.de">Michael Schierl</a>
*
* @param <R>
* Type of the revision metadata
*/
public class Annotate<R> {
private final List<R> revisions;
private final int[] lineNumbers;
private R currentRevision;
private final List<Object> currentLines;
private final List<Integer> currentLineMap;
/**
* Creates a new annotation generator.
*
* @param revision
* Revision metadata for the revision to be annotated
* @param targetLines
* Lines of the revision to be annotated
*/
public Annotate(R revision, List<?> targetLines) {
revisions = new ArrayList<R>();
lineNumbers = new int[targetLines.size()];
currentRevision = revision;
currentLines = new ArrayList<Object>(targetLines);
currentLineMap = new ArrayList<Integer>();
for (int i = 0; i < lineNumbers.length; i++) {
lineNumbers[i] = -1;
revisions.add(null);
currentLineMap.add(i);
}
}
/**
* Check whether there are still lines that are unannotated. In that case,
* more older revisions should be retrieved and passed to the function. Note
* that as soon as you pass an empty revision, all lines will be annotated
* (with a later revision), therefore if you do not have any more revisions,
* pass an empty revision to annotate the rest of the lines.
*/
public boolean areLinesUnannotated() {
for (int i = 0; i < lineNumbers.length; i++) {
if (lineNumbers[i] == -1 || revisions.get(i) == null)
return true;
}
return false;
}
/**
* Add the previous revision and update annotation info.
*
* @param revision
* Revision metadata for this revision
* @param lines
* Lines of this revision
*/
public void addRevision(R revision, List<?> lines) {
Patch patch = DiffUtils.diff(currentLines, lines);
int lineOffset = 0; // remember number of already deleted/added lines
for (Delta d : patch.getDeltas()) {
Chunk original = d.getOriginal();
Chunk revised = d.getRevised();
int pos = original.getPosition() + lineOffset;
// delete lines
for (int i = 0; i < original.getSize(); i++) {
int origLine = currentLineMap.remove(pos);
currentLines.remove(pos);
if (origLine != -1) {
lineNumbers[origLine] = original.getPosition() + i;
revisions.set(origLine, currentRevision);
}
}
for (int i = 0; i < revised.getSize(); i++) {
currentLines.add(pos + i, revised.getLines().get(i));
currentLineMap.add(pos + i, -1);
}
lineOffset += revised.getSize() - original.getSize();
}
currentRevision = revision;
if (!currentLines.equals(lines))
throw new RuntimeException("Patch application failed");
}
/**
* Return the result of the annotation. It will be a List of the same length
* as the target revision, for which every entry states the revision where
* the line appeared last.
*/
public List<R> getAnnotatedRevisions() {
return Collections.unmodifiableList(revisions);
}
/**
* Return the result of the annotation. It will be a List of the same length
* as the target revision, for which every entry states the line number in
* the revision where the line appeared last.
*/
public int[] getAnnotatedLineNumbers() {
return (int[]) lineNumbers.clone();
}
}
我还把它发给了 Dmitry Naumenko(连同一些测试用例),以防他想要包含它。
关于java - 是否有支持 Annotate/Blame 的 Java Diff 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4443568/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您