Я не могу вернуться к определенной ревизии. Ниже приведен код, который я использую.
После запуска этого кода я ожидаю увидеть только "Строку 1" и "Строку 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