1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![allow(missing_docs)]
use std::cell::{Ref, RefMut};

use git_ref::{file, packed};

use crate::{
    easy,
    easy::{borrow, PackCache},
};

impl Clone for easy::State {
    fn clone(&self) -> Self {
        easy::State {
            packed_refs: self.packed_refs.clone(),
            ..Default::default()
        }
    }
}

impl easy::ModifieablePackedRefsBuffer {
    fn assure_packed_refs_uptodate(&mut self, file: &file::Store) -> Result<(), packed::buffer::open::Error> {
        let packed_refs_modified_time = || file.packed_refs_path().metadata().and_then(|m| m.modified()).ok();
        if self.packed_refs.is_none() {
            self.packed_refs = file.packed_buffer()?;
            if self.packed_refs.is_some() {
                self.modified = packed_refs_modified_time();
            }
        } else {
            let recent_modification = packed_refs_modified_time();
            match (&self.modified, recent_modification) {
                (None, None) => {}
                (Some(_), None) => {
                    self.packed_refs = None;
                    self.modified = None
                }
                (Some(cached_time), Some(modified_time)) => {
                    if *cached_time < modified_time {
                        self.packed_refs = file.packed_buffer()?;
                        self.modified = Some(modified_time);
                    }
                }
                (None, Some(modified_time)) => {
                    self.packed_refs = file.packed_buffer()?;
                    self.modified = Some(modified_time);
                }
            }
        }
        Ok(())
    }
}

impl easy::State {
    // TODO: this method should be on the Store itself, as one day there will be reftable support which lacks packed-refs
    pub(crate) fn assure_packed_refs_uptodate(
        &self,
        file: &file::Store,
    ) -> Result<parking_lot::MappedRwLockReadGuard<'_, Option<packed::Buffer>>, packed::buffer::open::Error> {
        let mut packed_refs = self.packed_refs.write();
        packed_refs.assure_packed_refs_uptodate(file)?;
        let packed_refs = parking_lot::RwLockWriteGuard::<'_, _>::downgrade(packed_refs);
        Ok(parking_lot::RwLockReadGuard::<'_, _>::map(packed_refs, |buffer| {
            &buffer.packed_refs
        }))
    }

    #[inline]
    pub(crate) fn try_borrow_mut_pack_cache(&self) -> Result<RefMut<'_, PackCache>, borrow::state::Error> {
        self.pack_cache.try_borrow_mut().map_err(Into::into)
    }

    #[inline]
    pub(crate) fn try_borrow_mut_buf(&self) -> Result<RefMut<'_, Vec<u8>>, borrow::state::Error> {
        self.buf.try_borrow_mut().map_err(Into::into)
    }

    #[inline]
    pub(crate) fn try_borrow_buf(&self) -> Result<Ref<'_, Vec<u8>>, borrow::state::Error> {
        self.buf.try_borrow().map_err(Into::into)
    }
}