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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// This file is part of radicle-surf
// <https://github.com/radicle-dev/radicle-surf>
//
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Types that represent diff(s) in a Git repo.

use std::{
    borrow::Cow,
    ops::Range,
    path::{Path, PathBuf},
    string::FromUtf8Error,
};

#[cfg(feature = "serde")]
use serde::{ser, ser::SerializeStruct, Serialize, Serializer};

use git_ext::Oid;

pub mod git;

/// The serializable representation of a `git diff`.
///
/// A [`Diff`] can be retrieved by the following functions:
///    * [`crate::Repository::diff`]
///    * [`crate::Repository::diff_commit`]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Diff {
    files: Vec<FileDiff>,
    stats: Stats,
}

impl Diff {
    /// Creates an empty diff.
    pub(crate) fn new() -> Self {
        Diff::default()
    }

    /// Returns an iterator of the file in the diff.
    pub fn files(&self) -> impl Iterator<Item = &FileDiff> {
        self.files.iter()
    }

    /// Returns owned files in the diff.
    pub fn into_files(self) -> Vec<FileDiff> {
        self.files
    }

    pub fn added(&self) -> impl Iterator<Item = &Added> {
        self.files().filter_map(|x| match x {
            FileDiff::Added(a) => Some(a),
            _ => None,
        })
    }

    pub fn deleted(&self) -> impl Iterator<Item = &Deleted> {
        self.files().filter_map(|x| match x {
            FileDiff::Deleted(a) => Some(a),
            _ => None,
        })
    }

    pub fn moved(&self) -> impl Iterator<Item = &Moved> {
        self.files().filter_map(|x| match x {
            FileDiff::Moved(a) => Some(a),
            _ => None,
        })
    }

    pub fn modified(&self) -> impl Iterator<Item = &Modified> {
        self.files().filter_map(|x| match x {
            FileDiff::Modified(a) => Some(a),
            _ => None,
        })
    }

    pub fn copied(&self) -> impl Iterator<Item = &Copied> {
        self.files().filter_map(|x| match x {
            FileDiff::Copied(a) => Some(a),
            _ => None,
        })
    }

    pub fn stats(&self) -> &Stats {
        &self.stats
    }

    fn update_stats(&mut self, diff: &DiffContent) {
        self.stats.files_changed += 1;
        if let DiffContent::Plain { hunks, .. } = diff {
            for h in hunks.iter() {
                for l in &h.lines {
                    match l {
                        Modification::Addition(_) => self.stats.insertions += 1,
                        Modification::Deletion(_) => self.stats.deletions += 1,
                        _ => (),
                    }
                }
            }
        }
    }

    pub fn insert_modified(
        &mut self,
        path: PathBuf,
        diff: DiffContent,
        old: DiffFile,
        new: DiffFile,
    ) {
        self.update_stats(&diff);
        let diff = FileDiff::Modified(Modified {
            path,
            diff,
            old,
            new,
        });
        self.files.push(diff);
    }

    pub fn insert_moved(
        &mut self,
        old_path: PathBuf,
        new_path: PathBuf,
        old: DiffFile,
        new: DiffFile,
        content: DiffContent,
    ) {
        self.update_stats(&DiffContent::Empty);
        let diff = FileDiff::Moved(Moved {
            old_path,
            new_path,
            old,
            new,
            diff: content,
        });
        self.files.push(diff);
    }

    pub fn insert_copied(
        &mut self,
        old_path: PathBuf,
        new_path: PathBuf,
        old: DiffFile,
        new: DiffFile,
        content: DiffContent,
    ) {
        self.update_stats(&DiffContent::Empty);
        let diff = FileDiff::Copied(Copied {
            old_path,
            new_path,
            old,
            new,
            diff: content,
        });
        self.files.push(diff);
    }

    pub fn insert_added(&mut self, path: PathBuf, diff: DiffContent, new: DiffFile) {
        self.update_stats(&diff);
        let diff = FileDiff::Added(Added { path, diff, new });
        self.files.push(diff);
    }

    pub fn insert_deleted(&mut self, path: PathBuf, diff: DiffContent, old: DiffFile) {
        self.update_stats(&diff);
        let diff = FileDiff::Deleted(Deleted { path, diff, old });
        self.files.push(diff);
    }
}

/// A file that was added within a [`Diff`].
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Added {
    /// The path to this file, relative to the repository root.
    pub path: PathBuf,
    pub diff: DiffContent,
    pub new: DiffFile,
}

/// A file that was deleted within a [`Diff`].
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Deleted {
    /// The path to this file, relative to the repository root.
    pub path: PathBuf,
    pub diff: DiffContent,
    pub old: DiffFile,
}

/// A file that was moved within a [`Diff`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Moved {
    /// The old path to this file, relative to the repository root.
    pub old_path: PathBuf,
    pub old: DiffFile,
    /// The new path to this file, relative to the repository root.
    pub new_path: PathBuf,
    pub new: DiffFile,
    pub diff: DiffContent,
}

#[cfg(feature = "serde")]
impl Serialize for Moved {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if self.old == self.new {
            let mut state = serializer.serialize_struct("Moved", 3)?;
            state.serialize_field("oldPath", &self.old_path)?;
            state.serialize_field("newPath", &self.new_path)?;
            state.serialize_field("current", &self.new)?;
            state.end()
        } else {
            let mut state = serializer.serialize_struct("Moved", 5)?;
            state.serialize_field("oldPath", &self.old_path)?;
            state.serialize_field("newPath", &self.new_path)?;
            state.serialize_field("old", &self.old)?;
            state.serialize_field("new", &self.new)?;
            state.serialize_field("diff", &self.diff)?;
            state.end()
        }
    }
}

/// A file that was copied within a [`Diff`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Copied {
    /// The old path to this file, relative to the repository root.
    pub old_path: PathBuf,
    /// The new path to this file, relative to the repository root.
    pub new_path: PathBuf,
    pub old: DiffFile,
    pub new: DiffFile,
    pub diff: DiffContent,
}

#[cfg(feature = "serde")]
impl Serialize for Copied {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if self.old == self.new {
            let mut state = serializer.serialize_struct("Copied", 3)?;
            state.serialize_field("oldPath", &self.old_path)?;
            state.serialize_field("newPath", &self.new_path)?;
            state.serialize_field("current", &self.new)?;
            state.end()
        } else {
            let mut state = serializer.serialize_struct("Copied", 5)?;
            state.serialize_field("oldPath", &self.old_path)?;
            state.serialize_field("newPath", &self.new_path)?;
            state.serialize_field("old", &self.old)?;
            state.serialize_field("new", &self.new)?;
            state.serialize_field("diff", &self.diff)?;
            state.end()
        }
    }
}

#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EofNewLine {
    OldMissing,
    NewMissing,
    BothMissing,
    NoneMissing,
}

impl Default for EofNewLine {
    fn default() -> Self {
        Self::NoneMissing
    }
}

/// A file that was modified within a [`Diff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Modified {
    pub path: PathBuf,
    pub diff: DiffContent,
    pub old: DiffFile,
    pub new: DiffFile,
}

/// The set of changes for a given file.
#[cfg_attr(
    feature = "serde",
    derive(Serialize),
    serde(tag = "type", rename_all = "camelCase")
)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DiffContent {
    /// The file is a binary file and so no set of changes can be provided.
    Binary,
    /// The set of changes, as [`Hunks`] for a plaintext file.
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    Plain {
        hunks: Hunks<Modification>,
        stats: FileStats,
        eof: EofNewLine,
    },
    Empty,
}

impl DiffContent {
    pub fn eof(&self) -> Option<EofNewLine> {
        match self {
            Self::Plain { eof, .. } => Some(eof.clone()),
            _ => None,
        }
    }

    pub fn stats(&self) -> Option<&FileStats> {
        match &self {
            DiffContent::Plain { stats, .. } => Some(stats),
            DiffContent::Empty => None,
            DiffContent::Binary => None,
        }
    }
}

/// File mode in a diff.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
pub enum FileMode {
    /// For regular files.
    Blob,
    /// For regular files that are executable.
    BlobExecutable,
    /// For directories.
    Tree,
    /// For symbolic links.
    Link,
    /// Used for Git submodules.
    Commit,
}

impl From<FileMode> for u32 {
    fn from(m: FileMode) -> Self {
        git2::FileMode::from(m).into()
    }
}

impl From<FileMode> for i32 {
    fn from(m: FileMode) -> Self {
        git2::FileMode::from(m).into()
    }
}

/// A modified file.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
pub struct DiffFile {
    /// File blob id.
    pub oid: Oid,
    /// File mode.
    pub mode: FileMode,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize),
    serde(tag = "status", rename_all = "camelCase")
)]
pub enum FileDiff {
    Added(Added),
    Deleted(Deleted),
    Modified(Modified),
    Moved(Moved),
    Copied(Copied),
}

impl FileDiff {
    pub fn path(&self) -> &Path {
        match self {
            FileDiff::Added(x) => x.path.as_path(),
            FileDiff::Deleted(x) => x.path.as_path(),
            FileDiff::Modified(x) => x.path.as_path(),
            FileDiff::Moved(x) => x.new_path.as_path(),
            FileDiff::Copied(x) => x.new_path.as_path(),
        }
    }
}

/// Statistics describing a particular [`FileDiff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FileStats {
    /// Get the total number of additions in a [`FileDiff`].
    pub additions: usize,
    /// Get the total number of deletions in a [`FileDiff`].
    pub deletions: usize,
}

/// Statistics describing a particular [`Diff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Stats {
    /// Get the total number of files changed in a [`Diff`]
    pub files_changed: usize,
    /// Get the total number of insertions in a [`Diff`].
    pub insertions: usize,
    /// Get the total number of deletions in a [`Diff`].
    pub deletions: usize,
}

/// A set of changes across multiple lines.
///
/// The parameter `T` can be an [`Addition`], [`Deletion`], or
/// [`Modification`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Hunk<T> {
    pub header: Line,
    pub lines: Vec<T>,
    /// Old line range.
    pub old: Range<u32>,
    /// New line range.
    pub new: Range<u32>,
}

/// A set of [`Hunk`] changes.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Hunks<T>(pub Vec<Hunk<T>>);

impl<T> Default for Hunks<T> {
    fn default() -> Self {
        Self(Default::default())
    }
}

impl<T> Hunks<T> {
    pub fn iter(&self) -> impl Iterator<Item = &Hunk<T>> {
        self.0.iter()
    }
}

impl<T> From<Vec<Hunk<T>>> for Hunks<T> {
    fn from(hunks: Vec<Hunk<T>>) -> Self {
        Self(hunks)
    }
}

/// The content of a single line.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Line(pub(crate) Vec<u8>);

impl Line {
    pub fn as_bytes(&self) -> &[u8] {
        self.0.as_slice()
    }

    pub fn from_utf8(self) -> Result<String, FromUtf8Error> {
        String::from_utf8(self.0)
    }

    pub fn from_utf8_lossy(&self) -> Cow<str> {
        String::from_utf8_lossy(&self.0)
    }
}

impl From<Vec<u8>> for Line {
    fn from(v: Vec<u8>) -> Self {
        Self(v)
    }
}

impl From<String> for Line {
    fn from(s: String) -> Self {
        Self(s.into_bytes())
    }
}

#[cfg(feature = "serde")]
impl Serialize for Line {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = std::str::from_utf8(&self.0).map_err(ser::Error::custom)?;

        serializer.serialize_str(s)
    }
}

/// Either the modification of a single [`Line`], or just contextual
/// information.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Modification {
    /// A line is an addition in a file.
    Addition(Addition),

    /// A line is a deletion in a file.
    Deletion(Deletion),

    /// A contextual line in a file, i.e. there were no changes to the line.
    Context {
        line: Line,
        line_no_old: u32,
        line_no_new: u32,
    },
}

#[cfg(feature = "serde")]
impl Serialize for Modification {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeMap as _;

        match self {
            Modification::Addition(addition) => {
                let mut map = serializer.serialize_map(Some(3))?;
                map.serialize_entry("line", &addition.line)?;
                map.serialize_entry("lineNo", &addition.line_no)?;
                map.serialize_entry("type", "addition")?;
                map.end()
            }
            Modification::Deletion(deletion) => {
                let mut map = serializer.serialize_map(Some(3))?;
                map.serialize_entry("line", &deletion.line)?;
                map.serialize_entry("lineNo", &deletion.line_no)?;
                map.serialize_entry("type", "deletion")?;
                map.end()
            }
            Modification::Context {
                line,
                line_no_old,
                line_no_new,
            } => {
                let mut map = serializer.serialize_map(Some(4))?;
                map.serialize_entry("line", line)?;
                map.serialize_entry("lineNoOld", line_no_old)?;
                map.serialize_entry("lineNoNew", line_no_new)?;
                map.serialize_entry("type", "context")?;
                map.end()
            }
        }
    }
}

/// A addition of a [`Line`] at the `line_no`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Addition {
    pub line: Line,
    pub line_no: u32,
}

#[cfg(feature = "serde")]
impl Serialize for Addition {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeStruct as _;

        let mut s = serializer.serialize_struct("Addition", 3)?;
        s.serialize_field("line", &self.line)?;
        s.serialize_field("lineNo", &self.line_no)?;
        s.serialize_field("type", "addition")?;
        s.end()
    }
}

/// A deletion of a [`Line`] at the `line_no`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Deletion {
    pub line: Line,
    pub line_no: u32,
}

#[cfg(feature = "serde")]
impl Serialize for Deletion {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeStruct as _;

        let mut s = serializer.serialize_struct("Deletion", 3)?;
        s.serialize_field("line", &self.line)?;
        s.serialize_field("lineNo", &self.line_no)?;
        s.serialize_field("type", "deletion")?;
        s.end()
    }
}

impl Modification {
    pub fn addition(line: impl Into<Line>, line_no: u32) -> Self {
        Self::Addition(Addition {
            line: line.into(),
            line_no,
        })
    }

    pub fn deletion(line: impl Into<Line>, line_no: u32) -> Self {
        Self::Deletion(Deletion {
            line: line.into(),
            line_no,
        })
    }

    pub fn context(line: impl Into<Line>, line_no_old: u32, line_no_new: u32) -> Self {
        Self::Context {
            line: line.into(),
            line_no_old,
            line_no_new,
        }
    }
}