Giter Site home page Giter Site logo

git-rev-sync-js's Introduction

git-rev-sync Build Status

Synchronously get the current git commit hash, tag, count, branch or commit message. Forked from git-rev. Supports Node 4+.

Example

var git = require('git-rev-sync');

console.log(git.short());
// 75bf4ee

console.log(git.long());
// 75bf4eea9aa1a7fd6505d0d0aa43105feafa92ef

console.log(git.branch());
// master

You can also run these examples via: npm run examples

Install

npm install git-rev-sync --save

API

var git = require('git-rev-sync');

git.short([filePath], [length]) → <String>

return the result of git rev-parse --short HEAD

  • optional filePath parameter can be used to run the command against a repo outside the current working directory
  • optional length parameter can be used to set the desired hash length (defaults to 7)

git.long([filePath]) → <String>

return the result of git rev-parse HEAD; optional filePath parameter can be used to run the command against a repo outside the current working directory

git.branch([filePath]) → <String>

return the current branch; optional filePath parameter can be used to run the command against a repo outside the current working directory

git.count() → <Number>

return the count of commits across all branches; this method will fail if the git command is not found in PATH

git.date() → <Date>

returns the date of the current commit; this method will fail if the git command is not found in PATH

git.hasUnstagedChanges() → <Boolean>

returns true if there are unstaged changes; this method will fail if the git command is not found in PATH

git.isDirty() → <Boolean>

returns true if there are uncommitted changes; this method will fail if the git command is not found in PATH

git.isTagDirty() → <Boolean>

returns true if the current tag is dirty; this method will fail if the git command is not found in PATH

git.message() → <String>

return the current commit message; this method will fail if the git command is not found in PATH

git.remoteUrl() → <String>

return the current remote URL; this method will fail if the git command is not found in PATH

git.tag([markDirty]) → <String>

return the current tag and mark as dirty if markDirty is truthful; this method will fail if the git command is not found in PATH

License

MIT

Donations

We're all in this together

git-rev-sync-js's People

Contributors

adam-lynch avatar adamreisnz avatar bxt avatar caseywebb avatar defunctzombie avatar ezolenko2 avatar henrikra avatar jantimon avatar junosuarez avatar kurttheviking avatar laysent avatar lorenzbiotex avatar marcoceppi avatar mikecousins avatar nickstanish avatar tblobaum avatar tiberiuzuld avatar timothyjones avatar wheeler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

git-rev-sync-js's Issues

[feature request] add .date() of last commit

Please add a method to get the date of the current git version (date of last commit).
Similar to the output of:

git log --no-color -n 1 --pretty=format:"%ad" --date=format:"%FT%TZ"

Would be great.
;-)

Thanx. WOK.

Function to get all the info

Would be great to have a function that could return all the information in an object in one go, instead of having to call each of them separately

Use in empty git repository

When used in an empty git repository I get:

Module build failed: Error: ENOENT: no such file or directory, open '/home/daurnimator/myproject/.git/packed-refs'
    at Object.fs.openSync (fs.js:663:18)
    at Object.fs.readFileSync (fs.js:568:33)
    at Object.long (/home/daurnimator/myproject/node_modules/git-rev-sync/index.js:109:31)

Discrepancy in length of short hash

Command line output of git rev-parse --short HEAD:

7b554a75

Output of require('git-rev-sync').short():

7b554a7

Can you please make .short() length consistent with what git itself is reporting?

This way I cannot reconcile Sentry releases because a shorter hash is reported in my app than in the command line script that I use to create releases.

Add --first-parent to tag()

Hi @kurttheviking ,

Please add the option --first-parent to gitRevSync.tag() describe command.
Option details is here

This option will return the tag which originated on the current branch, currently if I make a hotfix in the master and merge backwards in develop the hotfix tag will be returned by gitRevSync.tag() instead of the dev tag.

Example:
Develop: 1.3.0-dev
Master: 1.2.1

After merge from master to develop. On develop branch the tag will be 1.2.1

long() throws an exception when running in a worktree on windows

When running on windows and within a git worktree, running long() throws the following error

Error: ENOENT: no such file or directory, open 'E:\projects\chartiq\finsemble\finsemble-mono\.git\worktrees\my-test-app\packed-refs'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at Object.long (E:\projects\chartiq\finsemble\finsemble-mono-citrix\packages\api\node_modules\git-rev-sync\index.js:108:31)

This is due to assuming a forward slash directory separator to ignore the worktree directory. I've simply replaced double-backslashes with forward slashes in an incoming PR.

Error when using git.short and git.branch

TypeError: Cannot read properties of undefined (reading 'filename')

Node version v19.0.0

This could be an error relating to module system. I'm using ESM imports.

Not a git repository

I get this error when running my project:

E:\Work\instachain\node_modules\git-rev-sync\index.js:20
      throw new Error('[git-rev-sync] failed to execute command: ' + result.stde
rr);
      ^

Error: [git-rev-sync] failed to execute command: fatal: Not a git repository (or
 any of the parent directories): .git

    at _command (E:\Work\instachain\node_modules\git-rev-sync\index.js:20:13)
    at Object.tag (E:\Work\instachain\node_modules\git-rev-sync\index.js:130:10)

    at Object.<anonymous> (E:/Work/instachain/webpack/dev.config.js:53:49)
    at Module._compile (module.js:570:32)
    at loader (E:\Work\instachain\node_modules\babel-register\lib\node.js:144:5)

    at Object.require.extensions.(anonymous function) [as .js] (E:\Work\instacha
in\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)

I am using the latest version of git-rev-sync

Also my project directory is a git repository.

I have tried to solve this issue by changing versions of this library but still can't manage to get it to work.

Thanks for any support!!!

Use "git rev-parse --short HEAD" to get short hash

Hi, thanks for this module. I've noticed you fiddle with files for getting the long hash, then get a substring of it to return the short version. This is not always correct because even if unlikely, it may happen that 2 different hashes have the same starting 7 characters.
If you check the documentation for "--short" you'll see that it always returns an unique value instead (if it is not, it will make the returned hash substring longer).
Please stick with the git commands, it is safer! ;)

Shelljs deletes OLDPWD env variable which creates error with webpack

Hi, we use your library in our webpack configuration file and we saw that the library shelljs which you use deletes the env variable OLDPWD which creates the error

ERROR in EnvironmentPlugin - OLDPWD environment variable is undefined.

You can pass an object with default values to suppress this warning.
See https://webpack.js.org/plugins/environment-plugin for example.

I also created an issue for shelljs.

Strange behavior of isDirty

I have some trouble with the isDirty function.

In one of my git projects I got always true returned. But the git status returns no changes.
I executed the git commands behind isDirty manually and got the same results:
git diff-index c7f53b57c5b8ea9a2e3c837a09bbef6dc668b561 . :100644 100644 e0de748c18601e6860e667b97c12b9de3212dfb2 0000000000000000000000000000000000000000 M recovery/downloadmanager/public/resources/version.json :100644 100644 a4bc9e8488bc52856ad6836643d4f06010ded752 0000000000000000000000000000000000000000 M recovery/downloadmanager/src/resources/password.json :100644 100644 e0de748c18601e6860e667b97c12b9de3212dfb2 0000000000000000000000000000000000000000 M recovery/downloadmanager/src/resources/version.json

but my git status show always:
developer@ubuntu:/data/ngc/recovery/downloadmanager$ git status On branch features/secure-image-file Your branch is up-to-date with 'origin/features/secure-image-file'. nothing to commit, working directory clean

Is there any idea to fix my issue?

"[git-rev-sync] no git repository found" when working with `git --work-tree`

A common pattern in production environments is to put a bare git repository (see repo/) outside the working directories (see releases/) so you can deploy with minimal disruption of the live release:

current -> symlink to releases/3
repo/
releases/1/
releases/2/
releases/3/
releases/3/package.json
releases/3/node_modules
releases/3/config/environment.js

Which deploys with a command like:

git --work-tree=/path/to/releases/3/ --git-dir=/path/to/repo/ checkout $branch -f

Running git.short() in the environment.js I keep getting this error:

[git-rev-sync] no git repository found

I tried:

git.short('/absolute/path/to/repo/');
git.short('../repo/');
git.short('../../repo/');
...

Whatever I try, can't seem to grab the git repository.

Any ideas?

No way to get "dirty" status?

something like the results of

git describe --always --dirty

could be useful. Maybe have .tag append "dirty" on the end?

How to specify short hash length example?

Hi,

I am trying to use the specifiy short hash length of 8 to match git. But I get "Cannot read property 'join' of null" errors.

That using stuff like:
git.short(8)
git.short(null, 8)

I don't want to specify a different repo, just the exactly repo im in

Any Ideas?

Doesn't work with git submodules

When you you have 2 repos: one is parent repo and one is submodule of parent repo this package doesn't work. It is because plugin always searches for .git folder. Working on submodule it doesn't work because submodule has .git FILE. This file tells where the actualy git folder is located for submodule.

If you want to demo this your self: do 2 repos. Make other submodule as parent module. Then try to use your package in submodule's folder. :) Tell me if you need more details

getting last tag

Hi there, There is a function in this lib that returns the current tag, but is there a way to get the latest tag on current branch?

git describe --abbrev=0

How to get tag output like: 3.5.3-18-dirty ?

I need my version number to be compiled into my app. I want it to reflect the latest tag, the number of commits since that tag, and whether the working copy is dirty.

This is how it is typically done:

git describe --tags --always --dirty
3.5.3-8-geb51d63-dirty

(I don't really care if the hash is there or not, so it could also be 3.5.3-8-dirty. But I need to know if the app was built off the actual commit that the tag is pointing to, or a later commit, which the -8 tells me.).

But when I run:

node
Welcome to Node.js v14.17.3.
Type ".help" for more information.
> var git = require('git-rev-sync');
> console.log(git.tag(true));
3.5.3
undefined

This plugin is only returning the tag name without the expected suffix.
I think the issue may be with the --abbrev=0. The git documentation says: "An of 0 will suppress long format, only showing the closest tag."

git version 2.38.1.windows.1

Failure running under windows when there isn't an entry in .git/refs/heads for this branch

When running under Windows and there isn't an entry in .git/refs/heads for this branch, invoking git-rev-sync produces the following error:

    ref = packfileRegex.exec(packfileContents)[1];
                                              ^
TypeError: Cannot read property '1' of null
    at long (C:\TEST\node_modules\git-rev-sync\index.js:95:47)

Steps to reproduce

  1. Create a new directory (e.g. C:\TEST as above), run git init.

  2. Add an .npignore with the following contents:

    /node_modules/
    
  3. Add an index.js with the following contents:

    var grs = require('git-rev-sync');
    console.log(grs.short());
  4. Add the previous files and commit the initial commit.

  5. Run npm install git-rev-sync.

  6. Now make a few branches: git branch aaa, git branch bbb.

    • Running node index.js at this point should work.
  7. Run git pack-refs --all (this deletes the refs from .git/refs/heads and places them in the .git/packed-refs).

  8. Run node index.js to observe the failure.

Proposed solution

Tracking the error, it seems to be caused by this line:

var refToFind = path.join('refs', 'heads', b);

In Windows, path.join() will produce the string refs\\heads\\BRANCH - i.e. using backslashes!

Proposed solution: simply use Array.prototype.join as:

var refToFind = ['refs', 'heads', b].join('/');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.