13

JGitにマージするにはどうすればよいですか?

masterブランチとマージしたいとしましょうfoo。これを行うにはどうすればよいですか?

4

4 に答える 4

9

マージするには、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に基づく多くの試行を意味します

于 2012-08-27T09:49:07.657 に答える
4

JGit リポジトリには、 Merge のさまざまなテスト クラスがあります。SimpleMergeTest

Merger ourMerger = MergeStrategy.OURS.newMerger(db);
boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c") });
assertTrue(merge);
于 2012-08-27T09:44:18.617 に答える
2

JGit には、2010 年以来、git resolve マージ戦略の本格的な Java 実装があります。例が必要な場合は、対応する JGit テスト ケースを見て、EGit が MergeCommand をどのように使用しているかを調べてくださいorg.eclipse.egit.core.op.MergeOperation

于 2012-09-01T08:40:58.000 に答える