patchkit 0.2.4

A library for parsing and manipulating patch 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
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
//! Quilt patch management
use std::collections::HashMap;
use std::io::BufRead;

/// The default directory for patches
pub const DEFAULT_PATCHES_DIR: &str = "patches";

/// The default series file name
pub const DEFAULT_SERIES_FILE: &str = "series";

/// Find the common prefix to use for patches
///
/// # Arguments
/// * `names` - An iterator of patch names
///
/// # Returns
/// The common prefix, or `None` if there is no common prefix
pub fn find_common_patch_suffix<'a>(names: impl Iterator<Item = &'a str>) -> Option<&'a str> {
    let mut suffix_count = HashMap::new();

    for name in names {
        if name == "series" || name == "00list" {
            continue;
        }

        if name.starts_with("README") {
            continue;
        }

        let suffix = name.find('.').map(|index| &name[index..]).unwrap_or("");
        suffix_count
            .entry(suffix)
            .and_modify(|count| *count += 1)
            .or_insert(1);
    }

    // Just find the suffix with the highest count and return it
    suffix_count
        .into_iter()
        .max_by_key(|(_, count)| *count)
        .map(|(suffix, _)| suffix)
}

#[cfg(test)]
mod find_common_patch_suffix_tests {
    #[test]
    fn test_find_common_patch_suffix() {
        let names = vec![
            "0001-foo.patch",
            "0002-bar.patch",
            "0003-baz.patch",
            "0004-qux.patch",
        ];
        assert_eq!(
            super::find_common_patch_suffix(names.into_iter()),
            Some(".patch")
        );
    }

    #[test]
    fn test_find_common_patch_suffix_no_common_suffix() {
        let names = vec![
            "0001-foo.patch",
            "0002-bar.patch",
            "0003-baz.patch",
            "0004-qux",
        ];
        assert_eq!(
            super::find_common_patch_suffix(names.into_iter()),
            Some(".patch")
        );
    }

    #[test]
    fn test_find_common_patch_suffix_no_patches() {
        let names = vec![
            "README",
            "0001-foo.patch",
            "0002-bar.patch",
            "0003-baz.patch",
        ];
        assert_eq!(
            super::find_common_patch_suffix(names.into_iter()),
            Some(".patch")
        );
    }
}

/// A entry in a series file
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SeriesEntry {
    /// A patch entry
    Patch {
        /// The name of the patch
        name: String,
        /// The options for patch
        options: Vec<String>,
    },
    /// A comment entry
    Comment(String),
}

/// A quilt series file
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Series {
    /// The entries in the series file
    pub entries: Vec<SeriesEntry>,
}

impl Series {
    /// Create a new series file
    pub fn new() -> Self {
        Self { entries: vec![] }
    }

    /// Get the number of patches in the series file
    pub fn len(&self) -> usize {
        self.entries
            .iter()
            .filter(|entry| matches!(entry, SeriesEntry::Patch { .. }))
            .count()
    }

    /// Check if the series file is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Check if the series file contains a patch
    pub fn contains(&self, name: &str) -> bool {
        self.entries.iter().any(|entry| match entry {
            SeriesEntry::Patch {
                name: entry_name, ..
            } => entry_name == name,
            _ => false,
        })
    }

    /// Read a series file from a reader
    pub fn read<R: std::io::Read>(reader: R) -> std::io::Result<Self> {
        let mut series = Self::new();

        let reader = std::io::BufReader::new(reader);

        for line in reader.lines() {
            let line = line?;
            let line = line.trim();

            if line.is_empty() {
                continue;
            }

            if line.starts_with('#') {
                series.entries.push(SeriesEntry::Comment(line.to_string()));
                continue;
            }

            let mut parts = line.split_whitespace();
            let name = parts.next().ok_or_else(|| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "missing patch name in series file",
                )
            })?;
            let options = parts.map(|s| s.to_string()).collect();

            series.entries.push(SeriesEntry::Patch {
                name: name.to_string(),
                options,
            });
        }

        Ok(series)
    }

    /// Remove a patch from the series file
    pub fn remove(&mut self, name: &str) {
        self.entries.retain(|entry| match entry {
            SeriesEntry::Patch {
                name: entry_name, ..
            } => entry_name != name,
            _ => true,
        });
    }

    /// Get an iterator over the patch names in the series file
    pub fn patches(&self) -> impl Iterator<Item = &str> {
        self.entries.iter().filter_map(|entry| match entry {
            SeriesEntry::Patch { name, .. } => Some(name.as_str()),
            _ => None,
        })
    }

    /// Append a patch to the series file
    pub fn append(&mut self, name: &str, options: Option<&[String]>) {
        self.entries.push(SeriesEntry::Patch {
            name: name.to_string(),
            options: options.map(|options| options.to_vec()).unwrap_or_default(),
        });
    }

    /// Write the series file to a writer
    pub fn write<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        for entry in &self.entries {
            match entry {
                SeriesEntry::Patch { name, options } => {
                    write!(writer, "{}", name)?;
                    for option in options {
                        write!(writer, " {}", option)?;
                    }
                    writeln!(writer)?;
                }
                SeriesEntry::Comment(comment) => {
                    writeln!(writer, "# {}", comment)?;
                }
            }
        }

        Ok(())
    }

    /// Get an iterator over the entries in the series file
    pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, SeriesEntry> {
        self.entries.iter()
    }
}

impl std::ops::Index<usize> for Series {
    type Output = SeriesEntry;

    fn index(&self, index: usize) -> &Self::Output {
        &self.entries[index]
    }
}

impl Default for Series {
    fn default() -> Self {
        Self::new()
    }
}

/// Read a .pc/.quilt_patches file
pub fn read_quilt_patches<R: std::io::Read>(mut reader: R) -> std::path::PathBuf {
    let mut p = String::new();
    reader.read_to_string(&mut p).unwrap();
    p.into()
}

/// Read a .pc/.quilt_series file
pub fn read_quilt_series<R: std::io::Read>(mut reader: R) -> std::path::PathBuf {
    let mut s = String::new();
    reader.read_to_string(&mut s).unwrap();
    s.into()
}

/// A quilt patch
pub struct QuiltPatch {
    /// The name of the patch
    pub name: String,

    /// The options for the patch
    pub options: Vec<String>,

    /// The patch contents
    pub patch: Vec<u8>,
}

impl QuiltPatch {
    /// Get the patch contents as a byte slice
    pub fn as_bytes(&self) -> &[u8] {
        &self.patch
    }

    /// Get the name of the patch
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the patch options
    pub fn options(&self) -> &[String] {
        &self.options
    }

    /// Get the patch contents
    pub fn parse(&self) -> Result<Vec<crate::unified::UnifiedPatch>, crate::unified::Error> {
        let lines = self.patch.split_inclusive(|&b| b == b'\n');
        crate::unified::parse_patches(lines.map(|x| x.to_vec()))
            .filter_map(|patch| match patch {
                Ok(crate::unified::PlainOrBinaryPatch::Plain(patch)) => Some(Ok(patch)),
                Ok(crate::unified::PlainOrBinaryPatch::Binary(_)) => None,
                Err(err) => Some(Err(err)),
            })
            .collect()
    }
}

/// Read quilt patches from a directory.
pub fn iter_quilt_patches(directory: &std::path::Path) -> impl Iterator<Item = QuiltPatch> + '_ {
    let series_path = directory.join("series");

    let series = if series_path.exists() {
        Series::read(std::fs::File::open(series_path).unwrap()).unwrap()
    } else {
        Series::new()
    };

    series
        .iter()
        .filter_map(move |entry| {
            let (patch, options) = match entry {
                SeriesEntry::Patch { name, options } => (name, options),
                SeriesEntry::Comment(_) => return None,
            };
            let p = directory.join(patch);
            let lines = std::fs::read_to_string(p).unwrap();
            Some(QuiltPatch {
                name: patch.to_string(),
                patch: lines.into_bytes(),
                options: options.clone(),
            })
        })
        .collect::<Vec<_>>()
        .into_iter()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_series_read() {
        let series = Series::read(
            r#"0001-foo.patch
# This is a comment
0002-bar.patch --reverse
0003-baz.patch --reverse --fuzz=3
"#
            .as_bytes(),
        )
        .unwrap();
        assert_eq!(series.len(), 3);
        assert_eq!(
            series[0],
            SeriesEntry::Patch {
                name: "0001-foo.patch".to_string(),
                options: vec![]
            }
        );
        assert_eq!(
            series[1],
            SeriesEntry::Comment("# This is a comment".to_string())
        );
        assert_eq!(
            series[2],
            SeriesEntry::Patch {
                name: "0002-bar.patch".to_string(),
                options: vec!["--reverse".to_string()]
            }
        );
        assert_eq!(
            series[3],
            SeriesEntry::Patch {
                name: "0003-baz.patch".to_string(),
                options: vec!["--reverse".to_string(), "--fuzz=3".to_string()]
            }
        );
    }

    #[test]
    fn test_series_write() {
        let mut series = Series::new();
        series.append("0001-foo.patch", None);
        series.append("0002-bar.patch", Some(&["--reverse".to_string()]));
        series.append(
            "0003-baz.patch",
            Some(&["--reverse".to_string(), "--fuzz=3".to_string()]),
        );

        let mut writer = vec![];
        series.write(&mut writer).unwrap();
        let series = String::from_utf8(writer).unwrap();
        assert_eq!(
            series,
            "0001-foo.patch\n0002-bar.patch --reverse\n0003-baz.patch --reverse --fuzz=3\n"
        );
    }

    #[test]
    fn test_series_remove() {
        let mut series = Series::new();
        series.append("0001-foo.patch", None);
        series.append("0002-bar.patch", Some(&["--reverse".to_string()]));
        series.append(
            "0003-baz.patch",
            Some(&["--reverse".to_string(), "--fuzz=3".to_string()]),
        );

        series.remove("0002-bar.patch");

        let mut writer = vec![];
        series.write(&mut writer).unwrap();
        let series = String::from_utf8(writer).unwrap();
        assert_eq!(
            series,
            "0001-foo.patch\n0003-baz.patch --reverse --fuzz=3\n"
        );
    }

    #[test]
    fn test_series_contains() {
        let mut series = Series::new();
        series.append("0001-foo.patch", None);
        series.append("0002-bar.patch", Some(&["--reverse".to_string()]));
        series.append(
            "0003-baz.patch",
            Some(&["--reverse".to_string(), "--fuzz=3".to_string()]),
        );

        assert!(series.contains("0002-bar.patch"));
        assert!(!series.contains("0004-qux.patch"));
    }

    #[test]
    fn test_series_patches() {
        let mut series = Series::new();
        series.append("0001-foo.patch", None);
        series.append("0002-bar.patch", Some(&["--reverse".to_string()]));
        series.append(
            "0003-baz.patch",
            Some(&["--reverse".to_string(), "--fuzz=3".to_string()]),
        );

        let patches: Vec<_> = series.patches().collect();
        assert_eq!(
            patches,
            &["0001-foo.patch", "0002-bar.patch", "0003-baz.patch"]
        );
    }

    #[test]
    fn test_series_is_empty() {
        let series = Series::new();
        assert!(series.is_empty());

        let mut series = Series::new();
        series.append("0001-foo.patch", None);
        assert!(!series.is_empty());
    }

    #[test]
    fn test_quilt_patch_parse() {
        let patch = QuiltPatch {
            name: "0001-foo.patch".to_string(),
            options: vec![],
            patch: b"--- a/foo\n+++ b/foo\n@@ -1,3 +1,3 @@\n foo\n bar\n-bar\n+bar\n".to_vec(),
        };

        let patches = patch.parse().unwrap();
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0],
            crate::unified::UnifiedPatch {
                orig_name: b"a/foo".to_vec(),
                mod_name: b"b/foo".to_vec(),
                orig_ts: None,
                mod_ts: None,
                hunks: vec![crate::unified::Hunk {
                    orig_pos: 1,
                    orig_range: 3,
                    mod_pos: 1,
                    mod_range: 3,
                    lines: vec![
                        crate::unified::HunkLine::ContextLine(b"foo\n".to_vec()),
                        crate::unified::HunkLine::ContextLine(b"bar\n".to_vec()),
                        crate::unified::HunkLine::RemoveLine(b"bar\n".to_vec()),
                        crate::unified::HunkLine::InsertLine(b"bar\n".to_vec())
                    ],
                    tail: None
                }]
            }
        );
    }

    #[test]
    fn test_series_read_empty_lines() {
        let series = Series::read(
            r#"0001-foo.patch

0002-bar.patch

"#
            .as_bytes(),
        )
        .unwrap();
        assert_eq!(series.len(), 2);
        assert_eq!(
            series[0],
            SeriesEntry::Patch {
                name: "0001-foo.patch".to_string(),
                options: vec![]
            }
        );
        assert_eq!(
            series[1],
            SeriesEntry::Patch {
                name: "0002-bar.patch".to_string(),
                options: vec![]
            }
        );
    }

    #[test]
    fn test_series_read_whitespace_lines() {
        let series = Series::read("0001-foo.patch \n   \n0002-bar.patch\n".as_bytes()).unwrap();
        assert_eq!(series.len(), 2);
        assert_eq!(
            series[0],
            SeriesEntry::Patch {
                name: "0001-foo.patch".to_string(),
                options: vec![]
            }
        );
        assert_eq!(
            series[1],
            SeriesEntry::Patch {
                name: "0002-bar.patch".to_string(),
                options: vec![]
            }
        );
    }
}