History

[ref] refactor

Sebastian Thiel committed 5 years ago

Commit e3791771 parent a8441461 file · +21 −21Browse files

Unified · Split

git-ref/src/store/file/transaction.rs+21−21
@@ -40,7 +40,6 @@ impl std::borrow::BorrowMut<RefEdit> for Edit { · 39 unmodified lines
4040 pub struct Transaction<'s> {
4141 store: &'s file::Store,
4242 packed_transaction: Option<packed::Transaction>,
43− packed: Option<packed::Buffer>,
4443 updates: Option<Vec<Edit>>,
4544 }
4645
@@ -236,8 +235,9 @@ impl<'s> Transaction<'s> { · 189 unmodified lines
236235 )
237236 .map_err(Error::PreprocessingFailed)?;
238237
239− if let Some(packed) = self.store.packed()? {
238+ if self.store.packed_refs_path().is_file() {
240239 let mut edits_for_packed_transaction = Vec::<RefEdit>::new();
240+ let mut needs_packed_refs_lookups = false;
241241 for edit in updates.iter() {
242242 let log_mode = match edit.update.change {
243243 Change::Update {
@@ -257,19 +257,23 @@ impl<'s> Transaction<'s> { · 13 unmodified lines
257257 Change::Delete { .. } => {
258258 edits_for_packed_transaction.push(edit.update.clone());
259259 }
260− _ => {}
260+ _ => {
261+ needs_packed_refs_lookups = true;
262+ }
261263 }
262264 }
263265
264− if !edits_for_packed_transaction.is_empty() {
265− self.packed_transaction = Some(
266− packed
267− .into_transaction(lock_fail_mode)
268− .map_err(Error::PackedTransactionAcquire)?
269− .prepare(edits_for_packed_transaction)?,
270− );
271− } else {
272− self.packed = Some(packed);
266+ // We create a transaction even for empty packed edits to assure nobody else can change
267+ // it while we perform checks against previous values.
268+ if !edits_for_packed_transaction.is_empty() || needs_packed_refs_lookups {
269+ if let Some(packed) = self.store.packed()? {
270+ self.packed_transaction = Some(
271+ packed
272+ .into_transaction(lock_fail_mode)
273+ .map_err(Error::PackedTransactionAcquire)?
274+ .prepare(edits_for_packed_transaction)?,
275+ );
276+ }
273277 }
274278 }
275279
@@ -278,10 +282,7 @@ impl<'s> Transaction<'s> { · 2 unmodified lines
278282 if let Err(err) = Self::lock_ref_and_apply_change(
279283 self.store,
280284 lock_fail_mode,
281− self.packed_transaction
282− .as_ref()
283− .map(|t| t.buffer())
284− .or_else(|| self.packed.as_ref()),
285+ self.packed_transaction.as_ref().map(|t| t.buffer()),
285286 change,
286287 ) {
287288 let err = match err {
@@ -440,10 +441,10 @@ impl<'s> Transaction<'s> { · 152 unmodified lines
440441 }
441442 }
442443 }
443− let packed = match self.packed_transaction {
444− Some(transaction) => Some(transaction.commit().map_err(Error::PackedTransactionCommit)?.1),
445− None => self.packed,
446− };
444+ let packed = self
445+ .packed_transaction
446+ .map(|t| t.commit().map(|r| r.1).map_err(Error::PackedTransactionCommit))
447+ .transpose()?;
447448 Ok((updates.into_iter().map(|edit| edit.update).collect(), packed))
448449 }
449450 None => panic!("BUG: must call prepare before commit"),
@@ -461,7 +462,6 @@ impl file::Store { · 11 unmodified lines
461462 Transaction {
462463 store: self,
463464 packed_transaction: None,
464− packed: None,
465465 updates: None,
466466 }
467467 }