Mailing List Archive

McCabe complexity for just changed files in a commit?
Hi folks.

Is there a way of getting the McCabe Complexity of just the functions and
methods (in Python) changed in a git commit?

I found radon, and it looks good. But I think it wants to do entire files,
no?

Thanks!
--
https://mail.python.org/mailman/listinfo/python-list
Re: McCabe complexity for just changed files in a commit? [ In reply to ]
On Sun, Oct 3, 2021 at 3:07 PM Dan Stromberg <drsalists@gmail.com> wrote:
>
> Hi folks.
>
> Is there a way of getting the McCabe Complexity of just the functions and
> methods (in Python) changed in a git commit?
>
> I found radon, and it looks good. But I think it wants to do entire files,
> no?

No idea about the complexity calculation, but perhaps the easiest way
is to ask git to list all files that changed in a particular commit,
then look at those files before and after, and compare the
complexities?

git diff-tree 1af743
1af743f411aa2b7278d1f1b3c30b447555ea55b8
:100644 100644 b5908b5a36a4749e0bef2a16a2733a7461a818dc
e00ba8738dbf3421288d31c60de9ee42a085c148 M auto-volume.py

The first line is the commit hash, and then each subsequent line names
a file that changed. The two hashes given are the old version and the
new version. Then you can:

git cat-file -p b5908b5a36a4749e0bef2a16a2733a7461a818dc

and

git cat-file -p e00ba8738dbf3421288d31c60de9ee42a085c148

to see the files - hopefully you can pipe that into radon and get the
complexities, and calculate the increase or decrease from that.

If the file was newly created in that commit, you'll see an initial
hash of all zeroes, and instead of "M" before the file name, it'll be
"A". Similarly, if a file gets deleted, there'll be "D" and the new
hash will be all zeroes.

Would that work?

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: McCabe complexity for just changed files in a commit? [ In reply to ]
On Sun, Oct 3, 2021 at 3:18 PM Chris Angelico <rosuav@gmail.com> wrote:
> git diff-tree 1af743
> 1af743f411aa2b7278d1f1b3c30b447555ea55b8
> :100644 100644 b5908b5a36a4749e0bef2a16a2733a7461a818dc
> e00ba8738dbf3421288d31c60de9ee42a085c148 M auto-volume.py
>
> The first line is the commit hash, and then each subsequent line names
> a file that changed. The two hashes given are the old version and the
> new version.

Note that these are very long lines, so they don't come out too well
in this post, but everything from ":100644" to "auto-volume.py" is a
single line of text.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: McCabe complexity for just changed files in a commit? [ In reply to ]
On Sat, Oct 02 2021 at 09:05:47 PM, Dan Stromberg <drsalists@gmail.com> wrote:
> Hi folks.
>
> Is there a way of getting the McCabe Complexity of just the functions and
> methods (in Python) changed in a git commit?
>
> I found radon, and it looks good. But I think it wants to do entire files,
> no?
>

Calculate your metric for this commit, calculate metric for parent
commit, and take the difference? That's how we do coverage metrics to
report stuff like "change increases/decreases coverage by N%".

--
regards,
kushal
--
https://mail.python.org/mailman/listinfo/python-list
Re: McCabe complexity for just changed files in a commit? [ In reply to ]
On Tue, Oct 5, 2021 at 3:44 PM Kushal Kumaran <kushal@locationd.net> wrote:
>
> On Sat, Oct 02 2021 at 09:05:47 PM, Dan Stromberg <drsalists@gmail.com> wrote:
> > Hi folks.
> >
> > Is there a way of getting the McCabe Complexity of just the functions and
> > methods (in Python) changed in a git commit?
> >
> > I found radon, and it looks good. But I think it wants to do entire files,
> > no?
> >
>
> Calculate your metric for this commit, calculate metric for parent
> commit, and take the difference? That's how we do coverage metrics to
> report stuff like "change increases/decreases coverage by N%".
>

What if it has multiple parents, like a merge commit that resolves conflicts?

(A merge that doesn't make any other changes won't affect coverage or
complexity, since it's all done by the commits on either side.)

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: McCabe complexity for just changed files in a commit? [ In reply to ]
On Tue, Oct 05 2021 at 03:55:22 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Oct 5, 2021 at 3:44 PM Kushal Kumaran <kushal@locationd.net> wrote:
>>
>> On Sat, Oct 02 2021 at 09:05:47 PM, Dan Stromberg <drsalists@gmail.com> wrote:
>> > Hi folks.
>> >
>> > Is there a way of getting the McCabe Complexity of just the functions and
>> > methods (in Python) changed in a git commit?
>> >
>> > I found radon, and it looks good. But I think it wants to do entire files,
>> > no?
>> >
>>
>> Calculate your metric for this commit, calculate metric for parent
>> commit, and take the difference? That's how we do coverage metrics to
>> report stuff like "change increases/decreases coverage by N%".
>>
>
> What if it has multiple parents, like a merge commit that resolves conflicts?
>
> (A merge that doesn't make any other changes won't affect coverage or
> complexity, since it's all done by the commits on either side.)
>

In my usecases, merges are asymmetric: one of the parents is useful for
reporting metric changes, and the others are not. The parent of
interest will be whichever parent was on the mainline branch (first
parent). Whether the merge commit resolved conflicts or not makes no
difference here.

In practice, this does not involve doing commit math. The point where
this kind of thing is done is in context of a pull request, where it is
clearer what commits you're interested in comparing. When merging
branch X into branch Y, the tools only need to look at the heads of
those branches. https://github.com/vuejs/vue/pull/9373 is a random
example of the kind of thing I'm thinking of.

--
regards,
kushal
--
https://mail.python.org/mailman/listinfo/python-list