Using git hook to add a license version and comment application at the top of source files

I am trying to create a pre-commit hook for git that edits comments at the top of each of my .h and .m files. I would like to modify the headline comments added by x-code to include the application version and license information. This would be useful when upgrading to a new version.

This is the text that is automatically inserted when I create the file in x-code:

// // myFile.h // myApp // // Created by Developer on 11/13/12. // Copyright (c) 2012 myCompany LLC. All rights reserved. // 

I would like the hook to change it to this:

 /* myFile.h myApp Version: 1.0 myApp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. myApp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with myApp. If not, see <http://www.gnu.org/licenses/> Copyright 2012 myCompany. All rights reserved. This notice may not be removed from this file. */ 

I thought that I would have a text file with the header text that I want to change. Or determine when the version number of the application changes. When this file is updated with the new version number and git, I commit git, then it will update all other files with the new version and change any new files to the correct header text. It seems like this might be helpful.

+6
source share
3 answers

I did this before using the equivalent perl inplace-edit '-i', but using a perl script, not just the command line, because the data is bigger.

I did the following that should suit your needs, given some setup and testing: D.

The new source header is defined in the __DATA__ section for the perl script.

Here is an illustration of how you can pass VERSION and COPY_YEAR through environment variables.

While I made some serious pearls, but it works on my test sites, it creates backups, although I would suggest you back up the files first.

I hope this helps in some way.

Usage example:

 $ perl add_header *.h *.m 

Script:

 use strict; my $extension = '.orig'; local $/ = undef; # slurp my $oldargv = undef; my $replacement = <DATA>; $replacement =~ s/{VERSION}/$ENV{VERSION} or '1.0.alpha'/e; $replacement =~ s/{COPY_YEAR}/$ENV{COPY_YEAR} or '2013'/e; LINE: while (<>) { if ( $ARGV ne $oldargv) { my $backup = undef; if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/; } rename($ARGV, $backup); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); my $oldargv = $ARGV; } s!^//.*Copy.*?//$!$replacement!sm; # THE BUSINESS END. } continue { print; } select(STDOUT); __DATA__ /** * My New Header... * Version info {VERSION} ]} * made {COPY_YEAR} */ 
0
source

Use smudge / clean filters (full chapter related to "Keyword Extension" most carefully)?

+2
source

I once did something similar, but took the version number from the annotated tag that we created when the release version was released. Build process:

  • Verify the code with the specified tag
  • Generate the version number from the tag (it has the form "foo_version_1_2" → 1.2) and write it to a file. It was so that the template could pull it out for display in the application
  • Build
  • Pack it

You can simply run the script to populate the version number, wherever you are at build time. I remember looking at dirty / clean filters, but that seemed like a bad approach.

The above would be less useful if you are trying to fill out a specific file, but then I would question the meaning of this since you are not using separate files in git. Why are you trying to do this?

0
source

All Articles