【问题标题】:Usage of 'pull' command in JgitJgit中'pull'命令的使用
【发布时间】:2012-11-04 04:45:57
【问题描述】:

我是 git 的新用户,正在使用 JGit 与远程 git 存储库进行交互。在 JGit 中,我最初使用 CloneCommand 来克隆一个 repo,它可以正常工作。但是,当我尝试使用 PullCommand(相当于 SVN 更新 AFAIK)时,本地 repo 内容不会更新。

这是我使用的代码:

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();  
}

这不会为我使用命令行推送到远程存储库的新文件更新本地存储库。但是,如果我删除本地存储库并再次进行克隆,则会反映所有更改。

请告诉我在 JGit 中使用PullCommand 的正确方法是什么。

编辑:

远程仓库的结构:

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

directory_1 和这两个文件是在初始克隆后从命令行推送的,我尝试了这段代码,以便它反映在本地存储库中,但没有发生。

用于克隆存储库的代码:

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

这里,gitlocalPathremotePath 是与上面相同的变量。

【问题讨论】:

  • 能否包含用于克隆存储库的代码?

标签: java git git-pull jgit


【解决方案1】:

我怀疑问题是当前分支没有上游配置(因此 pull 不会合并获取的分支)。

要查看拉取期间发生的情况,请检查 pullCmd.call() 的结果:

PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus();  // this should be interesting

【讨论】:

  • 感谢您的建议。 getMergeStatus.isSuccessful() 的结果为真。但是,这些更改仍然没有在本地反映出来。有什么想法吗?
  • 什么状态?应该是FAST_FORWARD。另外,您是否使用命令行 git 客户端查看了克隆的存储库,它看起来是否正常(例如 git status)?
  • 我查了一下,上面写着ALREADY_UP_TO_DATE。猜猜这就是问题所在。但不知何故,来自远程存储库的文件没有更新。 git status 在分支 master 上显示,并显示一些之前删除的文件。
【解决方案2】:

文档说Git 类的构造函数如下:

构造一个新的 Git 对象,该对象可以与指定的 git 存储库交互。此类方法返回的所有命令类将始终与此 git 存储库交互。

所以,正如我建议的那样,你必须将远程仓库的路径传递给构造函数,现在你正试图从本地仓库中提取。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。对我来说,解决方案是在克隆后设置 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");
    

    通过拉动后的这些更改,我看到了本地反映的更改。

    【讨论】:

      【解决方案4】:

      从私有存储库尝试此代码用于 git pull

      try {
              Repository localRepo = new FileRepository(localPath + "/.git");
              Git git = new Git(localRepo);
              CredentialsProvider cp = new UsernamePasswordCredentialsProvider(userName, pass);
              git.pull().setCredentialsProvider(cp).call();
              git.close();
          } catch (Exception e) {
          }
      

      【讨论】:

        猜你喜欢
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-27
        • 1970-01-01
        • 2016-05-21
        • 2012-10-11
        • 1970-01-01
        相关资源
        最近更新 更多