Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current Git commit version from within Rails app?

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.

like image 553
Carson Cole Avatar asked Dec 06 '11 21:12

Carson Cole


2 Answers

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
like image 57
Patrick Reiner Avatar answered Sep 21 '22 07:09

Patrick Reiner


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.

like image 29
meagar Avatar answered Sep 23 '22 07:09

meagar