3

I can't figure out a nice, succinct title for this question, so bear with me!

My overall goal is to have a variable in my source code which give the date, time, (and possibly commit id) of a class so that when exceptions get caught, I could output the error along with that said variable so I can easily track the source code version giving me the grief.

I'd like to accomplish one of two things. When I edit a java file such that mercurial or git picks up a change, I'd like for the class which got modified to get a new field:

public static final String commitTime = "yyyymmdd - hhmmss - <commitid>";

Or if inner class is too much, something appended to the bottom of the file such as:

public lass failname_stamp {
  public static final String commitTime = "yyyymmdd - hhmmss - <commitid>";
}

Or something to that effect.

This doesn't have to be specific to mercurial or git, however it would be a feature that ties in with them. So when a commit happens on a file, a process will run to add such data to the file itself.

I imagine such a tool would need to integrate with my IDE (eclipse and intelliJ) if not a 3rd party plugin for my mercurial or git instance.

I know I have seen source code files with comment fields at the top such as:

 /**
 * @since  1.2
 */

But I'd like something more thorough and specific and not relying on a human to manually update comments.

E.S.
  • 2,733
  • 6
  • 36
  • 71
  • I tried Googling for similar situations and found http://stackoverflow.com/questions/6611171/how-to-insert-version-numbers-in-our-java-jars-that-a-user-can-access, http://stackoverflow.com/questions/690419/build-and-version-numbering-for-java-projects-ant-cvs-hudson. Both mention using properties files, instead of modifying the source. I don't think these links do exactly what you want, but maybe they'd give you ideas about alternative approaches. – ajb Aug 09 '16 at 01:03
  • Why would you need that? Don't you know the overall version of your running code? Can't you then find the version of a particular source file that was included in that overall version? If not, then you have bigger problems. – Andreas Aug 09 '16 at 01:10
  • @Andreas, what if I am not the only person using my code... – E.S. Aug 09 '16 at 18:29
  • @ajb, Thanks for the links and ideas! – E.S. Aug 09 '16 at 18:29

1 Answers1

3

The source itself cannot know the version being checked-in, unless you manually add some kind of version string - but that's both tedious and failure-prone.

However, I believe that you do not want this in the source code, but you want to generate this information at build time (as e.g. also different build environments might lead to different results) where you might generate a property file shipped with the object code.

I don't speak Java, but I solved that problem for a python project of mine:

  1. When it is run from the repository, the code version is queried and used, e.g. for bug / crash reports Querying hg for the checked-out version
  2. When run outside a repository, the programme looks for a generated version file. I do ship my code bundles with such pre-generated version file; it's automatically generated and bundled with the otherwise un-touched code in the build script used on my compile farm. Checking for presence of pre-generated version info, if code executed outside repository

I strongly suggest to employ a similar mechanism with your code: It's both, flexible and easy to use. Query for repository properties and fallback to use of a property file which you ship with your code bundles (and report 'unknown version' if neither is present)

planetmaker
  • 5,884
  • 3
  • 28
  • 37