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 likegrep
, but instead of searching in the filesystem, it searches in the repository. It's faster thangrep -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 likegrep -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.
Written by Janos Gyerik
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Git
Authors
khasinski
591.1K
dmichaelavila
500.4K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#