dirstate_map.rs
471 lines
| 14.3 KiB
| application/rls-services+xml
|
RustLexer
|
r42998 | // dirstate_map.rs | ||
// | ||||
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | ||||
// | ||||
// This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | ||||
|
r47871 | use crate::dirstate::parsers::Timestamp; | ||
|
r42998 | use crate::{ | ||
|
r47891 | dirstate::EntryState, | ||
r48310 | dirstate::MTIME_UNSET, | |||
r48300 | dirstate::SIZE_FROM_OTHER_PARENT, | |||
dirstate::SIZE_NON_NORMAL, | ||||
r48310 | dirstate::V1_RANGEMASK, | |||
|
r43108 | pack_dirstate, parse_dirstate, | ||
|
r47879 | utils::hg_path::{HgPath, HgPathBuf}, | ||
|
r48126 | CopyMap, DirsMultiset, DirstateEntry, DirstateError, DirstateParents, | ||
StateMap, | ||||
|
r42998 | }; | ||
|
r46135 | use micro_timer::timed; | ||
|
r44278 | use std::collections::HashSet; | ||
|
r42998 | use std::iter::FromIterator; | ||
use std::ops::Deref; | ||||
#[derive(Default)] | ||||
pub struct DirstateMap { | ||||
state_map: StateMap, | ||||
pub copy_map: CopyMap, | ||||
pub dirs: Option<DirsMultiset>, | ||||
pub all_dirs: Option<DirsMultiset>, | ||||
|
r44775 | non_normal_set: Option<HashSet<HgPathBuf>>, | ||
other_parent_set: Option<HashSet<HgPathBuf>>, | ||||
|
r42998 | } | ||
/// Should only really be used in python interface code, for clarity | ||||
impl Deref for DirstateMap { | ||||
type Target = StateMap; | ||||
fn deref(&self) -> &Self::Target { | ||||
&self.state_map | ||||
} | ||||
} | ||||
|
r43227 | impl FromIterator<(HgPathBuf, DirstateEntry)> for DirstateMap { | ||
|
r46162 | fn from_iter<I: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>( | ||
iter: I, | ||||
) -> Self { | ||||
|
r42998 | Self { | ||
state_map: iter.into_iter().collect(), | ||||
..Self::default() | ||||
} | ||||
} | ||||
} | ||||
impl DirstateMap { | ||||
pub fn new() -> Self { | ||||
Self::default() | ||||
} | ||||
pub fn clear(&mut self) { | ||||
|
r46185 | self.state_map = StateMap::default(); | ||
|
r42998 | self.copy_map.clear(); | ||
|
r44775 | self.non_normal_set = None; | ||
self.other_parent_set = None; | ||||
|
r42998 | } | ||
r48492 | pub fn set_v1_inner(&mut self, filename: &HgPath, entry: DirstateEntry) { | |||
self.state_map.insert(filename.to_owned(), entry); | ||||
} | ||||
|
r42998 | /// Add a tracked file to the dirstate | ||
pub fn add_file( | ||||
&mut self, | ||||
|
r43227 | filename: &HgPath, | ||
|
r42998 | entry: DirstateEntry, | ||
r48310 | // XXX once the dust settle this should probably become an enum | |||
r48314 | added: bool, | |||
r48316 | merged: bool, | |||
r48310 | from_p2: bool, | |||
possibly_dirty: bool, | ||||
|
r48126 | ) -> Result<(), DirstateError> { | ||
|
r48834 | let state; | ||
let size; | ||||
let mtime; | ||||
r48314 | if added { | |||
r48310 | assert!(!possibly_dirty); | |||
assert!(!from_p2); | ||||
|
r48834 | state = EntryState::Added; | ||
size = SIZE_NON_NORMAL; | ||||
mtime = MTIME_UNSET; | ||||
r48316 | } else if merged { | |||
assert!(!possibly_dirty); | ||||
assert!(!from_p2); | ||||
|
r48834 | state = EntryState::Merged; | ||
size = SIZE_FROM_OTHER_PARENT; | ||||
mtime = MTIME_UNSET; | ||||
r48310 | } else if from_p2 { | |||
assert!(!possibly_dirty); | ||||
|
r48834 | state = EntryState::Normal; | ||
size = SIZE_FROM_OTHER_PARENT; | ||||
mtime = MTIME_UNSET; | ||||
r48310 | } else if possibly_dirty { | |||
|
r48834 | state = EntryState::Normal; | ||
size = SIZE_NON_NORMAL; | ||||
mtime = MTIME_UNSET; | ||||
r48310 | } else { | |||
|
r48834 | state = EntryState::Normal; | ||
size = entry.size() & V1_RANGEMASK; | ||||
mtime = entry.mtime() & V1_RANGEMASK; | ||||
r48310 | } | |||
|
r48834 | let mode = entry.mode(); | ||
let entry = DirstateEntry::from_v1_data(state, mode, size, mtime); | ||||
|
r48838 | let old_state = self.get(filename).map(|e| e.state()); | ||
if old_state.is_none() || old_state == Some(EntryState::Removed) { | ||||
|
r42998 | if let Some(ref mut dirs) = self.dirs { | ||
|
r44227 | dirs.add_path(filename)?; | ||
|
r42998 | } | ||
} | ||||
|
r48838 | if old_state.is_none() { | ||
|
r42998 | if let Some(ref mut all_dirs) = self.all_dirs { | ||
|
r44227 | all_dirs.add_path(filename)?; | ||
|
r42998 | } | ||
} | ||||
self.state_map.insert(filename.to_owned(), entry.to_owned()); | ||||
|
r47878 | if entry.is_non_normal() { | ||
|
r44775 | self.get_non_normal_other_parent_entries() | ||
.0 | ||||
.insert(filename.to_owned()); | ||||
|
r42998 | } | ||
|
r47878 | if entry.is_from_other_parent() { | ||
|
r44775 | self.get_non_normal_other_parent_entries() | ||
.1 | ||||
.insert(filename.to_owned()); | ||||
|
r42998 | } | ||
|
r44227 | Ok(()) | ||
|
r42998 | } | ||
/// Mark a file as removed in the dirstate. | ||||
/// | ||||
/// The `size` parameter is used to store sentinel values that indicate | ||||
/// the file's previous state. In the future, we should refactor this | ||||
/// to be more explicit about what that state is. | ||||
pub fn remove_file( | ||||
&mut self, | ||||
|
r43227 | filename: &HgPath, | ||
r48300 | in_merge: bool, | |||
|
r48126 | ) -> Result<(), DirstateError> { | ||
r48300 | let old_entry_opt = self.get(filename); | |||
|
r48838 | let old_state = old_entry_opt.map(|e| e.state()); | ||
r48300 | let mut size = 0; | |||
if in_merge { | ||||
// XXX we should not be able to have 'm' state and 'FROM_P2' if not | ||||
// during a merge. So I (marmoute) am not sure we need the | ||||
// conditionnal at all. Adding double checking this with assert | ||||
// would be nice. | ||||
if let Some(old_entry) = old_entry_opt { | ||||
// backup the previous state | ||||
|
r48834 | if old_entry.state() == EntryState::Merged { | ||
r48300 | size = SIZE_NON_NORMAL; | |||
|
r48834 | } else if old_entry.state() == EntryState::Normal | ||
&& old_entry.size() == SIZE_FROM_OTHER_PARENT | ||||
r48300 | { | |||
// other parent | ||||
size = SIZE_FROM_OTHER_PARENT; | ||||
self.get_non_normal_other_parent_entries() | ||||
.1 | ||||
.insert(filename.to_owned()); | ||||
} | ||||
} | ||||
} | ||||
|
r48838 | if old_state.is_some() && old_state != Some(EntryState::Removed) { | ||
|
r42998 | if let Some(ref mut dirs) = self.dirs { | ||
dirs.delete_path(filename)?; | ||||
} | ||||
} | ||||
|
r48838 | if old_state.is_none() { | ||
|
r42998 | if let Some(ref mut all_dirs) = self.all_dirs { | ||
|
r44315 | all_dirs.add_path(filename)?; | ||
|
r42998 | } | ||
} | ||||
r48300 | if size == 0 { | |||
self.copy_map.remove(filename); | ||||
} | ||||
|
r42998 | |||
|
r48834 | self.state_map | ||
.insert(filename.to_owned(), DirstateEntry::new_removed(size)); | ||||
|
r44775 | self.get_non_normal_other_parent_entries() | ||
.0 | ||||
.insert(filename.to_owned()); | ||||
|
r42998 | Ok(()) | ||
} | ||||
/// Remove a file from the dirstate. | ||||
/// Returns `true` if the file was previously recorded. | ||||
pub fn drop_file( | ||||
&mut self, | ||||
|
r43227 | filename: &HgPath, | ||
|
r48126 | ) -> Result<bool, DirstateError> { | ||
|
r48838 | let old_state = self.get(filename).map(|e| e.state()); | ||
|
r43063 | let exists = self.state_map.remove(filename).is_some(); | ||
|
r42998 | |||
if exists { | ||||
|
r48838 | if old_state != Some(EntryState::Removed) { | ||
|
r42998 | if let Some(ref mut dirs) = self.dirs { | ||
dirs.delete_path(filename)?; | ||||
} | ||||
} | ||||
if let Some(ref mut all_dirs) = self.all_dirs { | ||||
all_dirs.delete_path(filename)?; | ||||
} | ||||
} | ||||
|
r44775 | self.get_non_normal_other_parent_entries() | ||
.0 | ||||
.remove(filename); | ||||
|
r42998 | |||
Ok(exists) | ||||
} | ||||
|
r46162 | pub fn clear_ambiguous_times( | ||
&mut self, | ||||
filenames: Vec<HgPathBuf>, | ||||
now: i32, | ||||
) { | ||||
|
r42998 | for filename in filenames { | ||
|
r46185 | if let Some(entry) = self.state_map.get_mut(&filename) { | ||
|
r48121 | if entry.clear_ambiguous_mtime(now) { | ||
|
r47875 | self.get_non_normal_other_parent_entries() | ||
.0 | ||||
.insert(filename.to_owned()); | ||||
|
r46185 | } | ||
} | ||||
|
r42998 | } | ||
} | ||||
r48492 | pub fn non_normal_entries_remove( | |||
&mut self, | ||||
key: impl AsRef<HgPath>, | ||||
) -> bool { | ||||
|
r44775 | self.get_non_normal_other_parent_entries() | ||
.0 | ||||
r48492 | .remove(key.as_ref()) | |||
} | ||||
pub fn non_normal_entries_add(&mut self, key: impl AsRef<HgPath>) { | ||||
self.get_non_normal_other_parent_entries() | ||||
.0 | ||||
.insert(key.as_ref().into()); | ||||
|
r44775 | } | ||
|
r47878 | |||
|
r46162 | pub fn non_normal_entries_union( | ||
&mut self, | ||||
other: HashSet<HgPathBuf>, | ||||
) -> Vec<HgPathBuf> { | ||||
|
r44775 | self.get_non_normal_other_parent_entries() | ||
.0 | ||||
.union(&other) | ||||
|
r45500 | .map(ToOwned::to_owned) | ||
|
r44775 | .collect() | ||
} | ||||
pub fn get_non_normal_other_parent_entries( | ||||
&mut self, | ||||
|
r44842 | ) -> (&mut HashSet<HgPathBuf>, &mut HashSet<HgPathBuf>) { | ||
|
r44775 | self.set_non_normal_other_parent_entries(false); | ||
|
r44842 | ( | ||
self.non_normal_set.as_mut().unwrap(), | ||||
self.other_parent_set.as_mut().unwrap(), | ||||
) | ||||
|
r44775 | } | ||
|
r44903 | /// Useful to get immutable references to those sets in contexts where | ||
/// you only have an immutable reference to the `DirstateMap`, like when | ||||
/// sharing references with Python. | ||||
/// | ||||
/// TODO, get rid of this along with the other "setter/getter" stuff when | ||||
/// a nice typestate plan is defined. | ||||
/// | ||||
/// # Panics | ||||
/// | ||||
/// Will panic if either set is `None`. | ||||
pub fn get_non_normal_other_parent_entries_panic( | ||||
&self, | ||||
) -> (&HashSet<HgPathBuf>, &HashSet<HgPathBuf>) { | ||||
( | ||||
self.non_normal_set.as_ref().unwrap(), | ||||
self.other_parent_set.as_ref().unwrap(), | ||||
) | ||||
} | ||||
|
r44775 | pub fn set_non_normal_other_parent_entries(&mut self, force: bool) { | ||
|
r46162 | if !force | ||
&& self.non_normal_set.is_some() | ||||
&& self.other_parent_set.is_some() | ||||
{ | ||||
|
r44775 | return; | ||
} | ||||
|
r42998 | let mut non_normal = HashSet::new(); | ||
let mut other_parent = HashSet::new(); | ||||
|
r47878 | for (filename, entry) in self.state_map.iter() { | ||
if entry.is_non_normal() { | ||||
|
r42998 | non_normal.insert(filename.to_owned()); | ||
} | ||||
|
r47878 | if entry.is_from_other_parent() { | ||
|
r42998 | other_parent.insert(filename.to_owned()); | ||
} | ||||
} | ||||
|
r44775 | self.non_normal_set = Some(non_normal); | ||
self.other_parent_set = Some(other_parent); | ||||
|
r42998 | } | ||
/// Both of these setters and their uses appear to be the simplest way to | ||||
/// emulate a Python lazy property, but it is ugly and unidiomatic. | ||||
/// TODO One day, rewriting this struct using the typestate might be a | ||||
/// good idea. | ||||
|
r48126 | pub fn set_all_dirs(&mut self) -> Result<(), DirstateError> { | ||
|
r42998 | if self.all_dirs.is_none() { | ||
|
r47863 | self.all_dirs = Some(DirsMultiset::from_dirstate( | ||
|
r48126 | self.state_map.iter().map(|(k, v)| Ok((k, *v))), | ||
r48756 | false, | |||
|
r47863 | )?); | ||
|
r42998 | } | ||
|
r44315 | Ok(()) | ||
|
r42998 | } | ||
|
r48126 | pub fn set_dirs(&mut self) -> Result<(), DirstateError> { | ||
|
r42998 | if self.dirs.is_none() { | ||
|
r43070 | self.dirs = Some(DirsMultiset::from_dirstate( | ||
|
r48126 | self.state_map.iter().map(|(k, v)| Ok((k, *v))), | ||
r48756 | true, | |||
|
r44315 | )?); | ||
|
r42998 | } | ||
|
r44315 | Ok(()) | ||
|
r42998 | } | ||
|
r46162 | pub fn has_tracked_dir( | ||
&mut self, | ||||
directory: &HgPath, | ||||
|
r48126 | ) -> Result<bool, DirstateError> { | ||
|
r44315 | self.set_dirs()?; | ||
Ok(self.dirs.as_ref().unwrap().contains(directory)) | ||||
|
r42998 | } | ||
|
r46162 | pub fn has_dir( | ||
&mut self, | ||||
directory: &HgPath, | ||||
|
r48126 | ) -> Result<bool, DirstateError> { | ||
|
r44315 | self.set_all_dirs()?; | ||
Ok(self.all_dirs.as_ref().unwrap().contains(directory)) | ||||
|
r42998 | } | ||
|
r46135 | #[timed] | ||
|
r47893 | pub fn read( | ||
|
r46162 | &mut self, | ||
|
r47893 | file_contents: &[u8], | ||
) -> Result<Option<DirstateParents>, DirstateError> { | ||||
|
r42998 | if file_contents.is_empty() { | ||
return Ok(None); | ||||
} | ||||
|
r45916 | let (parents, entries, copies) = parse_dirstate(file_contents)?; | ||
self.state_map.extend( | ||||
entries | ||||
.into_iter() | ||||
.map(|(path, entry)| (path.to_owned(), entry)), | ||||
); | ||||
self.copy_map.extend( | ||||
copies | ||||
.into_iter() | ||||
.map(|(path, copy)| (path.to_owned(), copy.to_owned())), | ||||
); | ||||
|
r47893 | Ok(Some(parents.clone())) | ||
|
r42998 | } | ||
pub fn pack( | ||||
&mut self, | ||||
parents: DirstateParents, | ||||
|
r47871 | now: Timestamp, | ||
|
r42998 | ) -> Result<Vec<u8>, DirstateError> { | ||
|
r46162 | let packed = | ||
pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?; | ||||
|
r42998 | |||
|
r44775 | self.set_non_normal_other_parent_entries(true); | ||
|
r42998 | Ok(packed) | ||
} | ||||
} | ||||
#[cfg(test)] | ||||
mod tests { | ||||
use super::*; | ||||
#[test] | ||||
fn test_dirs_multiset() { | ||||
let mut map = DirstateMap::new(); | ||||
assert!(map.dirs.is_none()); | ||||
assert!(map.all_dirs.is_none()); | ||||
|
r44315 | assert_eq!(map.has_dir(HgPath::new(b"nope")).unwrap(), false); | ||
|
r42998 | assert!(map.all_dirs.is_some()); | ||
assert!(map.dirs.is_none()); | ||||
|
r44315 | assert_eq!(map.has_tracked_dir(HgPath::new(b"nope")).unwrap(), false); | ||
|
r42998 | assert!(map.dirs.is_some()); | ||
} | ||||
#[test] | ||||
fn test_add_file() { | ||||
let mut map = DirstateMap::new(); | ||||
assert_eq!(0, map.len()); | ||||
map.add_file( | ||||
|
r43227 | HgPath::new(b"meh"), | ||
|
r48834 | DirstateEntry::from_v1_data(EntryState::Normal, 1337, 1337, 1337), | ||
r48310 | false, | |||
false, | ||||
r48314 | false, | |||
r48316 | false, | |||
|
r44342 | ) | ||
.unwrap(); | ||||
|
r42998 | |||
assert_eq!(1, map.len()); | ||||
|
r44842 | assert_eq!(0, map.get_non_normal_other_parent_entries().0.len()); | ||
assert_eq!(0, map.get_non_normal_other_parent_entries().1.len()); | ||||
|
r42998 | } | ||
#[test] | ||||
fn test_non_normal_other_parent_entries() { | ||||
|
r44775 | let mut map: DirstateMap = [ | ||
|
r42998 | (b"f1", (EntryState::Removed, 1337, 1337, 1337)), | ||
(b"f2", (EntryState::Normal, 1337, 1337, -1)), | ||||
(b"f3", (EntryState::Normal, 1337, 1337, 1337)), | ||||
(b"f4", (EntryState::Normal, 1337, -2, 1337)), | ||||
(b"f5", (EntryState::Added, 1337, 1337, 1337)), | ||||
(b"f6", (EntryState::Added, 1337, 1337, -1)), | ||||
(b"f7", (EntryState::Merged, 1337, 1337, -1)), | ||||
(b"f8", (EntryState::Merged, 1337, 1337, 1337)), | ||||
(b"f9", (EntryState::Merged, 1337, -2, 1337)), | ||||
(b"fa", (EntryState::Added, 1337, -2, 1337)), | ||||
(b"fb", (EntryState::Removed, 1337, -2, 1337)), | ||||
] | ||||
.iter() | ||||
.map(|(fname, (state, mode, size, mtime))| { | ||||
( | ||||
|
r43227 | HgPathBuf::from_bytes(fname.as_ref()), | ||
|
r48834 | DirstateEntry::from_v1_data(*state, *mode, *size, *mtime), | ||
|
r42998 | ) | ||
}) | ||||
.collect(); | ||||
|
r44842 | let mut non_normal = [ | ||
|
r48856 | b"f1", b"f2", b"f4", b"f5", b"f6", b"f7", b"f8", b"f9", b"fa", | ||
b"fb", | ||||
|
r42998 | ] | ||
.iter() | ||||
|
r43227 | .map(|x| HgPathBuf::from_bytes(x.as_ref())) | ||
|
r42998 | .collect(); | ||
let mut other_parent = HashSet::new(); | ||||
|
r43227 | other_parent.insert(HgPathBuf::from_bytes(b"f4")); | ||
|
r44775 | let entries = map.get_non_normal_other_parent_entries(); | ||
|
r42998 | |||
|
r46162 | assert_eq!( | ||
(&mut non_normal, &mut other_parent), | ||||
(entries.0, entries.1) | ||||
); | ||||
|
r42998 | } | ||
} | ||||