From 6802c85597be85d68f87c123ad3d6bd65d35d9b9 Mon Sep 17 00:00:00 2001 From: zeerooth Date: Tue, 12 Nov 2024 22:16:56 +0100 Subject: [PATCH] fix mixed up tag names while history keeping --- yamabiko/src/lib.rs | 9 ++++--- yamabiko/src/replica.rs | 55 +++++++++++++++++++++++++++++++++++++++-- yamabiko/src/test.rs | 5 +++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/yamabiko/src/lib.rs b/yamabiko/src/lib.rs index 5766202..eb7d6b1 100644 --- a/yamabiko/src/lib.rs +++ b/yamabiko/src/lib.rs @@ -527,7 +527,7 @@ impl Collection { } } - fn prepare_remote_push_tags(&self, head: Oid, target: Oid) -> Result<(), git2::Error> { + fn prepare_history_tags(&self, head: Oid, target: Oid) -> Result<(), git2::Error> { let remotes = self.repository.remotes()?; let current_time = Utc::now(); let tag_name = format!( @@ -536,9 +536,12 @@ impl Collection { &target.to_string()[0..7], current_time.timestamp() ); + self.repository + .reference(format!("refs/tags/{}", tag_name).as_str(), head, true, "")?; for remote in remotes.iter().flatten() { let ref_name = format!("refs/history_tags/{}/{}", remote, tag_name); self.repository.reference(&ref_name, head, true, "")?; + debug!("Prepared {} for push", &ref_name); } Ok(()) } @@ -558,7 +561,7 @@ impl Collection { ErrorCode::NotFound => error::RevertError::InvalidOperationTarget, _ => e.into(), })?; - self.prepare_remote_push_tags(current_commit.id(), target_commit.id())?; + self.prepare_history_tags(current_commit.id(), target_commit.id())?; } repo.reset(target_commit.as_object(), git2::ResetType::Soft, None)?; Ok(()) @@ -593,7 +596,7 @@ impl Collection { debug!("Current commit to revert: {:?}", target_commit.as_object()); } if keep_history { - self.prepare_remote_push_tags(current_commit.id(), target_commit.id())?; + self.prepare_history_tags(current_commit.id(), target_commit.id())?; } repo.reset(target_commit.as_object(), git2::ResetType::Soft, None)?; Ok(()) diff --git a/yamabiko/src/replica.rs b/yamabiko/src/replica.rs index d8e94a6..0e5884c 100644 --- a/yamabiko/src/replica.rs +++ b/yamabiko/src/replica.rs @@ -33,7 +33,7 @@ impl Replicator { ) -> Result { let repo = Self::load_or_create_repo(repo_path)?; let remote_name_formatted = format!("_repl_{}", remote_name); - Self::ensure_remote(&repo, remote_name, remote_url)?; + Self::ensure_remote(&repo, &remote_name_formatted, remote_url)?; Ok(Self { repository: repo, remote_name: remote_name_formatted, @@ -96,7 +96,7 @@ impl Replicator { let last_part = ref_name.split('/').last().unwrap(); let tag_name = format!("refs/tags/{}", last_part); self.repository.tag_lightweight( - tag_name.as_str(), + last_part, reference.peel_to_commit()?.as_object(), true, )?; @@ -179,6 +179,7 @@ impl Replicator { debug!("Pushing {} failed: {}", reference, _result); return Ok(()); } + debug!("Pushing {} to {} succeeded", reference, self.remote_name); tags_to_remove.push(reference.to_string()); Ok(()) }); @@ -215,6 +216,8 @@ pub struct RemoteCredentials { #[cfg(test)] mod tests { + use git2::Reference; + use crate::{ replica::{ReplicationMethod, Replicator}, test::{create_db, SampleDbStruct}, @@ -310,4 +313,52 @@ mod tests { let result = repl.replicate(); assert!(result.is_err()); } + + #[test] + fn test_replica_add_and_remove_history_tags() { + let (db, _td) = create_db(); + let (db_backup, _td_backup) = create_db(); + let repl = Replicator::initialize( + _td.path(), + "test", + _td_backup.path().to_str().unwrap(), + ReplicationMethod::All, + None, + ) + .unwrap(); + db.set( + "a", + SampleDbStruct::new(String::from("initial a value")), + OperationTarget::Main, + ) + .unwrap(); + db.set( + "a", + SampleDbStruct::new(String::from("new a value")), + OperationTarget::Main, + ) + .unwrap(); + db.revert_n_commits(1, OperationTarget::Main, true).unwrap(); + repl.replicate().unwrap(); + + let db_tags: Vec = db + .repository() + .references_glob("refs/tags/*") + .unwrap() + .map(|x| x.unwrap()) + .collect(); + assert_eq!(db_tags.len(), 1); + let tag = db_tags.first().unwrap(); + assert!(tag.name().unwrap().starts_with("refs/tags/revert")); + + let db_tags: Vec = db_backup + .repository() + .references_glob("refs/tags/*") + .unwrap() + .map(|x| x.unwrap()) + .collect(); + assert_eq!(db_tags.len(), 1); + let backup_tag = db_tags.first().unwrap(); + assert_eq!(backup_tag.name().unwrap(), tag.name().unwrap()); + } } diff --git a/yamabiko/src/test.rs b/yamabiko/src/test.rs index 6b9d074..8e1f505 100644 --- a/yamabiko/src/test.rs +++ b/yamabiko/src/test.rs @@ -47,7 +47,10 @@ impl ComplexDbStruct { pub fn create_db() -> (Collection, TempDir) { #[cfg(test)] let _ = SimpleLogger::new().init(); - let tmpdir = Builder::new().keep(false).tempdir().unwrap(); + let keep_test_dir = !std::env::var("YAMABIKO_KEEP_TEST_DIR") + .unwrap_or(String::from("")) + .is_empty(); + let tmpdir = Builder::new().keep(keep_test_dir).tempdir().unwrap(); debug!("Using tmpdir {:?} for this test", tmpdir.path().to_str()); ( Collection::initialize(tmpdir.path(), DataFormat::Json).unwrap(),