git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Graph sloc tool for git repos

From
Jeff King <peff@peff.net>
Date
Mar 14, 2016, 02:50 UTC
Message-ID
<20160314025052.GB19753@sigill.intra.peff.net>
In-Reply-To
<20160314023545.GA19753@sigill.intra.peff.net>
On Sun, Mar 13, 2016 at 10:35:45PM -0400, Jeff King wrote:
Show 10 quoted lines
> Like (this is back on the "we resolved as master" version of my example,
> to illustrate how the merge is shown):
> 
>   $ git log --first-parent -m --numstat --oneline
>   4244c8a resolved
>   1       1       file
>   b9bbaf9 side
>   1       0       file
>   09037ef base
>   1       0       file

You'd probably want --reverse, of course, since the point is to build up the count in the same order as time flows.

So this is the working version I came up with:
    git log --reverse --first-parent -m --format=%ct --numstat |
    perl -lne '
      if (/^\d+$/) {
              if (defined $time) {
                      print "$time $total"
              }
              $time = $&;
      } elsif (/^(\d+)\s+(\d+)\s/) {
              $total += $1 - $2;
      }
      END {
        # flush last entry
        print "$time $total";
      }
    '
For my git.git repo, the final line it produces is:
    1457666843 789457

which should be the final sloc-count right now. If I count the lines in the lines in HEAD, it's close but not quite the same:

   $ git ls-tree -r HEAD |
     awk '{print $3}' |
     xargs -n1 git cat-file blob |
     wc -l
   790437

I'd guess that the difference comes from a few files which are treated as binary (and thus get "-" in their numstat output, but happen to have newlines which cause "wc" to increment its count).

-Peff
Previous: Jeff King
Message 3 of 3 in “Graph sloc tool for git repos”
  1. Kai HendryMar 12, 2016
  2. Jeff KingMar 14, 2016
  3. Jeff KingMar 14, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.