Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

nodegit-kit

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodegit-kit

Promises for git commands like init, add, commit, status, diff

Source
npmnpm
Version
0.3.1
Version published
Weekly downloads
478
54.19%
Maintainers
1
Weekly downloads
 
Created
Source

NodeGit-Kit

Build Status Dependencies Dev Dependencies

Promises for git commands such as git init, git status, git add *, git diff, git log and git commit -am"commit message".

Comments are welcome at nodegit-kit/issues

Install

npm i --save nodegit-kit

Usage

var git = require('nodegit-kit');

git.open('../repo-path/new/or/existing')
.then(function(repo){

     // git diff
    return git.diff(repo)
    .then(function(diff){
        console.log(diff);

        // git commit -am"commit message"
        return git.commit(repo, {
            'message': 'commit message'
        });
    })
    .then(function(){
        // git log
        return git.log(repo);
    })
    .then(function(log){
        console.log(log);
    });
})
.catch(function(error){
    console.error(error);
});

API

open (path[, options])

Returns repository, if no repo is found, creates dir and initializes repository.

  • path String
  • options Object
    • init Boolean defaults to true
git.open('../repo-path/new/or/existing', {
    init: false
})
.then(function(repo){
});

init (path[, options])

Ensures directory exists, initializes, creates a first commit and returns repo.

  • path String
  • options Object
    • bare Number defaults to 0
    • commit Boolean defaults to true
    • message String defaults to 'initial commit'
git.init('../repo-path/new/or/existing', {
    base: 0,
    commit: true,
    message: 'my first commit'
})
.then(function(repo){
});

commit (repo[, options])

Checks if status has pending changes, commits, returns Oid else returns null.

  • repo NodeGit Repository instance
  • options
    • message String defaults to 'update'
git.open('../repo-path/new/or/existing')
.then(function(repo){
    // git commit -am"a new commit"
    return git.commit(repo, {
        message: 'a new commit'
    })
    .then(function(oid){
        console.log(oid);
    });
});

status (repo)

Returns an Array of changed files and their status.

git.open('../repo-path/new/or/existing')
.then(function(repo){
    // git status
    return git.status(repo)
    .then(function(status){
        console.log(status);
    });
});

log (repo)

Returns an Array of all commits.

git.open('../repo-path/new/or/existing')
.then(function(repo){
    // git log
    return git.log(repo)
    .then(function(log){
        console.log(log);
    });
});

diff (repo[, commit[, commit]])

Returns an Array of modified files and their diffs.

git.open('../repo-path/new/or/existing')
.then(function(repo){
    // git diff
    return git.diff(repo)
    .then(function(diff){
        console.log(diff);
    });
});

Get a diff of a commit

git.open('../repo-path/new/or/existing')
.then(function(repo){
    return git.log(repo)
    .then(function(history){
        return history[0].commit;
    })
    .then(function(commit){
        // git diff <commit>
        return git.diff(repo, commit);
    })
    .then(function(diff){
        console.log(diff);
    });
});

Get a diff between 2 commits

git.open('../repo-path/new/or/existing')
.then(function(repo){
    return git.log(repo)
    .then(function(history){
        var commit1 = history[0].commit;
        var commit2 = history[2].commit;
        // git diff <from> <to>
        return git.diff(repo, commit1, commit2);
    })
    .then(function(diff){
        console.log(diff);
    });
});

config

Allows to write/read global and local values. Local values are stored in the Git directory ./git/config and overrule global configurations. Git locks the config when changing configurations, therefore modifications can not be done in parallel, e.g. Promise.all multiple individual git.config.set calls will throw a failed to lock file for writing error.

See also 8.1 Customizing Git - Git Configuration (Git SCM Documentation)

Example set name and email for a specific repository

Setting user name and email similar to git config user.name "John Doe" and git config user.email johndoe@example.com.

git.open('my/repository')
.then(function(repo){
    return git.config.set(repo, {
        'user.name': 'John Doe',
        'user.email': 'johndoe@example.com'
    });
});

Example reading user.name and user.email

Similar to git config user.name returns config for a repository if there any or else the global Git configuration.

git.open('my/repository')
.then(function(repo){
    return git.config.get(repo, ['user.name', 'user.email']);
})
.then(function(configs){
    // [ 'John Doe', 'johndoe@example.com' ]
});

Example working with global Git configuration

When no repo is given setting and getting config will operate in --global mode and read and write to ~/.gitconfig (or ~/.config/git/config).

git.config.get(['user.name', 'user.email'])
.then(function(config){
    // [ 'John Doe', 'johndoe@example.com' ]
});
// warning this will change your global git config
git.config.set({
    'user.name': 'John Doe',
    'user.email': 'johndoe@example.com'
});

Test

npm install

npm test

# debug nodegit-kit
DEBUG=kit* npm test

# debug all
DEBUG=* npm test

Keywords

promise

FAQs

Package last updated on 04 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts