3

First time using NodeGit and having issues with the Reset function.

I'm trying to reset a folder that has been cloned to the current HEAD of origin/master.

Even though i'm giving it a target, it says it's still required:

Error: Object target is required.

Current code:

var Reset = nodegit.Reset;
var templateDir = this.templatePath('/folder');

nodegit.Repository.open(templateDir)
    .then(function(repo) {
        repository = repo;

        Reset.reset(repository, templateDir, Reset.TYPE.HARD, {
            remoteCallbacks: {
                credentials: function(url, userName) {
                    return nodegit.Cred.sshKeyNew(userName, sshPublicKey, sshPrivateKey, "");
                }
            }
        })
        .done(function(repo) {
            console.log("reset done");
        });
    });

templateDir is the full path to the folder using Yeoman's templatePath.

Wondering if anyone can give me insight into what i'm doing wrong or missing. I didn't see an example for this in their Example folder.

My expected end result would be equal to running this in terminal:

git reset --hard origin/master
Shane
  • 564
  • 2
  • 9

1 Answers1

1

You can check the test case that does a hard reset for an example.

The gist is that templateDir is the commit object that you want to reset to. You don't really need remoteCallbacks either unless you want to do a fetch or some sort of remote operation.

Try this:

var Reset = nodegit.Reset;
var templateDir = this.templatePath('/folder');
var repository = repo;

nodegit.Repository.open(templateDir)
.then(function(repo) {
    repository = repo;

    return repository.fetch('origin');
})
.then(function() {
    return repository.getBranchCommit('origin/HEAD');
})
.then(function(originHeadCommit) {
    return Reset.reset(repository, originHeadCommit, Reset.TYPE.HARD);
})
.done(function(repo) {
    console.log("reset done");
});
johnhaley81
  • 1,153
  • 10
  • 14
  • Thanks for the help. Now i'm getting a `Error: Reference 'refs/remotes/origin/HEAD' not found` when running the above. So getBranchCommit isn't finding the remote head, any advice? – Shane Mar 26 '15 at 19:34
  • I just added a fetch to the example. Give that a try. – johnhaley81 Mar 26 '15 at 20:32
  • I think we're getting close. Here is what i'm working with now: https://gist.github.com/7674b50839b4e11c23b3 - It's not throwing any errors now but it's also not reseting the files. – Shane Mar 26 '15 at 20:49
  • Also, if I change the getBranchCommit to `origin` it gives the same error as my first comment. – Shane Mar 26 '15 at 21:02
  • 1
    Sorry, just now getting back into this. I figured out my issue with the above running but not reseting the repo. I just had to change `getBranchCommit('origin');` to `getBranchCommit('origin/master');`. It was essentially looking at my local repo's head instead of the remote. My final code ended up like this https://gist.github.com/anonymous/4cca3669d82853ce79b1 – Shane Apr 03 '15 at 16:28