Skip to content

Commit

Permalink
Now it's possible to update packed refs using the shared code (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 1, 2022
1 parent e8de0ef commit 78222c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
29 changes: 19 additions & 10 deletions git-features/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod reload_on_demand {
pub fn assure_uptodate<E>(
state: &ReloadIfChangedStorage<T>,
mut current_modification_time: impl FnMut() -> Option<std::time::SystemTime>,
open: impl FnOnce() -> Result<T, E>,
open: impl FnOnce() -> Result<Option<T>, E>,
) -> Result<Option<OwnShared<ReloadIfChanged<T>>>, E> {
let state_opt_lock = get_ref(state);
let recent_modification = current_modification_time();
Expand All @@ -128,11 +128,18 @@ mod reload_on_demand {
// in the common case, we check again and do what we do only if we are
// still in the same situation, writers pile up.
match (&mut *state, current_modification_time()) {
(Some(state), Some(modified_time)) if state.modified < modified_time => {
*state = OwnShared::new(ReloadIfChanged {
value: open()?,
modified: modified_time,
});
(Some(state_opt), Some(modified_time)) if state_opt.modified < modified_time => {
match open()? {
Some(value) => {
*state_opt = OwnShared::new(ReloadIfChanged {
value,
modified: modified_time,
});
}
None => {
*state = None;
}
}
}
_ => {}
}
Expand All @@ -148,10 +155,12 @@ mod reload_on_demand {
let mut state = get_mut(state);
// Still in the same situation? If so, load the buffer.
if let (None, Some(modified_time)) = (&*state, current_modification_time()) {
*state = Some(OwnShared::new(ReloadIfChanged {
value: open()?,
modified: modified_time,
}));
*state = open()?.map(|value| {
OwnShared::new(ReloadIfChanged {
value,
modified: modified_time,
})
});
}
(*state).clone()
}
Expand Down
10 changes: 10 additions & 0 deletions git-ref/src/store/file/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ pub(crate) mod modifiable {
.and_then(|packed| Ok(Some(modified).zip(packed)))
})
}
pub(crate) fn assure_packed_refs_uptodate2(
&self,
) -> Result<Option<OwnShared<git_features::fs::ReloadIfChanged<packed::Buffer>>>, packed::buffer::open::Error>
{
git_features::fs::ReloadIfChanged::assure_uptodate(
&self.packed2,
|| self.packed_refs_path().metadata().and_then(|m| m.modified()).ok(),
|| self.open_packed_buffer(),
)
}

/// Always reload the internally cached packed buffer from disk. This can be necessary if the caller knows something changed
/// but fears the change is not picked up due to lack of precision in fstat mtime calls.
Expand Down

0 comments on commit 78222c2

Please sign in to comment.