Skip to content

Commit e72ea04

Browse files
committed
Add context and normal diff modes
1 parent 2e84164 commit e72ea04

File tree

15 files changed

+1526
-36
lines changed

15 files changed

+1526
-36
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/target
2+
/lib/normal-diff/target
3+
/lib/context-diff/target
4+
/lib/unified-diff/target
25
Cargo.lock
36
*.swp

Cargo.toml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
[package]
2-
name = "unified-diff"
3-
version = "0.2.1"
4-
authors = [
5-
"Michael Howell <[email protected]>",
6-
"The Rust Project Developers"
1+
[workspace]
2+
members = [
3+
"lib/unified-diff",
4+
"lib/context-diff",
5+
"lib/normal-diff",
6+
"bin/diff",
77
]
8-
edition = "2018"
9-
description = "An implementation of the GNU unified diff format"
10-
license = "MIT OR Apache-2.0"
11-
repository = "https://github.com/notriddle/rust-unified-diff"
12-
exclude = [ "fuzz" ]
13-
14-
[dependencies]
15-
diff = "0.1.10"

README.md

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
1-
A GNU unified diff generator. Oracle tested against GNU patch 2.7.6
1+
A package (currently just `diff`, but eventually more) of programs related to finding differences between files.
22

3-
Based on the incomplete diff generator in https://github.com/rust-lang/rust/blob/master/src/tools/compiletest/src/runtest.rs,
4-
but it implements a different format.
3+
Based on the incomplete diff generator in https://github.com/rust-lang/rust/blob/master/src/tools/compiletest/src/runtest.rs, and made to be compatible with GNU's diff and patch tools.
54

65
```
7-
~/unified-diff$ cargo run Cargo.lock Cargo.toml
6+
~/diffutils$ cargo run -- -u3 Cargo.lock Cargo.toml
87
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
9-
Running `target/debug/unified-diff Cargo.lock Cargo.toml`
8+
Running `target/debug/diff -u3 Cargo.lock Cargo.toml`
109
--- Cargo.lock
1110
+++ Cargo.toml
12-
@@ -1,14 +1,14 @@
11+
@@ -1,39 +1,7 @@
1312
-# This file is automatically @generated by Cargo.
1413
-# It is not intended for manual editing.
14+
-version = 3
15+
-
16+
-[[package]]
17+
-name = "context-diff"
18+
-version = "0.1.0"
19+
-dependencies = [
20+
- "diff 0.1.12",
21+
-]
22+
-
23+
-[[package]]
24+
-name = "diff"
25+
-version = "0.1.0"
26+
-dependencies = [
27+
- "context-diff",
28+
- "normal-diff",
29+
- "unified-diff",
30+
-]
31+
-
1532
-[[package]]
1633
-name = "diff"
1734
-version = "0.1.12"
1835
-source = "registry+https://github.com/rust-lang/crates.io-index"
1936
-checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
2037
-
2138
-[[package]]
22-
+[package]
23-
name = "unified-diff"
24-
version = "0.1.0"
39+
-name = "normal-diff"
40+
-version = "0.1.0"
41+
-dependencies = [
42+
- "diff 0.1.12",
43+
-]
44+
-
45+
-[[package]]
46+
-name = "unified-diff"
47+
-version = "0.3.0"
2548
-dependencies = [
26-
- "diff",
27-
+authors = [
28-
+ "Michael Howell <[email protected]>",
29-
+ "The Rust Project Developers"
49+
- "diff 0.1.12",
50+
+[workspace]
51+
+members = [
52+
+ "lib/unified-diff",
53+
+ "lib/context-diff",
54+
+ "lib/normal-diff",
55+
+ "bin/diff",
3056
]
31-
+edition = "2018"
32-
+
33-
+[[bin]]
34-
+name = "unified-diff"
35-
+
36-
+[dependencies]
37-
+diff = "0.1.10"
38-
~/unified-diff$ rustup override set nightly
39-
~/unified-diff$ cargo fuzz run fuzz_patch
4057
```
4158

bin/diff/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "diff"
3+
version = "0.1.0"
4+
authors = ["Michael Howell <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
context-diff = { path = "../../lib/context-diff" }
11+
normal-diff = { path = "../../lib/normal-diff" }
12+
unified-diff = { path = "../../lib/unified-diff" }

bin/diff/src/main.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::params::*;
2+
use std::env;
3+
4+
use std::fs;
5+
use std::io::{self, Write};
6+
7+
mod params;
8+
9+
fn main() -> Result<(), String> {
10+
let opts = env::args_os();
11+
let Params {
12+
from,
13+
to,
14+
context_count,
15+
format,
16+
} = parse_params(opts)?;
17+
// read files
18+
let from_content = match fs::read(&from) {
19+
Ok(from_content) => from_content,
20+
Err(e) => {
21+
return Err(format!("Failed to read from-file: {}", e));
22+
}
23+
};
24+
let to_content = match fs::read(&to) {
25+
Ok(to_content) => to_content,
26+
Err(e) => {
27+
return Err(format!("Failed to read from-file: {}", e));
28+
}
29+
};
30+
// run diff
31+
let result: Vec<u8> = match format {
32+
Format::Normal => normal_diff::diff(&from_content, &to_content),
33+
Format::Unified => unified_diff::diff(
34+
&from_content,
35+
&from.to_string_lossy(),
36+
&to_content,
37+
&to.to_string_lossy(),
38+
context_count,
39+
),
40+
Format::Context => context_diff::diff(
41+
&from_content,
42+
&from.to_string_lossy(),
43+
&to_content,
44+
&to.to_string_lossy(),
45+
context_count,
46+
),
47+
};
48+
io::stdout().write_all(&result).unwrap();
49+
Ok(())
50+
}

0 commit comments

Comments
 (0)