Skip to content

Commit d9fe9e4

Browse files
Add pre-commit config with linting for C++ and Python (#121)
* Add pre-commit config with linting for C++ and python * Some ruff fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ade2c75 commit d9fe9e4

File tree

110 files changed

+1673
-1165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+1673
-1165
lines changed

.clang-format

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# From https://github.com/man-group/sparrow with sparrow-specific regexes removed.
2+
3+
BasedOnStyle: Mozilla
4+
5+
AccessModifierOffset: '-4'
6+
AlignAfterOpenBracket: BlockIndent
7+
AlignEscapedNewlines: Left
8+
AllowAllArgumentsOnNextLine: false
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: false
13+
AllowShortIfStatementsOnASingleLine: false
14+
# Forbid one line lambdas because clang-format makes a weird split when
15+
# single instructions lambdas are too long.
16+
AllowShortLambdasOnASingleLine: Empty
17+
AllowShortLoopsOnASingleLine: false
18+
AlwaysBreakAfterDefinitionReturnType: None
19+
AlwaysBreakAfterReturnType: None
20+
AlwaysBreakTemplateDeclarations: Yes
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
BreakBeforeBinaryOperators: NonAssignment
24+
BreakBeforeBraces: Allman
25+
BreakBeforeTernaryOperators: true
26+
BreakConstructorInitializers: BeforeComma
27+
BreakInheritanceList: AfterComma
28+
BreakStringLiterals: false
29+
ColumnLimit: '110'
30+
ConstructorInitializerIndentWidth: '4'
31+
ContinuationIndentWidth: '4'
32+
Cpp11BracedListStyle: true
33+
DerivePointerAlignment: false
34+
DisableFormat: false
35+
EmptyLineAfterAccessModifier: Always
36+
EmptyLineBeforeAccessModifier: Always
37+
ExperimentalAutoDetectBinPacking: true
38+
IncludeBlocks: Regroup
39+
IncludeCategories:
40+
- Regex: <[^.]+>
41+
Priority: 1
42+
- Regex: <.+>
43+
Priority: 2
44+
- Regex: '".+"'
45+
Priority: 5
46+
IndentCaseLabels: true
47+
IndentPPDirectives: AfterHash
48+
IndentWidth: '4'
49+
IndentWrappedFunctionNames: false
50+
InsertBraces: true
51+
InsertTrailingCommas: Wrapped
52+
KeepEmptyLinesAtTheStartOfBlocks: false
53+
LambdaBodyIndentation: Signature
54+
Language: Cpp
55+
MaxEmptyLinesToKeep: '2'
56+
NamespaceIndentation: All
57+
ObjCBlockIndentWidth: '4'
58+
ObjCSpaceAfterProperty: false
59+
ObjCSpaceBeforeProtocolList: false
60+
PackConstructorInitializers: Never
61+
PenaltyBreakAssignment: 100000
62+
PenaltyBreakBeforeFirstCallParameter: 0
63+
PenaltyBreakComment: 10
64+
PenaltyBreakOpenParenthesis: 0
65+
PenaltyBreakTemplateDeclaration: 0
66+
PenaltyExcessCharacter: 10
67+
PenaltyIndentedWhitespace: 0
68+
PenaltyReturnTypeOnItsOwnLine: 10
69+
PointerAlignment: Left
70+
QualifierAlignment: Custom # Experimental
71+
QualifierOrder: [inline, static, constexpr, const, volatile, type]
72+
ReflowComments: true
73+
SeparateDefinitionBlocks: Always
74+
SortIncludes: CaseInsensitive
75+
SortUsingDeclarations: true
76+
SpaceAfterCStyleCast: true
77+
SpaceAfterTemplateKeyword: true
78+
SpaceBeforeAssignmentOperators: true
79+
SpaceBeforeParens: ControlStatements
80+
SpaceInEmptyParentheses: false
81+
SpacesBeforeTrailingComments: '2'
82+
SpacesInAngles: false
83+
SpacesInCStyleCastParentheses: false
84+
SpacesInContainerLiterals: false
85+
SpacesInParentheses: false
86+
SpacesInSquareBrackets: false
87+
Standard: c++20
88+
TabWidth: '4'
89+
UseTab: Never

.pre-commit-config.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v6.0.0
4+
hooks:
5+
- id: check-merge-conflict
6+
- id: check-toml
7+
- id: check-yaml
8+
- id: debug-statements
9+
- id: end-of-file-fixer
10+
- id: mixed-line-ending
11+
- id: trailing-whitespace
12+
13+
- repo: https://github.com/asottile/pyupgrade
14+
rev: v3.21.2
15+
hooks:
16+
- id: pyupgrade
17+
args: ['--py312-plus']
18+
19+
- repo: https://github.com/pre-commit/mirrors-clang-format
20+
rev: v21.1.2
21+
hooks:
22+
- id: clang-format
23+
args: [--style=file]
24+
exclude_types: [javascript,json]
25+
26+
- repo: https://github.com/astral-sh/ruff-pre-commit
27+
rev: v0.14.0
28+
hooks:
29+
- id: ruff-check
30+
args: [--fix]
31+
- id: ruff-format

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ The CLI is tested using `python`. From the top-level directory:
2929
pytest -v
3030
```
3131

32+
`pre-commit` runs automatically on `git commit`. To run it manually use:
33+
34+
```bash
35+
pre-commit run --all-files
36+
```
37+
3238
# WebAssembly build and deployment
3339

3440
The `wasm` directory contains everything needed to build the local `git2cpp` source code as an

dev-environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- libgit2
77
- cmake
88
- pkg-config
9+
- pre-commit
910
- python
1011
- pytest
1112
- termcolor-cpp

docs/create_markdown.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def sanitise_line(line):
2626
return line
2727

2828

29-
# Process a single subcommand, adding new subcommands found to to_process.
29+
# Process a single subcommand, adding new subcommands found to to_process.
3030
def process(args, to_process):
3131
cmd = args + ["--help"]
3232
cmd_string = " ".join(cmd)
@@ -37,7 +37,7 @@ def process(args, to_process):
3737
p = subprocess.run(cmd, capture_output=True, text=True, check=True)
3838

3939
# Write output markdown file, identifying subcommands at the same time to provide
40-
# links to the subcommand markdown files.
40+
# links to the subcommand markdown files.
4141
subcommands = []
4242
with open(filename, "w") as f:
4343
f.write(f"({filename})=\n") # Target for links.
@@ -52,7 +52,9 @@ def process(args, to_process):
5252
if match:
5353
subcommand = match.group(2)
5454
subcommand_filename = get_filename(args + [subcommand])
55-
line = match.group(1) + f"[{subcommand}]({subcommand_filename})" + match.group(3)
55+
line = (
56+
match.group(1) + f"[{subcommand}]({subcommand_filename})" + match.group(3)
57+
)
5658
subcommands.append(subcommand)
5759
elif line.startswith("SUBCOMMANDS:"):
5860
in_subcommand_section = True
@@ -74,10 +76,10 @@ def process(args, to_process):
7476

7577

7678
if __name__ == "__main__":
77-
# Modify the PATH so that git2cpp is found by name, as using a full path will cause the help
79+
# Modify the PATH so that git2cpp is found by name, as using a full path will cause the help
7880
# pages to write that full path.
79-
git2cpp_dir = Path(__file__).parent.parent / 'build'
80-
os.environ["PATH"] = f'{git2cpp_dir}{os.pathsep}{os.environ["PATH"]}'
81+
git2cpp_dir = Path(__file__).parent.parent / "build"
82+
os.environ["PATH"] = f"{git2cpp_dir}{os.pathsep}{os.environ['PATH']}"
8183

8284
to_process = [["git2cpp"]]
8385
while len(to_process) > 0:

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is not a python project but it contains python tests so here are settings for python linting.
2+
[tool.ruff]
3+
line-length = 100
4+
target-version = "py312"
5+
[tool.ruff.format]
6+
line-ending = "lf"

src/main.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#include <CLI/CLI.hpp>
21
#include <cmath>
3-
#include <git2.h> // For version number only
42
#include <iostream>
53

6-
#include "utils/git_exception.hpp"
7-
#include "version.hpp"
4+
#include <CLI/CLI.hpp>
5+
#include <git2.h> // For version number only
6+
87
#include "subcommand/add_subcommand.hpp"
98
#include "subcommand/branch_subcommand.hpp"
109
#include "subcommand/checkout_subcommand.hpp"
@@ -21,12 +20,14 @@
2120
#include "subcommand/rebase_subcommand.hpp"
2221
#include "subcommand/remote_subcommand.hpp"
2322
#include "subcommand/reset_subcommand.hpp"
23+
#include "subcommand/revlist_subcommand.hpp"
24+
#include "subcommand/revparse_subcommand.hpp"
25+
#include "subcommand/rm_subcommand.hpp"
2426
#include "subcommand/stash_subcommand.hpp"
2527
#include "subcommand/status_subcommand.hpp"
2628
#include "subcommand/tag_subcommand.hpp"
27-
#include "subcommand/revparse_subcommand.hpp"
28-
#include "subcommand/revlist_subcommand.hpp"
29-
#include "subcommand/rm_subcommand.hpp"
29+
#include "utils/git_exception.hpp"
30+
#include "version.hpp"
3031

3132
int main(int argc, char** argv)
3233
{
@@ -69,7 +70,8 @@ int main(int argc, char** argv)
6970

7071
if (version->count())
7172
{
72-
std::cout << "git2cpp version " << GIT2CPP_VERSION_STRING << " (libgit2 " << LIBGIT2_VERSION << ")" << std::endl;
73+
std::cout << "git2cpp version " << GIT2CPP_VERSION_STRING << " (libgit2 " << LIBGIT2_VERSION
74+
<< ")" << std::endl;
7375
}
7476
else if (app.get_subcommands().size() == 0)
7577
{
@@ -86,7 +88,8 @@ int main(int argc, char** argv)
8688
std::cerr << e.what() << std::endl;
8789
exit_code = e.error_code();
8890
}
89-
catch (std::exception& e) {
91+
catch (std::exception& e)
92+
{
9093
std::cerr << e.what() << std::endl;
9194
exit_code = 1;
9295
}

src/subcommand/add_subcommand.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
#include "add_subcommand.hpp"
2+
13
#include <git2.h>
24

3-
#include "add_subcommand.hpp"
45
#include "../wrapper/index_wrapper.hpp"
56
#include "../wrapper/repository_wrapper.hpp"
67

7-
88
add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app)
99
{
10-
auto *sub = app.add_subcommand("add", "Add file contents to the index");
10+
auto* sub = app.add_subcommand("add", "Add file contents to the index");
1111

1212
sub->add_option("<files>", m_add_files, "Files to add");
1313

@@ -16,10 +16,14 @@ add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app)
1616
// sub->add_flag("-u,--update", update_flag, "");
1717
// sub->add_flag("-v,--verbose", verbose_flag, "");
1818

19-
sub->callback([this]() { this->run(); });
19+
sub->callback(
20+
[this]()
21+
{
22+
this->run();
23+
}
24+
);
2025
};
2126

22-
2327
void add_subcommand::run()
2428
{
2529
auto directory = get_current_git_path();

src/subcommand/add_subcommand.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class add_subcommand
1212
void run();
1313

1414
private:
15+
1516
bool m_all_flag = false;
1617
std::vector<std::string> m_add_files;
1718
};

src/subcommand/branch_subcommand.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#include "../subcommand/branch_subcommand.hpp"
2+
13
#include <iostream>
24

3-
#include "../subcommand/branch_subcommand.hpp"
45
#include "../wrapper/repository_wrapper.hpp"
56

67
branch_subcommand::branch_subcommand(const libgit2_object&, CLI::App& app)
@@ -14,9 +15,18 @@ branch_subcommand::branch_subcommand(const libgit2_object&, CLI::App& app)
1415
sub->add_flag("-r,--remotes", m_remote_flag, "List or delete (if used with -d) the remote-tracking branches");
1516
sub->add_flag("-l,--list", m_list_flag, "List branches");
1617
sub->add_flag("-f,--force", m_force_flag, "Skips confirmation");
17-
sub->add_flag("--show-current", m_show_current_flag, "Print the name of the current branch. In detached HEAD state, nothing is printed.");
18+
sub->add_flag(
19+
"--show-current",
20+
m_show_current_flag,
21+
"Print the name of the current branch. In detached HEAD state, nothing is printed."
22+
);
1823

19-
sub->callback([this]() { this->run(); });
24+
sub->callback(
25+
[this]()
26+
{
27+
this->run();
28+
}
29+
);
2030
}
2131

2232
void branch_subcommand::run()

0 commit comments

Comments
 (0)