Introduction to Git Rev Sync
Git Rev Sync is a powerful Node.js library that allows developers to access information about the current Git repository, such as the latest commit hash, branch name, and repository status. This library is particularly useful for automated build scripts, version tracking, and deployment processes.
Key Features and APIs of Git Rev Sync
Below are some of the key APIs provided by Git Rev Sync along with their explanations and code snippets to help you integrate them into your applications.
Get the Latest Commit Hash
const git = require('git-rev-sync');
const commitHash = git.long();
console.log('Latest Commit Hash:', commitHash);
Get the Short Commit Hash
const git = require('git-rev-sync');
const shortCommit = git.short();
console.log('Short Commit Hash:', shortCommit);
Get the Current Branch Name
const git = require('git-rev-sync');
const branch = git.branch();
console.log('Current Branch:', branch);
Check if the Repository is Dirty
const git = require('git-rev-sync');
const isDirty = git.isDirty();
console.log('Is Repository Dirty:', isDirty);
Get the Latest Commit Date
const git = require('git-rev-sync');
const commitDate = git.date();
console.log('Latest Commit Date:', commitDate);
Get the Current Repository Tag
const git = require('git-rev-sync');
const tag = git.tag();
console.log('Current Tag:', tag);
Example Application Using Git Rev Sync APIs
Let’s build a simple application that displays Git repository information on a web dashboard.
Step 1: Install Dependencies
npm install git-rev-sync express
Step 2: Create the Application
const express = require('express');
const git = require('git-rev-sync');
const app = express();
app.get('/', (req, res) => {
res.send(`
Git Repository Information Dashboard
Latest Commit Hash: ${git.long()}
Short Commit Hash: ${git.short()}
Current Branch: ${git.branch()}
Is Repository Dirty: ${git.isDirty()}
Latest Commit Date: ${git.date()}
Current Tag: ${git.tag()}
`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Running the Application
To run the application, use the following command:
node app.js
Your Git repository information will now be accessible at http://localhost:3000
.