comparison rust/hg-core/src/revlog/changelog.rs @ 51363:d626e5e7bbbe stable

rust-changelog: don't panic on empty file lists
author Arun Kulshreshtha <akulshreshtha@janestreet.com>
date Tue, 30 Jan 2024 22:14:02 +0000
parents 27e773aa607d
children 508fd40dc86a
comparison
equal deleted inserted replaced
51362:e7be2ddfb4c2 51363:d626e5e7bbbe
3 use crate::revlog::{Node, NodePrefix}; 3 use crate::revlog::{Node, NodePrefix};
4 use crate::revlog::{Revlog, RevlogEntry, RevlogError}; 4 use crate::revlog::{Revlog, RevlogEntry, RevlogError};
5 use crate::utils::hg_path::HgPath; 5 use crate::utils::hg_path::HgPath;
6 use crate::vfs::Vfs; 6 use crate::vfs::Vfs;
7 use crate::{Graph, GraphError, UncheckedRevision}; 7 use crate::{Graph, GraphError, UncheckedRevision};
8 use itertools::Itertools; 8 use itertools::{Either, Itertools};
9 use std::ascii::escape_default; 9 use std::ascii::escape_default;
10 use std::borrow::Cow; 10 use std::borrow::Cow;
11 use std::fmt::{Debug, Formatter}; 11 use std::fmt::{Debug, Formatter};
12 use std::iter;
12 13
13 /// A specialized `Revlog` to work with changelog data format. 14 /// A specialized `Revlog` to work with changelog data format.
14 pub struct Changelog { 15 pub struct Changelog {
15 /// The generic `revlog` format. 16 /// The generic `revlog` format.
16 pub(crate) revlog: Revlog, 17 pub(crate) revlog: Revlog,
226 &self.bytes[self.user_end + 1..self.timestamp_end] 227 &self.bytes[self.user_end + 1..self.timestamp_end]
227 } 228 }
228 229
229 /// The files changed in this revision. 230 /// The files changed in this revision.
230 pub fn files(&self) -> impl Iterator<Item = &HgPath> { 231 pub fn files(&self) -> impl Iterator<Item = &HgPath> {
231 self.bytes[self.timestamp_end + 1..self.files_end] 232 if self.timestamp_end == self.files_end {
232 .split(|b| b == &b'\n') 233 Either::Left(iter::empty())
233 .map(HgPath::new) 234 } else {
235 Either::Right(
236 self.bytes[self.timestamp_end + 1..self.files_end]
237 .split(|b| b == &b'\n')
238 .map(HgPath::new),
239 )
240 }
234 } 241 }
235 242
236 /// The change description. 243 /// The change description.
237 pub fn description(&self) -> &[u8] { 244 pub fn description(&self) -> &[u8] {
238 &self.bytes[self.files_end + 2..] 245 &self.bytes[self.files_end + 2..]
354 changelog.entry_for_rev(NULL_REVISION.into())?.data()?, 361 changelog.entry_for_rev(NULL_REVISION.into())?.data()?,
355 ChangelogRevisionData::null() 362 ChangelogRevisionData::null()
356 ); 363 );
357 Ok(()) 364 Ok(())
358 } 365 }
359 } 366
367 #[test]
368 fn test_empty_files_list() {
369 assert!(ChangelogRevisionData::null()
370 .files()
371 .collect_vec()
372 .is_empty());
373 }
374 }