2

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!

Zoso
  • 3,273
  • 1
  • 16
  • 27

2 Answers2

1

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

Hoall
  • 184
  • 16
  • Thanks. That gets me one step ahead. Now on to the sed/awk part. – Zoso Jun 26 '17 at 14:59
  • Thanks for your link but no it doesn't help much. That link too deals with files having a standard format for replacing text. I need something which modifies text conditionally (the conditions that I have listed). – Zoso Jun 26 '17 at 17:41
  • Well depending on the number of files you'll be faster with a template doing it manually. Maybe with a bin in the for loop. Good luck – Hoall Jun 26 '17 at 17:44
  • Well I am actually using your answer and doing it manually: (Since I had already committed so) git reset HEAD^ for i in `git diff --name-only `; do vim $i; done git commit --reuse-message=HEAD@{1} – Zoso Jun 26 '17 at 18:02
0

I once merged a script that does it on the gem5 project:

That setup uses git filter-repo via its Python interface. It:

  • goes over each commit in a range
  • modifies the copyright for each one only on modified files using a simple Python regular expression, updating the copyright header year to the current year
  • amends each commit separately.

I should actually have done it as a separately installable CLI thing rather than put it into gem5, would have helped a bunch of people. Maybe I will some day.

Related:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985