3

Would appreciate some help with nodegit. I got it to checkout a branch (branch-development-modular-alpha-2.0.0) with:

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.
      })
    })
  })
})

Now I'd like to pull the changes from the same branch and I have something like this however it's not updating with the latest changes from the branch.

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

What am I doing wrong?

Chris F.
  • 493
  • 5
  • 17
  • You know that under the hood it's just executing git commands right? Is there a reason you're using this module? – Darkrum Mar 28 '18 at 03:02
  • 3
    @Darkrum That is incorrect. [NodeGit](http://www.nodegit.org/) is a JavaScript binding around [libgit2](https://libgit2.github.com/) which is a pure C implementation of Git's core features. – rcjsuen Mar 28 '18 at 03:09
  • canu share the complete code? m facing issues – Sunil Garg Mar 09 '23 at 14:30

2 Answers2

1

A pull would be a fetch + merge.

Except the merge would be origin/master to master.
Or origin/branch to branch.

In your case, the nodegit merge function call should then be:

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

As Jamie Counsell adds in the comments:

I ran into that error too.

It ends up it was because I was retrieving the branch name using nodegit as well, which gave something like origin/refs/heads/master instead of just master.

Calling branch.shorthand() got me "master"

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Getting '{ Error: no reference found for shorthand 'branch-development-modular-alpha-2.0.0' errno: -3, errorFunction: 'Reference.dwim' }' :-/ – Chris F. Mar 28 '18 at 13:32
  • @ChrisF. Is that branch one already present on the remote side? – VonC Mar 28 '18 at 13:54
  • The branch is present on the remote, but on local even though I checkedOut the branch I can only see "master". Am I supposed to create the branch locally first? – Chris F. Mar 28 '18 at 14:10
  • @ChrisF. locally, can you do in command line `git branch -avv`? – VonC Mar 28 '18 at 14:33
  • I am getting this: * (HEAD detached at origin/branch-development-modular-alpha-2.0.0) master remotes/origin/branch-development-modular-alpha-2.0.0 remotes/origin/master – Chris F. Mar 28 '18 at 17:46
  • You need to create a local branch first. Checking out a remote branch will detach `HEAD`. See [here](https://stackoverflow.com/a/11262780/749719). – rcjsuen Mar 29 '18 at 22:54
  • @rcjsuen Here, 'branch' is a local branch (that must indeed be checked out). I merge a remote branch in it. – VonC Mar 30 '18 at 04:32
  • @VonC Sorry, I was trying to explain to Chris why his `HEAD` was in a detached state. Your answer/code looks fine, yes. – rcjsuen Mar 30 '18 at 21:32
  • @ChrisF. I ran into that error too. It ends up it was because I was retrieving the branch name using nodegit as well, which gave something like `origin/refs/heads/master` instead of just `master`. Calling `branch.shorthand()` got me `"master"`. – Jamie Counsell Jan 08 '19 at 22:20
  • @JamieCounsell Thank you. I have included your comment in the answer for more visibility. – VonC Jan 08 '19 at 22:22
0

Below is the pull example in nodegit repo:

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!");
  });
Marinos An
  • 9,481
  • 6
  • 63
  • 96