Precommit hooks for MATLAB
- MATLAB R2022b+
- Python
pre-commitinstalled [pip instal pre-commit]
Tested on Windows 10, MATLAB R2023b.
- Install
pre-committo your Python environment. - Add to your
.pre-commit-config.yaml:repos: - repo: https://github.com/dapperfu/pre-commit-hooks-matlab rev: 2023.09.018 hooks: - id: simulink_close_subsystems - id: simulink_zoom_subsystems - id: matlab_smart_indent - id: matlab_justify - Run
pre-commit run --all-files --verbose
simulink_close_subsystems- Closes all open subsystem windows and tabs. Leaves the model to open directly to the top level Simulink model.simulink_zoom_subsystems- Scales all SubSystems to fit the window.matlab_smart_indent- RunTidyCodesmartIndenton all M-files. Ensuring consistent formatting.matlab_justify- RunTidyCodejustifyon all m-files aligning=.
Hooks to be implemented.
matlab_align_io- RunalignIO.mon a model so all Inports and Outports.simulink_unit_delay_prettify- RununitDelayPrettify.mon a model so all unit delay blocks are aligned with the block they came out of.matlab_mccabe- Fail forcheckcode- Display the McCabe cyclomatic complexity of each function in the file. In general, lower complexity values indicate programs that are easier to understand and modify. Evidence suggests that programs with higher complexity values are more likely to contain errors. Frequently, you can lower the complexity of a function by dividing it into smaller, simpler functions. Some people advocate splitting up programs that have a complexity value over 10.
matlab_code_issues- Fail for 'codeIssues`.simulink_slbuild-slbuild- Build standalone executable file or model reference target for modelpolyspace_bug_finder- [Polyspace Bug Finder]](https://www.mathworks.com/products/polyspace-bug-finder.html) Integration?- Polyspace Bug Finder identifies run-time errors, concurrency issues, security vulnerabilities, and other defects in C and C++ embedded software. Using static analysis, including semantic analysis, Polyspace Bug Finder analyzes software control flow, data flow, and interprocedural behavior. By highlighting defects as soon as they are detected, it lets you triage and fix bugs early in the development process.
- Polyspace Bug Finder checks compliance with coding rule standards such as MISRA C®, MISRA C++, AUTOSAR C++14, CERT® C, CERT C++, and custom naming conventions. It generates reports consisting of bugs found, code-rule violations, and code quality metrics, including cyclomatic complexity.
- [Optional]: Fork to your own repository
Checkout to somewhere in your workspace.
git clone https://github.com/dapperfu/pre-commit-hooks-matlab.git
cd <your project>
pre-commit try-repo ../pre-commit-hooks-matlab --all-files --verbose
`pre-commit`` will run and check your Project repository for m-files and Simulink Models.