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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::Arc;
use std::{fmt, str};
use crate::{
error::ErrorKind,
manifest::ManifestEntry,
types::{DateTime, NodeHash, Revision, RevisionLogEntry},
};
/// Changeset's header.
pub struct ChangesetHeader {
/// Parent 1.
pub p1: Option<Revision>,
/// Parent 2.
pub p2: Option<Revision>,
/// Manifest id hash.
pub manifestid: NodeHash,
/// User.
pub user: Vec<u8>,
/// Time.
pub time: DateTime,
/// Extra properties (like b"branch" shows current branch name, b"closed" equal to b"1" means branch is closed).
pub extra: HashMap<Vec<u8>, Vec<u8>>,
/// Files paths.
pub files: Vec<Vec<u8>>,
/// Comment to revision.
pub comment: Vec<u8>,
}
impl Debug for ChangesetHeader {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
writeln!(
fmt,
"ChangesetHeader(\np1:{:?}\np2:{:?}\nmanifestid:{}\nuser:{}\ntime:{:?}\nextra:{}\nfiles:\n{}\ncomment:\n{}\n)",
self.p1,
self.p2,
self.manifestid,
str::from_utf8(&self.user).expect("bad user utf8"),
self.time,
self.extra
.iter()
.map(|(key, value)| format!(
"{}:{}",
str::from_utf8(&key).expect("bad extra key utf8"),
str::from_utf8(&value).unwrap_or("bad extra value utf8")
))
.collect::<Vec<_>>()
.join(","),
self.files.iter()
.map(|key| str::from_utf8(&key).unwrap().to_string())
.collect::<Vec<_>>()
.join("\n"),
str::from_utf8(&self.comment).expect("bad comment utf8"),
)
}
}
impl ChangesetHeader {
pub(crate) fn from_entry_bytes(
entry: &RevisionLogEntry,
data: &[u8],
) -> Result<Self, ErrorKind> {
let mut lines = data.split(|&x| x == b'\n');
if let (Some(manifestid), Some(user), Some(timeextra)) = (
lines
.next()
.and_then(|x| str::from_utf8(x).ok())
.and_then(|x| x.parse().ok()),
lines.next(),
lines.next(),
) {
let mut parts = timeextra.splitn(3, |&x| x == b' ');
if let (Some(time), Some(tz), extra) = (
parts
.next()
.and_then(|x| str::from_utf8(x).ok())
.and_then(|x| x.parse().ok()),
parts
.next()
.and_then(|x| str::from_utf8(x).ok())
.and_then(|x| x.parse().ok()),
parts.next(),
) {
let mut files = Vec::new();
while let Some(file) = lines.next() {
if file.is_empty() {
break;
}
files.push(file.into());
}
Ok(ChangesetHeader {
p1: entry.p1,
p2: entry.p2,
manifestid,
user: user.into(),
time: DateTime::from_timestamp(time, tz)?,
extra: extra
.map(|x| {
x.split(|&y| y == 0)
.filter_map(|y| {
let mut z = y.splitn(2, |&k| k == b':');
if let (Some(key), Some(value)) =
(z.next().map(unescape), z.next().map(unescape))
{
Some((key, value))
} else {
None
}
})
.collect()
})
.unwrap_or_else(HashMap::new),
files,
comment: lines.collect::<Vec<_>>().join(&b'\n'),
})
} else {
Err(ErrorKind::Parser)
}
} else {
Err(ErrorKind::Parser)
}
}
}
fn unescape<'a, S: IntoIterator<Item = &'a u8>>(s: S) -> Vec<u8> {
let mut ret = Vec::new();
let mut quote = false;
for c in s.into_iter() {
match *c {
b'0' if quote => {
quote = false;
ret.push(b'\0');
}
b'n' if quote => {
quote = false;
ret.push(b'\n');
}
b'r' if quote => {
quote = false;
ret.push(b'\r');
}
b'\\' if quote => {
quote = false;
ret.push(b'\\');
}
c if quote => {
quote = false;
ret.push(b'\\');
ret.push(c)
}
b'\\' => {
assert!(!quote);
quote = true;
}
c => {
quote = false;
ret.push(c);
}
}
}
ret
}
/// `Changeset` info about revision, contains revision number, header and files
/// with bodies and metadata.
#[derive(Debug)]
pub struct Changeset {
/// Revision.
pub revision: Revision,
/// Header with parents, manifest, user, date, extra, files, and comment attributes.
pub header: ChangesetHeader,
/// File list for revision.
pub files: Vec<ChangesetFile>,
}
/// Changeset's file. Contains path info, internal blob from Mercurial
/// (use [file_content](fn.file_content.html) for actual file data) and meta information.
#[derive(Debug)]
pub struct ChangesetFile {
/// Path.
pub path: Vec<u8>,
/// Data of file, `None` - file was deleted, otherwise it was added or modified.
pub data: Option<Arc<[u8]>>,
/// Meta information, `None` - file was deleted, otherwise it was added or modified.
pub manifest_entry: Option<ManifestEntry>,
}