Search code examples
node.jsyeomannodegit

Yeoman using NodeGit's Reset, getting constant object error


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

Solution

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