Skip to content

Commit 9e123f5

Browse files
authored
Add C++ and CMake pre-commit hooks (#568)
* ci(pre-commit): add CI workflow for checking pre-commit hooks * ci(pre-commit): add hooks for cpp and CMake * refactor(pre-commit.yml): now uses the workflow on main * docs(README.md): update status badge for pre-commit * refactor(ci): move local hooks into a different config file * docs(README.md): update status badge for pre-commit * refactor(ci): pre-commit.yml now only runs on pull_request and workflow_dispatch * ci(pre-commit-local): ignore build/include_order in ament_cpplint since conflict with clang-format * refactor: fix ament_cpplint and cppcheck warnings * ci(pre-commit-config-local): increase line length to 120 for ament_cpplint due to some lines being longer * refactor: fix ament_cpplint and cppcheck warnings * chore(pre-commit): exclude test dirs from ament_cppcheck to avoid false positives * ci(pre-commit): update workflow to run on push to main and pull_request
1 parent 38af952 commit 9e123f5

32 files changed

Lines changed: 178 additions & 106 deletions

File tree

.github/workflows/pre-commit.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: pre-commit
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types: [ opened, synchronize, reopened, ready_for_review ]
9+
workflow_dispatch:
10+
jobs:
11+
call_reusable_workflow:
12+
uses: vortexntnu/vortex-ci/.github/workflows/reusable-pre-commit.yml@main
13+
with:
14+
ros_distro: 'humble'
15+
config_path: '.pre-commit-config-local.yaml'

.pre-commit-config-local.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# To use:
2+
#
3+
# pre-commit run --all-files -c .pre-commit-config-local.yaml
4+
#
5+
# Or to install it for automatic checks on commit:
6+
#
7+
# pre-commit install -c .pre-commit-config-local.yaml
8+
#
9+
# To update this file:
10+
#
11+
# pre-commit autoupdate -c .pre-commit-config-local.yaml
12+
#
13+
# See https://pre-commit.com/ for documentation
14+
#
15+
# NOTE: This configuration uses local hooks specific to ROS2 (ament_* linters)
16+
17+
repos:
18+
- repo: local
19+
hooks:
20+
- id: ament_cppcheck
21+
name: ament_cppcheck
22+
description: Static code analysis of C/C++ files.
23+
entry: env AMENT_CPPCHECK_ALLOW_SLOW_VERSIONS=1 ament_cppcheck
24+
language: system
25+
files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$
26+
exclude: (^|/)(test|tests)/.* # exclude test dirs since ament_cppcheck misparses GTest macros and throws false syntax errors
27+
- repo: local
28+
hooks:
29+
- id: ament_cpplint
30+
name: ament_cpplint
31+
description: Static code analysis of C/C++ files.
32+
entry: ament_cpplint
33+
language: system
34+
files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$
35+
args: [
36+
"--linelength=120",
37+
"--filter=-whitespace/newline,-legal/copyright,-build/include_order" # ignore build/include_order since it conflicts with .clang-format
38+
]
39+
# CMake hooks
40+
- repo: local
41+
hooks:
42+
- id: ament_lint_cmake
43+
name: ament_lint_cmake
44+
description: Check format of CMakeLists.txt files.
45+
entry: ament_lint_cmake
46+
language: system
47+
files: CMakeLists\.txt$

control/dp_adapt_backs_controller/include/dp_adapt_backs_controller/dp_adapt_backs_controller.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef DP_ADAPT_BACKS_CONTROLLER_HPP
2-
#define DP_ADAPT_BACKS_CONTROLLER_HPP
1+
#ifndef DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_HPP_
2+
#define DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_HPP_
33

44
#include <eigen3/Eigen/Dense>
55
#include <vortex/utils/types.hpp>
@@ -59,4 +59,4 @@ class DPAdaptBacksController {
5959

6060
} // namespace vortex::control
6161

62-
#endif // DP_ADAPT_BACKS_CONTROLLER_HPP
62+
#endif // DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_HPP_

control/dp_adapt_backs_controller/include/dp_adapt_backs_controller/dp_adapt_backs_controller_ros.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
#ifndef DP_ADAPT_BACKS_CONTROLLER_ROS_HPP
2-
#define DP_ADAPT_BACKS_CONTROLLER_ROS_HPP
1+
#ifndef DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_ROS_HPP_
2+
#define DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_ROS_HPP_
33

44
#include <chrono>
55
#include <geometry_msgs/msg/pose_stamped.hpp>
66
#include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
77
#include <geometry_msgs/msg/twist_with_covariance_stamped.hpp>
88
#include <geometry_msgs/msg/wrench_stamped.hpp>
9+
#include <memory>
910
#include <nav_msgs/msg/odometry.hpp>
1011
#include <rclcpp/rclcpp.hpp>
1112
#include <std_msgs/msg/bool.hpp>
1213
#include <std_msgs/msg/float64_multi_array.hpp>
1314
#include <std_msgs/msg/string.hpp>
15+
#include <string>
1416
#include <vortex/utils/types.hpp>
1517
#include <vortex_msgs/msg/reference_filter.hpp>
1618
#include "dp_adapt_backs_controller/dp_adapt_backs_controller.hpp"
@@ -93,4 +95,4 @@ class DPAdaptBacksControllerNode : public rclcpp::Node {
9395

9496
} // namespace vortex::control
9597

96-
#endif
98+
#endif // DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_ROS_HPP_

control/dp_adapt_backs_controller/include/dp_adapt_backs_controller/dp_adapt_backs_controller_utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef DP_ADAPT_BACKS_CONTROLLER_UTILS_HPP
2-
#define DP_ADAPT_BACKS_CONTROLLER_UTILS_HPP
1+
#ifndef DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_UTILS_HPP_
2+
#define DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_UTILS_HPP_
33

44
#include <vortex/utils/types.hpp>
55
#include "dp_adapt_backs_controller/typedefs.hpp"
@@ -50,4 +50,4 @@ Eigen::Matrix6x12d calculate_Y_v(const vortex::utils::types::Nu& nu);
5050

5151
} // namespace vortex::control
5252

53-
#endif
53+
#endif // DP_ADAPT_BACKS_CONTROLLER__DP_ADAPT_BACKS_CONTROLLER_UTILS_HPP_

control/dp_adapt_backs_controller/include/dp_adapt_backs_controller/typedefs.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @brief Contains the Eigen typedefs for the controller.
44
*/
55

6-
#ifndef VORTEX_DP_ADAPT_BACKSTEPPING_CONTROLLER_TYPEDEFS_H
7-
#define VORTEX_DP_ADAPT_BACKSTEPPING_CONTROLLER_TYPEDEFS_H
6+
#ifndef DP_ADAPT_BACKS_CONTROLLER__TYPEDEFS_HPP_
7+
#define DP_ADAPT_BACKS_CONTROLLER__TYPEDEFS_HPP_
88

99
#include <eigen3/Eigen/Dense>
1010

@@ -19,4 +19,4 @@ typedef Eigen::Matrix<double, 12, 12> Matrix12d;
1919

2020
} // namespace Eigen
2121

22-
#endif
22+
#endif // DP_ADAPT_BACKS_CONTROLLER__TYPEDEFS_HPP_

control/dp_adapt_backs_controller/src/dp_adapt_backs_controller_ros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "dp_adapt_backs_controller/dp_adapt_backs_controller_utils.hpp"
99
#include "dp_adapt_backs_controller/typedefs.hpp"
1010

11-
const std::string start_message = R"(
11+
constexpr std::string_view start_message = R"(
1212
____ ____ ____ _ _ _
1313
| _ \| _ \ / ___|___ _ __ | |_ _ __ ___ | | | ___ _ __
1414
| | | | |_) | | | / _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|

control/pid_controller_dp/include/pid_controller_dp/pid_controller.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#ifndef PID_CONTROLLER_HPP
2-
#define PID_CONTROLLER_HPP
1+
#ifndef PID_CONTROLLER_DP__PID_CONTROLLER_HPP_
2+
#define PID_CONTROLLER_DP__PID_CONTROLLER_HPP_
33

44
#include "pid_controller_dp/typedefs.hpp"
55

66
class PIDController {
77
public:
8-
explicit PIDController();
8+
PIDController();
99

1010
// @brief Calculate the control input tau
1111
// @param eta: struct containing the vehicle pose [position, orientation]
@@ -44,4 +44,4 @@ class PIDController {
4444
double dt_;
4545
};
4646

47-
#endif
47+
#endif // PID_CONTROLLER_DP__PID_CONTROLLER_HPP_

control/pid_controller_dp/include/pid_controller_dp/pid_controller_conversions.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef PID_CONTROLLER_CONVERSIONS_HPP
2-
#define PID_CONTROLLER_CONVERSIONS_HPP
1+
#ifndef PID_CONTROLLER_DP__PID_CONTROLLER_CONVERSIONS_HPP_
2+
#define PID_CONTROLLER_DP__PID_CONTROLLER_CONVERSIONS_HPP_
33

44
#include <cmath>
55
#include <eigen3/Eigen/Geometry>
@@ -15,4 +15,4 @@ types::Eta eta_convert_from_ros_to_eigen(
1515
types::Nu nu_convert_from_ros_to_eigen(
1616
const geometry_msgs::msg::TwistWithCovarianceStamped::SharedPtr msg);
1717

18-
#endif
18+
#endif // PID_CONTROLLER_DP__PID_CONTROLLER_CONVERSIONS_HPP_

control/pid_controller_dp/include/pid_controller_dp/pid_controller_ros.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef PID_CONTROLLER_ROS_HPP
2-
#define PID_CONTROLLER_ROS_HPP
1+
#ifndef PID_CONTROLLER_DP__PID_CONTROLLER_ROS_HPP_
2+
#define PID_CONTROLLER_DP__PID_CONTROLLER_ROS_HPP_
33

44
#include <chrono>
55
#include <geometry_msgs/msg/pose_stamped.hpp>
@@ -11,6 +11,7 @@
1111
#include <std_msgs/msg/bool.hpp>
1212
#include <std_msgs/msg/float64_multi_array.hpp>
1313
#include <std_msgs/msg/string.hpp>
14+
#include <string>
1415
#include <variant>
1516
#include <vortex_msgs/msg/reference_filter.hpp>
1617
#include "pid_controller_dp/pid_controller.hpp"
@@ -19,7 +20,7 @@
1920
// @brief Class for the PID controller node
2021
class PIDControllerNode : public rclcpp::Node {
2122
public:
22-
explicit PIDControllerNode();
23+
PIDControllerNode();
2324

2425
private:
2526
// @brief Callback function for the killswitch topic
@@ -95,4 +96,4 @@ class PIDControllerNode : public rclcpp::Node {
9596
std::string software_mode_;
9697
};
9798

98-
#endif
99+
#endif // PID_CONTROLLER_DP__PID_CONTROLLER_ROS_HPP_

0 commit comments

Comments
 (0)