diffy 0.5.0

Tools for finding and manipulating differences between files
Documentation
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
//! Utilities for parsing unified diff patches containing multiple files.
//!
//! This module provides [`PatchSet`] for parsing patches that contain changes
//! to multiple files, like the output of `git diff` or `git format-patch`.

pub(crate) mod error;
mod parse;
#[cfg(test)]
mod tests;

use alloc::borrow::Cow;
use alloc::borrow::ToOwned;
use core::fmt;

use crate::Patch;
use crate::binary::BinaryPatch;
use crate::utils::Text;

pub use error::PatchSetParseError;
use error::PatchSetParseErrorKind;
pub use parse::PatchSet;

/// Options for parsing patch content.
///
/// Use [`ParseOptions::unidiff()`] or [`ParseOptions::gitdiff()`]
/// to create options for the desired format.
///
/// ## Binary Files
///
/// When parsing git diffs, binary file changes are detected by:
///
/// * `Binary files a/path and b/path differ` (`git diff` without `--binary` flag)
/// * `GIT binary patch` (from `git diff --binary`)
///
/// Note that this is not a documented Git behavior,
/// so the implementation here is subject to change if Git changes.
///
/// ## Example
///
/// ```
/// use diffy::patch_set::ParseOptions;
/// use diffy::patch_set::PatchSet;
///
/// let s = "\
/// --- original
/// +++ modified
/// @@ -1 +1 @@
/// -old
/// +new
/// ";
///
/// let patches: Vec<_> = PatchSet::parse(s, ParseOptions::unidiff())
///     .collect::<Result<_, _>>()
///     .unwrap();
/// assert_eq!(patches.len(), 1);
/// ```
#[derive(Debug, Clone)]
pub struct ParseOptions {
    pub(crate) format: Format,
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum Format {
    /// Standard unified diff format.
    UniDiff,
    /// Git extended diff format.
    GitDiff,
}

impl ParseOptions {
    /// Parse as standard [unified diff] format.
    ///
    /// Supported:
    ///
    /// * `---`/`+++` file headers
    /// * `@@ ... @@` hunks
    /// * modify and rename files
    /// * create files (`--- /dev/null`)
    /// * delete files (`+++ /dev/null`)
    /// * Skip preamble, headers, and email signature trailer
    ///
    /// [unified diff]: https://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html
    pub fn unidiff() -> Self {
        Self {
            format: Format::UniDiff,
        }
    }

    /// Parse as [git extended diff format][git-diff-format].
    ///
    /// Supports all features of [`unidiff()`](Self::unidiff) plus:
    ///
    /// * `diff --git` headers
    /// * Extended headers (`new file mode`, `deleted file mode`, etc.)
    /// * Rename/copy detection (`rename from`/`rename to`, `copy from`/`copy to`)
    /// * Binary file detection
    ///
    /// [git-diff-format]: https://git-scm.com/docs/diff-format
    pub fn gitdiff() -> Self {
        Self {
            format: Format::GitDiff,
        }
    }
}

/// File mode extracted from git extended headers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileMode {
    /// `100644` regular file
    Regular,
    /// `100755` executable file
    Executable,
    /// `120000` symlink
    Symlink,
    /// `160000` gitlink (submodule)
    Gitlink,
}

impl core::str::FromStr for FileMode {
    type Err = PatchSetParseError;

    fn from_str(mode: &str) -> Result<Self, Self::Err> {
        match mode {
            "100644" => Ok(Self::Regular),
            "100755" => Ok(Self::Executable),
            "120000" => Ok(Self::Symlink),
            "160000" => Ok(Self::Gitlink),
            _ => Err(PatchSetParseErrorKind::InvalidFileMode(mode.to_owned()).into()),
        }
    }
}

/// The kind of patch content in a [`FilePatch`].
#[derive(Clone, PartialEq, Eq)]
pub enum PatchKind<'a, T: ToOwned + ?Sized> {
    /// Text patch with hunks.
    Text(Patch<'a, T>),
    /// Binary patch (literal or delta encoded, or marker-only).
    Binary(BinaryPatch<'a>),
}

impl<T: ?Sized, O> fmt::Debug for PatchKind<'_, T>
where
    T: ToOwned<Owned = O> + fmt::Debug,
    O: core::borrow::Borrow<T> + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PatchKind::Text(patch) => f.debug_tuple("Text").field(patch).finish(),
            PatchKind::Binary(patch) => f.debug_tuple("Binary").field(patch).finish(),
        }
    }
}

impl<'a, T: ToOwned + ?Sized> PatchKind<'a, T> {
    /// Returns the text patch, or `None` if this is a binary patch.
    pub fn as_text(&self) -> Option<&Patch<'a, T>> {
        match self {
            PatchKind::Text(patch) => Some(patch),
            PatchKind::Binary(_) => None,
        }
    }

    /// Returns the binary patch, or `None` if this is a text patch.
    pub fn as_binary(&self) -> Option<&BinaryPatch<'a>> {
        match self {
            PatchKind::Binary(patch) => Some(patch),
            PatchKind::Text(_) => None,
        }
    }

    /// Returns `true` if this is a binary diff.
    pub fn is_binary(&self) -> bool {
        matches!(self, PatchKind::Binary(_))
    }
}

/// A single file's patch with operation metadata.
///
/// This combines a [`PatchKind`] with a [`FileOperation`]
/// that indicates what kind of file operation this patch represents
/// (create, delete, modify, or rename).
#[derive(Clone, PartialEq, Eq)]
pub struct FilePatch<'a, T: ToOwned + ?Sized> {
    operation: FileOperation<'a, T>,
    kind: PatchKind<'a, T>,
    old_mode: Option<FileMode>,
    new_mode: Option<FileMode>,
}

impl<T: ?Sized, O> fmt::Debug for FilePatch<'_, T>
where
    T: ToOwned<Owned = O> + fmt::Debug,
    O: core::borrow::Borrow<T> + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FilePatch")
            .field("operation", &self.operation)
            .field("kind", &self.kind)
            .field("old_mode", &self.old_mode)
            .field("new_mode", &self.new_mode)
            .finish()
    }
}

impl<'a, T: ToOwned + ?Sized> FilePatch<'a, T> {
    fn new(
        operation: FileOperation<'a, T>,
        patch: Patch<'a, T>,
        old_mode: Option<FileMode>,
        new_mode: Option<FileMode>,
    ) -> Self {
        Self {
            operation,
            kind: PatchKind::Text(patch),
            old_mode,
            new_mode,
        }
    }

    fn new_binary(
        operation: FileOperation<'a, T>,
        patch: BinaryPatch<'a>,
        old_mode: Option<FileMode>,
        new_mode: Option<FileMode>,
    ) -> Self {
        Self {
            operation,
            kind: PatchKind::Binary(patch),
            old_mode,
            new_mode,
        }
    }

    /// Returns the file operation for this patch.
    pub fn operation(&self) -> &FileOperation<'a, T> {
        &self.operation
    }

    /// Returns the patch content.
    pub fn patch(&self) -> &PatchKind<'a, T> {
        &self.kind
    }

    /// Consumes the [`FilePatch`] and returns the underlying [`PatchKind`].
    pub fn into_patch(self) -> PatchKind<'a, T> {
        self.kind
    }

    /// Returns the file mode before applying this patch (when known).
    ///
    /// This is typically populated for
    ///
    /// * mode changes (`old mode <mode>` header)
    /// * deletions (`deleted file mode <mode>` header)
    pub fn old_mode(&self) -> Option<&FileMode> {
        self.old_mode.as_ref()
    }

    /// Returns the file mode **after** applying this patch (when known).
    ///
    /// This is typically populated for
    ///
    /// * mode changes (the `new mode <mode>` header)
    /// * creations (the `new file mode <mode>` header)
    pub fn new_mode(&self) -> Option<&FileMode> {
        self.new_mode.as_ref()
    }
}

/// The operation to perform based on a patch.
///
/// This is determined by examining the `---` and `+++` header lines
/// of a unified diff patch, and git extended headers when available.
#[derive(PartialEq, Eq)]
pub enum FileOperation<'a, T: ToOwned + ?Sized> {
    /// Delete a file (`+++ /dev/null`).
    Delete(Cow<'a, T>),
    /// Create a new file (`--- /dev/null`).
    Create(Cow<'a, T>),
    /// Modify a file.
    ///
    /// * If `original == modified`, this is an in-place modification.
    /// * If they differ, the caller decides how to handle, e.g., treat as rename or error.
    ///
    /// Usually, the caller needs to strip the prefix from the paths to determine.
    Modify {
        /// The original path before the modification.
        original: Cow<'a, T>,
        /// The resulting path after the modification.
        modified: Cow<'a, T>,
    },
    /// Rename a file (move from `from` to `to`, delete `from`).
    ///
    /// Only produced when git extended headers explicitly indicate a rename.
    Rename {
        /// The source path before the rename.
        from: Cow<'a, T>,
        /// The destination path after the rename.
        to: Cow<'a, T>,
    },
    /// Copy a file (copy from `from` to `to`, keep `from`).
    ///
    /// Only produced when git extended headers explicitly indicate a copy.
    Copy {
        /// The source path that is copied from.
        from: Cow<'a, T>,
        /// The destination path that is copied to.
        to: Cow<'a, T>,
    },
}

impl<T: ToOwned + ?Sized> Clone for FileOperation<'_, T> {
    fn clone(&self) -> Self {
        match self {
            Self::Delete(p) => Self::Delete(p.clone()),
            Self::Create(p) => Self::Create(p.clone()),
            Self::Modify { original, modified } => Self::Modify {
                original: original.clone(),
                modified: modified.clone(),
            },
            Self::Rename { from, to } => Self::Rename {
                from: from.clone(),
                to: to.clone(),
            },
            Self::Copy { from, to } => Self::Copy {
                from: from.clone(),
                to: to.clone(),
            },
        }
    }
}

impl<T: ?Sized, O> fmt::Debug for FileOperation<'_, T>
where
    T: ToOwned<Owned = O> + fmt::Debug,
    O: core::borrow::Borrow<T> + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Delete(p) => f.debug_tuple("Delete").field(p).finish(),
            Self::Create(p) => f.debug_tuple("Create").field(p).finish(),
            Self::Modify { original, modified } => f
                .debug_struct("Modify")
                .field("original", original)
                .field("modified", modified)
                .finish(),
            Self::Rename { from, to } => f
                .debug_struct("Rename")
                .field("from", from)
                .field("to", to)
                .finish(),
            Self::Copy { from, to } => f
                .debug_struct("Copy")
                .field("from", from)
                .field("to", to)
                .finish(),
        }
    }
}

impl<T: Text + ?Sized> FileOperation<'_, T> {
    /// Strip the first `n` path components from the paths in this operation.
    ///
    /// This is similar to the `-p` option in GNU patch. For example,
    /// `strip_prefix(1)` on a path `a/src/lib.rs` would return `src/lib.rs`.
    pub fn strip_prefix(&self, n: usize) -> FileOperation<'_, T> {
        fn strip<T: Text + ?Sized>(path: &T, n: usize) -> &T {
            let mut remaining = path;
            for _ in 0..n {
                match remaining.split_at_exclusive("/") {
                    Some((_first, rest)) => remaining = rest,
                    None => return remaining,
                }
            }
            remaining
        }

        match self {
            FileOperation::Delete(path) => FileOperation::Delete(Cow::Borrowed(strip(path, n))),
            FileOperation::Create(path) => FileOperation::Create(Cow::Borrowed(strip(path, n))),
            FileOperation::Modify { original, modified } => FileOperation::Modify {
                original: Cow::Borrowed(strip(original, n)),
                modified: Cow::Borrowed(strip(modified, n)),
            },
            FileOperation::Rename { from, to } => FileOperation::Rename {
                from: Cow::Borrowed(strip(from, n)),
                to: Cow::Borrowed(strip(to, n)),
            },
            FileOperation::Copy { from, to } => FileOperation::Copy {
                from: Cow::Borrowed(strip(from, n)),
                to: Cow::Borrowed(strip(to, n)),
            },
        }
    }

    /// Returns `true` if this is a file creation operation.
    pub fn is_create(&self) -> bool {
        matches!(self, FileOperation::Create(_))
    }

    /// Returns `true` if this is a file deletion operation.
    pub fn is_delete(&self) -> bool {
        matches!(self, FileOperation::Delete(_))
    }

    /// Returns `true` if this is a file modification.
    pub fn is_modify(&self) -> bool {
        matches!(self, FileOperation::Modify { .. })
    }

    /// Returns `true` if this is a rename operation.
    pub fn is_rename(&self) -> bool {
        matches!(self, FileOperation::Rename { .. })
    }

    /// Returns `true` if this is a copy operation.
    pub fn is_copy(&self) -> bool {
        matches!(self, FileOperation::Copy { .. })
    }
}