npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsg2021/git-state

v5.1.0

Published

Get the current state of any git repository

Downloads

86

Readme

@jsg2021/git-state

Forked from https://github.com/NextThought/git-state

Get the current state of any git repository.

Installation

npm install @jsg2021/git-state

Usage

var git = require('@nti/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,
                        //      stashes: 0 }
  })
})

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[, options], 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
  • remoteBranch - The remote tracking branch of 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)
  • behind - The amount of commits the current branch is behind 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
  • stashes - The number of stored stashes

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

checkSync(path[, options])

Synchronous version of check().

Can throw error. This typically happens if you are running a too old version of Node.js (< 0.12), if git isn't found or if the path isn't a git repository.

untracked(path[, options], 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.

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

untrackedSync(path[, options])

Synchronous version of untracked().

Can throw error. This typically happens if you are running a too old version of Node.js (< 0.12), if git isn't found or if the path isn't a git repository.

dirty(path[, options], 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.

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

dirtySync(path[, options])

Synchronous version of dirty().

Can throw error. This typically happens if you are running a too old version of Node.js (< 0.12), if git isn't found or if the path isn't a git repository.

branch(path[, options], 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.

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

branchSync(path[, options])

Synchronous version of branch(). Returns null if no branch is set.

remoteBranch(path[, options], callback)

Get the remote for 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 remote name.

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

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

remoteBranchSync(path[, options])

Synchronous version of remoteBranch(). Returns null if no branch is set.

ahead(path[, options], 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.

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

aheadSync(path[, options])

Synchronous version of ahead().

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

behind(path[, options], callback)

Get the amount of commits the current branch in the git repository at the given path is behind 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 behind of its remote.

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

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

behindSync(path[, options])

Synchronous version of behind().

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

commit(path[, options], 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.

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

commitSync(path[, options])

Synchronous version of commit().

Can throw error. This typically happens if you are running a too old version of Node.js (< 0.12), if git isn't found or if the path isn't a git repository.

stashes(path[, options], callback)

Get the amount of stashed changes 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 stashed changes.

Supports the following options:

  • maxBuffer - largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (default: 200*1024)

stashesSync(path[, options])

Synchronous version of stashes().

Can throw error. This typically happens if you are running a too old version of Node.js (< 0.12), if git isn't found or if the path isn't a git repository.

message(path[, options], callback)

Get the commit message of the latest commit.

The callback will be called with two arguments: An optional error object and a string containing the commit message.

messageSync(path[, options])

Synchronous version of message().

Can throw error. This typically happens if you are running a too old version of Node.js (< 0.12), if git isn't found or if the path isn't a git repository.

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/stashes - 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

License

MIT