From 9039e0d242fa4d65e3fd7921459ef5b735b74aa7 Mon Sep 17 00:00:00 2001
From: Mathieu Courtois <mathieu.courtois@edf.fr>
Date: Wed, 18 Dec 2024 20:51:18 +0100
Subject: [PATCH] [#34305] add check-prepush option in ~/.gitconfig

---
 lib/aslint/config.py           |  2 ++
 lib/hgaster/hooks/codeaster.py | 61 ++++++++++++++++++++++------------
 lib/hgaster/hooks/generic.py   |  2 +-
 3 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/lib/aslint/config.py b/lib/aslint/config.py
index da7aca6..81d2c64 100644
--- a/lib/aslint/config.py
+++ b/lib/aslint/config.py
@@ -95,6 +95,7 @@ class AsterCfgSection(object):  # pragma pylint: disable=R0902
         "check_commit",
         "check_submit",
         "check_precommit",
+        "check_prepush",
         "docaster_uri",
         "docaster_cafile",
         "clang_format",
@@ -132,6 +133,7 @@ class AsterCfgSection(object):  # pragma pylint: disable=R0902
         self.check_commit = _read("check-commit", "warn")
         self.check_submit = _read("check-submit", "warn")
         self.check_precommit = _read("check-precommit", "undef")
+        self.check_prepush = _read("check-prepush", "undef")
         # installation root of devtools
         self.aslint_root = get_absolute_dirname(__file__)
         self.devtools_root = get_absolute_dirname(osp.join(self.aslint_root, os.pardir))
diff --git a/lib/hgaster/hooks/codeaster.py b/lib/hgaster/hooks/codeaster.py
index 22a7e47..98203e1 100644
--- a/lib/hgaster/hooks/codeaster.py
+++ b/lib/hgaster/hooks/codeaster.py
@@ -54,28 +54,9 @@ def aslint(repopath, paths=(), amend=False):
     revs = ("HEAD~1",) if amend else None
 
     # logger.title("running aslint...")
-    choice = ASCFG.get("check.precommit")
-    if choice == "no":
-        logger.warn(
-            "pre-commit checkings skipped because of 'check-precommit'"
-            " value in your ~/.gitconfig file"
-        )
+    if _check_preference("pre-commit") == OK:
         return OK
-    elif choice == "undef":
-        logger.info(
-            "pre-commit: If you don't plan to submit your work "
-            "you can disable these checkings by adding "
-            "'check-precommit = no' in the [aster] section in "
-            "your ~/.gitconfig file."
-        )
-        logger.info(
-            "pre-commit: To hide this message you can set the value " "'check-precommit = yes'."
-        )
-    elif choice != "yes":
-        logger.warn(
-            "unknown choice {0!r} for 'check.precommit'"
-            " (see ~/.gitconfig) must be 'yes' or 'no'.".format(choice)
-        )
+
     dchg = get_changed_files(repopath, revs=revs, files=paths)
     changes = filter_dict(dchg)
     report = Report()
@@ -141,6 +122,8 @@ def run_ctest_minimal(repopath):
     checkcontext.reponame = get_repo_name(repopath)
     if checkcontext.reponame != "src":
         return OK
+    if _check_preference("pre-push") == OK:
+        return OK
 
     logger.warning("Run a minimal list of testcases (for about 30 seconds).")
 
@@ -166,3 +149,39 @@ def run_ctest_minimal(repopath):
     if call(cmd):
         return NOOK
     return OK
+
+
+def _check_preference(hook):
+    """Check if a checking is enabled or disabled in '~/.gitconfig'.
+
+    Arguments:
+        hook (str): Hook name.
+
+    Returns:
+        bool: OK if the hook should be skipped, NOOK otherwise.
+    """
+    from aslint.config import ASCFG
+
+    assert hook in ("pre-commit", "pre-push"), hook
+    option = f"check.{hook.replace('-', '')}"
+    gitopt = option.replace(".", "-")
+
+    choice = ASCFG.get(option)
+    if choice == "no":
+        logger.warn(
+            "%s checkings skipped because of '%s'" " value in your ~/.gitconfig file", hook, gitopt
+        )
+        return OK
+    elif choice in (None, "undef"):
+        logger.info(
+            "%s: If you want to disable these checkings, just add "
+            "'%s = no' in the [aster] section in your ~/.gitconfig file.",
+            hook,
+            gitopt,
+        )
+        logger.info("%s: To hide this message you can set the value '%s = yes'.", hook, gitopt)
+    elif choice != "yes":
+        logger.warn(
+            "unknown choice %r for '%s' (see ~/.gitconfig) must be 'yes' or 'no'.", choice, option
+        )
+    return NOOK
diff --git a/lib/hgaster/hooks/generic.py b/lib/hgaster/hooks/generic.py
index 96f7feb..99397b2 100644
--- a/lib/hgaster/hooks/generic.py
+++ b/lib/hgaster/hooks/generic.py
@@ -14,7 +14,7 @@ from aslint.config import ASCFG
 from aslint.baseutils import force_list
 from aslint.logger import logger
 
-from ..ext_utils import RE_ISSUE, get_issues_from_descr, is_admin
+from ..ext_utils import RE_ISSUE, get_issues_from_descr
 
 OK, NOOK = 0, 1
 
-- 
GitLab