gitpatch 0.7.1

Parse patches in the unified diff format
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
use chrono::DateTime;
use gitpatch::{File, FileMetadata, Line, ParseError, Patch};

use pretty_assertions::assert_eq;

#[test]
fn test_parse() -> Result<(), ParseError<'static>> {
    let sample = "\
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido\n";
    let patch = Patch::from_single(sample)?;
    assert_eq!(
        patch.old,
        File {
            path: "before.py".into(),
            meta: None
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "after.py".into(),
            meta: None
        }
    );
    assert!(patch.end_newline);

    assert_eq!(format!("{}\n", patch), sample);

    Ok(())
}

#[test]
fn test_parse_no_newline_indicator() -> Result<(), ParseError<'static>> {
    let sample = "\
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido
\\ No newline at end of file\n";
    let patch = Patch::from_single(sample)?;
    assert_eq!(
        patch.old,
        File {
            path: "before.py".into(),
            meta: None
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "after.py".into(),
            meta: None
        }
    );
    assert!(!patch.end_newline);

    assert_eq!(format!("{}\n", patch), sample);

    Ok(())
}

#[test]
fn test_parse_timestamps() -> Result<(), ParseError<'static>> {
    let sample = "\
--- before.py\t2002-02-21 23:30:39.942229878 -0800
+++ after.py\t2002-02-21 23:30:50 -0800
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido
\\ No newline at end of file";
    let patch = Patch::from_single(sample)?;
    assert_eq!(
        patch.old,
        File {
            path: "before.py".into(),
            meta: Some(FileMetadata::DateTime(
                DateTime::parse_from_rfc3339("2002-02-21T23:30:39.942229878-08:00").unwrap()
            )),
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "after.py".into(),
            meta: Some(FileMetadata::DateTime(
                DateTime::parse_from_rfc3339("2002-02-21T23:30:50-08:00").unwrap()
            )),
        }
    );
    assert!(!patch.end_newline);

    // to_string() uses Display but adds no trailing newline
    assert_eq!(patch.to_string(), sample);

    Ok(())
}

#[test]
fn test_parse_other() -> Result<(), ParseError<'static>> {
    let sample = "\
--- before.py\t08f78e0addd5bf7b7aa8887e406493e75e8d2b55
+++ after.py\te044048282ce75186ecc7a214fd3d9ba478a2816
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido\n";
    let patch = Patch::from_single(sample)?;
    assert_eq!(
        patch.old,
        File {
            path: "before.py".into(),
            meta: Some(FileMetadata::Other(
                "08f78e0addd5bf7b7aa8887e406493e75e8d2b55".into()
            )),
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "after.py".into(),
            meta: Some(FileMetadata::Other(
                "e044048282ce75186ecc7a214fd3d9ba478a2816".into()
            )),
        }
    );
    assert!(patch.end_newline);

    assert_eq!(format!("{}\n", patch), sample);

    Ok(())
}

#[test]
fn test_parse_escaped() -> Result<(), ParseError<'static>> {
    let sample = "\
--- before.py\t\"asdf \\\\ \\n \\t \\0 \\r \\\" \"
+++ \"My Work/after.py\"\t\"My project is cool! Wow!!; SELECT * FROM USERS;\"
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido\n";
    let patch = Patch::from_single(sample)?;
    assert_eq!(
        patch.old,
        File {
            path: "before.py".into(),
            meta: Some(FileMetadata::Other("asdf \\ \n \t \0 \r \" ".into())),
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "My Work/after.py".into(),
            meta: Some(FileMetadata::Other(
                "My project is cool! Wow!!; SELECT * FROM USERS;".into()
            )),
        }
    );
    assert!(patch.end_newline);

    assert_eq!(format!("{}\n", patch), sample);

    Ok(())
}

#[test]
fn test_parse_triple_plus_minus() -> Result<(), ParseError<'static>> {
    // Our parser has some hacky rules to make sure that lines starting with +++ or --- aren't
    // interpreted as regular addition/removal lines that could be part of a hunk. This test checks
    // to make sure that code is working.
    let sample = r#"--- main.c
+++ main.c
@@ -1,4 +1,7 @@
+#include<stdio.h>
+
 int main() {
 double a;
---a;
+++a;
+printf("%d\n", a);
 }
"#;
    let patches = Patch::from_multiple(sample).unwrap();
    assert_eq!(patches.len(), 1);

    let patch = &patches[0];
    assert_eq!(
        patch.old,
        File {
            path: "main.c".into(),
            meta: None
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "main.c".into(),
            meta: None
        }
    );
    assert!(patch.end_newline);

    assert_eq!(patch.hunks.len(), 1);
    assert_eq!(patch.hunks[0].lines.len(), 8);

    assert_eq!(format!("{}\n", patch), sample);

    Ok(())
}

//FIXME: This test should NOT panic. When we have more sophisticated chunk line parsing that
// actually takes the hunk ranges into account, the #[should_panic] annotation should be removed.
// See the FIXME comment on top of the chunk_line parser.
#[test]
#[should_panic]
fn test_parse_triple_plus_minus_hack() {
    // Our parser has some hacky rules to make sure that lines starting with +++ or --- aren't
    // interpreted as regular addition/removal lines that could be part of a hunk. This test
    // demonstrates that these rules are not foolproof. The only differences between this test and
    // test_parse_triple_plus_minus are `--- a` and `+++ a` vs `---a` and `+++a`. In either case,
    // we should be able to determine that those lines do not start a new patch based on the ranges
    // provided for the hunk.
    let sample = r#"--- main.c
+++ main.c
@@ -1,4 +1,7 @@
+#include<stdio.h>
+
 int main() {
 double a;
--- a;
+++ a;
+printf("%d\n", a);
 }
"#;
    let patches = Patch::from_multiple(sample).unwrap();
    assert_eq!(patches.len(), 1);

    let patch = &patches[0];
    assert_eq!(
        patch.old,
        File {
            path: "main.c".into(),
            meta: None
        }
    );
    assert_eq!(
        patch.new,
        File {
            path: "main.c".into(),
            meta: None
        }
    );
    assert!(patch.end_newline);

    assert_eq!(patch.hunks.len(), 1);
    assert_eq!(patch.hunks[0].lines.len(), 8);

    assert_eq!(format!("{}\n", patch), sample);
}

#[test]
fn binary_diff_with_crlf() {
    let sample = "Binary files old.bin and new.bin differ\r\n";

    let patch = Patch::from_single(sample).unwrap();
    assert_eq!(patch.old.path, "old.bin");
    assert_eq!(patch.old.meta, None);
    assert_eq!(patch.new.path, "new.bin");
    assert_eq!(patch.new.meta, None);
    assert_eq!(patch.hunks, []);
}

#[test]
fn binary_diff_with_ambiguous_names() {
    // The main goal here is that this doesn't crash or error:
    // the format with quotes means for some names there is no
    // unambiguous meaning.
    let sample = "Binary files a differ program and a patcher binary and a wiggler differ\r\n";

    let patch = Patch::from_single(sample).unwrap();
    assert_eq!(patch.old.path, "a differ program");
    assert_eq!(patch.old.meta, None);
    assert_eq!(patch.new.path, "a patcher binary and a wiggler");
    assert_eq!(patch.new.meta, None);
    assert_eq!(patch.hunks, []);
}

#[test]
fn binary_diff_with_spaces_in_name() {
    let sample = "Binary files an old binary and a new binary differ\n";

    let patch = Patch::from_single(sample).unwrap();
    assert_eq!(patch.old.path, "an old binary");
    assert_eq!(patch.old.meta, None);
    assert_eq!(patch.new.path, "a new binary");
    assert_eq!(patch.new.meta, None);
    assert_eq!(patch.hunks, []);
}

#[test]
fn single_binary_diff() {
    let sample = "Binary files old.bin and new.bin differ\n";

    let patch = Patch::from_single(sample).unwrap();
    assert_eq!(patch.old.path, "old.bin");
    assert_eq!(patch.old.meta, None);
    assert_eq!(patch.new.path, "new.bin");
    assert_eq!(patch.new.meta, None);
    assert_eq!(patch.hunks, []);
}

#[test]
fn multiple_binary_diffs() {
    let sample = "Binary files old.bin and new.bin differ
Binary files old1.bin and new1.bin differ
";
    let patches = Patch::from_multiple(sample).unwrap();
    assert_eq!(patches.len(), 2);
    assert_eq!(patches[0].old.path, "old.bin");
    assert_eq!(patches[1].old.path, "old1.bin");
}

#[test]
fn binary_diff_with_text_diff() {
    let sample = "\
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido
Binary files old.bin and new.bin differ
";
    let patches = Patch::from_multiple(sample).unwrap();
    assert_eq!(patches.len(), 2);
    assert_eq!(patches[0].old.path, "before.py");
    assert_eq!(patches[1].old.path, "old.bin");
}

#[test]
fn parses_file_renames_with_some_diff() {
    let sample = "\
diff --git a/src/ast.rs b/src/ast-2.rs
similarity index 99%
rename from src/ast.rs
rename to src/ast-2.rs
index d923f10..b47f160 100644
--- a/src/ast.rs
+++ b/src/ast-2.rs
@@ -4,6 +4,7 @@ use std::fmt;
 use chrono::{DateTime, FixedOffset};
 
 use crate::parser::{parse_multiple_patches, parse_single_patch, ParseError};
+use new_crate;
 
 /// A complete patch summarizing the differences between two files
 #[derive(Debug, Clone, Eq, PartialEq)]
";

    let patches = Patch::from_multiple(sample).unwrap();
    assert_eq!(patches.len(), 1);
    assert_eq!(patches[0].old.path, "a/src/ast.rs");
    assert_eq!(patches[0].new.path, "b/src/ast-2.rs");

    assert!(patches[0].hunks[0]
        .lines
        .iter()
        .any(|line| matches!(line, Line::Add("use new_crate;"))));
}

#[test]
fn parses_file_rename_with_100_pct_similarity() {
    let sample = "\
diff --git a/original-path.rs b/new-path.rs
similarity index 100%
rename from original-path.rs
rename to new-path.rs
";

    let patches = Patch::from_multiple(sample).unwrap();

    assert_eq!(patches.len(), 1);
    assert_eq!(patches[0].old.path, "original-path.rs");
    assert_eq!(patches[0].new.path, "new-path.rs");
    assert!(patches[0].hunks.is_empty());
}