1

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).

13
  • 1
    See Git keyword substitution like those in Subversion?, this should basically provide what you are looking for.
    – Doc Brown
    Nov 7 '19 at 13:20
  • 2
    Why do you assume this is a common problem? I don't claim long experience in SW development. But I never ever used that kind of information. Also, why do you believe running programs right from git repository is common?
    – Euphoric
    Nov 7 '19 at 13:31
  • 1
    @Euphoric Yes, it's a useful thing to have. It means that a "Help->About" dialog can tell you the exact version of the software. Even the venerable SCCS from 1972 could to it.
    – Simon B
    Nov 7 '19 at 13:40
  • 6
    Again. Why do you believe the program will run from git repository? Last time I checked, 99.99% of all software runs outside it's repository. That means that the information has to be put there during compilation. And compiler can put that there by simply calling git status.
    – Euphoric
    Nov 7 '19 at 13:43
  • 3
    To me, a commit hash and the version of the software are two different things. You might tag commit 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.
    – neilsimp1
    Nov 7 '19 at 13:58
2

I think that it is not the job of the SCM to maintain version numbers of executables. In my experience with Java, it is Maven or Gradle that does the build, tags files in SCM and produces the executable artifact for a particular software version. The built executable is typically deployed to a container like Nexus. However this does not mean it is easy to access the version number in the build tool but that is a different question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.