Patchwork contrib: add check for use of admonitions and its validity

login
register
mail settings
Submitter Rishabh Madan
Date Aug. 5, 2017, 3:24 a.m.
Message ID <cc695f1458baf2c39ffd.1501903453@bunty>
Download mbox | patch
Permalink /patch/22692/
State Changes Requested
Headers show

Comments

Rishabh Madan - Aug. 5, 2017, 3:24 a.m.
# HG changeset patch
# User Rishabh Madan <rishabhmadan96@gmail.com>
# Date 1501903168 -19800
#      Sat Aug 05 08:49:28 2017 +0530
# Node ID cc695f1458baf2c39ffdec4a1256a93fd659d62d
# Parent  609606d217659e0a6c1cf6f907b6512be5340e57
contrib: add check for use of admonitions and its validity

While using releasenotes extension, we will be using admonitions in commit messages.
This check will look for an admonition within the message. If it exists, it will
verify if it is stated under default or custom admonition. The check fails if the
admonition is not present in any of them.
Yuya Nishihara - Aug. 5, 2017, 1:36 p.m.
On Sat, 05 Aug 2017 08:54:13 +0530, Rishabh Madan wrote:
> # HG changeset patch
> # User Rishabh Madan <rishabhmadan96@gmail.com>
> # Date 1501903168 -19800
> #      Sat Aug 05 08:49:28 2017 +0530
> # Node ID cc695f1458baf2c39ffdec4a1256a93fd659d62d
> # Parent  609606d217659e0a6c1cf6f907b6512be5340e57
> contrib: add check for use of admonitions and its validity
> 
> While using releasenotes extension, we will be using admonitions in commit messages.
> This check will look for an admonition within the message. If it exists, it will
> verify if it is stated under default or custom admonition. The check fails if the
> admonition is not present in any of them.
> 
> diff -r 609606d21765 -r cc695f1458ba contrib/check-commit
> --- a/contrib/check-commit	Thu Jul 20 01:30:41 2017 -0700
> +++ b/contrib/check-commit	Sat Aug 05 08:49:28 2017 +0530
> @@ -21,9 +21,16 @@
>  import re
>  import sys
>  
> +from mercurial import (
> +    config,
> +    hg,
> +    ui as uimod,
> +)

check-commit seems to try not depending on mercurial modules. Maybe it's easier
to implement the check function as a command of the releasenotes extension?

>  if __name__ == "__main__":
>      exitcode = 0
>      node = os.environ.get("HG_NODE")
> -
>      if node:
>          commit = readcommit(node)
> -        exitcode = checkcommit(commit)
> +        exitcode = checkcommit(commit, node)
>      elif sys.argv[1:]:
>          for node in sys.argv[1:]:
>              exitcode |= checkcommit(readcommit(node), node)

test-check-commit.t feeds patches to stdin, in which case, no node value
is available.
Augie Fackler - Aug. 7, 2017, 6:21 p.m.
On Sat, Aug 05, 2017 at 10:36:46PM +0900, Yuya Nishihara wrote:
> On Sat, 05 Aug 2017 08:54:13 +0530, Rishabh Madan wrote:
> > # HG changeset patch
> > # User Rishabh Madan <rishabhmadan96@gmail.com>
> > # Date 1501903168 -19800
> > #      Sat Aug 05 08:49:28 2017 +0530
> > # Node ID cc695f1458baf2c39ffdec4a1256a93fd659d62d
> > # Parent  609606d217659e0a6c1cf6f907b6512be5340e57
> > contrib: add check for use of admonitions and its validity
> >
> > While using releasenotes extension, we will be using admonitions in commit messages.
> > This check will look for an admonition within the message. If it exists, it will
> > verify if it is stated under default or custom admonition. The check fails if the
> > admonition is not present in any of them.
> >
> > diff -r 609606d21765 -r cc695f1458ba contrib/check-commit
> > --- a/contrib/check-commit	Thu Jul 20 01:30:41 2017 -0700
> > +++ b/contrib/check-commit	Sat Aug 05 08:49:28 2017 +0530
> > @@ -21,9 +21,16 @@
> >  import re
> >  import sys
> >
> > +from mercurial import (
> > +    config,
> > +    hg,
> > +    ui as uimod,
> > +)
>
> check-commit seems to try not depending on mercurial modules. Maybe it's easier
> to implement the check function as a command of the releasenotes extension?

Good idea! I like that.

>
> >  if __name__ == "__main__":
> >      exitcode = 0
> >      node = os.environ.get("HG_NODE")
> > -
> >      if node:
> >          commit = readcommit(node)
> > -        exitcode = checkcommit(commit)
> > +        exitcode = checkcommit(commit, node)
> >      elif sys.argv[1:]:
> >          for node in sys.argv[1:]:
> >              exitcode |= checkcommit(readcommit(node), node)
>
> test-check-commit.t feeds patches to stdin, in which case, no node value
> is available.
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff -r 609606d21765 -r cc695f1458ba contrib/check-commit
--- a/contrib/check-commit	Thu Jul 20 01:30:41 2017 -0700
+++ b/contrib/check-commit	Sat Aug 05 08:49:28 2017 +0530
@@ -21,9 +21,16 @@ 
 import re
 import sys
 
+from mercurial import (
+    config,
+    hg,
+    ui as uimod,
+)
+
 commitheader = r"^(?:# [^\n]*\n)*"
 afterheader = commitheader + r"(?!#)"
 beforepatch = afterheader + r"(?!\n(?!@@))"
+admonitioncheck = r"\.\. ([a-zA-Z0-9_]+)::"
 
 errors = [
     (beforepatch + r".*[(]bc[)]", "(BC) needs to be uppercase"),
@@ -59,6 +66,7 @@ 
     exitcode = 0
     printed = node is None
     hits = []
+    exitcode = checkadmonition(commit, node)
     signtag = (afterheader +
           r'Added (tag [^ ]+|signature) for changeset [a-f0-9]{12}')
     if re.search(signtag, commit):
@@ -95,13 +103,38 @@ 
 def readcommit(node):
     return os.popen("hg export %s" % node).read()
 
+def getcustomadmonitions(node):
+    repo = hg.repository(uimod.ui(), '.')
+    ctx = repo[node]
+    p = config.config()
+
+    def read(f, sections=None, remap=None):
+        if f in ctx:
+            data = ctx[f].data()
+            p.parse(f, data, sections, remap, read)
+
+    if '.hgreleasenotes' in ctx:
+        read('.hgreleasenotes')
+    return p['sections'].keys()
+
+def checkadmonition(commit, node=None):
+    admonitions = ["fix", "feature", "bc", "api", "perf"]
+    x = re.search(admonitioncheck, commit)
+    if x:
+        if node:
+            admonitions += getcustomadmonitions(node)
+        if x.group(1) in admonitions:
+            return 0
+        else:
+            print("admonition: %s is invalid" % x.group(1))
+            return 1
+
 if __name__ == "__main__":
     exitcode = 0
     node = os.environ.get("HG_NODE")
-
     if node:
         commit = readcommit(node)
-        exitcode = checkcommit(commit)
+        exitcode = checkcommit(commit, node)
     elif sys.argv[1:]:
         for node in sys.argv[1:]:
             exitcode |= checkcommit(readcommit(node), node)