37

Is there a way to obtain the number of changed lines of code over a certain time period in a mercurial repository? Something along the lines of what statsvn does would be great, but anything counting the number of changed lines of code within 6 months will do (including a clever combination of arguments to hg log).

Braiam
  • 1
  • 11
  • 47
  • 78
  • 1
    http://code.google.com/p/gource/ will do it and look pretty in the process. :-) – Ken Jun 16 '10 at 20:22

3 Answers3

27

The hg churn extension is what you want.

You can get visual results with hg activity or hg chart.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
18

Edit: hg diff and hg log both support a --stat option that can do this for you, only better and quicker.


I made an alias called lines to count changed lines (not necessarily lines of code) for me. Try putting this alias in your .hgrc file:

[alias]
lines = !echo `hg log -pr $@ | grep "^+" | wc -l` Additions; echo `hg log -pr $@ | grep "^-" | wc -l` Deletions; 

Then pass it the revision first, followed by any optional arguments:

hg lines tip or hg lines 123:456 -u brian

Sometimes you want to know the number of lines changed excluding whitespace-only changes. This requires using diff -w underneath instead of log -p. I set up a linesw alias for this:

#ignore whitespace
linesw = ![[ $1 =~ : ]] && r=$1 || r="$1~1:$1"; echo `hg diff -wr $r | grep "^+\([^+]\|$\)" | wc -l` Additions; echo `hg diff -wr $r | grep "^-\([^-]\|$\)" | wc -l` Deletions; 

hg linesw tip or hg lines 123:456

Note they behave slightly differently because diff and log behave differently -- for example, log will take a --user parameter while diff will not, and when passing a range, log will show changes commited in the first revision given in the range, while diff will not.

This has only been tested using bash.

undefined
  • 6,208
  • 3
  • 49
  • 59
  • Don't use the hg log command. This also includes all changes in the linear repo hostory between the two commit IDs, including commits that were not committed in the current branch. – Paul Mar 06 '18 at 10:20
11

I needed to do this, and spent quite a bit of time with the hg churn extension and similar solutions.

In the end, I found that what worked best for me was CLOC (Count Lines of Code): http://cloc.sourceforge.net/

You can give it two folders containing two versions of a project, and it will count all of the lines that are the same, modified, added, removed. It recognises multiple languages and itemises code, comments and blank lines.

To use it, I pulled out the two versions of my code from Hg into two parallel folders, and then used cloc --diff --ignore-whitespace

Irish Buffer
  • 852
  • 1
  • 8
  • 11
  • 4
    That's a nice solution to the "problem" as you just download one Perl file. Note the syntax is: `./cloc.pl --ignore-whitespace --ignore-case --diff ` – Pascal Nov 05 '11 at 15:36
  • its not a fast as git diff --shortstat, but it does a much better job of finding duplicate code that has been copied. – cmcginty Jan 17 '13 at 00:04