Node git info

Posted on Wednesday, May 13, 2015






I have an odd thing I would like to do in Node Express.  I want to display the most SHA of the latest commit.  And I also want to display the name of the current branch.








On the command line you can run the following to get the SHA of the latest commit.


   > git rev-parse HEAD





To get the name of the current branch run the following.


   > git rev-parse --abbrev-ref HEAD




How do you do this in Node?

I am sure there are several libraries out there that can help me out.

Poking around…


I found this node library git-repo-info https://github.com/rwjblue/git-repo-info [1]

Run the following command to install this module and save it to the package.json


> npm install -S git-repo-info





Here is my app.js




var express = require('express');
var config = require('config');
var gitInfo = require('git-repo-info')();

var app = express();
var server;

var start = exports.start = function start(port, callback) {
   
server = app.listen(port, callback);
};

var stop = exports.stop = function stop(callback) {
   
server.close(callback);
};

app.get('/git-info', function sendResponse(req, res) {
    res.
json(
        {
           
"commit-sha" : gitInfo.sha,
           
"branch" : gitInfo.branch
       
});
});



Now if I run it and open http://localhost:3000/git-info




Nice!  That worked pretty well.

Pretty simple easy node library to pull in and use.
 




 

References

[1]        node git-repo-info github page
                Accessed 5/2015


No comments:

Post a Comment