|
| 1 | +#include <algorithm> |
| 2 | +#include <fstream> |
1 | 3 | #include <iostream> |
2 | 4 |
|
| 5 | +#include "../utils/git_exception.hpp" |
| 6 | +#include "../wrapper/index_wrapper.hpp" |
| 7 | +#include "../wrapper/object_wrapper.hpp" |
| 8 | +#include "../wrapper/commit_wrapper.hpp" |
| 9 | +#include "../wrapper/remote_wrapper.hpp" |
3 | 10 | #include "../wrapper/repository_wrapper.hpp" |
4 | 11 |
|
5 | 12 | repository_wrapper::~repository_wrapper() |
@@ -29,6 +36,11 @@ repository_wrapper repository_wrapper::clone(std::string_view url, std::string_v |
29 | 36 | return rw; |
30 | 37 | } |
31 | 38 |
|
| 39 | +std::string repository_wrapper::git_path() const |
| 40 | +{ |
| 41 | + return git_repository_path(*this); |
| 42 | +} |
| 43 | + |
32 | 44 | git_repository_state_t repository_wrapper::state() const |
33 | 45 | { |
34 | 46 | return git_repository_state_t(git_repository_state(*this)); |
@@ -271,6 +283,86 @@ void repository_wrapper::reset(const object_wrapper& target, git_reset_t reset_t |
271 | 283 | throw_if_error(git_reset(*this, target, reset_type, &checkout_options)); |
272 | 284 | } |
273 | 285 |
|
| 286 | +size_t repository_wrapper::shallow_depth_from_head() const |
| 287 | +{ |
| 288 | + size_t depth = 0; |
| 289 | + if (!this->is_shallow()) |
| 290 | + { |
| 291 | + return depth; |
| 292 | + } |
| 293 | + |
| 294 | + std::string git_path = this->git_path(); |
| 295 | + std::string shallow_path = git_path + "shallow"; |
| 296 | + |
| 297 | + std::vector<git_oid> boundaries_list; |
| 298 | + std::ifstream f(shallow_path); |
| 299 | + std::string line; |
| 300 | + while (std::getline(f, line)) |
| 301 | + { |
| 302 | + if (!line.empty()) |
| 303 | + { |
| 304 | + git_oid commit_oid; |
| 305 | + git_oid_fromstrp(&commit_oid, line.c_str()); |
| 306 | + boundaries_list.push_back(commit_oid); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + if (boundaries_list.size()==0) |
| 311 | + { |
| 312 | + return depth; |
| 313 | + } |
| 314 | + |
| 315 | + commit_wrapper head_commit = this->find_commit("HEAD"); |
| 316 | + commit_list_wrapper commits_list = head_commit.get_parents_list(); |
| 317 | + std::vector<size_t> depth_list(commits_list.size(), 0); |
| 318 | + std::vector<size_t> final_depths(boundaries_list.size(), 0); |
| 319 | + size_t has_parent = commits_list.size() > 0; |
| 320 | + while (has_parent) |
| 321 | + { |
| 322 | + has_parent = 0; |
| 323 | + std::vector<commit_wrapper> temp_commits_list; |
| 324 | + std::vector<size_t> temp_depth_list; |
| 325 | + commit_list_wrapper parent_list({}); |
| 326 | + std::vector<size_t> has_parent_list; |
| 327 | + |
| 328 | + for (size_t i = 0; commits_list.size(); i++) |
| 329 | + { |
| 330 | + const commit_wrapper& commit = commits_list[i]; |
| 331 | + size_t depth = depth_list[i]; |
| 332 | + const git_oid& oid = commit.oid(); |
| 333 | + bool is_boundary = std::find_if(boundaries_list.cbegin(), boundaries_list.cend(), [oid](const git_oid& val) {return git_oid_equal(&oid, &val);}) != boundaries_list.cend(); |
| 334 | + if (is_boundary) |
| 335 | + { |
| 336 | + final_depths.push_back(depth + 1); |
| 337 | + } |
| 338 | + else |
| 339 | + { |
| 340 | + parent_list = commit.get_parents_list(); |
| 341 | + if (parent_list.size() > 0) |
| 342 | + { |
| 343 | + has_parent_list.push_back(1); |
| 344 | + for (size_t j = 0; parent_list.size(); j++) |
| 345 | + { |
| 346 | + const commit_wrapper& c = parent_list[j]; |
| 347 | + temp_commits_list.push_back(std::move(const_cast<commit_wrapper&>(c))); |
| 348 | + temp_depth_list.push_back(depth + 1); |
| 349 | + } |
| 350 | + } |
| 351 | + else |
| 352 | + { |
| 353 | + has_parent_list.push_back(0); |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + depth_list = temp_depth_list; |
| 358 | + commits_list = commit_list_wrapper(std::move(temp_commits_list)); |
| 359 | + has_parent = *std::max_element(has_parent_list.begin(), has_parent_list.end()); |
| 360 | + } |
| 361 | + |
| 362 | + depth = *std::max_element(final_depths.begin(), final_depths.end()); |
| 363 | + return depth; |
| 364 | +} |
| 365 | + |
274 | 366 | // Trees |
275 | 367 |
|
276 | 368 | void repository_wrapper::checkout_tree(const object_wrapper& target, const git_checkout_options opts) |
|
0 commit comments