gpt4 book ai didi

java - 如何使用 JGIT 恢复到修订版

转载 作者:行者123 更新时间:2023-12-02 10:49:44 26 4
gpt4 key购买 nike

我无法恢复到特定版本。以下是我正在运行的代码。

运行此代码后,我希望在文件中仅看到“Line 1”和“Line 2”。但是,当我打开它时,共有 3 行。我希望提交一个新的修订版,其中包括提交 2 中的内容。

import java.io.File;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RevertCommand;
import org.eclipse.jgit.revwalk.RevCommit;

public class JGit1 {

public static void main(String[] args) {
try {
// create a clean repository
File path = new File("c:/temp/agit/gitrepo");
if (path.exists()) {
FileUtils.deleteDirectory(path);
}
Git git = Git.init().setDirectory(path).call();
System.out.println("Created a new repository at " + git.getRepository().getDirectory());

// Create a new file and add it to the index
File newFile = new File(path, "file1.txt");
FileUtils.writeStringToFile(newFile, "Line 1\r\n", "UTF-8", true);
git.add().addFilepattern("file1.txt").call();
RevCommit rev1 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 1").call();

// commit some changes
FileUtils.writeStringToFile(newFile, "Line 2\r\n", "UTF-8", true);
git.add().addFilepattern("file1.txt").call();
RevCommit rev2 = git.commit().setAll(true).setAuthor("test", "test@test.com").setMessage("Commit Log 2").call();

// commit some changes
FileUtils.writeStringToFile(newFile, "Line 3\r\n", "UTF-8", true);
git.add().addFilepattern("file1.txt").call();
RevCommit rev3 = git.commit().setAll(true).setAuthor("test", "test@test.com").setMessage("Commit Log 3").call();

RevertCommand revertCommand = git.revert();
// revert to revision 2
revertCommand.include(rev2);
RevCommit revCommit = revertCommand.call();


// print logs
Iterable<RevCommit> gitLog = git.log().call();
Iterator<RevCommit> it = gitLog.iterator();
while (it.hasNext()) {
RevCommit logMessage = it.next();
System.out.println(logMessage.getName() + " - " + logMessage.getFullMessage());
}

} catch (Exception ex) {
ex.printStackTrace();
}
}
}

编辑:更正代码。一点点进步。现在打开文件时,内容为:

Line 1
<<<<<<< master
Line 2
Line 3
=======
>>>>>>> 54e7037 Commit Log 2

但是,我希望

Line 1
Line 2

最佳答案

RevertCommand.include() 的 javadoc状态包含对要恢复的提交的引用,因此您实际上需要指定应恢复的提交,即 rev3,而不是要重置到的提交。

您实际上尝试的是恢复提交 rev2 中所做的更改,在这种情况下,这会导致与提交 rev3 中的更改发生 merge 冲突。这可以在调用后通过 getUnmergedPaths()/getFailingResults() 的 revertComment 看到。

因此,以下内容应添加第四次提交,以恢复 rev3 中的更改:

            RevertCommand revertCommand = git.revert();
// revert to revision 2
revertCommand.include(rev3);
RevCommit revCommit = revertCommand.call();
System.out.println("Reverted: " + revCommit);
System.out.println("Reverted refs: " + revertCommand.getRevertedRefs());
System.out.println("Unmerged paths: " + revertCommand.getUnmergedPaths());
System.out.println("Failing results: " + revertCommand.getFailingResult());

// print logs
gitLog = git.log().call();
for (RevCommit logMessage : gitLog) {
System.out.println("After revert: " + logMessage.getName() + " - " + logMessage.getFullMessage());
}

System.out.println("File contents: " + FileUtils.readFileToString(newFile, "UTF-8"));

关于java - 如何使用 JGIT 恢复到修订版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com