pankajpatel / git-state

Get the current state of any git repository

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

git-state

Get the current state of any git repository.

Build status js-standard-style

Installation

npm install git-state

Usage

var git = require('git-state')

var path = '/path/to/git/repo'

git.isGit(path, function (exists) {
  if (!exists) return

  git.check(path, function (err, result) {
    if (err) throw err
    console.log(result) // => { branch: 'master',
                        //      ahead: 0,
                        //      dirty: 9,
                        //      untracked: 1,
                        //      issues: true }
  })
})

API

isGit(path, callback)

Calls the callback with a boolean which is either true or false depending on if the given path contains a git repository.

isGitSync(path)

Synchronous version of isGit() which returns either true or false depending on if the given path contains a git repository.

check(path, callback)

Will check the state of the git repository at the given path and call the callback. The callback will be called with two arguments: An optional error object and a result object.

The result object contains the following properties:

  • branch - The currently checked out branch
  • ahead - The amount of commits the current branch is ahead of the remote (may be NaN if there for instance is no remote)
  • dirty - The number of dirty files
  • untracked - The number of untracked files
  • issues - A generic boolean which is true if the repository is in a non-clean state (e.g. it's dirty, contains untracked files, is ahead of its remote or is currently not on master)

untracked(path, callback)

Get the amount of untracked files in the git repository at the given path.

The callback will be called with two arguments: An optional error object and a number representing the amount of untracked files.

dirty(path, callback)

Get the amount of dirty files in the git repository at the given path.

The callback will be called with two arguments: An optional error object and a number representing the amount of dirty files.

branch(path, callback)

Get the currently checked out branch in the git repository at the given path.

The callback will be called with two arguments: An optional error object and a string with the branch name.

If the branch name cannot be found, a falsy value will be returned.

ahead(path, callback)

Get the amount of commits the current branch in the git repository at the given path is ahead of its remote.

The callback will be called with two arguments: An optional error object and a number indicating the amount of commits the branch is ahead of its remote.

If the number cannot be determined, NaN will be returned instead.

commit(path, callback)

Get the short-hash (e.g. 7b0a3ab) for the latest commit at HEAD in the git repository at the given path.

The callback will be called with two arguments: An optional error object and a string containing the short-hash.

Shell implementation

Inside the bin folder is a set of shell scripts which will perform the same checks as the isGit() and check() functions, but just in pure bash. This allows you for instance to modify your command prompt without having to invoke node (which can be kind of slow if done at every request). In fact this is exactly what the git-ps1 module does for you.

  • bin/isgit - exit code will be 0 if cwd is inside a git repo, otherwise 1
  • bin/ahead - exit code will be 0 if result is 0, otherwise 1
  • bin/branch - exit code will be 0 if result is master, otherwise 1
  • bin/dirty - exit code will be 0 if result is 0, otherwise 1
  • bin/untracked - exit code will be 0 if result is 0, otherwise 1
  • bin/commit - exit code will be 0 if a commit can be found, otherwise 1
  • bin/issues - will combine all of the above into one script which will exit with exit code 0 if all pass or 1 if one of them fail

License

MIT

About

Get the current state of any git repository

License:MIT License


Languages

Language:JavaScript 100.0%