Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix add_entries
  • Loading branch information
SandrineP committed Jul 17, 2025
commit ed4fe0eb5a4fee17e191d3ad6dd085472864e221
7 changes: 1 addition & 6 deletions src/subcommand/add_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ void add_subcommand::run()
}
else
{
for (const auto& path : add_files)
{
git_index_entry* entry;
entry->path = path.c_str();
index.add_entry(entry);
}
index.add_entry(add_files);
}
}
17 changes: 13 additions & 4 deletions src/wrapper/index_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../wrapper/repository_wrapper.hpp"

#include <iostream>
#include <vector>

index_wrapper::~index_wrapper()
{
Expand All @@ -17,15 +18,23 @@ index_wrapper index_wrapper::init(repository_wrapper& rw)
return index;
}

void index_wrapper::add_entry(const git_index_entry* entry)
void index_wrapper::add_entries(std::vector<std::string> patterns)
{
throwIfError(git_index_add(*this, entry));
add_impl(std::move(patterns));
}

void index_wrapper::add_all()
{
const char* patterns[] = {"."};
git_strarray array{(char**)patterns, 1};
add_impl({{"."}});
}

void index_wrapper::add_impl(std::vector<std::string> patterns)
{
git_strarray array{new char*[patterns.size()], patterns.size()};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaks. I think it would be worth having a wrapper of git_strarray, that correctly manages the memory and use it here. It will be useful for other commands anyway.

for (size_t i=0; i<patterns.size(); ++i)
{
array.strings[i] = const_cast<char*>(patterns[i].c_str());
}
throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL));
throwIfError(git_index_write(*this));
}
6 changes: 5 additions & 1 deletion src/wrapper/index_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <string>
#include <vector>

#include <git2.h>

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

static index_wrapper init(repository_wrapper& rw);

void add_entry(const git_index_entry* entry);
void add_entries(std::vector<std::string> patterns);
void add_all();


private:

index_wrapper() = default;
void add_impl(std::vector<std::string> patterns);
};
10 changes: 9 additions & 1 deletion test/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import pytest


@pytest.mark.parametrize("all_flag", ["-A", "--all", "--no-ignore-removal"])
@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"])
def test_add(git2cpp_path, all_flag):
with open("./test/mook_file.txt", "x") as f:
pass
f.close()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line isn't needed as the f will be automatically closed when the with ... block is exited. The same applies to 4 lines below.


with open("./test/mook_file_2.txt", "x") as f:
pass
f.close()

cmd_add = [git2cpp_path, 'add']
if all_flag != "":
cmd_add.append(all_flag)
else:
cmd_add.append("test/mook_file.txt")
subprocess.run(cmd_add, capture_output=True, text=True)

cmd_status = [git2cpp_path, 'status', "--long"]
Expand All @@ -27,3 +33,5 @@ def test_add(git2cpp_path, all_flag):
# assert "modified" in p.stdout
#
os.remove("./test/mook_file.txt")
os.remove("./test/mook_file_2.txt")
subprocess.run(cmd_add, capture_output=True, text=True)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line just to undo the add, to leave the test directory at the end the same as it was at the start? If so can you add a comment saying this, otherwise it will look suspicious in future to have this line without any following assert.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the reason why ! I'll write a comment.