Skip to content

Commit

Permalink
📌 Vendor patch-rs 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mleduque committed Feb 4, 2024
1 parent fa2b2da commit b7807aa
Show file tree
Hide file tree
Showing 29 changed files with 1,967 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["modda-cli"]
members = ["modda-cli", "patch-rs"]
2 changes: 1 addition & 1 deletion modda-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ indoc ="2.0.4"
itertools = "0.12.0"
lazy_static = "1.4.0"
log = "0.4.20"
patch = "0.7.0"
patch = { path = "../patch-rs" }
path-clean = "1.0.1"
path-absolutize = "3.1.1"
percent-encoding = "2.3.1"
Expand Down
2 changes: 2 additions & 0 deletions patch-rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
16 changes: 16 additions & 0 deletions patch-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# CHANGELOG

## [Unreleased]
### Changed

## [v0.7]
### Breaking
- Filename parsing now expects (and renders) a tab character after the filename instead of a space character, before any metadata. Seems like all diff programs actually follow this convention, and git will even render unquoted filenames with spaces, so the previous parsing would produce incorrect results. Thanks [@keith](https://github.com/keith) for reporting.

### Fixed
- Don't break (hopefully) on diffs with windows-style CRLF line endings. I don't have samples to verify with, but it Seems Like Maybe It Will Do The Right Thing? (it will still ony render diffs with line endings as `\n`. Please open a feature request if you want this.) Thanks [@jacobtread](https://github.com/jacobtread) for reporting.
- Parse (and save) hunk hints after range info instead of (incorrectly) treating them like Context lines. Thanks [@keith](https://github.com/keith) and [@wfraser](https://github.com/wfraser).

## [v0.6]
### Changed
- Upgrade nom to 0.7! from [@compiler-errors](https://github.com/compiler-errors)
18 changes: 18 additions & 0 deletions patch-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "patch"
version = "0.7.0"
authors = ["phil <[email protected]>"]
description = "Parse patches in the unified diff format"
repository = "https://github.com/uniphil/patch-rs"
readme = "README.md"
keywords = ["patch", "diff", "parse", "nom"]
license = "MIT"
edition = "2018"

[dependencies]
nom = "7.1.0"
nom_locate = "4.0.0"
chrono = "0.4.19"

[dev-dependencies]
pretty_assertions = "1.0.0"
21 changes: 21 additions & 0 deletions patch-rs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 uniphil

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.
17 changes: 17 additions & 0 deletions patch-rs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Patch

[![Checks](https://github.com/uniphil/patch-rs/actions/workflows/checks.yml/badge.svg)](https://github.com/uniphil/patch-rs/actions/workflows/checks.yml)
[![Crates.io Badge](https://img.shields.io/crates/v/patch.svg)](https://crates.io/crates/patch)
[![docs.rs](https://docs.rs/patch/badge.svg)](https://docs.rs/patch)
[![Lines of Code](https://tokei.rs/b1/github/uniphil/patch-rs)](https://github.com/uniphil/patch-rs)

Rust crate for parsing and producing patch files in the [Unified Format].

The parser attempts to be forgiving enough to be compatible with diffs produced
by programs like git. It accomplishes this by ignoring the additional code
context and information provided in the diff by those programs.

See the **[Documentation]** for more information and for examples.

[Unified Format]: https://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html
[Documentation]: https://docs.rs/patch
65 changes: 65 additions & 0 deletions patch-rs/examples/apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Demonstrates how to apply a parsed diff to a file

use patch::{Line, Patch};

fn apply(diff: Patch, old: &str) -> String {
let old_lines = old.lines().collect::<Vec<&str>>();
let mut out: Vec<&str> = vec![];
let mut old_line = 0;
for hunk in diff.hunks {
while old_line < hunk.old_range.start - 1 {
out.push(old_lines[old_line as usize]);
old_line += 1;
}
old_line += hunk.old_range.count;
for line in hunk.lines {
match line {
Line::Add(s) | Line::Context(s) => out.push(s),
Line::Remove(_) => {}
}
}
}
out.join("\n")
}

static LAO: &str = "\
The Way that can be told of is not the eternal Way;
The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
The Named is the mother of all things.
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
so we may see their outcome.
The two are the same,
But after they are produced,
they have different names.
";

static RAW_DIFF: &str = "\
--- lao 2002-02-21 23:30:39.942229878 -0800
+++ tzu 2002-02-21 23:30:50.442260588 -0800
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+The named is the mother of all things.
+
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
@@ -9,3 +8,6 @@
The two are the same,
But after they are produced,
they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!
";

fn main() {
let diff = Patch::from_single(RAW_DIFF).unwrap();
let new = apply(diff, LAO);
println!("should be tzu:\n\n{}", new);
}
8 changes: 8 additions & 0 deletions patch-rs/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
with import <nixpkgs> {};

stdenv.mkDerivation {
name = "rust";
buildInputs = [
cargo
];
}
Loading

0 comments on commit b7807aa

Please sign in to comment.