JGitにマージするにはどうすればよいですか?
master
ブランチとマージしたいとしましょうfoo
。これを行うにはどうすればよいですか?
マージするには、MergeCommand
(パッケージorg.eclipse.jgit.api内の)を.の後に使用できますCheckoutCommand
。確かにJgitには例がないため、例を提供します。
Git git = ... // you get it through a CloneCommand, InitCommand
// or through the file system
CheckoutCommand coCmd = git.checkout();
// Commands are part of the api module, which include git-like calls
coCmd.setName("master");
coCmd.setCreateBranch(false); // probably not needed, just to make sure
coCmd.call(); // switch to "master" branch
MergeCommand mgCmd = git.merge();
mgCmd.include("foo"); // "foo" is considered as a Ref to a branch
MergeResult res = mgCmd.call(); // actually do the merge
if (res.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING)){
System.out.println(res.getConflicts().toString());
// inform the user he has to handle the conflicts
}
私はコードを試していなかったので、完璧ではないかもしれませんが、それはほんの始まりを提供するためのものです。そして、私は輸入品を含めませんでした。JGitを使用した開発は、javadocに基づく多くの試行を意味します
JGit リポジトリには、 Merge のさまざまなテスト クラスがあります。SimpleMergeTest
Merger ourMerger = MergeStrategy.OURS.newMerger(db);
boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c") });
assertTrue(merge);
JGit には、2010 年以来、git resolve マージ戦略の本格的な Java 実装があります。例が必要な場合は、対応する JGit テスト ケースを見て、EGit が MergeCommand をどのように使用しているかを調べてくださいorg.eclipse.egit.core.op.MergeOperation
。