Add pre-commit config with linting for C++ and Python by ianthomas23 · Pull Request #121 · QuantStack/git2cpp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions .clang-format
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/asottile/pyupgrade
rev: v3.21.2
hooks:
- id: pyupgrade
args: ['--py312-plus']

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v21.1.2
hooks:
- id: clang-format
args: [--style=file]
exclude_types: [javascript,json]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.0
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-format
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ The CLI is tested using `python`. From the top-level directory:
pytest -v
```

`pre-commit` runs automatically on `git commit`. To run it manually use:

```bash
pre-commit run --all-files
```

# WebAssembly build and deployment

The `wasm` directory contains everything needed to build the local `git2cpp` source code as an
Expand Down
1 change: 1 addition & 0 deletions dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies:
- libgit2
- cmake
- pkg-config
- pre-commit
- python
- pytest
- termcolor-cpp
Expand Down
14 changes: 8 additions & 6 deletions docs/create_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def sanitise_line(line):
return line


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

# Write output markdown file, identifying subcommands at the same time to provide
# links to the subcommand markdown files.
# links to the subcommand markdown files.
subcommands = []
with open(filename, "w") as f:
f.write(f"({filename})=\n") # Target for links.
Expand All @@ -52,7 +52,9 @@ def process(args, to_process):
if match:
subcommand = match.group(2)
subcommand_filename = get_filename(args + [subcommand])
line = match.group(1) + f"[{subcommand}]({subcommand_filename})" + match.group(3)
line = (
match.group(1) + f"[{subcommand}]({subcommand_filename})" + match.group(3)
)
subcommands.append(subcommand)
elif line.startswith("SUBCOMMANDS:"):
in_subcommand_section = True
Expand All @@ -74,10 +76,10 @@ def process(args, to_process):


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

to_process = [["git2cpp"]]
while len(to_process) > 0:
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is not a python project but it contains python tests so here are settings for python linting.
[tool.ruff]
line-length = 100
target-version = "py312"
[tool.ruff.format]
line-ending = "lf"
21 changes: 12 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <CLI/CLI.hpp>
#include <cmath>
#include <git2.h> // For version number only
#include <iostream>

#include "utils/git_exception.hpp"
#include "version.hpp"
#include <CLI/CLI.hpp>
#include <git2.h> // For version number only

#include "subcommand/add_subcommand.hpp"
#include "subcommand/branch_subcommand.hpp"
#include "subcommand/checkout_subcommand.hpp"
Expand All @@ -21,12 +20,14 @@
#include "subcommand/rebase_subcommand.hpp"
#include "subcommand/remote_subcommand.hpp"
#include "subcommand/reset_subcommand.hpp"
#include "subcommand/revlist_subcommand.hpp"
#include "subcommand/revparse_subcommand.hpp"
#include "subcommand/rm_subcommand.hpp"
#include "subcommand/stash_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"
#include "subcommand/tag_subcommand.hpp"
#include "subcommand/revparse_subcommand.hpp"
#include "subcommand/revlist_subcommand.hpp"
#include "subcommand/rm_subcommand.hpp"
#include "utils/git_exception.hpp"
#include "version.hpp"

int main(int argc, char** argv)
{
Expand Down Expand Up @@ -69,7 +70,8 @@ int main(int argc, char** argv)

if (version->count())
{
std::cout << "git2cpp version " << GIT2CPP_VERSION_STRING << " (libgit2 " << LIBGIT2_VERSION << ")" << std::endl;
std::cout << "git2cpp version " << GIT2CPP_VERSION_STRING << " (libgit2 " << LIBGIT2_VERSION
<< ")" << std::endl;
}
else if (app.get_subcommands().size() == 0)
{
Expand All @@ -86,7 +88,8 @@ int main(int argc, char** argv)
std::cerr << e.what() << std::endl;
exit_code = e.error_code();
}
catch (std::exception& e) {
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
exit_code = 1;
}
Expand Down
14 changes: 9 additions & 5 deletions src/subcommand/add_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "add_subcommand.hpp"

#include <git2.h>

#include "add_subcommand.hpp"
#include "../wrapper/index_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"


add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app)
{
auto *sub = app.add_subcommand("add", "Add file contents to the index");
auto* sub = app.add_subcommand("add", "Add file contents to the index");

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

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

sub->callback([this]() { this->run(); });
sub->callback(
[this]()
{
this->run();
}
);
};


void add_subcommand::run()
{
auto directory = get_current_git_path();
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/add_subcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class add_subcommand
void run();

private:

bool m_all_flag = false;
std::vector<std::string> m_add_files;
};
16 changes: 13 additions & 3 deletions src/subcommand/branch_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../subcommand/branch_subcommand.hpp"

#include <iostream>

#include "../subcommand/branch_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"

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

sub->callback([this]() { this->run(); });
sub->callback(
[this]()
{
this->run();
}
);
}

void branch_subcommand::run()
Expand Down
36 changes: 20 additions & 16 deletions src/subcommand/checkout_subcommand.cpp
Loading
Loading