1 #include "ostree_ref.h"
10 #include "logging/logging.h"
11 #include "utilities/utils.h"
15 OSTreeRef::OSTreeRef(
const OSTreeRepo &repo,
const string &ref_name) : ref_name_(ref_name) {
16 if (boost::filesystem::is_regular_file(repo.root() /
"/refs/heads/" / ref_name)) {
17 std::ifstream f((repo.root() /
"/refs/heads/" / ref_name).string(), std::ios::in | std::ios::binary);
19 std::istream_iterator<char> start(f);
20 std::istream_iterator<char> end;
21 string res(start, end);
24 while (!res.empty() && res[res.size() - 1] ==
'\n') {
25 res.resize(res.size() - 1);
34 OSTreeRef::OSTreeRef(
const TreehubServer &serve_repo,
string ref_name)
35 : is_valid(true), ref_name_(std::move(ref_name)) {
37 serve_repo.InjectIntoCurl(Url(), curl_handle.get());
38 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, &OSTreeRef::curl_handle_write);
39 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA,
this);
40 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
41 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_FAILONERROR,
true);
42 CURLcode rc = curl_easy_perform(curl_handle.get());
46 ref_content_ = http_response_.str();
49 void OSTreeRef::PushRef(
const TreehubServer &push_target, CURL *curl_handle)
const {
52 push_target.InjectIntoCurl(Url(), curl_handle);
53 curlEasySetoptWrapper(curl_handle, CURLOPT_WRITEFUNCTION, &OSTreeRef::curl_handle_write);
54 curlEasySetoptWrapper(curl_handle, CURLOPT_WRITEDATA,
this);
55 curlEasySetoptWrapper(curl_handle, CURLOPT_PRIVATE,
this);
57 curlEasySetoptWrapper(curl_handle, CURLOPT_POST, 1);
58 curlEasySetoptWrapper(curl_handle, CURLOPT_POSTFIELDSIZE, ref_content_.size());
59 curlEasySetoptWrapper(curl_handle, CURLOPT_COPYPOSTFIELDS, ref_content_.c_str());
60 curlEasySetoptWrapper(curl_handle, CURLOPT_VERBOSE, get_curlopt_verbose());
63 bool OSTreeRef::IsValid()
const {
return is_valid; }
65 string OSTreeRef::Url()
const {
return "refs/heads/" + ref_name_; }
69 size_t OSTreeRef::curl_handle_write(
void *buffer,
size_t size,
size_t nmemb,
void *userp) {
70 auto *that =
static_cast<OSTreeRef *
>(userp);
72 that->http_response_.write(
static_cast<const char *
>(buffer),
static_cast<std::streamsize
>(size * nmemb));