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

From: "Abhradeep Chakraborty via GitGitGadget" <[email-masked]>
Date: Fri, 15 Apr 2022 12:03:29 +0000

From: Abhradeep Chakraborty <[email-masked]>

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 <[email-masked]>
---
    add a coccinelle script to check the option usage strings
    
    Fixes #636 [1]
    
    There was a previous patch request
    [https://lore.kernel.org/git/[email-masked]/]
    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/[email-masked]/
    
    [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