Skip to content

Commit 3be4166

Browse files
committed
add add subcommand
1 parent 7ba5a9c commit 3be4166

File tree

9 files changed

+121
-9
lines changed

9 files changed

+121
-9
lines changed

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_flag("-A,--all,--no-ignore-removal", all_flag, "");
13+
// sub->add_flag("-n,--dryrun", dryrun_flag, "");
14+
// sub->add_flag("-u,--update", update_flag, "");
15+
// sub->add_flag("-v,--verbose", verbose_flag, "");
16+
17+
sub->callback([this]() { this->run(); });
18+
};
19+
20+
void add_subcommand::run()
21+
{
22+
auto directory = get_current_git_path();
23+
auto bare = false;
24+
auto repo = repository_wrapper::init(directory, bare);
25+
26+
index_wrapper index = repo.get_index();
27+
28+
if (all_flag)
29+
{
30+
index.add_all();
31+
}
32+
// else
33+
// {
34+
// index.add_entry();
35+
// }
36+
}

src/subcommand/add_subcommand.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 all_flag = false;
16+
};

src/subcommand/status_subcommand.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
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

2217
sub->add_flag("-s,--short", short_flag, "Give the output in the short-format.");
2318
sub->add_flag("--long", long_flag, "Give the output in the long-format. This is the default.");
@@ -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{};

src/version.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#define GIT2CPP_VERSION_MAJOR 0
44
#define GIT2CPP_VERSION_MINOR 0
5-
#define GIT2CPP_VERSION_PATCH 1
5+
#define GIT2CPP_VERSION_PATCH 2
66

77
// e.g. ".rc0"
88
#define GIT2CPP_VERSION_SUFFIX

src/wrapper/index_wrapper.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "index_wrapper.hpp"
2+
#include "../utils/git_exception.hpp"
3+
#include "../wrapper/repository_wrapper.hpp"
4+
5+
index_wrapper::~index_wrapper()
6+
{
7+
git_index_free(p_resource);
8+
p_resource=nullptr;
9+
}
10+
11+
index_wrapper index_wrapper::init(repository_wrapper& rw)
12+
{
13+
index_wrapper index;
14+
throwIfError(git_repository_index(&(index.p_resource), rw));
15+
return index;
16+
}
17+
18+
void index_wrapper::add_entry(const git_index_entry* entry)
19+
{
20+
index_wrapper index;
21+
throwIfError(git_index_add(index, entry));
22+
}
23+
24+
void index_wrapper::add_all()
25+
{
26+
index_wrapper index;
27+
git_strarray array = {0}; // array of strings, array of path patterns
28+
throwIfError(git_index_add_all(index, &array, 0, NULL, NULL));
29+
}

src/wrapper/index_wrapper.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <git2.h>
4+
5+
#include "../utils/common.hpp"
6+
7+
class repository_wrapper;
8+
9+
class index_wrapper : public wrapper_base<git_index>
10+
{
11+
public:
12+
13+
~index_wrapper();
14+
15+
index_wrapper(index_wrapper&&) = default;
16+
index_wrapper& operator=(index_wrapper&&) = default;
17+
18+
static index_wrapper init(repository_wrapper& rw);
19+
20+
void add_entry(const git_index_entry* entry);
21+
void add_all();
22+
23+
24+
private:
25+
26+
index_wrapper() = default;
27+
};

src/wrapper/repository_wrapper.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include "../utils/git_exception.hpp"
21
#include "repository_wrapper.hpp"
3-
2+
#include "../utils/git_exception.hpp"
43

54
repository_wrapper::~repository_wrapper()
65
{
@@ -21,3 +20,9 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b
2120
throwIfError(git_repository_init(&(rw.p_resource), directory.c_str(), bare));
2221
return rw;
2322
}
23+
24+
index_wrapper repository_wrapper::get_index() const
25+
{
26+
repository_wrapper rw;
27+
index_wrapper::init(rw);
28+
}

src/wrapper/repository_wrapper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <git2.h>
66

77
#include "../utils/common.hpp"
8+
#include "../wrapper/index_wrapper.hpp"
89

910
class repository_wrapper : public wrapper_base<git_repository>
1011
{
@@ -17,6 +18,7 @@ class repository_wrapper : public wrapper_base<git_repository>
1718

1819
static repository_wrapper init(const std::string& directory, bool bare);
1920
static repository_wrapper open(const std::string& directory);
21+
index_wrapper get_index() const;
2022

2123
private:
2224

0 commit comments

Comments
 (0)