Installations
npm install @jsg2021/git-state
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=4.0.0
Node Version
16.17.0
NPM Version
8.19.3
Score
72.6
Supply Chain
99.4
Quality
75.3
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
jsg2021
Download Statistics
Total Downloads
2,814
Last Day
1
Last Week
12
Last Month
109
Last Year
1,009
GitHub Statistics
MIT License
1 Stars
91 Commits
1 Watchers
2 Branches
1 Contributors
Updated on Mar 04, 2023
Bundle Size
5.03 kB
Minified
1.29 kB
Minified + Gzipped
Package Meta Information
Latest Version
5.1.0
Package Id
@jsg2021/git-state@5.1.0
Unpacked Size
30.00 kB
Size
5.88 kB
File Count
8
NPM Version
8.19.3
Node Version
16.17.0
Total Downloads
Cumulative downloads
Total Downloads
2,814
Last Day
0%
1
Compared to previous day
Last Week
-70.7%
12
Compared to previous week
Last Month
186.8%
109
Compared to previous month
Last Year
-28.6%
1,009
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@jsg2021/git-state
Forked from https://github.com/NextThought/git-state
Get the current state of any git repository.
Installation
1npm install @jsg2021/git-state
Usage
1var git = require('@nti/git-state') 2 3var path = '/path/to/git/repo' 4 5git.isGit(path, function (exists) { 6 if (!exists) return 7 8 git.check(path, function (err, result) { 9 if (err) throw err 10 console.log(result) // => { branch: 'master', 11 // ahead: 0, 12 // dirty: 9, 13 // untracked: 1, 14 // stashes: 0 } 15 }) 16})
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 branchremoteBranch
- The remote tracking branch of the currently checked out branchahead
- The amount of commits the current branch is ahead of the remote (may beNaN
if there for instance is no remote)behind
- The amount of commits the current branch is behind of the remote (may beNaN
if there for instance is no remote)dirty
- The number of dirty filesuntracked
- The number of untracked filesstashes
- 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 1bin/ahead
- exit code will be 0 if result is0
, otherwise 1bin/branch
- exit code will be 0 if result ismaster
, otherwise 1bin/dirty
- exit code will be 0 if result is0
, otherwise 1bin/untracked
- exit code will be 0 if result is0
, otherwise 1bin/stashes
- exit code will be 0 if result is0
, otherwise 1bin/commit
- exit code will be 0 if a commit can be found, otherwise 1
License

No vulnerabilities found.

No security vulnerabilities found.