Skip to content

Commit ef291ed

Browse files
committed
edit add subcommand
1 parent 90a95da commit ef291ed

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

src/subcommand/add_subcommand.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app)
99
{
1010
auto *sub = app.add_subcommand("add", "Add file contents to the index");
1111

12+
sub->add_option("files", add_files, "Files to add");
13+
1214
sub->add_flag("-A,--all,--no-ignore-removal", all_flag, "");
1315
// sub->add_flag("-n,--dryrun", dryrun_flag, "");
1416
// sub->add_flag("-u,--update", update_flag, "");
@@ -17,20 +19,26 @@ add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app)
1719
sub->callback([this]() { this->run(); });
1820
};
1921

22+
2023
void add_subcommand::run()
2124
{
2225
auto directory = get_current_git_path();
2326
auto bare = false;
2427
auto repo = repository_wrapper::init(directory, bare);
2528

26-
index_wrapper index = repo.get_index();
29+
index_wrapper index = repo.make_index();
2730

2831
if (all_flag)
2932
{
3033
index.add_all();
3134
}
32-
// else
33-
// {
34-
// index.add_entry();
35-
// }
35+
else
36+
{
37+
for (const auto& path : add_files)
38+
{
39+
git_index_entry* entry;
40+
entry->path = path.c_str();
41+
index.add_entry(entry);
42+
}
43+
}
3644
}

src/subcommand/add_subcommand.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ class add_subcommand
1313

1414
private:
1515
bool all_flag = false;
16+
std::vector<std::string> add_files;
1617
};

src/wrapper/index_wrapper.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ index_wrapper index_wrapper::init(repository_wrapper& rw)
1717

1818
void index_wrapper::add_entry(const git_index_entry* entry)
1919
{
20-
index_wrapper index;
21-
throwIfError(git_index_add(index, entry));
20+
throwIfError(git_index_add(*this, entry));
2221
}
2322

2423
void index_wrapper::add_all()
2524
{
26-
index_wrapper index;
2725
git_strarray array = {0}; // array of strings, array of path patterns
28-
throwIfError(git_index_add_all(index, &array, 0, NULL, NULL));
26+
throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL));
2927
}

src/wrapper/repository_wrapper.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b
2121
return rw;
2222
}
2323

24-
index_wrapper repository_wrapper::get_index() const
24+
index_wrapper repository_wrapper::make_index()
2525
{
26-
repository_wrapper rw;
27-
index_wrapper index = index_wrapper::init(rw);
26+
index_wrapper index = index_wrapper::init(*this);
2827
return index;
2928
}

src/wrapper/repository_wrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class repository_wrapper : public wrapper_base<git_repository>
1818

1919
static repository_wrapper init(const std::string& directory, bool bare);
2020
static repository_wrapper open(const std::string& directory);
21-
index_wrapper get_index() const;
21+
index_wrapper make_index();
2222

2323
private:
2424

0 commit comments

Comments
 (0)