Update copyright headers across changed files in a project

Issue

I work on a project where the copyright headers are mandatory. All files have them but recently the guidelines for the headers changed and hence need to be updated. I am working on Linux platform (but files are accessible through Windows as well). Some files have the new headers (but need to be updated in the year field), some have the old header, some have it in a very crude manner not conforming to the standard (yes, they unfortunately got through review) and some don’t have it at all (yes, these too got through review).

Hence I had three requirements:

  1. Change the old headers to the updated headers, along with updated years

  2. For those which have new headers, their date(more precisely year) needs to be updated.

  3. Add headers to those that don’t have any

I don’t want to do this for all files in the project, only ones that I am changing for my fix. In other words, only files shown in output of git status. The possible scenarios while updating the date can be:

  1. 2011-2014, 2016 ==> 2012-2014,2016-2017
  2. 2011-2012, 2014-2015 ==> 2011-2012, 2014-2015, 2017
  3. 2011-2016 ==> 2012-2017
  4. 2011-2017 ==> No change

Also it’s .java, .cpp, .c, .h and .xml files in the project.

I saw this but it only appears to add headers. I am not too proficient in sed and awk. I went through this but it assumes a standard start of all files and this but it too involves a common header. Is it possible to do this ?

EDIT: I accepted @Hoall ‘ s answer and did it manually since doing it using sed seemed too troublesome for now!

Solution

Under linux you could do somthing like this:

for i in `git diff --name-only --cached`; do echo $i; done

Replace the echo command with an sed, awk or so to replace the complete header within the file.

EDIT:

For the sed part see sed: replace a block of text

Answered By – Hoall

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published