-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathload_deps.cc
173 lines (149 loc) · 4.71 KB
/
load_deps.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "pkg/load_deps.h"
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "boost/filesystem.hpp"
#include "boost/interprocess/sync/file_lock.hpp"
#include "boost/interprocess/sync/scoped_lock.hpp"
#include "fmt/format.h"
#include "utl/parser/file.h"
#include "utl/to_set.h"
#include "cista/hashing.h"
#include "pkg/dependency_loader.h"
#include "pkg/exec.h"
#include "pkg/git.h"
#include "pkg/read_deps.h"
namespace fs = boost::filesystem;
namespace pkg {
void load_deps(fs::path const& repo, fs::path const& deps_root,
bool const clone_https, bool const force, bool const recursive) {
if (!boost::filesystem::is_directory(deps_root)) {
boost::filesystem::create_directories(deps_root);
}
auto const iterator = [&](dep* d, branch_commit const& bc) {
if (fs::is_directory(d->path_)) {
executor e;
try {
// Fetch if commit is not known.
if (!commit_exists(d, d->commit_) ||
(d->commit_ != bc.commit_ && !commit_exists(d, bc.commit_))) {
fmt::print("{}: fetch\n", d->name());
std::cout << std::flush;
e.exec(d->path_, "git remote set-url {}", d->url_);
e.exec(d->path_, "git fetch origin");
}
// Select latest known commit.
if (d->commit_ != bc.commit_ &&
commit_time(d, d->commit_) < commit_time(d, bc.commit_)) {
d->commit_ = bc.commit_;
d->branch_ = bc.branch_;
}
} catch (std::exception const& ex) {
fmt::print("Rev-Update failed for {}: {}\n", d->name(), ex.what());
e.print_trace();
}
} else {
fmt::print("cloning: {}\n", d->name());
std::cout << std::flush;
std::function<void(int)> clone_retry = [&](int i) {
executor e;
try {
git_clone(e, d, clone_https);
} catch (std::exception const& ex) {
fmt::print("Repo checkout failed for {}: {}\n", d->name(), ex.what());
e.print_trace();
std::cout << "Retry clone\n";
if (i != 0) {
clone_retry(i - 1);
}
}
};
clone_retry(2);
}
};
auto const hash = [&]() {
auto const pred = dep::root(repo);
auto h = cista::BASE_HASH;
for (auto const& d : read_deps(deps_root, &pred, recursive)) {
h = cista::hash_combine(h, cista::hash(d.name()), cista::hash(d.commit_));
}
return h;
}();
auto const name = (fs::absolute(repo) / ".pkg.mutex").generic_string();
{ auto const create_file_if_not_exists = std::ofstream{name}; }
auto lock = boost::interprocess::file_lock{name.c_str()};
if (!lock.try_lock()) {
std::cout << "waiting for lock" << std::endl;
} else {
lock.unlock();
}
auto const guard = boost::interprocess::scoped_lock{lock};
try {
auto f = std::fstream{};
f.exceptions(std::ios_base::failbit | std::ios_base::badbit);
f.open((repo / ".pkg.lock").generic_string().c_str(),
std::ios_base::in | std::ios_base::out);
auto lock_hash = cista::hash_t{};
f >> lock_hash;
auto const check_lock_file = [&]() {
auto dep = std::string{};
auto commit = std::string{};
while (!f.eof() && f.peek() != EOF && f >> dep >> commit) {
std::string line;
std::getline(f, line);
if (get_commit(deps_root / dep) != commit) {
return false;
}
}
return true;
};
if (lock_hash == hash && check_lock_file()) {
return;
}
} catch (std::exception const& e) {
}
dependency_loader l{deps_root};
auto repeat = false;
do {
repeat = false;
l.retrieve(repo, iterator, recursive);
for (auto const& d : l.get_all()) {
if (d->url_ == ROOT || d->commit_ == get_commit(d->path_)) {
continue;
}
executor ex;
try {
git_attach(ex, d, force);
fmt::print("{}: checkout {}\n", d->name(), git_shorten(d, d->commit_));
repeat = true;
} catch (std::exception const& e) {
fmt::print("Checkout failed for {}: {}\n", d->name(), e.what());
ex.print_trace();
}
}
} while (repeat);
{
std::ofstream of{(deps_root / "CMakeLists.txt").generic_string().c_str()};
of << "cmake_minimum_required(VERSION 3.10)\n"
<< "project(" + deps_root.string() << ")\n\n";
for (auto const& v : l.sorted()) {
if (v->url_ == ROOT) {
continue;
}
of << "add_subdirectory(" << v->name() << " EXCLUDE_FROM_ALL)\n";
}
}
{
std::ofstream of{(repo / ".pkg.lock").generic_string().c_str()};
of << hash << "\n";
for (auto const& v : l.sorted()) {
if (v->url_ == ROOT) {
continue;
}
of << v->name() << " " << v->commit_ << "\n";
}
}
}
} // namespace pkg