如何使用NodeGit从分支中拉取代码?

3

希望您能帮助我解决 nodegit 的问题。我已经通过以下命令成功切换到分支 (branch-development-modular-alpha-2.0.0):

var cloneRepo = nodegit
.Clone(cloneURL, repoPath, cloneOptions)
.then(function (repo) {
  repo.getBranch('refs/remotes/origin/' + branch).then(function(reference) {
    repo.checkoutRef(reference);

    // Go through repo's submodules
    nodegit.Submodule.foreach(repo, function (submodule) {
      var submoduleName = submodule.name();
      var submoduleURL = submodule.url();
      var submodulePath = repoPath + "/" + submodule.path();

      // Clone the submodule
      nodegit
      .Clone(submoduleURL, submodulePath, cloneOptions)
      .then(function (repo) {
        // TODO: DO SOMETHING.
      })
    })
  })
})

现在我想从同一个分支中拉取更改,但是我有类似这样的内容,但它没有更新为分支中最新的更改。

nodegit
.Repository
.open(path.resolve(__dirname, repoPath))
.then(function (repo) {
  existingRepository = repo;

  return existingRepository.fetchAll(cloneOptions.fetchOpts);
})
.then(function () {
  return existingRepository.mergeBranches('master', 'refs/remotes/origin/' + branch);
})
.catch(function(error) {
  console.log(error);
})

我做错了什么?

你知道在幕后它只是执行git命令,对吧?你使用这个模块有什么原因吗? - Darkrum
3
那是不正确的。NodeGit是围绕着libgit2构建的JavaScript绑定,而libgit2是Git核心功能的纯C实现。 - rcjsuen
能否分享完整的代码?我遇到了问题。 - Sunil Garg
2个回答

1

一个pull操作等同于fetch + merge。

不同的是,merge的源分支是origin/master到master。
或者是origin/branch到branch。

在你的情况下,nodegit merge函数的调用应该是:

return existingRepository.mergeBranches(branch, 'refs/remotes/origin/' + branch);

正如 Jamie Counsell评论中所说:

我也遇到了那个错误。

最终发现是因为我使用了 nodegit 检索分支名称,它返回的是类似于 origin/refs/heads/master 而不是仅仅的 master

调用 branch.shorthand() 得到了 "master"


分支在远程存在,但是在本地尽管我已经checkout了该分支,我只能看到“master”。我应该先在本地创建该分支吗? - Chris F.
@ChrisF. 你能在本地命令行中执行 git branch -avv 吗? - VonC
我得到了这个:*(指向 origin/branch-development-modular-alpha-2.0.0 的 HEAD 分离状态) master remotes/origin/branch-development-modular-alpha-2.0.0 remotes/origin/master - Chris F.
@ChrisF。我也遇到了那个错误。最终发现是因为我使用nodegit检索分支名称,它返回了类似于“origin/refs/heads/master”而不是只有“master”的内容。调用branch.shorthand()得到了"master" - Jamie Counsell
@JamieCounsell 谢谢你。我已经将你的评论包含在答案中,以增加其可见性。 - VonC
显示剩余5条评论

0
以下是nodegit存储库中的示例拉取
var nodegit = require("../");
var path = require("path");

var repoDir = "../../test";

var repository;

// Open a repository that needs to be fetched and fast-forwarded
nodegit.Repository.open(path.resolve(__dirname, repoDir))
  .then(function(repo) {
    repository = repo;

    return repository.fetchAll({
      callbacks: {
        credentials: function(url, userName) {
          return nodegit.Cred.sshKeyFromAgent(userName);
        },
        certificateCheck: function() {
          return 0;
        }
      }
    });
  })
  // Now that we're finished fetching, go ahead and merge our local branch
  // with the new one
  .then(function() {
    return repository.mergeBranches("master", "origin/master");
  })
  .done(function() {
    console.log("Done!");
  });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接