Last Updated: February 25, 2016
·
963
· janosgyerik

Git tip: find the files with the most comments

Do you have a junky project with lots of commented out code? Git can help you find the files with the biggest damage:

​git grep -c '^\s*//' | sort -n -t: -k2 -r | head

That is:

  • git grep is exactly like grep, but instead of searching in the filesystem, it searches in the repository. It's faster than grep -r .
  • The pattern we're looking for is lines that start with some (or no) whitespace followed by //. In other words, lines that contain only comment, nothing else
  • The output of git grep -c is like grep -c, a list of filenames, with the count of matches separated by a :
  • To sort the output by the counts, use -n to sort numerically, with : as the column separator, and the second column as the key
  • The sort is ascending by default, so reverse it and get the head only = first 10 records, descending order by count of matches

The solution is not perfect. For example block comments like /* .... */ are not included. But it's better than nothing, and can help finding and cleaning up obsolete code in your projects. Or you can find other inventive uses for git grep.

1 Response
Add your response

Or for specifically commented-out code (as opposed to "deliberate" comments) in Lisp:

git grep -c '^\s*;.*(' | sort -n -t: -k2 -r | head

over 1 year ago ·