summaryrefslogtreecommitdiffstats
path: root/vendor/unified-diff
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/unified-diff
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/unified-diff')
-rw-r--r--vendor/unified-diff/.cargo-checksum.json1
-rw-r--r--vendor/unified-diff/Cargo.lock16
-rw-r--r--vendor/unified-diff/Cargo.toml26
-rw-r--r--vendor/unified-diff/LICENSE-APACHE176
-rw-r--r--vendor/unified-diff/LICENSE-MIT23
-rw-r--r--vendor/unified-diff/README.md41
-rw-r--r--vendor/unified-diff/src/lib.rs782
-rw-r--r--vendor/unified-diff/src/main.rs55
8 files changed, 1120 insertions, 0 deletions
diff --git a/vendor/unified-diff/.cargo-checksum.json b/vendor/unified-diff/.cargo-checksum.json
new file mode 100644
index 000000000..f05cedbd2
--- /dev/null
+++ b/vendor/unified-diff/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.lock":"9b2054193327a3fe1b5c1f415e79724b986049b91e47ea65076c82a739f25661","Cargo.toml":"bb089781e8727948b88b3605033fd7803b2285fb2ad4d50c00396f4ccc34ecb0","LICENSE-APACHE":"62c7a1e35f56406896d7aa7ca52d0cc0d272ac022b5d2796e7d6905db8a3636a","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"e17f411d50ca5c7b920da38366b407c25f76b723598c87501723444a0799ecf8","src/lib.rs":"165ea387f42d59fd98f4fc48b07b09ddff0bd00426431827f5fbb18d94fecdfc","src/main.rs":"7be0771064cce707d6e071f8613cded6cb310e1849c1a86918ff9aa7d515c5b6"},"package":"496a3d395ed0c30f411ceace4a91f7d93b148fb5a9b383d5d4cff7850f048d5f"} \ No newline at end of file
diff --git a/vendor/unified-diff/Cargo.lock b/vendor/unified-diff/Cargo.lock
new file mode 100644
index 000000000..a8e784589
--- /dev/null
+++ b/vendor/unified-diff/Cargo.lock
@@ -0,0 +1,16 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "diff"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
+
+[[package]]
+name = "unified-diff"
+version = "0.2.1"
+dependencies = [
+ "diff",
+]
diff --git a/vendor/unified-diff/Cargo.toml b/vendor/unified-diff/Cargo.toml
new file mode 100644
index 000000000..fe895fb30
--- /dev/null
+++ b/vendor/unified-diff/Cargo.toml
@@ -0,0 +1,26 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+edition = "2018"
+name = "unified-diff"
+version = "0.2.1"
+authors = ["Michael Howell <michael@notriddle.com>", "The Rust Project Developers"]
+exclude = ["fuzz"]
+description = "An implementation of the GNU unified diff format"
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/notriddle/rust-unified-diff"
+
+[[bin]]
+name = "unified-diff"
+[dependencies.diff]
+version = "0.1.10"
diff --git a/vendor/unified-diff/LICENSE-APACHE b/vendor/unified-diff/LICENSE-APACHE
new file mode 100644
index 000000000..1b5ec8b78
--- /dev/null
+++ b/vendor/unified-diff/LICENSE-APACHE
@@ -0,0 +1,176 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
diff --git a/vendor/unified-diff/LICENSE-MIT b/vendor/unified-diff/LICENSE-MIT
new file mode 100644
index 000000000..31aa79387
--- /dev/null
+++ b/vendor/unified-diff/LICENSE-MIT
@@ -0,0 +1,23 @@
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/vendor/unified-diff/README.md b/vendor/unified-diff/README.md
new file mode 100644
index 000000000..103c01053
--- /dev/null
+++ b/vendor/unified-diff/README.md
@@ -0,0 +1,41 @@
+A GNU unified diff generator. Oracle tested against GNU patch 2.7.6
+
+Based on the incomplete diff generator in https://github.com/rust-lang/rust/blob/master/src/tools/compiletest/src/runtest.rs,
+but it implements a different format.
+
+```
+~/unified-diff$ cargo run Cargo.lock Cargo.toml
+ Finished dev [unoptimized + debuginfo] target(s) in 0.00s
+ Running `target/debug/unified-diff Cargo.lock Cargo.toml`
+--- Cargo.lock
++++ Cargo.toml
+@@ -1,14 +1,14 @@
+-# This file is automatically @generated by Cargo.
+-# It is not intended for manual editing.
+-[[package]]
+-name = "diff"
+-version = "0.1.12"
+-source = "registry+https://github.com/rust-lang/crates.io-index"
+-checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
+-
+-[[package]]
++[package]
+ name = "unified-diff"
+ version = "0.1.0"
+-dependencies = [
+- "diff",
++authors = [
++ "Michael Howell <michael@notriddle.com>",
++ "The Rust Project Developers"
+ ]
++edition = "2018"
++
++[[bin]]
++name = "unified-diff"
++
++[dependencies]
++diff = "0.1.10"
+~/unified-diff$ rustup override set nightly
+~/unified-diff$ cargo fuzz run fuzz_patch
+```
+
diff --git a/vendor/unified-diff/src/lib.rs b/vendor/unified-diff/src/lib.rs
new file mode 100644
index 000000000..2b756a481
--- /dev/null
+++ b/vendor/unified-diff/src/lib.rs
@@ -0,0 +1,782 @@
+use std::collections::VecDeque;
+use std::io::Write;
+
+#[derive(Debug, PartialEq)]
+pub enum DiffLine {
+ Context(Vec<u8>),
+ Expected(Vec<u8>),
+ Actual(Vec<u8>),
+ MissingNL,
+}
+
+#[derive(Debug, PartialEq)]
+struct Mismatch {
+ pub line_number_expected: u32,
+ pub line_number_actual: u32,
+ pub lines: Vec<DiffLine>,
+}
+
+impl Mismatch {
+ fn new(line_number_expected: u32, line_number_actual: u32) -> Mismatch {
+ Mismatch {
+ line_number_expected,
+ line_number_actual,
+ lines: Vec::new(),
+ }
+ }
+}
+
+// Produces a diff between the expected output and actual output.
+fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatch> {
+ let mut line_number_expected = 1;
+ let mut line_number_actual = 1;
+ let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size);
+ let mut lines_since_mismatch = context_size + 1;
+ let mut results = Vec::new();
+ let mut mismatch = Mismatch::new(0, 0);
+
+ let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect();
+ let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect();
+
+ debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1);
+ // ^ means that underflow here is impossible
+ let expected_lines_count = expected_lines.len() as u32 - 1;
+ let actual_lines_count = actual_lines.len() as u32 - 1;
+
+ if expected_lines.last() == Some(&&b""[..]) {
+ expected_lines.pop();
+ }
+
+ if actual_lines.last() == Some(&&b""[..]) {
+ actual_lines.pop();
+ }
+
+ for result in diff::slice(&expected_lines, &actual_lines) {
+ match result {
+ diff::Result::Left(str) => {
+ if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
+ results.push(mismatch);
+ mismatch = Mismatch::new(
+ line_number_expected - context_queue.len() as u32,
+ line_number_actual - context_queue.len() as u32,
+ );
+ }
+
+ while let Some(line) = context_queue.pop_front() {
+ mismatch.lines.push(DiffLine::Context(line.to_vec()));
+ }
+
+ if mismatch.lines.last() == Some(&DiffLine::MissingNL) {
+ mismatch.lines.pop();
+ match mismatch.lines.pop() {
+ Some(DiffLine::Actual(res)) => {
+ // We have to make sure that Actual (the + lines)
+ // always come after Expected (the - lines)
+ mismatch.lines.push(DiffLine::Expected(str.to_vec()));
+ if line_number_expected > expected_lines_count {
+ mismatch.lines.push(DiffLine::MissingNL)
+ }
+ mismatch.lines.push(DiffLine::Actual(res));
+ mismatch.lines.push(DiffLine::MissingNL);
+ }
+ _ => unreachable!("unterminated Left and Common lines shouldn't be followed by more Left lines"),
+ }
+ } else {
+ mismatch.lines.push(DiffLine::Expected(str.to_vec()));
+ if line_number_expected > expected_lines_count {
+ mismatch.lines.push(DiffLine::MissingNL)
+ }
+ }
+ line_number_expected += 1;
+ lines_since_mismatch = 0;
+ }
+ diff::Result::Right(str) => {
+ if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
+ results.push(mismatch);
+ mismatch = Mismatch::new(
+ line_number_expected - context_queue.len() as u32,
+ line_number_actual - context_queue.len() as u32,
+ );
+ }
+
+ while let Some(line) = context_queue.pop_front() {
+ debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL));
+ mismatch.lines.push(DiffLine::Context(line.to_vec()));
+ }
+
+ mismatch.lines.push(DiffLine::Actual(str.to_vec()));
+ if line_number_actual > actual_lines_count {
+ mismatch.lines.push(DiffLine::MissingNL)
+ }
+ line_number_actual += 1;
+ lines_since_mismatch = 0;
+ }
+ diff::Result::Both(str, _) => {
+ // if one of them is missing a newline and the other isn't, then they don't actually match
+ if (line_number_actual > actual_lines_count)
+ && (line_number_expected > expected_lines_count)
+ {
+ if context_queue.len() < context_size {
+ while let Some(line) = context_queue.pop_front() {
+ debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL));
+ mismatch.lines.push(DiffLine::Context(line.to_vec()));
+ }
+ if lines_since_mismatch < context_size {
+ mismatch.lines.push(DiffLine::Context(str.to_vec()));
+ mismatch.lines.push(DiffLine::MissingNL);
+ }
+ }
+ lines_since_mismatch = 0;
+ } else if line_number_actual > actual_lines_count {
+ if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
+ results.push(mismatch);
+ mismatch = Mismatch::new(
+ line_number_expected - context_queue.len() as u32,
+ line_number_actual - context_queue.len() as u32,
+ );
+ }
+ while let Some(line) = context_queue.pop_front() {
+ debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL));
+ mismatch.lines.push(DiffLine::Context(line.to_vec()));
+ }
+ mismatch.lines.push(DiffLine::Expected(str.to_vec()));
+ mismatch.lines.push(DiffLine::Actual(str.to_vec()));
+ mismatch.lines.push(DiffLine::MissingNL);
+ lines_since_mismatch = 0;
+ } else if line_number_expected > expected_lines_count {
+ if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
+ results.push(mismatch);
+ mismatch = Mismatch::new(
+ line_number_expected - context_queue.len() as u32,
+ line_number_actual - context_queue.len() as u32,
+ );
+ }
+ while let Some(line) = context_queue.pop_front() {
+ debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL));
+ mismatch.lines.push(DiffLine::Context(line.to_vec()));
+ }
+ mismatch.lines.push(DiffLine::Expected(str.to_vec()));
+ mismatch.lines.push(DiffLine::MissingNL);
+ mismatch.lines.push(DiffLine::Actual(str.to_vec()));
+ lines_since_mismatch = 0;
+ } else {
+ debug_assert!(context_queue.len() <= context_size);
+ if context_queue.len() >= context_size {
+ let _ = context_queue.pop_front();
+ }
+ if lines_since_mismatch < context_size {
+ mismatch.lines.push(DiffLine::Context(str.to_vec()));
+ } else if context_size > 0 {
+ context_queue.push_back(str);
+ }
+ lines_since_mismatch += 1;
+ }
+ line_number_expected += 1;
+ line_number_actual += 1;
+ }
+ }
+ }
+
+ results.push(mismatch);
+ results.remove(0);
+
+ if results.len() == 0 && expected_lines_count != actual_lines_count {
+ let mut mismatch = Mismatch::new(expected_lines.len() as u32, actual_lines.len() as u32);
+ // empty diff and only expected lines has a missing line at end
+ if expected_lines_count != expected_lines.len() as u32 {
+ mismatch.lines.push(DiffLine::Expected(
+ expected_lines
+ .pop()
+ .expect("can't be empty; produced by split()")
+ .to_vec(),
+ ));
+ mismatch.lines.push(DiffLine::MissingNL);
+ mismatch.lines.push(DiffLine::Actual(
+ actual_lines
+ .pop()
+ .expect("can't be empty; produced by split()")
+ .to_vec(),
+ ));
+ results.push(mismatch);
+ } else if actual_lines_count != actual_lines.len() as u32 {
+ mismatch.lines.push(DiffLine::Expected(
+ expected_lines
+ .pop()
+ .expect("can't be empty; produced by split()")
+ .to_vec(),
+ ));
+ mismatch.lines.push(DiffLine::Actual(
+ actual_lines
+ .pop()
+ .expect("can't be empty; produced by split()")
+ .to_vec(),
+ ));
+ mismatch.lines.push(DiffLine::MissingNL);
+ results.push(mismatch);
+ }
+ }
+
+ results
+}
+
+pub fn diff(
+ expected: &[u8],
+ expected_filename: &str,
+ actual: &[u8],
+ actual_filename: &str,
+ context_size: usize,
+) -> Vec<u8> {
+ let mut output =
+ format!("--- {}\t\n+++ {}\t\n", expected_filename, actual_filename).into_bytes();
+ let diff_results = make_diff(expected, actual, context_size);
+ if diff_results.len() == 0 {
+ return Vec::new();
+ };
+ for result in diff_results {
+ let mut line_number_expected = result.line_number_expected;
+ let mut line_number_actual = result.line_number_actual;
+ let mut expected_count = 0;
+ let mut actual_count = 0;
+ for line in &result.lines {
+ match line {
+ DiffLine::Expected(_) => {
+ expected_count += 1;
+ }
+ DiffLine::Context(_) => {
+ expected_count += 1;
+ actual_count += 1;
+ }
+ DiffLine::Actual(_) => {
+ actual_count += 1;
+ }
+ DiffLine::MissingNL => {}
+ }
+ }
+ // Let's imagine this diff file
+ //
+ // --- a/something
+ // +++ b/something
+ // @@ -2,0 +3,1 @@
+ // + x
+ //
+ // In the unified diff format as implemented by GNU diff and patch,
+ // this is an instruction to insert the x *after* the preexisting line 2,
+ // not before. You can demonstrate it this way:
+ //
+ // $ echo -ne '--- a/something\t\n+++ b/something\t\n@@ -2,0 +3,1 @@\n+ x\n' > diff
+ // $ echo -ne 'a\nb\nc\nd\n' > something
+ // $ patch -p1 < diff
+ // patching file something
+ // $ cat something
+ // a
+ // b
+ // x
+ // c
+ // d
+ //
+ // Notice how the x winds up at line 3, not line 2. This requires contortions to
+ // work with our diffing algorithm, which keeps track of the "intended destination line",
+ // not a line that things are supposed to be placed after. It's changing the first number,
+ // not the second, that actually affects where the x goes.
+ //
+ // # change the first number from 2 to 3, and now the x is on line 4 (it's placed after line 3)
+ // $ echo -ne '--- a/something\t\n+++ b/something\t\n@@ -3,0 +3,1 @@\n+ x\n' > diff
+ // $ echo -ne 'a\nb\nc\nd\n' > something
+ // $ patch -p1 < diff
+ // patching file something
+ // $ cat something
+ // a
+ // b
+ // c
+ // x
+ // d
+ // # change the third number from 3 to 1000, and it's obvious that it's the first number that's
+ // # actually being read
+ // $ echo -ne '--- a/something\t\n+++ b/something\t\n@@ -2,0 +1000,1 @@\n+ x\n' > diff
+ // $ echo -ne 'a\nb\nc\nd\n' > something
+ // $ patch -p1 < diff
+ // patching file something
+ // $ cat something
+ // a
+ // b
+ // x
+ // c
+ // d
+ //
+ // Now watch what happens if I add a context line:
+ //
+ // $ echo -ne '--- a/something\t\n+++ b/something\t\n@@ -2,1 +3,2 @@\n+ x\n c\n' > diff
+ // $ echo -ne 'a\nb\nc\nd\n' > something
+ // $ patch -p1 < diff
+ // patching file something
+ // Hunk #1 succeeded at 3 (offset 1 line).
+ //
+ // It technically "succeeded", but this is a warning. We want to produce clean diffs.
+ // Now that I have a context line, I'm supposed to say what line it's actually on, which is the
+ // line that the x will wind up on, and not the line immediately before.
+ //
+ // $ echo -ne '--- a/something\t\n+++ b/something\t\n@@ -3,1 +3,2 @@\n+ x\n c\n' > diff
+ // $ echo -ne 'a\nb\nc\nd\n' > something
+ // $ patch -p1 < diff
+ // patching file something
+ // $ cat something
+ // a
+ // b
+ // x
+ // c
+ // d
+ //
+ // I made this comment because this stuff is not obvious from GNU's
+ // documentation on the format at all.
+ if expected_count == 0 {
+ line_number_expected -= 1
+ }
+ if actual_count == 0 {
+ line_number_actual -= 1
+ }
+ let exp_ct = if expected_count == 1 {
+ String::new()
+ } else {
+ format!(",{}", expected_count)
+ };
+ let act_ct = if actual_count == 1 {
+ String::new()
+ } else {
+ format!(",{}", actual_count)
+ };
+ writeln!(
+ output,
+ "@@ -{}{} +{}{} @@",
+ line_number_expected, exp_ct, line_number_actual, act_ct
+ )
+ .expect("write to Vec is infallible");
+ for line in result.lines {
+ match line {
+ DiffLine::Expected(e) => {
+ write!(output, "-").expect("write to Vec is infallible");
+ output.write_all(&e).expect("write to Vec is infallible");
+ writeln!(output).unwrap();
+ }
+ DiffLine::Context(c) => {
+ write!(output, " ").expect("write to Vec is infallible");
+ output.write_all(&c).expect("write to Vec is infallible");
+ writeln!(output).unwrap();
+ }
+ DiffLine::Actual(r) => {
+ write!(output, "+",).expect("write to Vec is infallible");
+ output.write_all(&r).expect("write to Vec is infallible");
+ writeln!(output).unwrap();
+ }
+ DiffLine::MissingNL => {
+ writeln!(output, r"\ No newline at end of file")
+ .expect("write to Vec is infallible");
+ }
+ }
+ }
+ }
+ output
+}
+
+#[test]
+fn test_permutations() {
+ // test all possible six-line files.
+ for &a in &[0, 1, 2] {
+ for &b in &[0, 1, 2] {
+ for &c in &[0, 1, 2] {
+ for &d in &[0, 1, 2] {
+ for &e in &[0, 1, 2] {
+ for &f in &[0, 1, 2] {
+ use std::fs::{self, File};
+ use std::io::Write;
+ use std::process::Command;
+ let mut alef = Vec::new();
+ let mut bet = Vec::new();
+ alef.write_all(if a == 0 { b"a\n" } else { b"b\n" })
+ .unwrap();
+ if a != 2 {
+ bet.write_all(b"b\n").unwrap();
+ }
+ alef.write_all(if b == 0 { b"c\n" } else { b"d\n" })
+ .unwrap();
+ if b != 2 {
+ bet.write_all(b"d\n").unwrap();
+ }
+ alef.write_all(if c == 0 { b"e\n" } else { b"f\n" })
+ .unwrap();
+ if c != 2 {
+ bet.write_all(b"f\n").unwrap();
+ }
+ alef.write_all(if d == 0 { b"g\n" } else { b"h\n" })
+ .unwrap();
+ if d != 2 {
+ bet.write_all(b"h\n").unwrap();
+ }
+ alef.write_all(if e == 0 { b"i\n" } else { b"j\n" })
+ .unwrap();
+ if e != 2 {
+ bet.write_all(b"j\n").unwrap();
+ }
+ alef.write_all(if f == 0 { b"k\n" } else { b"l\n" })
+ .unwrap();
+ if f != 2 {
+ bet.write_all(b"l\n").unwrap();
+ }
+ // This test diff is intentionally reversed.
+ // We want it to turn the alef into bet.
+ let diff = diff(&alef, "a/alef", &bet, "target/alef", 2);
+ File::create("target/ab.diff")
+ .unwrap()
+ .write_all(&diff)
+ .unwrap();
+ let mut fa = File::create("target/alef").unwrap();
+ fa.write_all(&alef[..]).unwrap();
+ let mut fb = File::create("target/bet").unwrap();
+ fb.write_all(&bet[..]).unwrap();
+ let _ = fa;
+ let _ = fb;
+ let output = Command::new("patch")
+ .arg("-p0")
+ .stdin(File::open("target/ab.diff").unwrap())
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ panic!("{:?}", output);
+ }
+ //println!("{}", String::from_utf8_lossy(&output.stdout));
+ //println!("{}", String::from_utf8_lossy(&output.stderr));
+ let alef = fs::read("target/alef").unwrap();
+ assert_eq!(alef, bet);
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+#[test]
+fn test_permutations_missing_line_ending() {
+ // test all possible six-line files with missing newlines.
+ for &a in &[0, 1, 2] {
+ for &b in &[0, 1, 2] {
+ for &c in &[0, 1, 2] {
+ for &d in &[0, 1, 2] {
+ for &e in &[0, 1, 2] {
+ for &f in &[0, 1, 2] {
+ for &g in &[0, 1, 2] {
+ use std::fs::{self, File};
+ use std::io::Write;
+ use std::process::Command;
+ let mut alef = Vec::new();
+ let mut bet = Vec::new();
+ alef.write_all(if a == 0 { b"a\n" } else { b"b\n" })
+ .unwrap();
+ if a != 2 {
+ bet.write_all(b"b\n").unwrap();
+ }
+ alef.write_all(if b == 0 { b"c\n" } else { b"d\n" })
+ .unwrap();
+ if b != 2 {
+ bet.write_all(b"d\n").unwrap();
+ }
+ alef.write_all(if c == 0 { b"e\n" } else { b"f\n" })
+ .unwrap();
+ if c != 2 {
+ bet.write_all(b"f\n").unwrap();
+ }
+ alef.write_all(if d == 0 { b"g\n" } else { b"h\n" })
+ .unwrap();
+ if d != 2 {
+ bet.write_all(b"h\n").unwrap();
+ }
+ alef.write_all(if e == 0 { b"i\n" } else { b"j\n" })
+ .unwrap();
+ if e != 2 {
+ bet.write_all(b"j\n").unwrap();
+ }
+ alef.write_all(if f == 0 { b"k\n" } else { b"l\n" })
+ .unwrap();
+ if f != 2 {
+ bet.write_all(b"l\n").unwrap();
+ }
+ match g {
+ 0 => {
+ alef.pop();
+ }
+ 1 => {
+ bet.pop();
+ }
+ 2 => {
+ alef.pop();
+ bet.pop();
+ }
+ _ => unreachable!(),
+ }
+ // This test diff is intentionally reversed.
+ // We want it to turn the alef into bet.
+ let diff = diff(&alef, "a/alefn", &bet, "target/alefn", 2);
+ File::create("target/abn.diff")
+ .unwrap()
+ .write_all(&diff)
+ .unwrap();
+ let mut fa = File::create("target/alefn").unwrap();
+ fa.write_all(&alef[..]).unwrap();
+ let mut fb = File::create("target/betn").unwrap();
+ fb.write_all(&bet[..]).unwrap();
+ let _ = fa;
+ let _ = fb;
+ let output = Command::new("patch")
+ .arg("-p0")
+ .stdin(File::open("target/abn.diff").unwrap())
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ panic!("{:?}", output);
+ }
+ //println!("{}", String::from_utf8_lossy(&output.stdout));
+ //println!("{}", String::from_utf8_lossy(&output.stderr));
+ let alef = fs::read("target/alefn").unwrap();
+ assert_eq!(alef, bet);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+#[test]
+fn test_permutations_empty_lines() {
+ // test all possible six-line files with missing newlines.
+ for &a in &[0, 1, 2] {
+ for &b in &[0, 1, 2] {
+ for &c in &[0, 1, 2] {
+ for &d in &[0, 1, 2] {
+ for &e in &[0, 1, 2] {
+ for &f in &[0, 1, 2] {
+ for &g in &[0, 1, 2, 3] {
+ use std::fs::{self, File};
+ use std::io::Write;
+ use std::process::Command;
+ let mut alef = Vec::new();
+ let mut bet = Vec::new();
+ alef.write_all(if a == 0 { b"\n" } else { b"b\n" }).unwrap();
+ if a != 2 {
+ bet.write_all(b"b\n").unwrap();
+ }
+ alef.write_all(if b == 0 { b"\n" } else { b"d\n" }).unwrap();
+ if b != 2 {
+ bet.write_all(b"d\n").unwrap();
+ }
+ alef.write_all(if c == 0 { b"\n" } else { b"f\n" }).unwrap();
+ if c != 2 {
+ bet.write_all(b"f\n").unwrap();
+ }
+ alef.write_all(if d == 0 { b"\n" } else { b"h\n" }).unwrap();
+ if d != 2 {
+ bet.write_all(b"h\n").unwrap();
+ }
+ alef.write_all(if e == 0 { b"\n" } else { b"j\n" }).unwrap();
+ if e != 2 {
+ bet.write_all(b"j\n").unwrap();
+ }
+ alef.write_all(if f == 0 { b"\n" } else { b"l\n" }).unwrap();
+ if f != 2 {
+ bet.write_all(b"l\n").unwrap();
+ }
+ match g {
+ 0 => {
+ alef.pop();
+ }
+ 1 => {
+ bet.pop();
+ }
+ 2 => {
+ alef.pop();
+ bet.pop();
+ }
+ 3 => {}
+ _ => unreachable!(),
+ }
+ // This test diff is intentionally reversed.
+ // We want it to turn the alef into bet.
+ let diff = diff(&alef, "a/alef_", &bet, "target/alef_", 2);
+ File::create("target/ab_.diff")
+ .unwrap()
+ .write_all(&diff)
+ .unwrap();
+ let mut fa = File::create("target/alef_").unwrap();
+ fa.write_all(&alef[..]).unwrap();
+ let mut fb = File::create("target/bet_").unwrap();
+ fb.write_all(&bet[..]).unwrap();
+ let _ = fa;
+ let _ = fb;
+ let output = Command::new("patch")
+ .arg("-p0")
+ .stdin(File::open("target/ab_.diff").unwrap())
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ panic!("{:?}", output);
+ }
+ //println!("{}", String::from_utf8_lossy(&output.stdout));
+ //println!("{}", String::from_utf8_lossy(&output.stderr));
+ let alef = fs::read("target/alef_").unwrap();
+ assert_eq!(alef, bet);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+#[test]
+fn test_permutations_missing_lines() {
+ // test all possible six-line files.
+ for &a in &[0, 1, 2] {
+ for &b in &[0, 1, 2] {
+ for &c in &[0, 1, 2] {
+ for &d in &[0, 1, 2] {
+ for &e in &[0, 1, 2] {
+ for &f in &[0, 1, 2] {
+ use std::fs::{self, File};
+ use std::io::Write;
+ use std::process::Command;
+ let mut alef = Vec::new();
+ let mut bet = Vec::new();
+ alef.write_all(if a == 0 { b"a\n" } else { b"" }).unwrap();
+ if a != 2 {
+ bet.write_all(b"b\n").unwrap();
+ }
+ alef.write_all(if b == 0 { b"c\n" } else { b"" }).unwrap();
+ if b != 2 {
+ bet.write_all(b"d\n").unwrap();
+ }
+ alef.write_all(if c == 0 { b"e\n" } else { b"" }).unwrap();
+ if c != 2 {
+ bet.write_all(b"f\n").unwrap();
+ }
+ alef.write_all(if d == 0 { b"g\n" } else { b"" }).unwrap();
+ if d != 2 {
+ bet.write_all(b"h\n").unwrap();
+ }
+ alef.write_all(if e == 0 { b"i\n" } else { b"" }).unwrap();
+ if e != 2 {
+ bet.write_all(b"j\n").unwrap();
+ }
+ alef.write_all(if f == 0 { b"k\n" } else { b"" }).unwrap();
+ if f != 2 {
+ bet.write_all(b"l\n").unwrap();
+ }
+ // This test diff is intentionally reversed.
+ // We want it to turn the alef into bet.
+ let diff = diff(&alef, "a/alefx", &bet, "target/alefx", 2);
+ File::create("target/abx.diff")
+ .unwrap()
+ .write_all(&diff)
+ .unwrap();
+ let mut fa = File::create("target/alefx").unwrap();
+ fa.write_all(&alef[..]).unwrap();
+ let mut fb = File::create("target/betx").unwrap();
+ fb.write_all(&bet[..]).unwrap();
+ let _ = fa;
+ let _ = fb;
+ let output = Command::new("patch")
+ .arg("-p0")
+ .stdin(File::open("target/abx.diff").unwrap())
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ panic!("{:?}", output);
+ }
+ //println!("{}", String::from_utf8_lossy(&output.stdout));
+ //println!("{}", String::from_utf8_lossy(&output.stderr));
+ let alef = fs::read("target/alefx").unwrap();
+ assert_eq!(alef, bet);
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+#[test]
+fn test_permutations_reverse() {
+ // test all possible six-line files.
+ for &a in &[0, 1, 2] {
+ for &b in &[0, 1, 2] {
+ for &c in &[0, 1, 2] {
+ for &d in &[0, 1, 2] {
+ for &e in &[0, 1, 2] {
+ for &f in &[0, 1, 2] {
+ use std::fs::{self, File};
+ use std::io::Write;
+ use std::process::Command;
+ let mut alef = Vec::new();
+ let mut bet = Vec::new();
+ alef.write_all(if a == 0 { b"a\n" } else { b"f\n" })
+ .unwrap();
+ if a != 2 {
+ bet.write_all(b"a\n").unwrap();
+ }
+ alef.write_all(if b == 0 { b"b\n" } else { b"e\n" })
+ .unwrap();
+ if b != 2 {
+ bet.write_all(b"b\n").unwrap();
+ }
+ alef.write_all(if c == 0 { b"c\n" } else { b"d\n" })
+ .unwrap();
+ if c != 2 {
+ bet.write_all(b"c\n").unwrap();
+ }
+ alef.write_all(if d == 0 { b"d\n" } else { b"c\n" })
+ .unwrap();
+ if d != 2 {
+ bet.write_all(b"d\n").unwrap();
+ }
+ alef.write_all(if e == 0 { b"e\n" } else { b"b\n" })
+ .unwrap();
+ if e != 2 {
+ bet.write_all(b"e\n").unwrap();
+ }
+ alef.write_all(if f == 0 { b"f\n" } else { b"a\n" })
+ .unwrap();
+ if f != 2 {
+ bet.write_all(b"f\n").unwrap();
+ }
+ // This test diff is intentionally reversed.
+ // We want it to turn the alef into bet.
+ let diff = diff(&alef, "a/alefr", &bet, "target/alefr", 2);
+ File::create("target/abr.diff")
+ .unwrap()
+ .write_all(&diff)
+ .unwrap();
+ let mut fa = File::create("target/alefr").unwrap();
+ fa.write_all(&alef[..]).unwrap();
+ let mut fb = File::create("target/betr").unwrap();
+ fb.write_all(&bet[..]).unwrap();
+ let _ = fa;
+ let _ = fb;
+ let output = Command::new("patch")
+ .arg("-p0")
+ .stdin(File::open("target/abr.diff").unwrap())
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ panic!("{:?}", output);
+ }
+ //println!("{}", String::from_utf8_lossy(&output.stdout));
+ //println!("{}", String::from_utf8_lossy(&output.stderr));
+ let alef = fs::read("target/alefr").unwrap();
+ assert_eq!(alef, bet);
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/vendor/unified-diff/src/main.rs b/vendor/unified-diff/src/main.rs
new file mode 100644
index 000000000..16ff8e228
--- /dev/null
+++ b/vendor/unified-diff/src/main.rs
@@ -0,0 +1,55 @@
+// Sample program. Do not use.
+use std::env;
+use std::fs;
+use std::io::{self, Write};
+use std::process;
+fn main() {
+ let mut o = env::args_os();
+ // parse CLI
+ let exe = match o.next() {
+ Some(from) => from,
+ None => {
+ eprintln!("Usage: [exe] [from] [to]");
+ process::exit(1);
+ }
+ };
+ let from = match o.next() {
+ Some(from) => from,
+ None => {
+ eprintln!("Usage: {} [from] [to]", exe.to_string_lossy());
+ process::exit(1);
+ }
+ };
+ let to = match o.next() {
+ Some(from) => from,
+ None => {
+ eprintln!("Usage: {} [from] [to]", exe.to_string_lossy());
+ process::exit(1);
+ }
+ };
+ // read files
+ let from_content = match fs::read(&from) {
+ Ok(from_content) => from_content,
+ Err(e) => {
+ eprintln!("Failed to read from-file: {}", e);
+ process::exit(2);
+ }
+ };
+ let to_content = match fs::read(&to) {
+ Ok(to_content) => to_content,
+ Err(e) => {
+ eprintln!("Failed to read to-file: {}", e);
+ process::exit(2);
+ }
+ };
+ // run diff
+ io::stdout()
+ .write_all(&unified_diff::diff(
+ &from_content,
+ &from.to_string_lossy(),
+ &to_content,
+ &to.to_string_lossy(),
+ 1,
+ ))
+ .unwrap();
+}