Skip to content

Commit 78f7356

Browse files
authored
feat: add add subcommand (#16)
* add add subcommand * fix cmake * edit add subcommand * fix 'add --all' * fix add_entries * update test * fix typo * integrate git_strarray wrapper * address review comments * address review comments
1 parent 45617f2 commit 78f7356

20 files changed

+194
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
build/
22
__pycache__/
3+
.cache/
4+
compile_commands.json

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ find_package(libgit2)
3939
# =====
4040

4141
set(GIT2CPP_SRC
42+
${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.cpp
43+
${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.hpp
4244
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp
4345
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp
4446
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
@@ -47,6 +49,8 @@ set(GIT2CPP_SRC
4749
${GIT2CPP_SOURCE_DIR}/utils/common.hpp
4850
${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp
4951
${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp
52+
${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.cpp
53+
${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.hpp
5054
${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.cpp
5155
${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.hpp
5256
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.cpp
@@ -59,4 +63,3 @@ set(GIT2CPP_SRC
5963

6064
add_executable(git2cpp ${GIT2CPP_SRC})
6165
target_link_libraries(git2cpp PRIVATE libgit2::libgit2package)
62-

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Developer's workflow using `micromamba` to manage the dependencies:
1313
```bash
1414
micromamba create -f dev-environment.yml
1515
micromamba activate git2cpp-dev
16-
cmake -Bbuild $CMAKE_INSALL_PREFIX=$CONDA_PREFIX
16+
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX
1717
cd build
1818
make -j8
1919
```

dev-environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ channels:
44
dependencies:
55
- cli11
66
- libgit2
7-
- meson
7+
- cmake
88
- pkg-config
99
- python
1010
- pytest

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "utils/git_exception.hpp"
66
#include "version.hpp"
7+
#include "subcommand/add_subcommand.hpp"
78
#include "subcommand/init_subcommand.hpp"
89
#include "subcommand/status_subcommand.hpp"
910

@@ -21,6 +22,7 @@ int main(int argc, char** argv)
2122
// Sub commands
2223
init_subcommand init(lg2_obj, app);
2324
status_subcommand status(lg2_obj, app);
25+
add_subcommand add(lg2_obj, app);
2426

2527
app.parse(argc, argv);
2628

src/subcommand/add_subcommand.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <git2.h>
2+
3+
#include "add_subcommand.hpp"
4+
#include "../wrapper/index_wrapper.hpp"
5+
#include "../wrapper/repository_wrapper.hpp"
6+
7+
8+
add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app)
9+
{
10+
auto *sub = app.add_subcommand("add", "Add file contents to the index");
11+
12+
sub->add_option("files", m_add_files, "Files to add");
13+
14+
sub->add_flag("-A,--all,--no-ignore-removal", m_all_flag, "");
15+
// sub->add_flag("-n,--dryrun", dryrun_flag, "");
16+
// sub->add_flag("-u,--update", update_flag, "");
17+
// sub->add_flag("-v,--verbose", verbose_flag, "");
18+
19+
sub->callback([this]() { this->run(); });
20+
};
21+
22+
23+
void add_subcommand::run()
24+
{
25+
auto directory = get_current_git_path();
26+
auto bare = false;
27+
auto repo = repository_wrapper::init(directory, bare);
28+
29+
index_wrapper index = repo.make_index();
30+
31+
if (m_all_flag)
32+
{
33+
index.add_all();
34+
}
35+
else
36+
{
37+
index.add_entries(m_add_files);
38+
}
39+
}

src/subcommand/add_subcommand.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <CLI/CLI.hpp>
4+
5+
#include "../utils/common.hpp"
6+
7+
class add_subcommand
8+
{
9+
public:
10+
11+
explicit add_subcommand(const libgit2_object&, CLI::App& app);
12+
void run();
13+
14+
private:
15+
bool m_all_flag = false;
16+
std::vector<std::string> m_add_files;
17+
};

src/subcommand/init_subcommand.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app)
66
{
77
auto *sub = app.add_subcommand("init", "Explanation of init here");
88

9-
sub->add_flag("--bare", bare, "info about bare arg");
9+
sub->add_flag("--bare", m_bare, "info about bare arg");
1010

1111
// If directory not specified, uses cwd.
12-
sub->add_option("directory", directory, "info about directory arg")
12+
sub->add_option("directory", m_directory, "info about directory arg")
1313
->check(CLI::ExistingDirectory | CLI::NonexistentPath)
1414
->default_val(get_current_git_path());
1515

@@ -18,5 +18,5 @@ init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app)
1818

1919
void init_subcommand::run()
2020
{
21-
repository_wrapper::init(directory, bare);
21+
repository_wrapper::init(m_directory, m_bare);
2222
}

src/subcommand/init_subcommand.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ class init_subcommand
1414
void run();
1515

1616
private:
17-
bool bare;
18-
std::string directory;
17+
bool m_bare;
18+
std::string m_directory;
1919
};

src/subcommand/status_subcommand.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@
1313
status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app)
1414
{
1515
auto *sub = app.add_subcommand("status", "Show modified files in working directory, staged for your next commit");
16-
// Displays paths that have differences between the index file and the current HEAD commit,
17-
// paths that have differences between the working tree and the index file, and paths in the
18-
// working tree that are not tracked by Git (and are not ignored by gitignore[5]).
19-
// The first are what you would commit by running git commit;
20-
// the second and third are what you could commit by running git add before running git commit.
2116

22-
sub->add_flag("-s,--short", short_flag, "Give the output in the short-format.");
23-
sub->add_flag("--long", long_flag, "Give the output in the long-format. This is the default.");
17+
sub->add_flag("-s,--short", m_short_flag, "Give the output in the short-format.");
18+
sub->add_flag("--long", m_long_flag, "Give the output in the long-format. This is the default.");
2419
// sub->add_flag("--porcelain[=<version>]", porcelain, "Give the output in an easy-to-parse format for scripts.
2520
// This is similar to the short output, but will remain stable across Git versions and regardless of user configuration.
2621
// See below for details. The version parameter is used to specify the format version. This is optional and defaults
2722
// to the original version v1 format.");
28-
sub->add_flag("-b,--branch", branch_flag, "Show the branch and tracking info even in short-format.");
23+
sub->add_flag("-b,--branch", m_branch_flag, "Show the branch and tracking info even in short-format.");
2924

3025
sub->callback([this]() { this->run(); });
3126
};
@@ -152,7 +147,7 @@ void print_entries(std::vector<print_entry> entries_to_print)
152147
}
153148
}
154149

155-
void print_not_tracked(std::vector<print_entry> entries_to_print, const std::set<std::string>& tracked_dir_set,
150+
void print_not_tracked(const std::vector<print_entry>& entries_to_print, const std::set<std::string>& tracked_dir_set,
156151
std::set<std::string>& untracked_dir_set)
157152
{
158153
std::vector<print_entry> not_tracked_entries_to_print{};
@@ -199,11 +194,11 @@ void status_subcommand::run()
199194
std::vector<std::string> ignored_to_print{};
200195

201196
output_format of = output_format::DEFAULT;
202-
if (short_flag)
197+
if (m_short_flag)
203198
{
204199
of = output_format::SHORT;
205200
}
206-
if (long_flag)
201+
if (m_long_flag)
207202
{
208203
of = output_format::LONG;
209204
}
@@ -220,7 +215,7 @@ void status_subcommand::run()
220215
}
221216
else
222217
{
223-
if (branch_flag)
218+
if (m_branch_flag)
224219
{
225220
std::cout << "## " << branch_name << std::endl;
226221
}

0 commit comments

Comments
 (0)