Skip to content

Commit 30493e6

Browse files
committed
Require libgit2 1.4.0+. Remove data_buffer methods
This change is slightly backward incompatible. libgit2 1.4.0 made a chagne to the `git_buf` structure and deprecated a bunch of associated functions, to the effect that `git_buf` is no longer modifiable by users. It is expected that `git_buf` should only be set by libgit2 itself as a return from various functions. So this change to the `data_buffer` class removes all modifier methods. A data_buffer is returned where libgit2 returns a `git_buf`, but we no longer allow the caller to modify the buffer data or grow the buffer. related: libgit2/libgit2#6078 Perhaps we should change methods returning a `data_buffer` to return a `std::string` instead?
1 parent 7703940 commit 30493e6

File tree

13 files changed

+69
-146
lines changed

13 files changed

+69
-146
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if(LIBGIT2_USE_VENDORED)
1212
else()
1313
find_package(PkgConfig)
1414
if(PkgConfig_FOUND)
15-
PKG_CHECK_MODULES(LIBGIT2 libgit2>=0.99)
15+
PKG_CHECK_MODULES(LIBGIT2 libgit2>=1.4.0)
1616
endif()
1717
endif()
1818

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,7 @@ virtual const char *what() const throw() { return message_; }
870870
| --- | --- |
871871
| `git_buf_contains_nul` | `data_buffer::contains_nul` |
872872
| `git_buf_dispose` | `data_buffer::~data_buffer` |
873-
| `git_buf_free` | `data_buffer::~data_buffer` |
874-
| `git_buf_grow` | `data_buffer::grow_to_size` |
875873
| `git_buf_is_binary` | `data_buffer::is_binary` |
876-
| `git_buf_set` | `data_buffer::set_buffer` |
877874
878875
### checkout
879876

ext/libgit2

Submodule libgit2 updated 617 files

include/cppgit2/data_buffer.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ class data_buffer : public libgit2_api {
1313
// Default construct a data buffer using GIT_BUF_INIT
1414
data_buffer();
1515

16-
// Construct buffer of size n
17-
// Contains a git_buf with char buffer of size n
18-
data_buffer(size_t n);
19-
2016
// Construct buffer from libgit2 C ptr
2117
data_buffer(const git_buf *c_ptr);
2218

@@ -32,16 +28,9 @@ class data_buffer : public libgit2_api {
3228
// Check quickly if buffer contains a NUL byte
3329
bool contains_nul() const;
3430

35-
// Resize buffer allocation to make more space
36-
// Currently, this will never shrink a buffer, only expand it
37-
void grow_to_size(size_t target_size);
38-
3931
// Check quickly if buffer looks like it contains binary data
4032
bool is_binary() const;
4133

42-
// Set buffer to a copy of some raw data
43-
void set_buffer(const std::string &buffer);
44-
4534
// Get string representation of data buffer
4635
std::string to_string() const;
4736

src/data_buffer.cpp

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,34 @@
33

44
namespace cppgit2 {
55

6-
/* Used as default value for git_buf->ptr so that people can always
7-
* assume ptr is non-NULL and zero terminated even for new git_bufs.
8-
*/
9-
char git_buf__initbuf[1];
10-
/* Use to initialize buffer structure when git_buf is on stack */
11-
#define GIT_BUF_INIT \
12-
{ git_buf__initbuf, 0, 0 }
13-
14-
data_buffer::data_buffer() { c_struct_ = GIT_BUF_INIT; }
15-
16-
data_buffer::data_buffer(size_t n) {
17-
c_struct_.ptr = (char *)malloc(n * sizeof(char));
18-
if (c_struct_.ptr)
19-
memset(c_struct_.ptr, '\0', n * sizeof(char));
20-
c_struct_.asize = n;
21-
c_struct_.size = 0;
22-
}
236

247
data_buffer::data_buffer(const git_buf *c_ptr) {
25-
c_struct_.ptr = (char *)malloc(c_ptr->asize * sizeof(char));
26-
c_struct_.asize = c_ptr->asize;
8+
c_struct_ = GIT_BUF_INIT;
279
c_struct_.size = c_ptr->size;
28-
if (c_struct_.ptr)
29-
strncpy(c_struct_.ptr, c_ptr->ptr, c_ptr->asize);
10+
c_struct_.ptr = c_ptr->ptr;
11+
c_struct_.reserved = c_ptr->reserved;
3012
}
3113

3214
data_buffer::~data_buffer() {
33-
if (c_struct_.size)
34-
git_buf_dispose(&c_struct_);
15+
git_buf_dispose(&c_struct_);
3516
}
3617

3718
data_buffer::data_buffer(data_buffer&& other) {
3819
c_struct_.size = other.c_struct_.size;
3920
c_struct_.ptr = other.c_struct_.ptr;
40-
c_struct_.asize = other.c_struct_.asize;
21+
c_struct_.reserved = other.c_struct_.reserved;
4122
other.c_struct_.size = 0;
42-
other.c_struct_.asize = 0;
23+
other.c_struct_.reserved = 0;
4324
other.c_struct_.ptr = nullptr;
4425
}
4526

4627
data_buffer& data_buffer::operator= (data_buffer&& other) {
4728
if (other.c_struct_.ptr != c_struct_.ptr) {
4829
c_struct_.size = other.c_struct_.size;
4930
c_struct_.ptr = other.c_struct_.ptr;
50-
c_struct_.asize = other.c_struct_.asize;
31+
c_struct_.reserved = other.c_struct_.reserved;
5132
other.c_struct_.size = 0;
52-
other.c_struct_.asize = 0;
33+
other.c_struct_.reserved = 0;
5334
other.c_struct_.ptr = nullptr;
5435
}
5536
return *this;
@@ -59,18 +40,8 @@ bool data_buffer::contains_nul() const {
5940
return git_buf_contains_nul(&c_struct_);
6041
}
6142

62-
void data_buffer::grow_to_size(size_t target_size) {
63-
if (git_buf_grow(&c_struct_, target_size))
64-
throw git_exception();
65-
}
66-
6743
bool data_buffer::is_binary() const { return git_buf_is_binary(&c_struct_); }
6844

69-
void data_buffer::set_buffer(const std::string &buffer) {
70-
if (git_buf_set(&c_struct_, buffer.c_str(), buffer.size()))
71-
throw git_exception();
72-
}
73-
7445
std::string data_buffer::to_string() const {
7546
if (c_struct_.size)
7647
return std::string(c_struct_.ptr);

src/diff.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ char diff::status_char(delta::type status) const {
8888
}
8989

9090
std::string diff::to_string(diff::format format_type) const {
91-
// TODO: Check this hardcoded size
92-
data_buffer result;
93-
if (git_diff_to_buf(result.c_ptr(), c_ptr_,
91+
git_buf result_buf = GIT_BUF_INIT;
92+
if (git_diff_to_buf(&result_buf, c_ptr_,
9493
static_cast<git_diff_format_t>(format_type)))
9594
throw git_exception();
95+
auto result = data_buffer(&result_buf);
9696
return result.to_string();
9797
}
9898

@@ -106,9 +106,10 @@ diff::stats diff::diff_stats() const {
106106
}
107107

108108
data_buffer diff::format_email(const format_email_options &options) {
109-
data_buffer result;
110-
if (git_diff_format_email(result.c_ptr(), c_ptr_, options.c_ptr()))
109+
git_buf result_buf = GIT_BUF_INIT;
110+
if (git_diff_format_email(&result_buf, c_ptr_, options.c_ptr()))
111111
throw git_exception();
112+
auto result = data_buffer(&result_buf);
112113
return result;
113114
}
114115

src/pack_builder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ void pack_builder::write(
132132
}
133133

134134
data_buffer pack_builder::write_to_buffer() {
135-
data_buffer result;
136-
if (git_packbuilder_write_buf(result.c_ptr(), c_ptr_))
135+
git_buf result = GIT_BUF_INIT;
136+
if (git_packbuilder_write_buf(&result, c_ptr_))
137137
throw git_exception();
138-
return result;
138+
return data_buffer(&result);
139139
}
140140

141141
size_t pack_builder::written() const { return git_packbuilder_written(c_ptr_); }

src/patch.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ size_t patch::size(bool include_context, bool include_hunk_headers,
133133
}
134134

135135
data_buffer patch::to_buffer() {
136-
data_buffer result;
137-
if (git_patch_to_buf(result.c_ptr(), c_ptr_))
136+
git_buf result = GIT_BUF_INIT;
137+
if (git_patch_to_buf(&result, c_ptr_))
138138
throw git_exception();
139-
return result;
139+
return data_buffer(&result);
140140
}
141141

142142
const git_patch *patch::c_ptr() const { return c_ptr_; }

src/refspec.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ refspec refspec::parse(const std::string &input, bool is_fetch) {
5050

5151
data_buffer
5252
refspec::transform_target_to_source_reference(const std::string &name) {
53-
data_buffer result;
54-
if (git_refspec_rtransform(result.c_ptr(), c_ptr_, name.c_str()))
53+
git_buf result = GIT_BUF_INIT;
54+
if (git_refspec_rtransform(&result, c_ptr_, name.c_str()))
5555
throw git_exception();
56-
return result;
56+
return data_buffer(&result);
5757
}
5858

5959
std::string refspec::source() const {
@@ -71,10 +71,10 @@ std::string refspec::to_string() const {
7171
}
7272

7373
data_buffer refspec::transform_reference(const std::string &name) {
74-
data_buffer result;
75-
if (git_refspec_transform(result.c_ptr(), c_ptr_, name.c_str()))
74+
git_buf result;
75+
if (git_refspec_transform(&result, c_ptr_, name.c_str()))
7676
throw git_exception();
77-
return result;
77+
return data_buffer(&result);
7878
}
7979

8080
const git_refspec *refspec::c_ptr() const { return c_ptr_; }

src/remote.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ remote remote::create_remote(const std::string &url,
7373
}
7474

7575
data_buffer remote::default_branch() const {
76-
data_buffer result;
77-
if (git_remote_default_branch(result.c_ptr(), c_ptr_))
76+
git_buf result = GIT_BUF_INIT;
77+
if (git_remote_default_branch(&result, c_ptr_))
7878
throw git_exception();
79-
return result;
79+
return data_buffer(&result);
8080
}
8181

8282
void remote::disconnect() {

src/repository.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ void repository::detach_head() const {
133133
std::string repository::discover_path(const std::string &start_path,
134134
bool across_fs,
135135
const std::string &ceiling_dirs) {
136-
// TODO: Update this hardcoded size
137-
data_buffer buffer;
138-
if (git_repository_discover(buffer.c_ptr(), start_path.c_str(), across_fs,
136+
git_buf buffer = GIT_BUF_INIT;
137+
if (git_repository_discover(&buffer, start_path.c_str(), across_fs,
139138
ceiling_dirs.c_str()))
140139
throw git_exception();
141-
return buffer.to_string();
140+
return data_buffer(&buffer).to_string();
142141
}
143142

144143
std::string repository::discover_path(const std::string &start_path) {
@@ -269,18 +268,19 @@ cppgit2::index repository::index() const {
269268
}
270269

271270
std::string repository::path(repository::item item) const {
272-
data_buffer buffer;
273-
if (git_repository_item_path(buffer.c_ptr(), c_ptr_,
271+
git_buf buffer = GIT_BUF_INIT;
272+
if (git_repository_item_path(&buffer, c_ptr_,
274273
static_cast<git_repository_item_t>(item)))
275274
throw git_exception();
276-
return buffer.to_string();
275+
return data_buffer(&buffer).to_string();
277276
}
278277

279278
std::string repository::message() const {
280-
data_buffer buffer;
281-
if (git_repository_message(buffer.c_ptr(), c_ptr_))
279+
git_buf buffer = GIT_BUF_INIT;
280+
if (git_repository_message(&buffer, c_ptr_))
282281
throw git_exception();
283-
return buffer.to_string();
282+
283+
return data_buffer(&buffer).to_string();
284284
}
285285

286286
void repository::remove_message() const { git_repository_message_remove(c_ptr_); }
@@ -652,10 +652,10 @@ std::string repository::branch_name(const reference &branch) const {
652652
}
653653

654654
std::string repository::branch_remote_name(const std::string &refname) const {
655-
data_buffer result;
656-
if (git_branch_remote_name(result.c_ptr(), c_ptr_, refname.c_str()))
655+
git_buf buf = GIT_BUF_INIT;
656+
if (git_branch_remote_name(&buf, c_ptr_, refname.c_str()))
657657
throw git_exception();
658-
return result.to_string();
658+
return data_buffer(&buf).to_string();
659659
}
660660

661661
void repository::set_branch_upstream(const reference &ref,
@@ -693,18 +693,18 @@ reference repository::branch_upstream(const std::string &local_branch_name) cons
693693
}
694694

695695
std::string repository::branch_upstream_name(const std::string &refname) const {
696-
data_buffer result;
697-
if (git_branch_upstream_name(result.c_ptr(), c_ptr_, refname.c_str()))
696+
git_buf buf = GIT_BUF_INIT;
697+
if (git_branch_upstream_name(&buf, c_ptr_, refname.c_str()))
698698
throw git_exception();
699-
return result.to_string();
699+
return data_buffer(&buf).to_string();
700700
}
701701

702702
std::string
703703
repository::branch_upstream_remote(const std::string &refname) const {
704-
data_buffer result;
705-
if (git_branch_upstream_remote(result.c_ptr(), c_ptr_, refname.c_str()))
704+
git_buf buf = GIT_BUF_INIT;
705+
if (git_branch_upstream_remote(&buf, c_ptr_, refname.c_str()))
706706
throw git_exception();
707-
return result.to_string();
707+
return data_buffer(&buf).to_string();
708708
}
709709

710710
reference repository::lookup_branch(const std::string &branch_name,
@@ -797,19 +797,19 @@ data_buffer repository::create_commit(const signature &author,
797797
const std::string &message,
798798
const tree &tree,
799799
const std::vector<commit> &parents) const {
800-
data_buffer result;
800+
git_buf buf = GIT_BUF_INIT;
801801
const char *message_encoding_c =
802802
message_encoding == "" ? NULL : message_encoding.c_str();
803803
std::vector<const git_commit *> parents_c;
804804
for (auto &p : parents) {
805805
parents_c.push_back(p.c_ptr());
806806
}
807-
if (git_commit_create_buffer(result.c_ptr(), c_ptr_, author.c_ptr(),
807+
if (git_commit_create_buffer(&buf, c_ptr_, author.c_ptr(),
808808
committer.c_ptr(), message_encoding_c,
809809
message.c_str(), tree.c_ptr(), parents.size(),
810810
parents_c.data()))
811811
throw git_exception();
812-
return result;
812+
return data_buffer(&buf);
813813
}
814814

815815
oid repository::create_commit(const std::string &commit_content,
@@ -829,11 +829,11 @@ oid repository::create_commit(const std::string &commit_content,
829829
std::pair<data_buffer, data_buffer>
830830
repository::extract_signature_from_commit(oid id,
831831
const std::string &signature_field) const {
832-
data_buffer sig, signed_data;
833-
if (git_commit_extract_signature(sig.c_ptr(), signed_data.c_ptr(), c_ptr_,
832+
git_buf sig, signed_data;
833+
if (git_commit_extract_signature(&sig, &signed_data, c_ptr_,
834834
id.c_ptr(), signature_field.c_str()))
835835
throw git_exception();
836-
return std::pair<data_buffer, data_buffer>{std::move(sig), std::move(signed_data)};
836+
return std::pair<data_buffer, data_buffer>{std::move(data_buffer(&sig)), std::move(data_buffer(&signed_data))};
837837
}
838838

839839
commit repository::lookup_commit(const oid &id) const {
@@ -904,12 +904,12 @@ void repository::add_ondisk_config_file(const cppgit2::config &cfg,
904904
data_buffer repository::create_diff_commit_as_email(
905905
const commit &commit, size_t patch_no, size_t total_patches,
906906
diff::format_email_flag flags, const diff::options &options) const {
907-
data_buffer result;
908-
if (git_diff_commit_as_email(result.c_ptr(), c_ptr_, commit.c_ptr_, patch_no,
907+
git_buf buf = GIT_BUF_INIT;
908+
if (git_diff_commit_as_email(&buf, c_ptr_, commit.c_ptr_, patch_no,
909909
total_patches, static_cast<uint32_t>(flags),
910910
options.c_ptr()))
911911
throw git_exception();
912-
return result;
912+
return data_buffer(&buf);
913913
}
914914

915915
diff repository::create_diff_index_to_index(const cppgit2::index &old_index,
@@ -1217,10 +1217,10 @@ oid repository::remove_note(const commit &notes_commit, const signature &author,
12171217
}
12181218

12191219
data_buffer repository::detault_notes_reference() const {
1220-
data_buffer result;
1221-
if (git_note_default_ref(result.c_ptr(), c_ptr_))
1220+
git_buf buf = GIT_BUF_INIT;
1221+
if (git_note_default_ref(&buf, c_ptr_))
12221222
throw git_exception();
1223-
return result;
1223+
return data_buffer(&buf);
12241224
}
12251225

12261226
void repository::for_each_note(
@@ -1774,10 +1774,10 @@ submodule repository::lookup_submodule(const std::string &name) const {
17741774
}
17751775

17761776
data_buffer repository::resolve_submodule_url(const std::string &url) const {
1777-
data_buffer result;
1778-
if (git_submodule_resolve_url(result.c_ptr(), c_ptr_, url.c_str()))
1777+
git_buf buf = GIT_BUF_INIT;
1778+
if (git_submodule_resolve_url(&buf, c_ptr_, url.c_str()))
17791779
throw git_exception();
1780-
return result;
1780+
return data_buffer(&buf);
17811781
}
17821782

17831783
void repository::set_submodule_branch(const std::string &submodule_name,

0 commit comments

Comments
 (0)