If I understand correctly there is no built-in functionality in Git to simply let the program output which version or which commit hash string it is when it is run. Why not? Would it not be a simple feature of git (or any other similar system) to generate a plain-text file in the repository with the latest commit hash string in a list, for the program to read when it runs so that I can query the running program for which version it is.
Currently I have added this functionality manually myself to several rather different projects in many different languages and it feels like I am repeating myself and that this feature should be part of git (or github/gitlab) itself.
What I would like is to solve the problem once and for all so that whenever I use git then it would be a simple task to make a running program tell which version it is.
I suppose that the easiest way to achieve it would be to let git generate a text file with the latest commit hashes and release versions for the repository. Would that do? Or is this problem already solved in some way that I am not aware of?
Solved (once and for all(?))
I think what I was looking for is described in this answer. I could make it work as described by adding the .gitattributes
file and then the $Id$
will work just like it did in CVS.
So what I did was adding the .gitattribute
and then I can do this in my program:
fn print_version() {
let version = format!(
"Version: {}",
"$Id: 3c044d47e723cd4e079e402dab29128b3631dbc6 $"
.replace("$Id: ", "")
.replace(" $", "")
);
print!("{}", &version);
}
The above id will be replaced at checkout and the function will print the version id (the commit id).
abc12345
as version 2.0, but I don't know that anybody besides developers cares about the commit hash over seeing version 2.0 in Help->About.