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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
use crate::clone::{self, GitClonable};
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::Error;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use tracing::info;
pub struct CachedCloner {
root: PathBuf,
}
pub fn cached_cloner(path: &Path) -> CachedCloner {
CachedCloner {
root: path.to_path_buf(),
}
}
pub struct CachedProject {
root: PathBuf,
clone_url: String,
}
pub struct CachedProjectCo {
root: PathBuf,
id: String,
clone_url: String,
local_reference: PathBuf,
}
impl CachedCloner {
pub fn project(&self, name: &str, clone_url: String) -> CachedProject {
// <root>/repo/<hash>/clone
// <root>/repo/<hash>/clone.lock
// <root>/repo/<hash>/<type>/<id>
// <root>/repo/<hash>/<type>/<id>.lock
let mut new_root = self.root.clone();
new_root.push("repo");
new_root.push(format!("{:x}", md5::compute(name)));
CachedProject {
root: new_root,
clone_url,
}
}
}
impl CachedProject {
pub fn clone_for(&self, use_category: String, id: String) -> Result<CachedProjectCo, Error> {
self.prefetch_cache()?;
let mut new_root = self.root.clone();
new_root.push(use_category);
Ok(CachedProjectCo {
root: new_root,
id,
clone_url: self.clone_from(),
local_reference: self.clone_to(),
})
}
fn prefetch_cache(&self) -> Result<PathBuf, Error> {
fs::create_dir_all(&self.root)?;
self.clone_repo()?;
self.fetch_repo()?;
Ok(self.clone_to())
}
}
impl CachedProjectCo {
pub fn checkout_origin_ref(&self, git_ref: &OsStr) -> Result<String, Error> {
let mut pref = OsString::from("origin/");
pref.push(git_ref);
self.checkout_ref(&pref)
}
pub fn checkout_ref(&self, git_ref: &OsStr) -> Result<String, Error> {
fs::create_dir_all(&self.root)?;
self.clone_repo()?;
self.fetch_repo()?;
self.clean()?;
self.checkout(git_ref)?;
// let build_dir = self.build_dir();
let canonicalized = fs::canonicalize(self.clone_to()).unwrap();
Ok(canonicalized.to_str().unwrap().to_string())
}
pub fn fetch_pr(&self, pr_id: u64) -> Result<(), Error> {
let mut lock = self.lock()?;
info!("Fetching PR #{}", pr_id);
let result = Command::new("git")
.arg("fetch")
.arg("origin")
.arg(format!("+refs/pull/{pr_id}/head:pr"))
.current_dir(self.clone_to())
.stdout(Stdio::null())
.status()?;
lock.unlock();
if result.success() {
Ok(())
} else {
Err(Error::other("Failed to fetch PR"))
}
}
pub fn commit_exists(&self, commit: &OsStr) -> bool {
let mut lock = self.lock().expect("Failed to lock");
info!("Checking if commit {:?} exists", commit);
let result = Command::new("git")
.arg("--no-pager")
.arg("show")
.arg(commit)
.current_dir(self.clone_to())
.stdout(Stdio::null())
.status()
.expect("git show <commit> failed");
lock.unlock();
result.success()
}
pub fn merge_commit(&self, commit: &OsStr) -> Result<(), Error> {
let mut lock = self.lock()?;
info!("Merging commit {:?}", commit);
let result = Command::new("git")
.arg("merge")
.arg("--no-gpg-sign")
.arg("-m")
.arg("Automatic merge for GrahamCOfBorg")
.arg(commit)
.current_dir(self.clone_to())
.stdout(Stdio::null())
.status()?;
lock.unlock();
if result.success() {
Ok(())
} else {
Err(Error::other("Failed to merge"))
}
}
pub fn commit_messages_from_head(&self, commit: &str) -> Result<Vec<String>, Error> {
let mut lock = self.lock()?;
let result = Command::new("git")
.arg("log")
.arg("--format=format:%s")
.arg(format!("HEAD..{commit}"))
.current_dir(self.clone_to())
.output()?;
lock.unlock();
if result.status.success() {
Ok(String::from_utf8_lossy(&result.stdout)
.lines()
.map(|l| l.to_owned())
.collect())
} else {
Err(Error::other(
String::from_utf8_lossy(&result.stderr).to_lowercase(),
))
}
}
pub fn files_changed_from_head(&self, commit: &str) -> Result<Vec<String>, Error> {
let mut lock = self.lock()?;
let result = Command::new("git")
.arg("diff")
.arg("--name-only")
.arg(format!("HEAD...{commit}"))
.current_dir(self.clone_to())
.output()?;
lock.unlock();
if result.status.success() {
Ok(String::from_utf8_lossy(&result.stdout)
.lines()
.map(|l| l.to_owned())
.collect())
} else {
Err(Error::other(
String::from_utf8_lossy(&result.stderr).to_lowercase(),
))
}
}
}
impl clone::GitClonable for CachedProjectCo {
fn clone_from(&self) -> String {
self.clone_url.clone()
}
fn clone_to(&self) -> PathBuf {
let mut clone_path = self.root.clone();
clone_path.push(&self.id);
clone_path
}
fn lock_path(&self) -> PathBuf {
let mut lock_path = self.root.clone();
lock_path.push(format!("{}.lock", self.id));
lock_path
}
fn extra_clone_args(&self) -> Vec<&OsStr> {
let local_ref = self.local_reference.as_ref();
vec![
OsStr::new("--shared"),
OsStr::new("--reference-if-able"),
local_ref,
]
}
}
impl clone::GitClonable for CachedProject {
fn clone_from(&self) -> String {
self.clone_url.clone()
}
fn clone_to(&self) -> PathBuf {
let mut clone_path = self.root.clone();
clone_path.push("clone");
clone_path
}
fn lock_path(&self) -> PathBuf {
let mut clone_path = self.root.clone();
clone_path.push("clone.lock");
clone_path
}
fn extra_clone_args(&self) -> Vec<&OsStr> {
vec![OsStr::new("--bare")]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_scratch::TestScratch;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
fn tpath(component: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(component)
}
fn make_pr_repo(bare: &Path, co: &Path) -> String {
let output = Command::new("bash")
.current_dir(tpath("./test-srcs"))
.arg("./make-pr.sh")
.arg(bare)
.arg(co)
.stdout(Stdio::piped())
.output()
.expect("building the test PR failed");
let stderr =
String::from_utf8(output.stderr).unwrap_or_else(|err| format!("warning: {err}"));
println!("{stderr}");
let hash = String::from_utf8(output.stdout).expect("Should just be a hash");
hash.trim().to_owned()
}
#[test]
pub fn test_commit_msg_list() {
let workingdir = TestScratch::new_dir("test-test-commit-msg-list");
let bare = TestScratch::new_dir("bare-commit-messages");
let mk_co = TestScratch::new_dir("mk-commit-messages");
let hash = make_pr_repo(&bare.path(), &mk_co.path());
let cloner = cached_cloner(&workingdir.path());
let project = cloner.project("commit-msg-list", bare.string());
let working_co = project
.clone_for("testing-commit-msgs".to_owned(), "123".to_owned())
.expect("clone should work");
working_co
.checkout_origin_ref(OsStr::new("master"))
.unwrap();
let expect: Vec<String> = vec!["check out this cool PR".to_owned()];
assert_eq!(
working_co
.commit_messages_from_head(&hash)
.expect("fetching messages should work",),
expect
);
}
#[test]
pub fn test_files_changed_list() {
let workingdir = TestScratch::new_dir("test-test-files-changed-list");
let bare = TestScratch::new_dir("bare-files-changed");
let mk_co = TestScratch::new_dir("mk-files-changed");
let hash = make_pr_repo(&bare.path(), &mk_co.path());
let cloner = cached_cloner(&workingdir.path());
let project = cloner.project("commit-files-changed-list", bare.string());
let working_co = project
.clone_for("testing-files-changed".to_owned(), "123".to_owned())
.expect("clone should work");
working_co
.checkout_origin_ref(OsStr::new("master"))
.unwrap();
let expect: Vec<String> = vec!["default.nix".to_owned(), "hi another file".to_owned()];
assert_eq!(
working_co
.files_changed_from_head(&hash)
.expect("fetching files changed should work",),
expect
);
}
}
|