2019-03-02 02:11:32 +01:00
|
|
|
|
use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Oid, Progress, PushOptions, RemoteCallbacks, Repository};
|
2019-03-01 14:58:49 +01:00
|
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
|
|
|
|
use log::{info, trace};
|
2019-03-02 01:34:16 +01:00
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2018-09-28 16:19:14 +02:00
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::string::String;
|
|
|
|
|
use std::str;
|
2019-03-02 02:11:32 +01:00
|
|
|
|
//use walkdir::WalkDir;
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
2019-03-02 01:34:16 +01:00
|
|
|
|
pub struct Job
|
|
|
|
|
{
|
|
|
|
|
pub id: u32,
|
|
|
|
|
pub key: String,
|
|
|
|
|
pub result_repository_path: PathBuf,
|
2019-03-02 03:06:39 +01:00
|
|
|
|
remote_name: String,
|
2019-03-02 01:34:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
pub struct BenchmarkRepository
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2019-03-01 15:57:35 +01:00
|
|
|
|
base_path: PathBuf,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
repository: Repository,
|
2019-03-02 01:34:16 +01:00
|
|
|
|
jobs: HashMap<u32, Job>,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
ssh_user: String,
|
2018-09-28 16:51:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TargetPath
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2018-09-28 16:51:46 +02:00
|
|
|
|
pub source: PathBuf,
|
|
|
|
|
pub destination: PathBuf,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BenchmarkRepository
|
|
|
|
|
{
|
|
|
|
|
fn progress_bar_style() -> ProgressStyle
|
|
|
|
|
{
|
|
|
|
|
ProgressStyle::default_bar()
|
|
|
|
|
.template("{bar:74.on_black} {percent:>3} %")
|
|
|
|
|
.progress_chars("█▉▊▋▌▍▎▏ ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn transfer_progress_callback(progress: Progress, progress_bar: &mut ProgressBar) -> bool
|
|
|
|
|
{
|
|
|
|
|
progress_bar.set_length(progress.total_objects() as u64);
|
|
|
|
|
progress_bar.set_position(progress.received_objects() as u64);
|
|
|
|
|
|
|
|
|
|
// continue the transfer
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn push_update_reference_callback(reference: &str, status: Option<&str>) -> Result<(), Error>
|
|
|
|
|
{
|
|
|
|
|
match status
|
|
|
|
|
{
|
|
|
|
|
None => trace!("Reference “{}” pushed successfully to remote “origin”", reference),
|
|
|
|
|
Some(error) => panic!("Couldn’t push reference “{}” to remote “origin”: {}", reference, error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset_origin(&self, remote_url: &str) -> &Self
|
|
|
|
|
{
|
2019-03-01 18:02:52 +01:00
|
|
|
|
if let Err(error) = self.repository.find_remote("origin")
|
|
|
|
|
.or_else(|_| self.repository.remote("origin", remote_url))
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2018-10-04 17:33:03 +02:00
|
|
|
|
panic!("Could not reset remote “origin”: {}", error);
|
|
|
|
|
}
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
|
|
|
|
info!("Reset origin to “{}”", remote_url);
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
fn fetch_branch(&self, remote_name: &str, branch_name: &str) -> &Self
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
let mut progress_bar = ProgressBar::new(0);
|
|
|
|
|
progress_bar.set_style(BenchmarkRepository::progress_bar_style());
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut remote_callbacks = RemoteCallbacks::new();
|
|
|
|
|
remote_callbacks.credentials(
|
|
|
|
|
|_, _, _|
|
|
|
|
|
{
|
2019-03-01 18:02:52 +01:00
|
|
|
|
match Cred::ssh_key_from_agent(&self.ssh_user)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(credentials) => Ok(credentials),
|
2019-03-01 18:02:52 +01:00
|
|
|
|
Err(error) => panic!("could not retrieve key pair for SSH authentication as user “{}”: {}", self.ssh_user, error),
|
2018-09-28 16:19:14 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
remote_callbacks.transfer_progress(
|
|
|
|
|
|progress| BenchmarkRepository::transfer_progress_callback(progress, &mut progress_bar));
|
|
|
|
|
|
|
|
|
|
let mut fetch_options = FetchOptions::new();
|
|
|
|
|
fetch_options.remote_callbacks(remote_callbacks);
|
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
let mut remote = match self.repository.find_remote(remote_name)
|
|
|
|
|
{
|
|
|
|
|
Ok(remote) => remote,
|
|
|
|
|
Err(error) => panic!("could not find remote “{}”", error),
|
|
|
|
|
};
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
info!("updating branch “{}”", branch_name);
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
if let Err(error) = remote.fetch(&[branch_name], Some(&mut fetch_options), None)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2019-03-02 03:06:39 +01:00
|
|
|
|
panic!("failed to fetch branch “{}” from remote “{}”: {}", branch_name, remote_name, error);
|
2018-09-28 16:19:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
|
|
|
|
|
|
trace!("Branch “{}” is up-to-date", branch_name);
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init(base_path: &Path) -> Repository
|
|
|
|
|
{
|
2019-03-01 15:57:35 +01:00
|
|
|
|
let repository_path = base_path.join("upstream");
|
|
|
|
|
|
|
|
|
|
let repository = match Repository::init_bare(&repository_path)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(repository) => repository,
|
2019-03-01 15:57:35 +01:00
|
|
|
|
Err(error) => panic!("failed to initialize Git repository in “{}”: {}", repository_path.display(), error),
|
2018-09-28 16:19:14 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 15:57:35 +01:00
|
|
|
|
info!("Initialized Git repository in “{}”", repository_path.display());
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
|
|
|
|
repository
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:11:32 +01:00
|
|
|
|
pub fn new(remote_url: &str, base_path: PathBuf, ssh_user: &str) -> BenchmarkRepository
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2019-03-01 15:57:35 +01:00
|
|
|
|
let repository = match Repository::open(&base_path)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(repository) =>
|
|
|
|
|
{
|
|
|
|
|
info!("Using existing Git repository");
|
|
|
|
|
|
|
|
|
|
repository
|
|
|
|
|
},
|
2019-03-01 15:57:35 +01:00
|
|
|
|
Err(_) => BenchmarkRepository::init(&base_path),
|
2018-09-28 16:19:14 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let benchmark_repository =
|
|
|
|
|
BenchmarkRepository
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2019-03-01 15:57:35 +01:00
|
|
|
|
base_path: base_path,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
repository: repository,
|
2019-03-02 01:34:16 +01:00
|
|
|
|
jobs: HashMap::new(),
|
2018-09-28 16:19:14 +02:00
|
|
|
|
ssh_user: ssh_user.to_string(),
|
2018-09-28 16:51:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
2018-09-28 16:19:14 +02:00
|
|
|
|
benchmark_repository
|
|
|
|
|
.reset_origin(remote_url)
|
2019-03-02 03:06:39 +01:00
|
|
|
|
.fetch_branch("origin", "config")
|
|
|
|
|
.fetch_branch("origin", "results");
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
|
|
|
|
benchmark_repository
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:11:32 +01:00
|
|
|
|
/*fn create_file_index_entry(&self, object_id: Oid, result_file_path: &Path) -> IndexEntry
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2019-03-02 02:11:32 +01:00
|
|
|
|
IndexEntry
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2019-03-02 02:11:32 +01:00
|
|
|
|
ctime: IndexTime::new(0, 0),
|
|
|
|
|
mtime: IndexTime::new(0, 0),
|
|
|
|
|
dev: 0,
|
|
|
|
|
ino: 0,
|
|
|
|
|
mode: 0o100644,
|
|
|
|
|
uid: 0,
|
|
|
|
|
gid: 0,
|
|
|
|
|
file_size: 0,
|
|
|
|
|
id: object_id,
|
|
|
|
|
flags: 0,
|
|
|
|
|
flags_extended: 0,
|
|
|
|
|
path: result_file_path.to_string_lossy().as_bytes().to_owned(),
|
|
|
|
|
}
|
|
|
|
|
}*/
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
2019-03-02 02:11:32 +01:00
|
|
|
|
fn create_directory_index_entry(&self, object_id: Oid, result_file_path: &Path) -> IndexEntry
|
|
|
|
|
{
|
2018-09-28 16:19:14 +02:00
|
|
|
|
IndexEntry
|
|
|
|
|
{
|
|
|
|
|
ctime: IndexTime::new(0, 0),
|
|
|
|
|
mtime: IndexTime::new(0, 0),
|
|
|
|
|
dev: 0,
|
|
|
|
|
ino: 0,
|
2019-03-02 02:11:32 +01:00
|
|
|
|
mode: 0o100755,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
uid: 0,
|
|
|
|
|
gid: 0,
|
|
|
|
|
file_size: 0,
|
|
|
|
|
id: object_id,
|
|
|
|
|
flags: 0,
|
|
|
|
|
flags_extended: 0,
|
|
|
|
|
path: result_file_path.to_string_lossy().as_bytes().to_owned(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:11:32 +01:00
|
|
|
|
/*fn read_file_as_index_entry(&self, file_path: &Path, result_file_path: &Path) -> IndexEntry
|
|
|
|
|
{
|
|
|
|
|
// create a new blob with the file contents
|
|
|
|
|
let object_id = match self.repository.blob_path(file_path)
|
|
|
|
|
{
|
|
|
|
|
Ok(object_id) => object_id,
|
|
|
|
|
Err(error) => panic!("Could not write blob for “{}”: {}", file_path.display(), error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!("Created object “{}” from “{}”", object_id, file_path.display());
|
|
|
|
|
|
|
|
|
|
self.create_file_index_entry(object_id, result_file_path)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
pub fn commit_directory(&self, directory_path: &TargetPath, branch_name: &str)
|
2018-09-28 16:51:46 +02:00
|
|
|
|
{
|
|
|
|
|
let mut file_paths = vec![];
|
|
|
|
|
|
|
|
|
|
for entry in WalkDir::new(&directory_path.source)
|
|
|
|
|
{
|
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
|
|
|
|
|
|
if entry.path().file_name().unwrap() == ".lock" || entry.file_type().is_dir()
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let relative_path = entry.path().strip_prefix(&directory_path.source).unwrap();
|
|
|
|
|
|
|
|
|
|
trace!("Adding “{}” (from “{}”) to autocommit list", relative_path.display(), entry.path().display());
|
|
|
|
|
|
|
|
|
|
file_paths.push(TargetPath{source: entry.path().to_owned(), destination: directory_path.destination.join(&relative_path)});
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
self.commit_files(&file_paths, branch_name);
|
2018-09-28 16:51:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
pub fn commit_files(&self, file_paths: &[TargetPath], branch_name: &str)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
let tip_reference_name = format!("refs/remotes/origin/{}", branch_name);
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let tip_reference = match self.repository.find_reference(&tip_reference_name)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not find reference “{}”: {}", tip_reference_name, error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let parent_tree = match tip_reference.peel_to_tree()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not peel reference to tree: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut index = match Index::new()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not create index: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match index.read_tree(&parent_tree)
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not read parent tree into index: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for target_path in file_paths
|
|
|
|
|
{
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let index_entry = self.read_file_as_index_entry(&target_path.source, &target_path.destination);
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
|
|
|
|
if let Err(error) = index.add(&index_entry)
|
|
|
|
|
{
|
|
|
|
|
panic!("Could not add index entry for “{}”: {}", target_path.destination.display(), error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let tree_object_id = match index.write_tree_to(&self.repository)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not write index to tree: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let tree = match self.repository.find_tree(tree_object_id)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could obtain tree: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!("Created tree object “{}”", tree_object_id);
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let signature = Signature::now(&self.user_name, &self.user_email).expect("Could not create signature");
|
2018-09-28 16:19:14 +02:00
|
|
|
|
let message = format!("Add files");
|
|
|
|
|
|
|
|
|
|
let parent = match tip_reference.peel_to_commit()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not peel reference: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let commit_id = match self.repository.commit(Some(&tip_reference_name), &signature, &signature, &message, &tree, &[&parent])
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not write commit: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let push_refspec = format!("refs/remotes/origin/{}:refs/heads/{}", branch_name, branch_name);
|
|
|
|
|
|
|
|
|
|
trace!("Created commit “{}”, using refspec “{}”", commit_id, push_refspec);
|
|
|
|
|
|
|
|
|
|
let mut remote_callbacks = RemoteCallbacks::new();
|
|
|
|
|
remote_callbacks.credentials(
|
|
|
|
|
|_, _, _|
|
|
|
|
|
{
|
2019-03-01 18:02:52 +01:00
|
|
|
|
match Cred::ssh_key_from_agent(&self.ssh_user)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => Ok(value),
|
2019-03-01 18:02:52 +01:00
|
|
|
|
Err(error) => panic!("could not retrieve key pair for SSH authentication as user “{}”: {}", self.ssh_user, error),
|
2018-09-28 16:19:14 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
remote_callbacks.push_update_reference(
|
|
|
|
|
|reference, status| BenchmarkRepository::push_update_reference_callback(reference, status));
|
|
|
|
|
|
|
|
|
|
let mut push_options = PushOptions::new();
|
|
|
|
|
push_options.remote_callbacks(remote_callbacks);
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let mut remote = self.repository.find_remote("origin").expect("");
|
2018-09-28 16:19:14 +02:00
|
|
|
|
remote.push(&[&push_refspec], Some(&mut push_options)).expect("couldn’t push");
|
2019-03-02 02:11:32 +01:00
|
|
|
|
}*/
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
|
|
|
|
pub fn file_exists(&self, file_path: &Path, branch_name: &str) -> bool
|
|
|
|
|
{
|
|
|
|
|
let tip_reference_name = format!("refs/remotes/origin/{}", branch_name);
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let tip_reference = match self.repository.find_reference(&tip_reference_name)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not find reference “{}”: {}", tip_reference_name, error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let tree = match tip_reference.peel_to_tree()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not peel reference to tree: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let object_id = match tree.get_path(file_path)
|
|
|
|
|
{
|
|
|
|
|
Ok(tree_entry) => tree_entry.id(),
|
2018-10-04 17:33:03 +02:00
|
|
|
|
Err(_) => return false,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
if let Err(_) = self.repository.find_blob(object_id)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
2018-10-04 17:33:03 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-09-28 16:19:14 +02:00
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn read_file(&self, file_path: &Path, branch_name: &str) -> Option<String>
|
|
|
|
|
{
|
|
|
|
|
let tip_reference_name = format!("refs/remotes/origin/{}", branch_name);
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let tip_reference = match self.repository.find_reference(&tip_reference_name)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not find reference “{}”: {}", tip_reference_name, error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let tree = match tip_reference.peel_to_tree()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("Could not peel reference to tree: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let object_id = match tree.get_path(file_path)
|
|
|
|
|
{
|
|
|
|
|
Ok(tree_entry) => tree_entry.id(),
|
2018-10-04 17:33:03 +02:00
|
|
|
|
Err(_) => return None,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-01 18:02:52 +01:00
|
|
|
|
let blob = match self.repository.find_blob(object_id)
|
2018-09-28 16:19:14 +02:00
|
|
|
|
{
|
|
|
|
|
Ok(blob) => blob,
|
2018-10-04 17:33:03 +02:00
|
|
|
|
Err(_) => return None,
|
2018-09-28 16:19:14 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let content = match std::str::from_utf8(blob.content())
|
|
|
|
|
{
|
|
|
|
|
Ok(content) => content,
|
|
|
|
|
Err(error) => panic!("Could not interpret file “{}” as UTF-8: {}", file_path.display(), error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some(content.to_owned())
|
|
|
|
|
}
|
2018-10-12 17:37:08 +02:00
|
|
|
|
|
2019-03-02 01:34:16 +01:00
|
|
|
|
pub fn create_job(&mut self, key: String) -> &Job
|
2019-03-02 01:02:21 +01:00
|
|
|
|
{
|
2019-03-02 01:34:16 +01:00
|
|
|
|
let id = self.jobs.len() as u32;
|
2019-03-02 03:06:39 +01:00
|
|
|
|
let remote_name = format!("job-{}", id);
|
|
|
|
|
let result_repository_path = self.base_path.join(&remote_name);
|
2018-10-12 17:37:08 +02:00
|
|
|
|
|
2019-03-02 00:53:57 +01:00
|
|
|
|
if result_repository_path.exists()
|
2018-10-12 17:37:08 +02:00
|
|
|
|
{
|
2019-03-02 00:53:57 +01:00
|
|
|
|
match std::fs::remove_dir_all(&result_repository_path)
|
2019-03-01 15:57:35 +01:00
|
|
|
|
{
|
|
|
|
|
Ok(_) => (),
|
2019-03-02 01:34:16 +01:00
|
|
|
|
Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", id, result_repository_path.display(), error),
|
2019-03-01 15:57:35 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
2018-10-12 17:37:08 +02:00
|
|
|
|
|
2019-03-02 00:53:57 +01:00
|
|
|
|
match Repository::init_bare(&result_repository_path)
|
2018-10-12 17:37:08 +02:00
|
|
|
|
{
|
2019-03-02 03:06:39 +01:00
|
|
|
|
Ok(_) => (),
|
2019-03-02 01:34:16 +01:00
|
|
|
|
Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", id, result_repository_path.display(), error),
|
2018-10-12 17:37:08 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-02 03:11:20 +01:00
|
|
|
|
match self.repository.remote(&remote_name, &format!("file://{}", std::fs::canonicalize(&result_repository_path).unwrap().display()))
|
2019-03-02 03:06:39 +01:00
|
|
|
|
{
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(error) => panic!("cannot create remote: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!("initialized result Git repository for job {} in “{}”", id, result_repository_path.display());
|
2018-10-12 17:37:08 +02:00
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
self.jobs.insert(id, Job{id, key, result_repository_path, remote_name});
|
2019-03-02 01:34:16 +01:00
|
|
|
|
self.jobs.get(&id).unwrap()
|
2018-10-12 17:37:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
fn tip_commit(&self, remote_name: &str, branch_name: &str) -> Result<git2::Commit, git2::Error>
|
2018-10-12 17:37:08 +02:00
|
|
|
|
{
|
2019-03-02 03:06:39 +01:00
|
|
|
|
let tip_reference_name = format!("refs/remotes/{}/{}", remote_name, branch_name);
|
|
|
|
|
self.repository.find_reference(&tip_reference_name).and_then(|tip_reference| tip_reference.peel_to_commit())
|
2019-03-02 00:53:57 +01:00
|
|
|
|
}
|
2019-03-01 18:02:52 +01:00
|
|
|
|
|
2019-03-02 00:53:57 +01:00
|
|
|
|
pub fn join(&self)
|
|
|
|
|
{
|
|
|
|
|
let mut active_job_ids = HashSet::new();
|
2019-03-01 18:02:52 +01:00
|
|
|
|
|
2019-03-02 01:34:16 +01:00
|
|
|
|
for (job_id, _) in &self.jobs
|
2019-03-02 00:53:57 +01:00
|
|
|
|
{
|
|
|
|
|
active_job_ids.insert(job_id);
|
|
|
|
|
}
|
2019-03-01 18:02:52 +01:00
|
|
|
|
|
2019-03-02 01:13:06 +01:00
|
|
|
|
while !active_job_ids.is_empty()
|
2019-03-02 00:53:57 +01:00
|
|
|
|
{
|
2019-03-02 01:35:37 +01:00
|
|
|
|
info!("waiting for jobs to finish");
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(2));
|
|
|
|
|
|
2019-03-02 02:11:32 +01:00
|
|
|
|
let mut changed = false;
|
|
|
|
|
|
2019-03-02 01:13:06 +01:00
|
|
|
|
active_job_ids.retain
|
|
|
|
|
(
|
|
|
|
|
|job_id|
|
|
|
|
|
{
|
2019-03-02 01:34:16 +01:00
|
|
|
|
let job = self.jobs.get(job_id).unwrap();
|
2018-10-12 17:37:08 +02:00
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
self.fetch_branch(&job.remote_name, "master");
|
|
|
|
|
|
|
|
|
|
let job_commit = match self.tip_commit(&job.remote_name, "master")
|
2019-03-02 01:13:06 +01:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
// Job is not done yet, so skip it until it is
|
|
|
|
|
Err(_) => return true,
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-02 01:37:12 +01:00
|
|
|
|
info!("job {} finished ({})", job_id, job.key);
|
2019-03-02 01:13:06 +01:00
|
|
|
|
|
2019-03-02 03:06:39 +01:00
|
|
|
|
let remote_commit = match self.tip_commit("origin", "results")
|
2019-03-02 01:13:06 +01:00
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("could not access tip commit of “results” branch: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-02 02:11:32 +01:00
|
|
|
|
let remote_tree = match remote_commit.tree()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("could not access tip commit tree of “results” branch: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let job_tree = match job_commit.tree()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("cannot read results of job {}: {}", job_id, error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut index = match Index::new()
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("could not create index: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match index.read_tree(&remote_tree)
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("could not read tree into index: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let result_path = Path::new(&job.key);
|
|
|
|
|
match index.add(&self.create_directory_index_entry(job_tree.id(), &result_path))
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(error) => panic!("could not add results to index: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let new_tree_object_id = match index.write_tree_to(&self.repository)
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("could not write index to tree: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let new_tree = match self.repository.find_tree(new_tree_object_id)
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
2019-03-02 02:38:26 +01:00
|
|
|
|
Err(error) => panic!("could not obtain tree: {}", error),
|
2019-03-02 02:11:32 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-02 02:39:54 +01:00
|
|
|
|
trace!("created tree object “{}”", new_tree_object_id);
|
2019-03-02 02:11:32 +01:00
|
|
|
|
|
|
|
|
|
let author = job_commit.author();
|
|
|
|
|
let committer = job_commit.committer();
|
|
|
|
|
let message = job_commit.message().unwrap_or("");
|
|
|
|
|
|
|
|
|
|
let tip_reference_name = "refs/remotes/origin/results";
|
|
|
|
|
let commit_id = match self.repository.commit(Some(&tip_reference_name), &author, &committer, &message, &new_tree, &[&remote_commit])
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(error) => panic!("could not write commit: {}", error),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let push_refspec = format!("refs/remotes/origin/results:refs/heads/results");
|
|
|
|
|
|
2019-03-02 02:39:54 +01:00
|
|
|
|
trace!("created commit “{}”, using refspec “{}”", commit_id, push_refspec);
|
2019-03-02 02:11:32 +01:00
|
|
|
|
|
|
|
|
|
let mut remote_callbacks = RemoteCallbacks::new();
|
|
|
|
|
remote_callbacks.credentials(
|
|
|
|
|
|_, _, _|
|
|
|
|
|
{
|
|
|
|
|
match Cred::ssh_key_from_agent(&self.ssh_user)
|
|
|
|
|
{
|
|
|
|
|
Ok(value) => Ok(value),
|
|
|
|
|
Err(error) => panic!("could not retrieve key pair for SSH authentication as user “{}”: {}", self.ssh_user, error),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
remote_callbacks.push_update_reference(
|
|
|
|
|
|reference, status| BenchmarkRepository::push_update_reference_callback(reference, status));
|
|
|
|
|
|
|
|
|
|
let mut push_options = PushOptions::new();
|
|
|
|
|
push_options.remote_callbacks(remote_callbacks);
|
|
|
|
|
|
|
|
|
|
let mut remote = self.repository.find_remote("origin").expect("");
|
|
|
|
|
remote.push(&[&push_refspec], Some(&mut push_options)).expect("couldn’t push");
|
|
|
|
|
|
|
|
|
|
changed = true;
|
|
|
|
|
|
2019-03-02 01:13:06 +01:00
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-10-12 17:37:08 +02:00
|
|
|
|
}
|
2019-03-02 01:38:22 +01:00
|
|
|
|
|
|
|
|
|
info!("all jobs done, all results uploaded");
|
2018-10-12 17:37:08 +02:00
|
|
|
|
}
|
2018-09-28 16:19:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests
|
|
|
|
|
{
|
|
|
|
|
}
|