changeset 46430:b84c3d43ff2e

churn: count lines that look like diff headers but are not Previously, churn cannot count added lines that start with "++ " or removed lines that start with "-- ". Differential Revision: https://phab.mercurial-scm.org/D9929
author Aay Jay Chan <aayjaychan@itopia.com.hk>
date Sat, 30 Jan 2021 18:30:11 +0800
parents e61c2dc6e1c2
children 645ee7225fab
files hgext/churn.py tests/test-churn.t
diffstat 2 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/churn.py	Mon Jan 25 12:31:40 2021 +0100
+++ b/hgext/churn.py	Sat Jan 30 18:30:11 2021 +0800
@@ -38,11 +38,16 @@
 def changedlines(ui, repo, ctx1, ctx2, fmatch):
     added, removed = 0, 0
     diff = b''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch))
+    inhunk = False
     for l in diff.split(b'\n'):
-        if l.startswith(b"+") and not l.startswith(b"+++ "):
+        if inhunk and l.startswith(b"+"):
             added += 1
-        elif l.startswith(b"-") and not l.startswith(b"--- "):
+        elif inhunk and l.startswith(b"-"):
             removed += 1
+        elif l.startswith(b"@"):
+            inhunk = True
+        elif l.startswith(b"d"):
+            inhunk = False
     return (added, removed)
 
 
--- a/tests/test-churn.t	Mon Jan 25 12:31:40 2021 +0100
+++ b/tests/test-churn.t	Sat Jan 30 18:30:11 2021 +0800
@@ -195,3 +195,22 @@
   alltogether     11 *********************************************************
 
   $ cd ..
+
+count lines that look like headings but are not
+
+  $ hg init not-headers
+  $ cd not-headers
+  $ cat > a <<EOF
+  > diff
+  > @@ -195,3 +195,21 @@
+  > -- a/tests/test-churn.t
+  > ++ b/tests/test-churn.t
+  > EOF
+  $ hg ci -Am adda -u user1
+  adding a
+  $ hg churn --diffstat
+  user1           +4/-0 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  $ hg rm a
+  $ hg ci -Am removea -u user1
+  $ hg churn --diffstat
+  user1           +4/-4 +++++++++++++++++++++++++++---------------------------