Refactoring to use remotes and not single repositories

This commit is contained in:
Patrick Lühne 2019-03-02 03:06:39 +01:00
parent 4e3efb6007
commit a7875ad363
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
1 changed files with 32 additions and 28 deletions

View File

@ -12,6 +12,7 @@ pub struct Job
pub id: u32,
pub key: String,
pub result_repository_path: PathBuf,
remote_name: String,
}
pub struct BenchmarkRepository
@ -70,7 +71,7 @@ impl BenchmarkRepository
self
}
fn fetch_branch(&self, branch_name: &str) -> &Self
fn fetch_branch(&self, remote_name: &str, branch_name: &str) -> &Self
{
let mut progress_bar = ProgressBar::new(0);
progress_bar.set_style(BenchmarkRepository::progress_bar_style());
@ -93,13 +94,17 @@ impl BenchmarkRepository
let mut fetch_options = FetchOptions::new();
fetch_options.remote_callbacks(remote_callbacks);
let mut origin = self.repository.find_remote("origin").expect("could not find remote “origin”");
info!("Updating branch “{}”", branch_name);
if let Err(error) = origin.fetch(&[branch_name], Some(&mut fetch_options), None)
let mut remote = match self.repository.find_remote(remote_name)
{
panic!("failed to fetch branch “{}” from remote “origin”: {}", branch_name, error);
Ok(remote) => remote,
Err(error) => panic!("could not find remote “{}", error),
};
info!("updating branch “{}”", branch_name);
if let Err(error) = remote.fetch(&[branch_name], Some(&mut fetch_options), None)
{
panic!("failed to fetch branch “{}” from remote “{}”: {}", branch_name, remote_name, error);
}
}
@ -149,8 +154,8 @@ impl BenchmarkRepository
benchmark_repository
.reset_origin(remote_url)
.fetch_branch("config")
.fetch_branch("results");
.fetch_branch("origin", "config")
.fetch_branch("origin", "results");
benchmark_repository
}
@ -389,7 +394,8 @@ impl BenchmarkRepository
pub fn create_job(&mut self, key: String) -> &Job
{
let id = self.jobs.len() as u32;
let result_repository_path = self.base_path.join(format!("job-{}", id));
let remote_name = format!("job-{}", id);
let result_repository_path = self.base_path.join(&remote_name);
if result_repository_path.exists()
{
@ -402,25 +408,26 @@ impl BenchmarkRepository
match Repository::init_bare(&result_repository_path)
{
Ok(repository) => repository,
Ok(_) => (),
Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", id, result_repository_path.display(), error),
};
info!("Initialized result Git repository for job {} in “{}”", id, result_repository_path.display());
match self.repository.remote(&remote_name, &format!("../{}", remote_name))
{
Ok(_) => (),
Err(error) => panic!("cannot create remote: {}", error),
};
self.jobs.insert(id, Job{id, key, result_repository_path});
info!("initialized result Git repository for job {} in “{}”", id, result_repository_path.display());
self.jobs.insert(id, Job{id, key, result_repository_path, remote_name});
self.jobs.get(&id).unwrap()
}
fn tip_commit<'repository>(repository: &'repository git2::Repository, from_remote: bool, branch_name: &str) -> Result<git2::Commit<'repository>, git2::Error>
fn tip_commit(&self, remote_name: &str, branch_name: &str) -> Result<git2::Commit, git2::Error>
{
let tip_reference_name = match from_remote
{
true => format!("refs/remotes/origin/{}", branch_name),
false => format!("refs/heads/{}", branch_name),
};
repository.find_reference(&tip_reference_name).and_then(|tip_reference| tip_reference.peel_to_commit())
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())
}
pub fn join(&self)
@ -444,13 +451,10 @@ impl BenchmarkRepository
|job_id|
{
let job = self.jobs.get(job_id).unwrap();
let job_repository = match Repository::open(&job.result_repository_path)
{
Ok(value) => value,
Err(error) => panic!("cannot access result repository for job {}: {}", job_id, error),
};
let job_commit = match Self::tip_commit(&job_repository, false, "master")
self.fetch_branch(&job.remote_name, "master");
let job_commit = match self.tip_commit(&job.remote_name, "master")
{
Ok(value) => value,
// Job is not done yet, so skip it until it is
@ -459,7 +463,7 @@ impl BenchmarkRepository
info!("job {} finished ({})", job_id, job.key);
let remote_commit = match Self::tip_commit(&self.repository, true, "results")
let remote_commit = match self.tip_commit("origin", "results")
{
Ok(value) => value,
Err(error) => panic!("could not access tip commit of “results” branch: {}", error),