Skip to content

Commit 0635a63

Browse files
committed
fix add_entries
1 parent 5cfa05e commit 0635a63

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/subcommand/add_subcommand.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ void add_subcommand::run()
3434
}
3535
else
3636
{
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-
}
37+
index.add_entry(add_files);
4338
}
4439
}

src/wrapper/index_wrapper.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../wrapper/repository_wrapper.hpp"
44

55
#include <iostream>
6+
#include <vector>
67

78
index_wrapper::~index_wrapper()
89
{
@@ -17,15 +18,23 @@ index_wrapper index_wrapper::init(repository_wrapper& rw)
1718
return index;
1819
}
1920

20-
void index_wrapper::add_entry(const git_index_entry* entry)
21+
void index_wrapper::add_entries(std::vector<std::string> patterns)
2122
{
22-
throwIfError(git_index_add(*this, entry));
23+
add_impl(std::move(patterns));
2324
}
2425

2526
void index_wrapper::add_all()
2627
{
27-
const char* patterns[] = {"."};
28-
git_strarray array{(char**)patterns, 1};
28+
add_impl({{"."}});
29+
}
30+
31+
void index_wrapper::add_impl(std::vector<std::string> patterns)
32+
{
33+
git_strarray array{new char*[patterns.size()], patterns.size()};
34+
for (size_t i=0; i<patterns.size(); ++i)
35+
{
36+
array.strings[i] = const_cast<char*>(patterns[i].c_str());
37+
}
2938
throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL));
3039
throwIfError(git_index_write(*this));
3140
}

src/wrapper/index_wrapper.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <string>
4+
#include <vector>
5+
36
#include <git2.h>
47

58
#include "../utils/common.hpp"
@@ -17,11 +20,12 @@ class index_wrapper : public wrapper_base<git_index>
1720

1821
static index_wrapper init(repository_wrapper& rw);
1922

20-
void add_entry(const git_index_entry* entry);
23+
void add_entries(std::vector<std::string> patterns);
2124
void add_all();
2225

2326

2427
private:
2528

2629
index_wrapper() = default;
30+
void add_impl(std::vector<std::string> patterns);
2731
};

test/test_add.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
import pytest
55

66

7-
@pytest.mark.parametrize("all_flag", ["-A", "--all", "--no-ignore-removal"])
7+
@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"])
88
def test_add(git2cpp_path, all_flag):
99
with open("./test/mook_file.txt", "x") as f:
1010
pass
1111
f.close()
1212

13+
with open("./test/mook_file_2.txt", "x") as f:
14+
pass
15+
f.close()
16+
1317
cmd_add = [git2cpp_path, 'add']
1418
if all_flag != "":
1519
cmd_add.append(all_flag)
20+
else:
21+
cmd_add.append("test/mook_file.txt")
1622
subprocess.run(cmd_add, capture_output=True, text=True)
1723

1824
cmd_status = [git2cpp_path, 'status', "--long"]
@@ -27,3 +33,5 @@ def test_add(git2cpp_path, all_flag):
2733
# assert "modified" in p.stdout
2834
#
2935
os.remove("./test/mook_file.txt")
36+
os.remove("./test/mook_file_2.txt")
37+
subprocess.run(cmd_add, capture_output=True, text=True)

0 commit comments

Comments
 (0)