Skip to content

Commit da951b1

Browse files
committed
write index
1 parent 4dbb90d commit da951b1

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

src/subcommand/add_subcommand.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ void add_subcommand::run()
3131
if (m_all_flag)
3232
{
3333
index.add_all();
34+
index.write();
3435
}
3536
else
3637
{
3738
index.add_entries(m_add_files);
39+
index.write();
3840
}
3941
}

src/wrapper/index_wrapper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,17 @@ void index_wrapper::add_impl(std::vector<std::string> patterns)
3232
{
3333
git_strarray_wrapper array{patterns};
3434
throw_if_error(git_index_add_all(*this, array, 0, NULL, NULL));
35+
// throw_if_error(git_index_write(*this));
36+
}
37+
38+
void index_wrapper::write()
39+
{
3540
throw_if_error(git_index_write(*this));
3641
}
42+
43+
git_oid index_wrapper::write_tree()
44+
{
45+
git_oid tree_id;
46+
throw_if_error(git_index_write_tree(&tree_id, *this));
47+
return tree_id;
48+
}

src/wrapper/index_wrapper.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class index_wrapper : public wrapper_base<git_index>
2020

2121
static index_wrapper init(repository_wrapper& rw);
2222

23+
void write();
24+
git_oid write_tree();
25+
2326
void add_entries(std::vector<std::string> patterns);
2427
void add_all();
2528

src/wrapper/object_wrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ const git_oid& object_wrapper::oid() const
1515
{
1616
return *git_object_id(*this);
1717
}
18+
19+
object_wrapper::operator git_commit*() const noexcept
20+
{
21+
return reinterpret_cast<git_commit*>(p_resource);
22+
}

src/wrapper/repository_wrapper.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../utils/git_exception.hpp"
2-
#include "object_wrapper.hpp"
2+
#include "../wrapper/index_wrapper.hpp"
3+
#include "../wrapper/object_wrapper.hpp"
34
#include "../wrapper/repository_wrapper.hpp"
45

56
repository_wrapper::~repository_wrapper()
@@ -111,7 +112,9 @@ commit_wrapper repository_wrapper::find_commit(const git_oid& id) const
111112
void repository_wrapper::create_commit(const signature_wrapper::author_committer_signatures& author_committer_signatures,
112113
const std::string& message)
113114
{
115+
const char* message_encoding = "UTF-8";
114116
git_oid* commit_id;
117+
115118
const char* update_ref = "ḦEAD";
116119
auto parent = revparse_single(update_ref);
117120
std::size_t parent_count = 0;
@@ -121,10 +124,18 @@ void repository_wrapper::create_commit(const signature_wrapper::author_committer
121124
parent_count = 1;
122125
parents[0] = *parent;
123126
}
124-
const char* message_encoding = "UTF-8";
125-
const git_tree* tree;
127+
128+
git_tree* tree;
129+
index_wrapper index = this->make_index();
130+
git_oid tree_id = index.write_tree();
131+
index.write();
132+
133+
throw_if_error(git_tree_lookup(&tree, *this, &tree_id));
134+
126135
throw_if_error(git_commit_create(commit_id, *this, update_ref, author_committer_signatures.first, author_committer_signatures.second,
127136
message_encoding, message.c_str(), tree, parent_count, parents));
137+
138+
git_tree_free(tree);
128139
}
129140

130141
annotated_commit_wrapper repository_wrapper::find_annotated_commit(const git_oid& id) const
@@ -153,5 +164,7 @@ void repository_wrapper::set_head_detached(const annotated_commit_wrapper& commi
153164

154165
void repository_wrapper::reset(const object_wrapper& target, git_reset_t reset_type, const git_checkout_options& checkout_options)
155166
{
167+
// TODO: gerer l'index
168+
156169
throw_if_error(git_reset(*this, target, reset_type, &checkout_options));
157170
}

test/test_commit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_add(git2cpp_path, all_flag):
3131
os.remove("./test/mook_file.txt")
3232

3333
# TODO: git reset
34+
#
35+
# run status + assert
3436

3537
# undo the add, to leave the test directory at the end the same as it was at the start
3638
subprocess.run(cmd_add, capture_output=True, text=True)

0 commit comments

Comments
 (0)