What is git-rev-sync?
The git-rev-sync npm package provides synchronous access to Git repository information, such as the current branch, commit hash, and other metadata. It is useful for embedding Git information into your application, such as for versioning or debugging purposes.
What are git-rev-sync's main functionalities?
Get the current branch name
This feature allows you to retrieve the name of the current branch in the Git repository.
const git = require('git-rev-sync');
const branch = git.branch();
console.log(branch);
Get the current commit hash
This feature allows you to get the full hash of the current commit.
const git = require('git-rev-sync');
const commit = git.long();
console.log(commit);
Get the short commit hash
This feature provides the short version of the current commit hash.
const git = require('git-rev-sync');
const shortCommit = git.short();
console.log(shortCommit);
Get the commit message
This feature retrieves the commit message of the current commit.
const git = require('git-rev-sync');
const message = git.message();
console.log(message);
Get the commit date
This feature allows you to get the date of the current commit.
const git = require('git-rev-sync');
const date = git.date();
console.log(date);
Other packages similar to git-rev-sync
git-revision-webpack-plugin
The git-revision-webpack-plugin package provides similar functionality but is specifically designed to work with Webpack. It allows you to embed Git revision information into your Webpack build, which can be useful for versioning and cache busting.
git-rev
The git-rev package offers similar functionalities to git-rev-sync but operates asynchronously. It provides methods to get the current branch, commit hash, and other Git metadata, but uses callbacks or promises instead of synchronous calls.
simple-git
The simple-git package is a more comprehensive Git library for Node.js that provides both synchronous and asynchronous methods to interact with a Git repository. It offers a wider range of Git operations compared to git-rev-sync, making it more versatile for complex Git interactions.
git-rev-sync
Synchronously get the current git commit hash, tag or branch
Example
var git = require('git-rev-sync');
console.log(git.short());
console.log(git.long());
console.log(git.branch());
console.log(git.tag());
console.log(git.log());
You can also run these examples via: npm run examples
Install
npm install git-rev-sync --save
Methods
var git = require('git-rev-sync');
.log() => <Array: <Tuple>>
return the git log of process.cwd()
as an array; each array contains the long commit hash, commit message, fuzzy commit time, and user
.short() => <String>
return the result of git rev-parse --short HEAD
.long() => <String>
return the result of git rev-parse HEAD
.tag() => <String>
return the current tag
.branch() => <String>
return the current branch
Warning
Not tested outside of a *nix system. See the execSync module notes on this topic.
License
MIT