Get hash of most recent git commit in Node

后端 未结 7 1790
礼貌的吻别
礼貌的吻别 2020-12-24 01:57

I\'d like to get the id/hash of the most recent commit on the current branch in NodeJS.

In NodeJS, I\'d like to get the most recent id/hash, with respect to git and

7条回答
  •  鱼传尺愫
    2020-12-24 02:25

    Using nodegit, with path_to_repo defined as a string containing the path to the repo you want to get the commit sha for. If you want to use the directory your process is running from, then replace path_to_repo with process.cwd():

    var Git = require( 'nodegit' );
    
    Git.Repository.open( path_to_repo ).then( function( repository ) {
      return repository.getHeadCommit( );
    } ).then( function ( commit ) {
      return commit.sha();
    } ).then( function ( hash ) {
      // use `hash` here
    } );
    

提交回复
热议问题