Skip to content

Commit fb6c35b

Browse files
committed
added apply api
1 parent 2351558 commit fb6c35b

File tree

10 files changed

+44
-22
lines changed

10 files changed

+44
-22
lines changed

Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
[package]
2-
name = "patch"
2+
name = "patch-apply"
33
version = "0.8.0"
4-
authors = ["phil <[email protected]>", "Eugene Hauptmann"]
5-
description = "Parse patches in the unified diff format"
6-
repository = "https://github.com/eugenehp/patch-rs"
4+
authors = ["phil <[email protected]>", "Eugene Hauptmann <[email protected]>"]
5+
description = "Parse and apply patches in the unified diff format"
6+
repository = "https://github.com/eugenehp/patch-apply-rs"
77
readme = "README.md"
8-
keywords = ["patch", "diff", "parse", "nom"]
8+
keywords = ["patch", "diff", "parse", "nom", "git"]
9+
categories = [
10+
"parsing",
11+
"development-tools",
12+
"parser-implementations"
13+
]
914
license = "MIT"
1015
edition = "2018"
1116

17+
1218
[[example]]
1319
name = "apply"
1420
path = "./examples/apply.rs"

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
MIT License
22

33
Copyright (c) 2016 uniphil
4+
Copyright (c) 2024 Eugene Hauptmann
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Patch
22

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

8-
Rust crate for parsing and producing patch files in the [Unified Format].
8+
Rust crate for parsing, producing and applying patch files in the [Unified Format].
99

1010
The parser attempts to be forgiving enough to be compatible with diffs produced
1111
by programs like git. It accomplishes this by ignoring the additional code
@@ -14,4 +14,4 @@ context and information provided in the diff by those programs.
1414
See the **[Documentation]** for more information and for examples.
1515

1616
[Unified Format]: https://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html
17-
[Documentation]: https://docs.rs/patch
17+
[Documentation]: https://docs.rs/patch-apply

examples/apply.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{fs, path::PathBuf};
44

5-
use patch::{Line, Patch};
5+
use patch_apply::{Line, Patch};
66

77
fn apply(patch: Patch, old: &str) -> String {
88
let old_lines = old.lines().collect::<Vec<&str>>();

examples/files/output.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/apply.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::{Line, Patch};
2+
3+
/// Apply patch to the string from the input file source
4+
pub fn apply(input: String, patch: Patch) -> String {
5+
let old_lines = input.lines().collect::<Vec<&str>>();
6+
let mut out: Vec<&str> = vec![];
7+
let mut old_line = 0;
8+
for hunk in patch.hunks {
9+
while old_line < hunk.old_range.start - 1 {
10+
out.push(old_lines[old_line as usize]);
11+
old_line += 1;
12+
}
13+
old_line += hunk.old_range.count;
14+
for line in hunk.lines {
15+
match line {
16+
Line::Add(s) | Line::Context(s) => out.push(s),
17+
Line::Remove(_) | Line::EndOfFile(_) => {}
18+
}
19+
}
20+
}
21+
out.join("\n")
22+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
4646
#![deny(unused_must_use)]
4747

48+
mod apply;
4849
mod ast;
4950
mod parser;
5051

52+
pub use apply::*;
5153
pub use ast::*;
5254
pub use parser::ParseError;

tests/parse_patch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chrono::DateTime;
2-
use patch::{File, FileMetadata, ParseError, Patch};
2+
use patch_apply::{File, FileMetadata, ParseError, Patch};
33

44
use pretty_assertions::assert_eq;
55

tests/parse_samples.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33

44
use pretty_assertions::assert_eq;
55

6-
use patch::Patch;
6+
use patch_apply::Patch;
77

88
#[test]
99
fn parse_samples() {

tests/regressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use patch::{File, FileMetadata, Hunk, Line, ParseError, Patch, Range};
1+
use patch_apply::{File, FileMetadata, Hunk, Line, ParseError, Patch, Range};
22

33
use pretty_assertions::assert_eq;
44

0 commit comments

Comments
 (0)