Skip to content

Commit

Permalink
fix mixed up tag names while history keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
zeerooth committed Nov 12, 2024
1 parent a78191b commit 6802c85
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
9 changes: 6 additions & 3 deletions yamabiko/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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(())
}
Expand All @@ -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(())
Expand Down Expand Up @@ -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(())
Expand Down
55 changes: 53 additions & 2 deletions yamabiko/src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Replicator {
) -> Result<Self, error::InitializationError> {
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,
Expand Down Expand Up @@ -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,
)?;
Expand Down Expand Up @@ -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(())
});
Expand Down Expand Up @@ -215,6 +216,8 @@ pub struct RemoteCredentials {

#[cfg(test)]
mod tests {
use git2::Reference;

use crate::{
replica::{ReplicationMethod, Replicator},
test::{create_db, SampleDbStruct},
Expand Down Expand Up @@ -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<Reference> = 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<Reference> = 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());
}
}
5 changes: 4 additions & 1 deletion yamabiko/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 6802c85

Please sign in to comment.