Clean up old remotes

This commit is contained in:
Patrick Lühne 2019-03-02 03:44:39 +01:00
parent 0b29ffc410
commit 3006d1b571
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -1,4 +1,4 @@
use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Oid, Progress, PushOptions, RemoteCallbacks, Repository}; use git2::{Cred, Error, FetchOptions, Index, Progress, PushOptions, RemoteCallbacks, Repository};
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use log::{info, trace}; use log::{info, trace};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -51,8 +51,8 @@ impl BenchmarkRepository
{ {
match status match status
{ {
None => trace!("Reference “{}” pushed successfully to remote “origin”", reference), None => trace!("reference “{}” pushed successfully to remote “origin”", reference),
Some(error) => panic!("Couldnt push reference “{}” to remote “origin”: {}", reference, error), Some(error) => panic!("couldnt push reference “{}” to remote “origin”: {}", reference, error),
}; };
Ok(()) Ok(())
@ -63,10 +63,10 @@ impl BenchmarkRepository
if let Err(error) = self.repository.find_remote("origin") if let Err(error) = self.repository.find_remote("origin")
.or_else(|_| self.repository.remote("origin", remote_url)) .or_else(|_| self.repository.remote("origin", remote_url))
{ {
panic!("Could not reset remote “origin”: {}", error); panic!("could not reset remote “origin”: {}", error);
} }
info!("Reset origin to “{}”", remote_url); info!("reset origin to “{}”", remote_url);
self self
} }
@ -115,6 +115,39 @@ impl BenchmarkRepository
self self
} }
fn clean_up_remotes(&self) -> &Self
{
let remotes = match self.repository.remotes()
{
Ok(remotes) => remotes,
Err(error) => panic!("could not enumerate remotes: {}", error),
};
for remote in remotes.iter()
{
let remote = match remote
{
Some(remote) => remote,
None => panic!("remote name not in UTF-8"),
};
if remote == "origin"
{
continue;
}
match self.repository.remote_delete(remote)
{
Ok(()) => (),
Err(error) => panic!("could not clean up remote “{}”: {}", remote, error),
};
}
trace!("cleaned up remotes");
self
}
fn init(base_path: &Path) -> Repository fn init(base_path: &Path) -> Repository
{ {
let repository_path = base_path.join("upstream"); let repository_path = base_path.join("upstream");
@ -125,7 +158,7 @@ impl BenchmarkRepository
Err(error) => panic!("failed to initialize Git repository in “{}”: {}", repository_path.display(), error), Err(error) => panic!("failed to initialize Git repository in “{}”: {}", repository_path.display(), error),
}; };
info!("Initialized Git repository in “{}”", repository_path.display()); info!("initialized Git repository in “{}”", repository_path.display());
repository repository
} }
@ -155,7 +188,8 @@ impl BenchmarkRepository
benchmark_repository benchmark_repository
.reset_origin(remote_url) .reset_origin(remote_url)
.fetch_branch("origin", "config") .fetch_branch("origin", "config")
.fetch_branch("origin", "results"); .fetch_branch("origin", "results")
.clean_up_remotes();
benchmark_repository benchmark_repository
} }
@ -415,7 +449,7 @@ impl BenchmarkRepository
match self.repository.remote(&remote_name, &format!("file://{}", std::fs::canonicalize(&result_repository_path).unwrap().display())) match self.repository.remote(&remote_name, &format!("file://{}", std::fs::canonicalize(&result_repository_path).unwrap().display()))
{ {
Ok(_) => (), Ok(_) => (),
Err(error) => panic!("cannot create remote: {}", error), Err(error) => panic!("could not create remote: {}", error),
}; };
info!("initialized result Git repository for job {} in “{}”", id, result_repository_path.display()); info!("initialized result Git repository for job {} in “{}”", id, result_repository_path.display());
@ -478,7 +512,7 @@ impl BenchmarkRepository
let job_tree = match job_commit.tree() let job_tree = match job_commit.tree()
{ {
Ok(value) => value, Ok(value) => value,
Err(error) => panic!("cannot read results of job {}: {}", job_id, error), Err(error) => panic!("could not read results of job {}: {}", job_id, error),
}; };
let mut upstream_index = match Index::new() let mut upstream_index = match Index::new()