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

[PATCH] add coccinelle script to check the option usage strings

From
Abhradeep Chakraborty via GitGitGadget <gitgitgadget@gmail.com>
Date
Apr 15, 2022, 12:03 UTC
Message-ID
<pull.1216.git.1650024209568.gitgitgadget@gmail.com>
From: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>

There is no check to see if usage strings for option flags are following the style guide or not. Style convention says, usage strings should not start with capital letter (unless needed) and it should not end with `.`.

Add a coccinelle script to check the option strings against the style convention.

Signed-off-by: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>
---
    add a coccinelle script to check the option usage strings
    
    Fixes #636 [1]
    
    There was a previous patch request
    [https://lore.kernel.org/git/pull.1147.git.1645030949730.gitgitgadget@gmail.com/]
    where I implemented it in different methods (first version was written
    in bash and the second version was to add some checks in the
    parse-options.c file). But dscho [https://github.com/dscho] was
    confident to use Coccinelle here. So, here is the Coccinelle version.
    
    Previous discussion link -
    https://lore.kernel.org/git/nycvar.QRO.7.76.6.2203071709540.11118@tvgsbejvaqbjf.bet/
    
    [1] https://github.com/gitgitgadget/git/issues/636
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1216%2FAbhra303%2Fadd_cocci_check-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1216/Abhra303/add_cocci_check-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1216
 contrib/coccinelle/usage_strings.cocci | 53 ++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 contrib/coccinelle/usage_strings.cocci
diff --git a/contrib/coccinelle/usage_strings.cocci b/contrib/coccinelle/usage_strings.cocci
new file mode 100644
index 00000000000..3fa34001a6b
--- /dev/null
+++ b/contrib/coccinelle/usage_strings.cocci
@@ -0,0 +1,53 @@
+@ usage_strings @
+identifier opts;
+constant opt_flag != OPT_GROUP;
+char[] e;
+@@
+
+
+
+struct option opts[] = {
+    ...,
+    opt_flag(...,<+... \(N_(e)\|e\) ...+>, ...),
+    ...,};
+
+
+@script:python string_checker depends on usage_strings@
+e << usage_strings.e;
+replacement;
+@@
+
+length = len(e)
+should_make_change = False
+if length > 2:
+    if e[length-2] == '.' and e[length-3] != '.':
+        coccinelle.replacement = e[:length-2] + '"'
+        should_make_change = True
+    else:
+        coccinelle.replacement = e
+    if e[1].isupper():
+        if not e[2].isupper():
+            coccinelle.replacement = coccinelle.replacement[0] + coccinelle.replacement[1].lower() + coccinelle.replacement[2:]
+            should_make_change = True
+if not should_make_change:
+    cocci.include_match(False)
+
+@ depends on string_checker@
+identifier usage_strings.opts;
+constant usage_strings.opt_flag;
+char[] usage_strings.e;
+identifier string_checker.replacement;
+@@
+
+struct option opts[] = {
+    ...,
+    opt_flag(...,
+    \(
+-    N_(e)
++    N_(replacement)
+    \|
+-    e
++    replacement
+    \)
+    , ...),
+    ...,};

base-commit: 4027e30c5395c9c1aeea85e99f51ac62f5148145
-- 
gitgitgadget
Next: Abhradeep Chakraborty
Message 1 of 2 in “add coccinelle script to check the option usage strings”
  1. add coccinelle script to check the option usage stringsAbhradeep Chakraborty via GitGitGadget, Apr 15, 2022
  2. Abhradeep ChakrabortyApr 20, 2022

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.