Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Nov 16, 2022
1 parent e86e159 commit b0a231a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 78 deletions.
39 changes: 38 additions & 1 deletion git-ref/tests/file/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ pub(crate) mod prepare_and_commit {
use git_actor::{Sign, Time};
use git_hash::ObjectId;
use git_object::bstr::BString;
use git_ref::file;
use git_ref::transaction::{Change, LogChange, PreviousValue, RefEdit, RefLog};
use git_ref::{file, Target};
use std::convert::TryInto;

fn reflog_lines(store: &file::Store, name: &str) -> crate::Result<Vec<git_ref::log::Line>> {
let mut buf = Vec::new();
Expand Down Expand Up @@ -41,6 +43,41 @@ pub(crate) mod prepare_and_commit {
}
}

fn create_at(name: &str) -> RefEdit {
RefEdit {
change: Change::Update {
log: LogChange::default(),
expected: PreviousValue::MustNotExist,
new: Target::Peeled(git_hash::Kind::Sha1.null()),
},
name: name.try_into().expect("valid"),
deref: false,
}
}

fn create_symbolic_at(name: &str, symbolic_target: &str) -> RefEdit {
RefEdit {
change: Change::Update {
log: LogChange::default(),
expected: PreviousValue::MustNotExist,
new: Target::Symbolic(symbolic_target.try_into().expect("valid target name")),
},
name: name.try_into().expect("valid"),
deref: false,
}
}

fn delete_at(name: &str) -> RefEdit {
RefEdit {
change: Change::Delete {
expected: PreviousValue::Any,
log: RefLog::AndReference,
},
name: name.try_into().expect("valid name"),
deref: false,
}
}

mod create_or_update;

mod delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use git_ref::{
};
use git_testtools::hex_to_id;

use crate::file::transaction::prepare_and_commit::{create_at, create_symbolic_at, delete_at};
use crate::file::{
store_with_packed_refs, store_writable,
transaction::prepare_and_commit::{committer, empty_store, log_line, reflog_lines},
Expand Down Expand Up @@ -73,16 +74,6 @@ fn intermediate_directories_are_removed_on_rollback() -> crate::Result {
for explicit_rollback in [false, true] {
let (dir, store) = empty_store()?;

let create_at = |name: &str| RefEdit {
change: Change::Update {
log: LogChange::default(),
expected: PreviousValue::MustNotExist,
new: Target::Peeled(git_hash::Kind::Sha1.null()),
},
name: name.try_into().expect("valid"),
deref: false,
};

let transaction = store.transaction().prepare(
[create_at("refs/heads/a/b/ref"), create_at("refs/heads/a/c/ref")],
Fail::Immediately,
Expand Down Expand Up @@ -241,19 +232,9 @@ fn reference_with_must_not_exist_constraint_cannot_be_created_if_it_exists_alrea
let head = store.try_find_loose("HEAD")?.expect("head exists already");
let target = head.target;

let res = store.transaction().prepare(
Some(RefEdit {
change: Change::Update {
log: LogChange::default(),
new: Target::Peeled(git_hash::Kind::Sha1.null()),
expected: PreviousValue::MustNotExist,
},
name: "HEAD".try_into()?,
deref: false,
}),
Fail::Immediately,
Fail::Immediately,
);
let res = store
.transaction()
.prepare(Some(create_at("HEAD")), Fail::Immediately, Fail::Immediately);
match res {
Err(transaction::prepare::Error::MustNotExist { full_name, actual, .. }) => {
assert_eq!(full_name, "HEAD");
Expand All @@ -268,55 +249,16 @@ fn reference_with_must_not_exist_constraint_cannot_be_created_if_it_exists_alrea
fn namespaced_updates_or_deletions_are_transparent_and_not_observable() -> crate::Result {
let (_keep, mut store) = empty_store()?;
store.namespace = git_ref::namespace::expand("foo")?.into();
let actual = vec![
delete_at("refs/for/deletion"),
create_symbolic_at("HEAD", "refs/heads/hello"),
];
let edits = store
.transaction()
.prepare(
vec![
RefEdit {
change: Change::Delete {
expected: PreviousValue::Any,
log: RefLog::AndReference,
},
name: "refs/for/deletion".try_into()?,
deref: false,
},
RefEdit {
change: Change::Update {
log: LogChange::default(),
new: Target::Symbolic("refs/heads/hello".try_into()?),
expected: PreviousValue::MustNotExist,
},
name: "HEAD".try_into()?,
deref: false,
},
],
Fail::Immediately,
Fail::Immediately,
)?
.prepare(actual.clone(), Fail::Immediately, Fail::Immediately)?
.commit(committer().to_ref())?;

assert_eq!(
edits,
vec![
RefEdit {
change: Change::Delete {
expected: PreviousValue::Any,
log: RefLog::AndReference,
},
name: "refs/for/deletion".try_into()?,
deref: false,
},
RefEdit {
change: Change::Update {
log: LogChange::default(),
new: Target::Symbolic("refs/heads/hello".try_into()?),
expected: PreviousValue::MustNotExist,
},
name: "HEAD".try_into()?,
deref: false,
}
]
);
assert_eq!(edits, actual);
Ok(())
}

Expand Down Expand Up @@ -425,15 +367,7 @@ fn cancellation_after_preparation_leaves_no_change() -> crate::Result {
);

let tx = tx.prepare(
Some(RefEdit {
change: Change::Update {
log: LogChange::default(),
new: Target::Symbolic("refs/heads/main".try_into().unwrap()),
expected: PreviousValue::MustNotExist,
},
name: "HEAD".try_into()?,
deref: false,
}),
Some(create_symbolic_at("HEAD", "refs/heads/main")),
Fail::Immediately,
Fail::Immediately,
)?;
Expand Down

0 comments on commit b0a231a

Please sign in to comment.