Evert
Evert

Reputation: 99687

Looking for a way to calculate the number of lines that have NOT changed between two commits

I'm working on a project that's under an old copyright, and I'm trying to make a good case to drop the copyright, because the project has significantly changed over the years.

(Don't worry, not stealing anything, I'm gathering a case to get the copyright holder to agree with me).

So what would really help, is an accurate statistic of what hasn't changed between two versions of a project.

I'm using git, and I'm having trouble finding a solution..

Upvotes: 2

Views: 71

Answers (1)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40021

I found a way but it isn't native git. It uses standard diff command and bash extensions. Anyway

This if for one file, you might need to script it to do it per file. It doesn't handle file renames and it relies on the command diff to sort out differences.

To get the original line count of file originalfile. In my example the original file is just two commits away but any refspec will work, perhaps the exact hash is the best.

git show HEAD^^:originalfile | wc

Then get the number of lines still not changed with this magic line

diff --unchanged-group-format='%<' --old-group-format='' --new-group-format='' --changed-group-format='' <(git show HEAD^^:original_file) <(git show HEAD:original_file) | wc

I'll explain it. First we set the format of unchanged-group-format to be just the actual line (%<) and all other groups (old, new and changed) to be empty (not even a new line).

Then we use the bash extension <(command) to pipe first the original version and then the current version of the file to diff. It's possible to compare two checkouts as well. It's piped to wc to count lines.

And the number of changed lines is just a subtraction away from what we already got.

Upvotes: 1

Related Questions