-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add add
subcommand
#16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
26a846b
39dc847
65485be
7e5ea3a
ed4fe0e
ccb3077
75f8c47
d232da9
893eec9
3e8c3d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
build/ | ||
__pycache__/ | ||
.cache/ | ||
compile_commands.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ channels: | |
dependencies: | ||
- cli11 | ||
- libgit2 | ||
- meson | ||
- cmake | ||
- pkg-config | ||
- python | ||
- pytest | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#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"); | ||
|
||
sub->add_option("files", add_files, "Files to add"); | ||
|
||
sub->add_flag("-A,--all,--no-ignore-removal", all_flag, ""); | ||
// sub->add_flag("-n,--dryrun", dryrun_flag, ""); | ||
// sub->add_flag("-u,--update", update_flag, ""); | ||
// sub->add_flag("-v,--verbose", verbose_flag, ""); | ||
|
||
sub->callback([this]() { this->run(); }); | ||
}; | ||
|
||
|
||
void add_subcommand::run() | ||
{ | ||
auto directory = get_current_git_path(); | ||
auto bare = false; | ||
auto repo = repository_wrapper::init(directory, bare); | ||
|
||
index_wrapper index = repo.make_index(); | ||
|
||
if (all_flag) | ||
{ | ||
index.add_all(); | ||
} | ||
else | ||
{ | ||
index.add_entries(add_files); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <CLI/CLI.hpp> | ||
|
||
#include "../utils/common.hpp" | ||
|
||
class add_subcommand | ||
{ | ||
public: | ||
|
||
explicit add_subcommand(const libgit2_object&, CLI::App& app); | ||
void run(); | ||
|
||
private: | ||
bool all_flag = false; | ||
std::vector<std::string> add_files; | ||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
#define GIT2CPP_VERSION_MAJOR 0 | ||
#define GIT2CPP_VERSION_MINOR 0 | ||
#define GIT2CPP_VERSION_PATCH 1 | ||
#define GIT2CPP_VERSION_PATCH 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why updating the version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 0.0.2 has been released but we forgot to change it there, so it's to catch up. |
||
|
||
// e.g. ".rc0" | ||
#define GIT2CPP_VERSION_SUFFIX | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
#include "index_wrapper.hpp" | ||||||
#include "../utils/git_exception.hpp" | ||||||
#include "../wrapper/repository_wrapper.hpp" | ||||||
|
||||||
#include <vector> | ||||||
|
||||||
index_wrapper::~index_wrapper() | ||||||
{ | ||||||
git_index_free(p_resource); | ||||||
p_resource=nullptr; | ||||||
} | ||||||
|
||||||
index_wrapper index_wrapper::init(repository_wrapper& rw) | ||||||
{ | ||||||
index_wrapper index; | ||||||
throwIfError(git_repository_index(&(index.p_resource), rw)); | ||||||
return index; | ||||||
} | ||||||
|
||||||
void index_wrapper::add_entries(std::vector<std::string> patterns) | ||||||
{ | ||||||
add_impl(std::move(patterns)); | ||||||
} | ||||||
|
||||||
void index_wrapper::add_all() | ||||||
{ | ||||||
add_impl({{"."}}); | ||||||
} | ||||||
|
||||||
void index_wrapper::add_impl(std::vector<std::string> patterns) | ||||||
{ | ||||||
git_strarray_wrapper array=git_strarray_wrapper(patterns); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
throwIfError(git_index_add_all(*this, array, 0, NULL, NULL)); | ||||||
throwIfError(git_index_write(*this)); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include <git2.h> | ||
|
||
#include "../utils/common.hpp" | ||
|
||
class repository_wrapper; | ||
|
||
class index_wrapper : public wrapper_base<git_index> | ||
{ | ||
public: | ||
|
||
~index_wrapper(); | ||
|
||
index_wrapper(index_wrapper&&) = default; | ||
index_wrapper& operator=(index_wrapper&&) = default; | ||
|
||
static index_wrapper init(repository_wrapper& rw); | ||
|
||
void add_entries(std::vector<std::string> patterns); | ||
void add_all(); | ||
|
||
|
||
private: | ||
|
||
index_wrapper() = default; | ||
void add_impl(std::vector<std::string> patterns); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import subprocess | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"]) | ||
def test_add(git2cpp_path, all_flag): | ||
with open("./test/mook_file.txt", "x") as f: | ||
pass | ||
f.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line isn't needed as the |
||
|
||
with open("./test/mook_file_2.txt", "x") as f: | ||
pass | ||
f.close() | ||
|
||
cmd_add = [git2cpp_path, 'add'] | ||
if all_flag != "": | ||
cmd_add.append(all_flag) | ||
else: | ||
cmd_add.append("test/mook_file.txt") | ||
subprocess.run(cmd_add, capture_output=True, text=True) | ||
|
||
cmd_status = [git2cpp_path, 'status', "--long"] | ||
p_status = subprocess.run(cmd_status, capture_output=True, text=True) | ||
|
||
assert "Changes to be committed" in p_status.stdout | ||
assert "new file" in p_status.stdout | ||
if all_flag != "": | ||
assert "Untracked files" not in p_status.stdout | ||
else: | ||
assert "Untracked files" in p_status.stdout | ||
|
||
os.remove("./test/mook_file.txt") | ||
os.remove("./test/mook_file_2.txt") | ||
subprocess.run(cmd_add, capture_output=True, text=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line just to undo the add, to leave the test directory at the end the same as it was at the start? If so can you add a comment saying this, otherwise it will look suspicious in future to have this line without any following There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's the reason why ! I'll write a comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convention: data member names should start with
m_
(orp_
) so it is easy to distinguish them from local variables.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_
stands for "member" andp_
stands for what ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pointer