Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 4b7f882

Browse files
committed
Remote craete_options
1 parent 3609884 commit 4b7f882

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,9 +1239,9 @@ The project is available under the [MIT](https://opensource.org/licenses/MIT) li
12391239
| `git_remote_create` | `repository::create_remote` |
12401240
| `git_remote_create_anonymous` | `repository::create_anonymous_remote` |
12411241
| `git_remote_create_detached` | `remote::create_detached_remote` |
1242-
| `git_remote_create_options_init` | |
1242+
| `git_remote_create_options_init` | `remote::create_options::create_options` |
12431243
| `git_remote_create_with_fetchspec` | `repository::create_remote` |
1244-
| `git_remote_create_with_opts` | |
1244+
| `git_remote_create_with_opts` | `remote::create_remote` |
12451245
| `git_remote_default_branch` | `remote::default_branch` |
12461246
| `git_remote_delete` | `repository::delete_remote` |
12471247
| `git_remote_disconnect` | `remote::disconnect` |

include/cppgit2/remote.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <cppgit2/bitmask_operators.hpp>
23
#include <cppgit2/data_buffer.hpp>
34
#include <cppgit2/fetch.hpp>
45
#include <cppgit2/indexer.hpp>
@@ -30,6 +31,56 @@ class remote : public libgit2_api {
3031
// is connected to the remote host.
3132
bool is_connected() const;
3233

34+
// Remote creation options flags
35+
enum class create_flag {
36+
// Ignore the repository apply.insteadOf configuration
37+
skip_insteadof = (1 << 0),
38+
// Don't build a fetchspec from the name if none is set
39+
default_fetchspec = (1 << 1)
40+
};
41+
42+
class create_options : public libgit2_api {
43+
public:
44+
create_options() : c_ptr_(nullptr) {
45+
auto ret =
46+
git_remote_create_init_options(&default_options_, GIT_REMOTE_CREATE_OPTIONS_VERSION);
47+
c_ptr_ = &default_options_;
48+
if (ret != 0)
49+
throw git_exception();
50+
}
51+
52+
create_options(git_remote_create_options *c_ptr) : c_ptr_(c_ptr) {}
53+
54+
// Version
55+
unsigned int version() const { return c_ptr_->version; }
56+
void set_version(unsigned int version) { c_ptr_->version = version; }
57+
58+
// Repository
59+
class repository repository() const;
60+
void set_repository(const class repository &repo);
61+
62+
// Name
63+
std::string name() const { return c_ptr_->name ? std::string(c_ptr_->name) : ""; }
64+
void set_name(const std::string &name) { c_ptr_->name = name.c_str(); }
65+
66+
// Fetchspec
67+
std::string fetchspec() const { return c_ptr_->fetchspec ? std::string(c_ptr_->fetchspec) : ""; }
68+
void set_fetchspec(const std::string &fetchspec) { c_ptr_->fetchspec = fetchspec.c_str(); }
69+
70+
// Flags
71+
create_flag flags() const { return static_cast<create_flag>(c_ptr_->flags); }
72+
void set_flags(const create_flag &flags) {
73+
c_ptr_->flags = static_cast<unsigned int>(flags);
74+
}
75+
76+
// Access libgit2 C ptr
77+
const git_remote_create_options *c_ptr() const { return c_ptr_; }
78+
79+
private:
80+
git_remote_create_options *c_ptr_;
81+
git_remote_create_options default_options_;
82+
};
83+
3384
// Create a copy of an existing remote.
3485
// All internal strings are also duplicated.
3586
// Callbacks are not duplicated.
@@ -43,6 +94,11 @@ class remote : public libgit2_api {
4394
// values (such as instead of url substitutions).
4495
static remote create_detached_remote(const std::string &url);
4596

97+
// Create a remote, with options.
98+
// This function allows more fine-grained control over the remote creation.
99+
static remote create_remote(const std::string &url,
100+
const create_options &options = create_options());
101+
46102
// Retrieve the name of the remote's default branch
47103
// This function must only be called after connecting.
48104
data_buffer default_branch() const;
@@ -117,5 +173,6 @@ class remote : public libgit2_api {
117173
ownership owner_;
118174
git_remote *c_ptr_;
119175
};
176+
ENABLE_BITMASK_OPERATORS(remote::create_flag);
120177

121178
} // namespace cppgit2

include/cppgit2/repository.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,7 @@ class repository : public libgit2_api {
13961396
private:
13971397
friend class index;
13981398
friend class pathspec;
1399+
friend class remote;
13991400
friend class submodule;
14001401
friend class tree_builder;
14011402
git_repository *c_ptr_;

src/remote.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ fetch::options::autotag remote::autotag_option() {
1717

1818
bool remote::is_connected() const { return git_remote_connected(c_ptr_); }
1919

20+
cppgit2::repository remote::create_options::repository() const {
21+
return cppgit2::repository(c_ptr_->repository);
22+
}
23+
24+
void remote::create_options::set_repository(const cppgit2::repository &repo) {
25+
c_ptr_->repository = repo.c_ptr_;
26+
}
27+
2028
remote remote::copy() const {
2129
remote result;
2230
if (git_remote_dup(&result.c_ptr_, c_ptr_))
@@ -31,6 +39,13 @@ remote remote::create_detached_remote(const std::string &url) {
3139
return result;
3240
}
3341

42+
remote remote::create_remote(const std::string &url, const create_options &options) {
43+
remote result;
44+
if (git_remote_create_with_opts(&result.c_ptr_, url.c_str(), options.c_ptr()))
45+
throw git_exception();
46+
return result;
47+
}
48+
3449
data_buffer remote::default_branch() const {
3550
data_buffer result;
3651
if (git_remote_default_branch(result.c_ptr(), c_ptr_))

0 commit comments

Comments
 (0)