Search code examples
perforcelines-of-code

Lines of code you have written


Out of curiosity, is there any way to get the number of lines of code you have written (in a specific project)?

I tried perforce with p4 describe #CLN | wc -l, but apart from so many edge cases (comments being included, new lines being added etc.), it skips the newly added files as well. Edge cases can be ignored, if we try to display physical line of code but newly added files still cause the issue.


Solution

  • The other answers seem to have missed the source-control history side of things.

    From http://forums.perforce.com/index.php?/topic/359-how-many-lines-of-code-have-i-written/

    Calculate the answer in multiple steps:

    1) Added files:

    p4 filelog ... | grep ' add on .* by <username>'
    p4 print -q foo#1 | wc -l
    

    2) Changed files:

    p4 describe <changelist> | grep "^>" | wc -l
    

    Combine all the counts together (scripting...), and you'll have a total.

    You might also want to get rid of whitespace lines, or lines without alphanumeric chars, with a grep?

    Also if you are doing it regularly, it would be more efficient to code the thing in P4Python and do it incrementally - keeping history and looking at only new commits.