Skip to content

Missing example: commit to a local repo (i.e. git add, git commit) #96

Closed

Description

@dcolens

It would be great if there was an example of how to commit the changes made to a file at path X in the workdir. Something like: https://github.com/libgit2/libgit2sharp/wiki/Git-commit.

Activity

  1. nkallen commented on Sep 28, 2013

    @nkallen
    Contributor

    I haven't tested any index-related functions, so I'm not sure if this is supported yet. If you have time I'd love for you to help -- I can walk you through how to add this functionality yourself, it's (hopefully) pretty easy.

    First, if you can research which raw C libgit2 functions would need to be available to do this functionality. Next, find each of these in the v0.18.json file in root directory of nodegit. If you can copy and paste those here, I'll show you how to make those available to javascript-land.

  2. dcolens commented on Sep 30, 2013

    @dcolens
    ContributorAuthor

    ok so I played more with it and I can add and commit a file like so: (assuming repo is the opened repo, file is the filename to be committed, Q and QFS are q.js and q-io.js)

    var msg = file + ' updated by John Doe';
    var author = git.Signature.now("John Doe", 'jdoe@example.com');
    
    
    return QFS.write(QFS.join(repo.workdir(), file), content)
    .then(function() {
        return Q.ninvoke(repo, 'openIndex')
    })
    .then(function(index) {
        logger.debug('openIndex');
        return Q.ninvoke(index, 'read')
        .then(function() {
            logger.debug('read');
            return Q.ninvoke(index, 'addByPath', file);
        })
        .then(function() {
            logger.debug('addByPath');
            return Q.ninvoke(index, 'write');
        })
        .then(function() {
            logger.debug('write');
            return Q.ninvoke(index, 'writeTree');
        });
    })
    .then(function(oid) {
        logger.debug('writeTree', oid);
        return Q.nfcall(git.Reference.oidForName, repo, 'HEAD')
        .then(function(head) {
            logger.debug('oidForName', head);
            return Q.ninvoke(repo, 'getCommit', head);
        })
        .then(function(commit) {
            logger.debug('getCommit', commit);
            return Q.ninvoke(repo, 'createCommit', 'HEAD', author, author, msg, oid, [commit]);
        });
    })
    .then(function(newCommit) {
        var sha = newCommit.sha();
        logger.info(msg + ' (commit '+ sha + ')');
        return sha;
    });
  3. nkallen commented on Oct 1, 2013

    @nkallen
    Contributor

    Hey,

    Special request. Can you turn this into an example to put in the examples directory? Could you remove the dependency on Q for the sake of the example -- just add in the usual callback junk. Call it "add-and-commit.js" or something?

  4. dcolens commented on Oct 2, 2013

    @dcolens
    ContributorAuthor

    Will do that

    thanks for your support

    Did
    On 01 Oct 2013, at 19:32, Nick Kallen notifications@github.com wrote:

    Hey,

    Special request. Can you turn this into an example to put in the examples directory? Could you remove the dependency on Q for the sake of the example -- just add in the usual callback junk. Call it "add-and-commit.js" or something?

    —
    Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions