Getting current Git commit version from within Rails app?
Asked Answered
R

4

18

How can I retrieve the current Git commit version from within a Ruby on Rails app?

Want to display the Git version (or maybe the last 6 letters or so) to serve as an App version.

Reactivate answered 6/12, 2011 at 21:30 Comment(0)
F
16

You can invoke the git command from within your script:

commit = `git show --pretty=%H`
puts commit

Depending on your environment you may want to use the full path to the git binary, and possibly specify the GIT_DIR via an environment variable or --git-dir.

Forejudge answered 6/12, 2011 at 21:34 Comment(3)
Learned something new today, thanks! Didn't know you could use back-quotes for shell commands.Reactivate
I haven't used it so I can't recommend it from experience but you can use grit if you want something that's more OO.Sybyl
Aaron, considered grit, but that's a more involved solution than my simple use.Reactivate
G
22

Like @meagar said, use backticks to execute the shell command from within your app, but you may find these two commands more useful:

Full hash:

git rev-parse HEAD

First 7 characters of hash:

git rev-parse --short HEAD
Gin answered 24/8, 2013 at 14:8 Comment(2)
I used commit = `git rev-parse HEAD`.chomp to remove the trailing newlineIain
So, in Rails console: puts `git rev-parse HEAD`Kandicekandinsky
F
16

You can invoke the git command from within your script:

commit = `git show --pretty=%H`
puts commit

Depending on your environment you may want to use the full path to the git binary, and possibly specify the GIT_DIR via an environment variable or --git-dir.

Forejudge answered 6/12, 2011 at 21:34 Comment(3)
Learned something new today, thanks! Didn't know you could use back-quotes for shell commands.Reactivate
I haven't used it so I can't recommend it from experience but you can use grit if you want something that's more OO.Sybyl
Aaron, considered grit, but that's a more involved solution than my simple use.Reactivate
P
5

A more robust solution would be git show --pretty=%H -q. The -q flag quiets the output.

In order to remove the newline that is part of the output, you can use chomp. For example: system('git show --pretty=%H -q').chomp

The selected answer has the potential to actually return the diff when the commit is not a merge commit. Verified on git version 2.16.2.windows.1.

Phobos answered 3/10, 2018 at 20:28 Comment(1)
Amazing. I asked this question 7 yrs ago, and was just utilizing the answers again in a Heroku app, and you actually posted an answer at the same time. Thanks!Reactivate
I
1

I presume that you want to include the app version in your HTML somewhere? The prerequisite is that you are deploying your repo with Capistrano in the default manner (you are uploading the repo, not sending up an archive file).

You can add some code to the Rails initializer as outlined here. That approach will get the SHA1 from the last commit, and make it available as an environment variable.

The other way to do it is have you Capistrano task generate a static file in the public directory with the commit SHA in it. You could include other info in this file that seems useful.

Inunction answered 6/12, 2011 at 21:39 Comment(1)
With capistrano 3, simply read the file REVISION, in current folder.Doorstone

© 2022 - 2024 — McMap. All rights reserved.