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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use std::{
    fmt::{self, Debug, Display},
    ops::{Add, Range, Sub},
    rc::Rc,
    str::FromStr,
    sync::Arc,
};

/// Repository requires flags.
/// Repositories contain a file (``.hg/requires``) containing a list of
/// features/capabilities that are *required* for clients to interface
/// with the repository.
use super::error::ErrorKind;
use bitflags::bitflags;
use chrono::{
    DateTime as ChronoDateTime, FixedOffset, Local, LocalResult, NaiveDateTime, TimeZone,
};
use sha1::{Digest, Sha1};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub(crate) enum RepositoryRequire {
    /// When present, revlogs are version 1 (**RevlogNG**).
    Revlogv1,
    /// The **store** repository layout is used.
    Store,
    /// The **fncache** layout hash encodes filenames with long paths and encodes reserved filenames.
    FnCache,
    /// Denotes that the store for a repository is shared from another location (defined by the ``.hg/sharedpath`` file).
    Shared,
    /// Derivative of ``shared``; the location of the store is relative to the store of this repository.
    RelShared,
    /// The *dotencode* layout encodes the first period or space in filenames to prevent issues on OS X and Windows.
    DotEncode,
    /// Denotes a revlog delta encoding format that was experimental and
    /// replaced by *generaldelta*. It should not be seen in the wild because
    /// it was never enabled by default.
    ParentDelta,
    /// Revlogs should be created with the *generaldelta* flag enabled. The
    /// generaldelta flag will cause deltas to be encoded against a parent
    /// revision instead of the previous revision in the revlog.
    GeneralDelta,
    /// Denotes that version 2 of manifests are being used.
    Manifestv2,
    /// Denotes that tree manifests are being used. Tree manifests are
    /// one manifest per directory (as opposed to a single flat manifest).
    TreeManifest,
    /// The working directory is sparse (only contains a subset of files).
    ExpSparse,
}

impl FromStr for RepositoryRequire {
    type Err = ErrorKind;

    fn from_str(value: &str) -> Result<RepositoryRequire, ErrorKind> {
        use RepositoryRequire::*;
        match value {
            "revlogv1" => Ok(Revlogv1),
            "store" => Ok(Store),
            "fncache" => Ok(FnCache),
            "shared" => Ok(Shared),
            "relshared" => Ok(RelShared),
            "dotencode" => Ok(DotEncode),
            "parentdelta" => Ok(ParentDelta),
            "generaldelta" => Ok(GeneralDelta),
            "manifestv2" => Ok(Manifestv2),
            "treemanifest" => Ok(TreeManifest),
            "exp-sparse" => Ok(ExpSparse),
            other => Err(ErrorKind::UnknownRequirement(other.into())),
        }
    }
}

/// Mercurial revision's index.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Revision(pub u32);

impl Revision {
    /// Return iterator for a range from index to `lim`.
    pub fn range_to(self, lim: Self) -> RevisionRange {
        RevisionRange(self.0, lim.0)
    }

    /// Return an open ended iterator from index.
    pub fn range(self) -> RevisionRange {
        RevisionRange(self.0, std::u32::MAX)
    }
}

impl From<u32> for Revision {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl Add<u32> for Revision {
    type Output = Self;

    fn add(self, other: u32) -> Self {
        Self(self.0 + other)
    }
}

impl Sub<u32> for Revision {
    type Output = Self;

    fn sub(self, other: u32) -> Self {
        assert!(self.0 >= other);
        Self(self.0 - other)
    }
}

impl From<Revision> for usize {
    fn from(value: Revision) -> usize {
        value.0 as usize
    }
}

/// Convert a `Revision` into an iterator of `Revision` values
/// starting at `Revision`'s value. ie, `Revision`(2).into_iter() => `Revision`(2), `Revision`(3), ...
impl<'a> IntoIterator for &'a Revision {
    type Item = Revision;
    type IntoIter = RevisionRange;

    fn into_iter(self) -> Self::IntoIter {
        self.range()
    }
}

/// An iterator over a range of `Revision`.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct RevisionRange(u32, u32);

impl Iterator for RevisionRange {
    type Item = Revision;

    fn next(&mut self) -> Option<Self::Item> {
        if self.0 < self.1 {
            let ret = Revision(self.0);
            self.0 += 1;
            Some(ret)
        } else {
            None
        }
    }
}

impl DoubleEndedIterator for RevisionRange {
    fn next_back(&mut self) -> Option<Revision> {
        if self.0 < self.1 {
            self.1 -= 1;
            let ret = Revision(self.1);
            Some(ret)
        } else {
            None
        }
    }
}

impl From<Range<usize>> for RevisionRange {
    fn from(value: Range<usize>) -> Self {
        Self(value.start as u32, value.end as u32)
    }
}

/// `Revlog` version number
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u16)]
pub enum Version {
    Revlog0 = 0,
    RevlogNG = 1,
}

bitflags! {
    /// `Revlog` features
    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
    pub struct Features: u16 {
        const INLINE        = 1;
        const GENERAL_DELTA = 1 << 1;
    }
}

bitflags! {
    /// Per-revision flags
    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
    pub struct IdxFlags: u16 {
        const EXTSTORED     = 1 << 13;
        const CENSORED      = 1 << 15;
    }
}

/// `Revlog` header
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct RevisionLogHeader {
    pub version: Version,
    pub features: Features,
}

#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NodeHash([u8; 20]);

const HEX_CHARS: &[u8] = b"0123456789abcdef";

impl NodeHash {
    pub fn to_hex(self) -> String {
        let mut v = Vec::with_capacity(40);
        for &byte in &self.0 {
            v.push(HEX_CHARS[(byte >> 4) as usize]);
            v.push(HEX_CHARS[(byte & 0xf) as usize]);
        }

        unsafe { String::from_utf8_unchecked(v) }
    }

    pub fn from_slice(data: &[u8]) -> Self {
        (&Sha1::digest(data)[..]).into()
    }
}

impl AsRef<[u8]> for NodeHash {
    fn as_ref(&self) -> &[u8] {
        &self.0[..]
    }
}

impl Display for NodeHash {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(&self.to_hex(), fmt)
    }
}

impl Debug for NodeHash {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "NodeHash({})", self)
    }
}

impl<'a> From<&'a [u8]> for NodeHash {
    fn from(value: &'a [u8]) -> Self {
        let mut data: [u8; 20] = Default::default();
        data.copy_from_slice(value);
        Self(data)
    }
}

impl FromStr for NodeHash {
    type Err = ErrorKind;

    fn from_str(s: &str) -> Result<Self, ErrorKind> {
        let mut ret = Self([0; 20]);

        for idx in 0..ret.0.len() {
            ret.0[idx] = match u8::from_str_radix(&s[(idx * 2)..(idx * 2 + 2)], 16) {
                Ok(v) => v,
                Err(_) => return Err(ErrorKind::Parser),
            }
        }

        Ok(ret)
    }
}

/// Entry entry for a revision
#[derive(Debug, Copy, Clone)]
pub struct RevisionLogEntry {
    pub offset: u64,         // offset of content (delta/literal) in datafile (or inlined)
    pub flags: IdxFlags,     // unused?
    pub compressed_len: u32, // compressed content size
    pub len: Option<u32>,    // size of final file (after applying deltas)
    pub baserev: Option<Revision>, // base/previous rev for deltas (None if literal)
    pub linkrev: Revision,   // changeset id
    pub p1: Option<Revision>, // parent p1
    pub p2: Option<Revision>, // parent p2
    pub nodeid: NodeHash,    // nodeid
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Delta {
    // Fragments should be in sorted order by start offset and should not overlap.
    pub(crate) fragments: Vec<Fragment>,
}

impl Delta {
    /// Construct a new Delta object. Verify that `frags` is sane, sorted and
    /// non-overlapping.
    pub fn new(fragments: Vec<Fragment>) -> Result<Self, ErrorKind> {
        Ok(Delta { fragments })
    }
    pub fn fragments(&self) -> &[Fragment] {
        self.fragments.as_slice()
    }
}
/// Represents a single contiguous modified region of text.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Fragment {
    pub start: usize,
    pub end: usize,
    pub content: Rc<[u8]>,
}

impl Fragment {
    // Return the length of the content
    pub fn content_length(&self) -> usize {
        self.content.len()
    }
}

impl Debug for Fragment {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmt,
            "Fragment(\nstart:{}\nend:{}\ncontent:{:?}\n)",
            self.start,
            self.end,
            std::str::from_utf8(&self.content)
        )
    }
}

#[derive(Debug, Clone)]
pub enum Chunk {
    /// Literal text of the revision
    Literal(Arc<[u8]>),
    /// Vector of `Delta`s against a previous version
    Deltas(Delta),
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DateTime(ChronoDateTime<FixedOffset>);

impl DateTime {
    #[inline]
    pub fn new(dt: ChronoDateTime<FixedOffset>) -> Self {
        DateTime(dt)
    }

    pub fn now() -> Self {
        let now = Local::now();
        DateTime(now.with_timezone(now.offset()))
    }

    pub fn from_timestamp(secs: i64, tz_offset_secs: i32) -> Result<Self, ErrorKind> {
        let tz = FixedOffset::west_opt(tz_offset_secs).ok_or_else(|| {
            ErrorKind::InvalidDateTime(format!("timezone offset out of range: {}", tz_offset_secs))
        })?;
        let dt = match tz.timestamp_opt(secs, 0) {
            LocalResult::Single(dt) => dt,
            _ => {
                return Err(ErrorKind::InvalidDateTime(format!(
                    "seconds out of range: {}",
                    secs
                )));
            }
        };
        Ok(Self::new(dt))
    }

    /// Construct a new `DateTime` from an RFC3339 string.
    ///
    /// RFC3339 is a standardized way to represent a specific moment in time. See
    /// https://tools.ietf.org/html/rfc3339.
    pub fn from_rfc3339(rfc3339: &str) -> Result<Self, ErrorKind> {
        let dt = ChronoDateTime::parse_from_rfc3339(rfc3339)?;
        Ok(Self::new(dt))
    }

    /// Retrieves the Unix timestamp in UTC.
    #[inline]
    pub fn timestamp_secs(&self) -> i64 {
        self.0.timestamp()
    }

    /// Retrieves the timezone offset, as represented by the number of seconds to
    /// add to convert local time to UTC.
    #[inline]
    pub fn tz_offset_secs(&self) -> i32 {
        // This is the same as the way Mercurial stores timezone offsets.
        self.0.offset().utc_minus_local()
    }

    #[inline]
    pub fn as_chrono(&self) -> &ChronoDateTime<FixedOffset> {
        &self.0
    }

    #[inline]
    pub fn into_chrono(self) -> ChronoDateTime<FixedOffset> {
        self.0
    }
}

impl Display for DateTime {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.0)
    }
}

/// Number of non-leap-nanoseconds since January 1, 1970 UTC
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct Timestamp(i64);

impl Timestamp {
    pub fn now() -> Self {
        DateTime::now().into()
    }

    pub fn from_timestamp_nanos(ts: i64) -> Self {
        Timestamp(ts)
    }

    pub fn timestamp_nanos(self) -> i64 {
        self.0
    }
}

impl From<DateTime> for Timestamp {
    fn from(dt: DateTime) -> Self {
        Timestamp(dt.0.timestamp_nanos())
    }
}

impl From<Timestamp> for DateTime {
    fn from(ts: Timestamp) -> Self {
        let ts_secs = ts.0 / 1_000_000_000;
        let ts_nsecs = (ts.0 % 1_000_000_000) as u32;
        DateTime::new(ChronoDateTime::<FixedOffset>::from_utc(
            NaiveDateTime::from_timestamp(ts_secs, ts_nsecs),
            FixedOffset::west(0),
        ))
    }
}

pub struct MercurialTag {
    pub node: NodeHash,
    pub name: String,
}

impl FromStr for MercurialTag {
    type Err = ErrorKind;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let mut parts = value.split_whitespace();
        if let (Some(node), Some(name)) = (
            parts
                .next()
                .and_then(|x| if x.len() == 40 { Some(x) } else { None })
                .and_then(|x| x.parse().ok()),
            parts.next().map(String::from),
        ) {
            Ok(Self { node, name })
        } else {
            Err(ErrorKind::Parser)
        }
    }
}