Skip to content

Commit

Permalink
fix(backend): Unconditionally make a full clone of the IC repo
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic committed Jan 4, 2024
1 parent d68b44b commit c2d1a31
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 63 deletions.
81 changes: 21 additions & 60 deletions rs/ic-management-backend/src/git_ic_repo.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use custom_error::custom_error;
use fs2::FileExt;
use log::info;
use std::fs::{metadata, OpenOptions};
use std::io::Write;
use log::{info, warn};
use std::path::PathBuf;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{collections::HashMap, fs::File};

const DEFAULT_DEPTH: usize = 10000;

custom_error! {IoError
Io {
source: std::io::Error,
Expand All @@ -21,12 +16,11 @@ custom_error! {IoError
pub struct IcRepo {
repo_path: PathBuf,
cache: HashMap<String, Vec<String>>,
lock_file_path: String,
}

impl IcRepo {
// Initialize the IcRepo struct with optional depth for shallow clone
pub fn new(depth: Option<usize>) -> anyhow::Result<Self> {
// Initialize the IcRepo struct, to work with a local clone of the IC repo
pub fn new() -> anyhow::Result<Self> {
let repo_path = match std::env::var("REPO_CACHE_PATH") {
Ok(path) => PathBuf::from(path),
Err(_) => match dirs::cache_dir() {
Expand Down Expand Up @@ -59,7 +53,6 @@ impl IcRepo {
let repo = Self {
repo_path: repo_path.clone(),
cache: HashMap::new(),
lock_file_path,
};

if repo_path.exists() {
Expand All @@ -84,17 +77,11 @@ impl IcRepo {
"Repo {} already exists, fetching new updates",
&repo_path.to_str().unwrap()
);
repo.refetch(depth.unwrap_or(DEFAULT_DEPTH))?;
repo.refetch()?;
} else {
info!("Repo {} does not exist, cloning", &repo_path.to_str().unwrap());
Command::new("git")
.args([
"clone",
"--depth",
&depth.unwrap_or(DEFAULT_DEPTH).to_string(),
"https://github.com/dfinity/ic",
repo_path.to_str().unwrap(),
])
.args(["clone", "https://github.com/dfinity/ic", repo_path.to_str().unwrap()])
.status()?;
}

Expand All @@ -103,38 +90,18 @@ impl IcRepo {
Ok(repo)
}

fn refetch(&self, depth: usize) -> anyhow::Result<()> {
let meta = metadata(&self.lock_file_path)?;
let last_modified = meta.modified()?.duration_since(UNIX_EPOCH)?.as_secs();
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();

if now - last_modified >= 60 {
Command::new("git")
.args([
"-C",
self.repo_path.to_str().unwrap(),
"fetch",
"origin",
"master",
"--depth",
&depth.to_string(),
])
.output()?;

// Touch the file to update its last modified time
OpenOptions::new()
.write(true)
.open(&self.lock_file_path)?
.write_all(b"")?;
}
fn refetch(&self) -> anyhow::Result<()> {
Command::new("git")
.args(["-C", self.repo_path.to_str().unwrap(), "pull", "--force", "origin"])
.output()?;
Ok(())
}

pub fn get_branches_with_commit(&mut self, commit_sha: &str) -> anyhow::Result<Vec<String>> {
let branches = match self.cache.get(commit_sha) {
Some(branches) => branches.clone(),
None => {
self.refetch(DEFAULT_DEPTH)?;
self.refetch()?;
let output = Command::new("git")
.args([
"-C",
Expand All @@ -153,7 +120,14 @@ impl IcRepo {
.map(|s| s.trim().trim_start_matches("refs/remotes/origin/").to_string())
.collect();

self.cache.insert(commit_sha.to_string(), branches.clone());
if branches.is_empty() {
warn!(
"No branches found for commit {} -- do you have a full repo clone?",
commit_sha
)
} else {
self.cache.insert(commit_sha.to_string(), branches.clone());
}
branches
}
};
Expand All @@ -165,24 +139,11 @@ impl IcRepo {
mod tests {
use super::*;

#[test]
fn test_new_repo() {
let ic_repo = IcRepo::new(Some(50));
assert!(ic_repo.is_ok());
}

#[test]
fn test_get_branches_with_commit() {
let mut ic_repo = IcRepo::new(Some(50)).unwrap();
let branches = ic_repo.get_branches_with_commit("some_commit_sha");
assert!(branches.is_ok());
}

#[test]
fn test_get_branches_with_nonexistent_commit() {
let mut ic_repo = IcRepo::new(Some(50)).unwrap();
let branches = ic_repo.get_branches_with_commit("nonexistent_commit_sha");
let mut ic_repo = IcRepo::new().unwrap();
let branches = ic_repo.get_branches_with_commit("80a6745673a28ee53d257b3fe19dcd6b7efa93d1");
assert!(branches.is_ok());
assert!(branches.unwrap().is_empty());
assert!(!branches.unwrap().is_empty());
}
}
9 changes: 6 additions & 3 deletions rs/ic-management-backend/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ impl ReleasesOps for ArtifactReleases {
fn get_active_branches(&self) -> Vec<String> {
const NUM_RELEASE_BRANCHES_TO_KEEP: usize = 2;
if self.releases.is_empty() {
warn!("No {} releases found", self.artifact);
warn!(
"No {} releases found in the registry. THIS MAY BE A BUG!",
self.artifact
);
} else {
info!(
"{} versions: {}",
Expand Down Expand Up @@ -221,7 +224,7 @@ impl RegistryState {
factsdb_guests: Vec::new(),
replica_releases: ArtifactReleases::new(Artifact::Replica),
hostos_releases: ArtifactReleases::new(Artifact::HostOs),
ic_repo: Some(IcRepo::new(None).expect("failed to init ic repo")),
ic_repo: Some(IcRepo::new().expect("failed to init ic repo")),
known_subnets: [
(
"uzr34-akd3s-xrdag-3ql62-ocgoh-ld2ao-tamcv-54e7j-krwgb-2gm4z-oqe",
Expand Down Expand Up @@ -337,11 +340,11 @@ impl RegistryState {
// commit is present
let mut commit_to_release: HashMap<String, Release> = HashMap::new();
blessed_versions.into_iter().for_each(|commit_hash| {
info!("Looking for branches that contain git rev: {}", commit_hash);
let ic_repo = self.ic_repo.as_mut().unwrap();
match ic_repo.get_branches_with_commit(commit_hash) {
// For each commit get a list of branches that have the commit
Ok(branches) => {
info!("Found {} branches for git rev: {}", branches.len(), commit_hash);
debug!("Commit {} ==> branches: {}", commit_hash, branches.join(", "));
for branch in branches.into_iter().sorted() {
match RE.captures(&branch) {
Expand Down

0 comments on commit c2d1a31

Please sign in to comment.