Ring Daemon 16.0.0
Loading...
Searching...
No Matches
gitserver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "gitserver.h"
18
19#include "fileutils.h"
20#include "logger.h"
21#include "gittransport.h"
22#include "manager.h"
23#include <opendht/thread_pool.h>
24#include <dhtnet/multiplexed_socket.h>
25#include <fmt/compile.h>
26
27#include <charconv>
28#include <ctime>
29#include <fstream>
30#include <git2.h>
31#include <iomanip>
32
33using namespace std::string_view_literals;
34constexpr auto FLUSH_PKT = "0000"sv;
35constexpr auto NAK_PKT = "0008NAK\n"sv;
36constexpr auto DONE_CMD = "done\n"sv;
37constexpr auto WANT_CMD = "want"sv;
38constexpr auto HAVE_CMD = "have"sv;
39constexpr auto SERVER_CAPABILITIES
40 = " HEAD\0side-band side-band-64k shallow no-progress include-tag"sv;
41
42namespace jami {
43
45{
46public:
47 Impl(const std::string& accountId,
48 const std::string& repositoryId,
49 const std::string& repository,
50 const std::shared_ptr<dhtnet::ChannelSocket>& socket)
51 : accountId_(accountId)
53 , repository_(repository)
54 , socket_(socket)
55 {
56 JAMI_DEBUG("[Account {}] [Conversation {}] [GitServer {}] created",
59 fmt::ptr(this));
60 // Check at least if repository is correct
62 if (git_repository_open(&repo, repository_.c_str()) != 0) {
63 socket_->shutdown();
64 return;
65 }
67
68 socket_->setOnRecv([this](const uint8_t* buf, std::size_t len) {
69 std::lock_guard lk(destroyMtx_);
70 if (isDestroying_)
71 return len;
72 if (parseOrder(std::string_view((const char*)buf, len)))
73 while(parseOrder());
74 return len;
75 });
76 }
78 stop();
79 JAMI_DEBUG("[Account {}] [Conversation {}] [GitServer {}] destroyed",
82 fmt::ptr(this));
83 }
84 void stop()
85 {
86 std::lock_guard lk(destroyMtx_);
87 if (isDestroying_.exchange(true)) {
88 socket_->setOnRecv({});
89 socket_->shutdown();
90 }
91 }
92 bool parseOrder(std::string_view buf = {});
93
94 void sendReferenceCapabilities(bool sendVersion = false);
95 bool NAK();
96 void ACKCommon();
97 bool ACKFirst();
98 void sendPackData();
99 std::map<std::string, std::string> getParameters(std::string_view pkt_line);
100
101 std::string accountId_ {};
102 std::string repositoryId_ {};
103 std::string repository_ {};
104 std::shared_ptr<dhtnet::ChannelSocket> socket_ {};
105 std::string wantedReference_ {};
106 std::string common_ {};
107 std::vector<std::string> haveRefs_ {};
108 std::string cachedPkt_ {};
109 std::mutex destroyMtx_ {};
110 std::atomic_bool isDestroying_ {false};
112};
113
114bool
116{
117 std::string pkt = std::move(cachedPkt_);
118 if (!buf.empty())
119 pkt += buf;
120
121 // Parse pkt len
122 // Reference: https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt#L51
123 // The first four bytes define the length of the packet and 0000 is a FLUSH pkt
124
125 unsigned int pkt_len = 0;
126 auto [p, ec] = std::from_chars(pkt.data(), pkt.data() + 4, pkt_len, 16);
127 if (ec != std::errc()) {
128 JAMI_ERROR("Unable to parse packet size");
129 }
130 if (pkt_len != pkt.size()) {
131 // Store next packet part
132 if (pkt_len == 0) {
133 // FLUSH_PKT
134 pkt_len = 4;
135 }
136 cachedPkt_ = pkt.substr(pkt_len);
137 }
138
139 auto pack = std::string_view(pkt).substr(4, pkt_len - 4);
140 if (pack == DONE_CMD) {
141 // Reference:
142 // https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L390 Do
143 // not do multi-ack, just send ACK + pack file
144 // In case of no common base, send NAK
145 JAMI_INFO("Peer negotiation is done. Answering to want order");
146 bool sendData;
147 if (common_.empty())
148 sendData = NAK();
149 else
150 sendData = ACKFirst();
151 if (sendData)
152 sendPackData();
153 return !cachedPkt_.empty();
154 } else if (pack.empty()) {
155 if (!haveRefs_.empty()) {
156 // Reference:
157 // https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L390
158 // Do not do multi-ack, just send ACK + pack file In case of no common base ACK
159 ACKCommon();
160 NAK();
161 }
162 return !cachedPkt_.empty();
163 }
164
165 auto lim = pack.find(' ');
166 auto cmd = pack.substr(0, lim);
167 auto dat = (lim < pack.size()) ? pack.substr(lim+1) : std::string_view{};
168 if (cmd == UPLOAD_PACK_CMD) {
169 // Cf: https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L166
170 // References discovery
171 JAMI_INFO("Upload pack command detected.");
172 auto version = 1;
173 auto parameters = getParameters(dat);
174 auto versionIt = parameters.find("version");
175 bool sendVersion = false;
176 if (versionIt != parameters.end()) {
177 try {
178 version = std::stoi(versionIt->second);
179 sendVersion = true;
180 } catch (...) {
181 JAMI_WARNING("Invalid version detected: {}", versionIt->second);
182 }
183 }
184 if (version == 1) {
186 } else {
187 JAMI_ERR("That protocol version is not yet supported (version: %u)", version);
188 }
189 } else if (cmd == WANT_CMD) {
190 // Reference:
191 // https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L229
192 // TODO can have more want
193 wantedReference_ = dat.substr(0, 40);
194 JAMI_INFO("Peer want ref: %s", wantedReference_.c_str());
195 } else if (cmd == HAVE_CMD) {
196 const auto& commit = haveRefs_.emplace_back(dat.substr(0, 40));
197 if (common_.empty()) {
198 // Detect first common commit
199 // Reference:
200 // https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L390
201 // TODO do not open repository every time
203 if (git_repository_open(&repo, repository_.c_str()) != 0) {
204 JAMI_WARN("Unable to open %s", repository_.c_str());
205 return !cachedPkt_.empty();
206 }
209 if (git_oid_fromstr(&commit_id, commit.c_str()) == 0) {
210 // Reference found
211 common_ = commit;
212 }
213 }
214 } else {
215 JAMI_WARNING("Unwanted packet received: {}", pkt);
216 }
217 return !cachedPkt_.empty();
218}
219
220std::string
222 return fmt::format(FMT_COMPILE("{:04x}"), value & 0x0FFFF);
223}
224
225void
227{
228 // Get references
229 // First, get the HEAD reference
230 // https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L166
232 if (git_repository_open(&repo, repository_.c_str()) != 0) {
233 JAMI_WARNING("Unable to open {}", repository_);
234 socket_->shutdown();
235 return;
236 }
238
239 // Answer with the version number
240 // **** When the client initially connects the server will immediately respond
241 // **** with a version number (if "version=1" is sent as an Extra Parameter),
242 std::error_code ec;
243 if (sendVersion) {
244 constexpr auto toSend = "000eversion 1\0"sv;
245 socket_->write(reinterpret_cast<const unsigned char*>(toSend.data()),
246 toSend.size(),
247 ec);
248 if (ec) {
249 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
250 socket_->shutdown();
251 return;
252 }
253 }
254
256 if (git_reference_name_to_id(&commit_id, rep.get(), "HEAD") < 0) {
257 JAMI_ERROR("Unable to get reference for HEAD");
258 socket_->shutdown();
259 return;
260 }
261 std::string currentHead = git_oid_tostr_s(&commit_id);
262
263 // Send references
264 std::ostringstream packet;
265 packet << toGitHex(5 + currentHead.size() + SERVER_CAPABILITIES.size());
266 packet << currentHead << SERVER_CAPABILITIES << "\n";
267
268 // Now, add other references
270 if (git_reference_list(&refs, rep.get()) == 0) {
271 for (std::size_t i = 0; i < refs.count; ++i) {
272 std::string_view ref = refs.strings[i];
273 if (git_reference_name_to_id(&commit_id, rep.get(), ref.data()) < 0) {
274 JAMI_WARNING("Unable to get reference for {}", ref);
275 continue;
276 }
278
279 packet << toGitHex(6 /* size + space + \n */ + currentHead.size() + ref.size());
280 packet << currentHead << " " << ref << "\n";
281 }
282 }
284
285 // And add FLUSH
286 packet << FLUSH_PKT;
287 auto toSend = packet.str();
288 socket_->write(reinterpret_cast<const unsigned char*>(toSend.data()), toSend.size(), ec);
289 if (ec) {
290 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
291 socket_->shutdown();
292 }
293}
294
295void
297{
298 std::error_code ec;
299 // Ack common base
300 if (!common_.empty()) {
301 auto toSend = fmt::format(FMT_COMPILE("{:04x}ACK {} continue\n"),
302 18 + common_.size() /* size + ACK + space * 2 + continue + \n */, common_);
303 socket_->write(reinterpret_cast<const unsigned char*>(toSend.c_str()), toSend.size(), ec);
304 if (ec) {
305 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
306 socket_->shutdown();
307 }
308 }
309}
310
311bool
313{
314 std::error_code ec;
315 // Ack common base
316 if (!common_.empty()) {
317 auto toSend = fmt::format(FMT_COMPILE("{:04x}ACK {}\n"),
318 9 + common_.size() /* size + ACK + space + \n */, common_);
319 socket_->write(reinterpret_cast<const unsigned char*>(toSend.c_str()), toSend.size(), ec);
320 if (ec) {
321 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
322 socket_->shutdown();
323 return false;
324 }
325 }
326 return true;
327}
328
329bool
331{
332 std::error_code ec;
333 // NAK
334 socket_->write(reinterpret_cast<const unsigned char*>(NAK_PKT.data()), NAK_PKT.size(), ec);
335 if (ec) {
336 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
337 socket_->shutdown();
338 return false;
339 }
340 return true;
341}
342
343void
345{
347 if (git_repository_open(&repo_ptr, repository_.c_str()) != 0) {
348 JAMI_WARN("Unable to open %s", repository_.c_str());
349 return;
350 }
352
354 if (git_packbuilder_new(&pb_ptr, repo.get()) != 0) {
355 JAMI_WARNING("Unable to open packbuilder for {}", repository_);
356 return;
357 }
359
360 std::string fetched = wantedReference_;
361 git_oid oid;
362 if (git_oid_fromstr(&oid, fetched.c_str()) < 0) {
363 JAMI_ERROR("Unable to get reference for commit {}", fetched);
364 return;
365 }
366
367 git_revwalk* walker_ptr = nullptr;
368 if (git_revwalk_new(&walker_ptr, repo.get()) < 0 || git_revwalk_push(walker_ptr, &oid) < 0) {
369 if (walker_ptr)
371 return;
372 }
375 // Add first commit
376 std::set<std::string> parents;
377 auto haveCommit = false;
378
379 while (!git_revwalk_next(&oid, walker.get())) {
380 // log until have refs
381 std::string id = git_oid_tostr_s(&oid);
382 haveCommit |= std::find(haveRefs_.begin(), haveRefs_.end(), id) != haveRefs_.end();
383 auto itParents = std::find(parents.begin(), parents.end(), id);
384 if (itParents != parents.end())
385 parents.erase(itParents);
386 if (haveCommit && parents.size() == 0 /* We are sure that all commits are there */)
387 break;
388 if (git_packbuilder_insert_commit(pb.get(), &oid) != 0) {
389 JAMI_WARN("Unable to open insert commit %s for %s",
391 repository_.c_str());
392 return;
393 }
394
395 // Get next commit to pack
397 if (git_commit_lookup(&commit_ptr, repo.get(), &oid) < 0) {
398 JAMI_ERR("Unable to look up current commit");
399 return;
400 }
402 auto parentsCount = git_commit_parentcount(commit.get());
403 for (unsigned int p = 0; p < parentsCount; ++p) {
404 // make sure to explore all branches
405 const git_oid* pid = git_commit_parent_id(commit.get(), p);
406 if (pid)
407 parents.emplace(git_oid_tostr_s(pid));
408 }
409 }
410
411 git_buf data = {};
412 if (git_packbuilder_write_buf(&data, pb.get()) != 0) {
413 JAMI_WARN("Unable to write pack data for %s", repository_.c_str());
414 return;
415 }
416
417 std::size_t sent = 0;
418 std::size_t len = data.size;
419 std::error_code ec;
420 std::vector<uint8_t> toSendData;
421 do {
422 // cf https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L166
423 // In 'side-band-64k' mode it will send up to 65519 data bytes plus 1 control code, for a
424 // total of up to 65520 bytes in a pkt-line.
425 std::size_t pkt_size = std::min(static_cast<std::size_t>(65515), len - sent);
426 std::string toSendHeader = toGitHex(pkt_size + 5);
427 toSendData.clear();
428 toSendData.reserve(pkt_size + 5);
429 toSendData.insert(toSendData.end(), toSendHeader.begin(), toSendHeader.end());
430 toSendData.push_back(0x1);
431 toSendData.insert(toSendData.end(), data.ptr + sent, data.ptr + sent + pkt_size);
432
433 socket_->write(reinterpret_cast<const unsigned char*>(toSendData.data()),
434 toSendData.size(),
435 ec);
436 if (ec) {
437 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
438 git_buf_dispose(&data);
439 return;
440 }
441 sent += pkt_size;
442 } while (sent < len);
443 git_buf_dispose(&data);
444 toSendData = {};
445
446 // And finish by a little FLUSH
447 socket_->write(reinterpret_cast<const uint8_t*>(FLUSH_PKT.data()), FLUSH_PKT.size(), ec);
448 if (ec) {
449 JAMI_WARNING("Unable to send data for {}: {}", repository_, ec.message());
450 }
451
452 // Clear sent data
453 haveRefs_.clear();
454 wantedReference_.clear();
455 common_.clear();
456 if (onFetchedCb_)
457 onFetchedCb_(fetched);
458}
459
460std::map<std::string, std::string>
462{
463 std::map<std::string, std::string> parameters;
464 std::string key, value;
465 auto isKey = true;
466 auto nullChar = 0;
467 for (auto letter: pkt_line) {
468 if (letter == '\0') {
469 // parameters such as host or version are after the first \0
470 if (nullChar != 0 && !key.empty()) {
471 parameters[std::move(key)] = std::move(value);
472 }
473 nullChar += 1;
474 isKey = true;
475 key.clear();
476 value.clear();
477 } else if (letter == '=') {
478 isKey = false;
479 } else if (nullChar != 0) {
480 if (isKey) {
481 key += letter;
482 } else {
483 value += letter;
484 }
485 }
486 }
487 return parameters;
488}
489
490GitServer::GitServer(const std::string& accountId,
491 const std::string& conversationId,
492 const std::shared_ptr<dhtnet::ChannelSocket>& client)
493{
494 auto path = (fileutils::get_data_dir() / accountId / "conversations" / conversationId).string();
495 pimpl_ = std::make_unique<GitServer::Impl>(accountId, conversationId, path, client);
496}
497
499{
500 stop();
501 pimpl_.reset();
502}
503
504void
506{
507 if (!pimpl_)
508 return;
509 pimpl_->onFetchedCb_ = cb;
510}
511
512void
514{
515 pimpl_->stop();
516}
517
518} // namespace jami
Impl(const std::string &accountId, const std::string &repositoryId, const std::string &repository, const std::shared_ptr< dhtnet::ChannelSocket > &socket)
Definition gitserver.cpp:47
void sendReferenceCapabilities(bool sendVersion=false)
std::map< std::string, std::string > getParameters(std::string_view pkt_line)
std::string repositoryId_
std::atomic_bool isDestroying_
bool parseOrder(std::string_view buf={})
std::shared_ptr< dhtnet::ChannelSocket > socket_
onFetchedCb onFetchedCb_
std::vector< std::string > haveRefs_
std::string wantedReference_
std::string repository_
GitServer(const std::string &accountId, const std::string &conversationId, const std::shared_ptr< dhtnet::ChannelSocket > &client)
Serve a conversation to a remote client This client will be able to fetch commits/clone the repositor...
void setOnFetched(const onFetchedCb &cb)
Add a callback which will be triggered when the peer gets the data.
void stop()
Stopping a GitServer will shut the channel down.
std::unique_ptr< git_commit, decltype(&git_commit_free)> GitCommit
std::unique_ptr< git_repository, decltype(&git_repository_free)> GitRepository
std::unique_ptr< git_packbuilder, decltype(&git_packbuilder_free)> GitPackBuilder
std::unique_ptr< git_revwalk, decltype(&git_revwalk_free)> GitRevWalker
constexpr auto HAVE_CMD
Definition gitserver.cpp:38
constexpr auto WANT_CMD
Definition gitserver.cpp:37
constexpr auto SERVER_CAPABILITIES
Definition gitserver.cpp:40
constexpr auto DONE_CMD
Definition gitserver.cpp:36
constexpr auto NAK_PKT
Definition gitserver.cpp:35
constexpr auto FLUSH_PKT
Definition gitserver.cpp:34
constexpr auto UPLOAD_PACK_CMD
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_ERROR(formatstr,...)
Definition logger.h:228
#define JAMI_DEBUG(formatstr,...)
Definition logger.h:226
#define JAMI_WARN(...)
Definition logger.h:217
#define JAMI_WARNING(formatstr,...)
Definition logger.h:227
#define JAMI_INFO(...)
Definition logger.h:215
const std::filesystem::path & get_data_dir()
static constexpr int version
void emitSignal(Args... args)
Definition ring_signal.h:64
std::string toGitHex(size_t value)
std::function< void(const std::string &)> onFetchedCb
Definition gitserver.h:32