Nodegit: Needed pull example or help with code

Created on 12 Jan 2015  路  8Comments  路  Source: nodegit/nodegit

Hi

Firstly, thanks for good work.

Is there a chance to provide by you PULL example (git fetch followed by git merge FETCH_HEAD) or can you help me how should I change below code to achieve "git merge FETCH_HEAD"?

I used your fetch example and it worked, my repo is in this stage:

"Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
nothing to commit, working directory clean"

I won't have any changes in repository in future and don't have now, so I'm looking for method that won't create commit and won't force me to push 'merge commit'.
I was trying to modify your merge-cleanly example, but it creates commit, so it's not perfect for me (BTW don't know if this correct, because source tree shows history strange, maybe something with date of commit 1973-11-29)

    var repo;
    var localReference, ourCommit;
    var remoteReference, theirCommit;
    var ourSignature = nodegit.Signature.create("user test", "[email protected]", 123456789, 60);

    Repository.open("master")
        .then(function(repository) {
            repo = repository;
            return repo.getReference('refs/remotes/origin/master');
        }).then(function(reference) {
            console.log(reference.target());
            remoteReference = reference;
            return repo.getReference('refs/heads/master');
        }).then(function(reference) {
            console.log(reference.target());
            localReference = reference;
            return repo.getCommit(localReference.target());
        }).then(function(commit) {
            ourCommit = commit;
            return repo.getCommit(remoteReference.target());
        }).then(function(commit) {
            theirCommit = commit;
            return Merge.commits(repo, ourCommit, theirCommit);
        }).then(function(index) {
            if (!index.hasConflicts()) {
                index.write();
                return index.writeTreeTo(repo);
            }
        }).then(function(oid) {
            return repo.createCommit('refs/heads/master', ourSignature, ourSignature, 'we merged their commit', oid, [ourCommit, theirCommit]);
        })
        .done(function(commitId) {
            console.log('New Commit: ', commitId);
            console.log("It worked!");
        },
        function() {
            console.log("It failed :( !");
        });

Thanks

Most helpful comment

So we currently don't have a method for a traditional CLI pull function. We do however have a convenience method for merging which will do a fast-forward if possible. You could then do something like

repo.fetchAll({
  credentials: function(url, userName) {
    return NodeGit.Cred.sshKeyFromAgent(userName);
  }
}).then(function() {
  repo.mergeBranches("master", "origin/master");
});

That should do what you're looking for. There are a few other things that's off with your code but we're doing support on our Gitter channel which you can swing by and we'll help you some more if you want.

I'm going to keep this open however until we get a pull example in.

All 8 comments

So we currently don't have a method for a traditional CLI pull function. We do however have a convenience method for merging which will do a fast-forward if possible. You could then do something like

repo.fetchAll({
  credentials: function(url, userName) {
    return NodeGit.Cred.sshKeyFromAgent(userName);
  }
}).then(function() {
  repo.mergeBranches("master", "origin/master");
});

That should do what you're looking for. There are a few other things that's off with your code but we're doing support on our Gitter channel which you can swing by and we'll help you some more if you want.

I'm going to keep this open however until we get a pull example in.

Made a commit for adding an example for pulling with a merge.

Thanks!

I am unable to get the example to work. I have a repo cloned and want to fast-forward / pull on it and it does not appear to fetch, and when I fetch manually with git fetch --all, this code with mergeBranches still does not fast forward. Any ideas? Let me know if there's more information I could provide / a way I could improve debugging this issue. Thanks in advance.

Are you getting any errors or console output?

No errors, that's what's strange. I ended up just deleting the directory and re-cloning each time.

My project and code can be seen here: https://github.com/Glavin001/GitLab-Pages/blob/master/routes/webhooks.js#L88-L117
I may be using something incorrectly.

Looking at the code it might be related to the re-use of objects. We currently don't deep clone the objects passed into the Clone.clone method and the internal C library takes ownership of those objects internally. It's been mentioned before that it's causing weird behaviors so we'll fix that in a future version.

For right now I'd try to make new objects for each call and see if that fixes the problem. Also, attaching a .done() to the end of the promise chain should give you some sort of way to see if an error happened in the chain since it's just rejecting the promise and then failing silently.

For more help come over to our gitter channel.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fatso83 picture fatso83  路  7Comments

codeimpossible picture codeimpossible  路  4Comments

DinkDonk picture DinkDonk  路  4Comments

getify picture getify  路  6Comments

Sergeeeek picture Sergeeeek  路  9Comments