12

私は git の新しいユーザーであり、JGitを使用してリモートの git リポジトリとやり取りしています。JGit では、CloneCommand最初はレポのクローンを作成していましたが、問題なく動作しました。ただし、PullCommandSVN update AFAIK に相当する を使用しようとすると、ローカル リポジトリの内容が更新されません。

これは私が使用したコードです:

private String localPath;
private Repository localRepo;
private Git git;

localPath = "/home/test/git_repo_test";
remotePath = "https://github.com/test/repo_1.git";

try {
    localRepo = new FileRepository(localPath + "/.git");
} catch (IOException e) {
    e.printStackTrace();  
}
git = new Git(localRepo);

PullCommand pullCmd = git.pull();
try {
    pullCmd.call();
} catch (GitAPIException e) {
    e.printStackTrace();  
}

これは、コマンド ラインを使用してリモート リポジトリにプッシュした新しいファイルのローカル リポジトリを更新しません。ただし、ローカル リポジトリを削除して再度クローンを作成すると、すべての変更が反映されます。

PullCommandJGitで使用する正しいアプローチを教えてください。

編集:

リモートリポジトリの構造:

root ____ file_1
  |______ directory_1
              |__________ file_2 
              |__________ file_3

directory_1 と 2 つのファイルは、最初のクローン作成後にコマンドラインからプッシュされます。ローカル リポジトリに反映されるように、このコードを試しましたが、これは発生していません。

リポジトリのクローンに使用されるコード:

File file = new File(localPath);
CloneCommand cloneCmd = git.cloneRepository();
try {
    cloneCmd.setURI(remotePath)
            .setDirectory(file)
            .call();
} catch (GitAPIException e) {
    e.printStackTrace();  
}

ここでgitlocalPathremotePathは上記と同じ変数です。

4

4 に答える 4

9

問題は、現在のブランチにアップストリーム構成がないことだと思います(したがって、プルはフェッチされたブランチをマージしません)。

プル中に何が起こったかを確認するには、次の結果を調べpullCmd.call()ます。

PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus();  // this should be interesting
于 2012-11-15T15:17:04.823 に答える
4

ドキュメンテーションは、Git次のクラスのコンストラクターについて述べています:

指定された git リポジトリとやり取りできる新しい Git オブジェクトを構築します。このクラスのメソッドによって返されるすべてのコマンド クラスは、常にこの git リポジトリと対話します。

したがって、私が提案できるように、リモート リポジトリのパスをコンストラクタに渡す必要があります。今度は、ローカル リポジトリからプルしようとしています。

于 2012-11-15T15:02:36.720 に答える
2

私は同じ問題に遭遇しました。私にとっての解決策は、クローン作成後にgit構成ファイルを設定することでした:

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI("<repo-uri>");
cloneCommand.setDirectory("<repo-dir>");
cloneCommand.call();

Git git = Git.open("<repo-dir>");
StoredConfig config = git.getRepository().getConfig();
config.setString("branch", "master", "merge", "refs/heads/master");
config.setString("branch", "master", "remote", "origin");
config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
config.setString("remote", "origin", "url", "<repo-uri>");
config.save();

プルするときは、リモート ブランチ名を「master」に、リモートを「origin」に設定します。

PullCommand pull = git.pull();
pull.setRemote("origin");
pull.setRemoteBranchName("master");

プル後のこれらの変更により、変更がローカルに反映されていることがわかります。

于 2019-04-27T20:46:15.950 に答える