]> git.proxmox.com Git - mirror_edk2.git/commitdiff
.pytool/Plugin/UncrustifyCheck: Add ignore file support
authorMichael Kubacki <michael.kubacki@microsoft.com>
Mon, 21 Mar 2022 19:59:07 +0000 (15:59 -0400)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 4 Apr 2022 15:18:31 +0000 (15:18 +0000)
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3880

Currently UncrustifyCheck.py provides the following exclusion
options:

1. Override the type of files UncrustifyCheck operates against by
default (.c and .h files). Using the "IgnoreStandardPaths"
configuration option.

2. By default, UncrustifyCheck skips files in git submodules and
ignored by git (the "SkipGitExclusions" configuration option can
override this behavior).

The goal of UncrustifyCheck is to provide consistent formatting
across the codebase. In some rare circumstances, maintainers might
need to exclude a specific file (or file pattern) within their
package. For example, a small set of auto-generated files from
another repository.

This change adds a new configuration option that can be specified
in a package CI YAML file to describe a list of files within the
package that should be ignored by UncrustifyCheck.

The configuration option is called "IgnoreFiles" and it uses similar
syntax to git ignore to ignore a list of files.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Sean Brogan <sean.brogan@microsoft.com>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
.pytool/Plugin/UncrustifyCheck/Readme.md
.pytool/Plugin/UncrustifyCheck/UncrustifyCheck.py

index 0c46fd241a7a7127f4fd46319742dc68612b2c81..efe7a573e4fa965afd6d86ae4fa99c9751d8837f 100644 (file)
@@ -41,6 +41,7 @@ The plugin can be configured with a few optional configuration options.
       "AdditionalIncludePaths": [], # Additional paths to check formatting (wildcards supported).\r
       "AuditOnly": False,           # Don't fail the build if there are errors.  Just log them.\r
       "ConfigFilePath": "",         # Custom path to an Uncrustify config file.\r
+      "IgnoreFiles": [],            # A list of file patterns to ignore.\r
       "IgnoreStandardPaths": [],    # Standard Plugin defined paths that should be ignored.\r
       "OutputFileDiffs": True,      # Output chunks of formatting diffs in the test case log.\r
                                     # This can significantly slow down the plugin on very large packages.\r
@@ -67,6 +68,12 @@ the test as skipped. This allows visibility into the failures without breaking t
 \r
 When specified in the config file, this is a package relative path to the Uncrustify configuration file.\r
 \r
+### `IgnoreFiles`\r
+\r
+This option supports .gitignore file and folder matching strings including wildcards.\r
+\r
+The files specified by this configuration option will not be processed by Uncrustify.\r
+\r
 ### `IgnoreStandardPaths`\r
 \r
 This plugin by default will check the below standard paths. A package configuration file can specify any of these paths\r
index 6920580646de8b0f0633f8dc11214804a8dbe701..22cca0ff11019c7dcb20e614a61590fd3c9f5e17 100644 (file)
@@ -18,6 +18,7 @@ from edk2toolext.environment.plugin_manager import PluginManager
 from edk2toolext.environment.plugintypes.ci_build_plugin import ICiBuildPlugin\r
 from edk2toolext.environment.plugintypes.uefi_helper_plugin import HelperFunctions\r
 from edk2toolext.environment.var_dict import VarDict\r
+from edk2toollib.gitignore_parser import parse_gitignore_lines\r
 from edk2toollib.log.junit_report_format import JunitReportTestCase\r
 from edk2toollib.uefi.edk2.path_utilities import Edk2Path\r
 from edk2toollib.utility_functions import  RunCmd\r
@@ -273,6 +274,24 @@ class UncrustifyCheck(ICiBuildPlugin):
             f"-c {self._app_config_file} -F {self._app_input_file_path} --if-changed --suffix {UncrustifyCheck.FORMATTED_FILE_EXTENSION}", outstream=output)\r
         self._app_output = output.getvalue().strip().splitlines()\r
 \r
+    def _get_files_ignored_in_config(self):\r
+        """"\r
+        Returns a function that returns true if a given file string path is ignored in the plugin configuration file and false otherwise.\r
+        """\r
+        ignored_files = []\r
+        if "IgnoreFiles" in self._package_config:\r
+            ignored_files = self._package_config["IgnoreFiles"]\r
+\r
+        # Pass "Package configuration file" as the source file path since\r
+        # the actual configuration file name is unknown to this plugin and\r
+        # this provides a generic description of the file that provided\r
+        # the ignore file content.\r
+        #\r
+        # This information is only used for reporting (not used here) and\r
+        # the ignore lines are being passed directly as they are given to\r
+        # this plugin.\r
+        return parse_gitignore_lines(ignored_files, "Package configuration file", self._abs_workspace_path)\r
+\r
     def _get_git_ignored_paths(self) -> List[str]:\r
         """"\r
         Returns a list of file absolute path strings to all files ignored in this git repository.\r
@@ -465,6 +484,19 @@ class UncrustifyCheck(ICiBuildPlugin):
             self._abs_file_paths_to_format.extend(\r
                 [str(path.resolve()) for path in pathlib.Path(self._abs_package_path).rglob(path)])\r
 \r
+        # Remove files ignore in the plugin configuration file\r
+        plugin_ignored_files = list(filter(self._get_files_ignored_in_config(), self._abs_file_paths_to_format))\r
+\r
+        if plugin_ignored_files:\r
+            logging.info(\r
+                f"{self._package_name} file count before plugin ignore file exclusion: {len(self._abs_file_paths_to_format)}")\r
+            for path in plugin_ignored_files:\r
+                if path in self._abs_file_paths_to_format:\r
+                    logging.info(f"  File ignored in plugin config file: {path}")\r
+                    self._abs_file_paths_to_format.remove(path)\r
+            logging.info(\r
+                f"{self._package_name} file count after plugin ignore file exclusion: {len(self._abs_file_paths_to_format)}")\r
+\r
         if not "SkipGitExclusions" in self._package_config or not self._package_config["SkipGitExclusions"]:\r
             # Remove files ignored by git\r
             logging.info(\r