History

feat: add `ident::apply()` to substitute `$Id$` with `$Id: <hex>$`

Sebastian Thiel committed 3 years ago

Commit 306c8ea1 parent 496445c4 files · +80 −0Browse files

Unified · Split

4 files
  1. Cargo.lock +2 −0
  2. gix-filter/Cargo.toml +3 −0
  3. gix-filter/src/lib.rs +26 −0
  4. gix-filter/tests/ident/mod.rs +49 −0
Cargo.lock+2−0
@@ -1650,6 +1650,8 @@ name = "gix-filter" · 1649 unmodified lines
16501650 version = "0.0.0"
16511651 dependencies = [
16521652 "bstr",
1653+ "gix-hash 0.11.3",
1654+ "gix-object 0.31.0",
16531655 ]
16541656
16551657 [[package]]
gix-filter/Cargo.toml+3−0
@@ -12,4 +12,7 @@ rust-version = "1.65" · 11 unmodified lines
1212 doctest = false
1313
1414 [dependencies]
15+gix-hash = { version = "^0.11.3", path = "../gix-hash" }
16+gix-object = { version = "^0.31.0", path = "../gix-object" }
17+
1518 bstr = { version = "1.5.0", default-features = false, features = ["std"] }
gix-filter/src/lib.rs+26−0
@@ -44,4 +44,30 @@ pub mod ident { · 43 unmodified lines
4444 }
4545 input
4646 }
47+
48+ /// Substitute all occurrences of `$Id$` with `$Id: <hexsha-of-input>$` if present and return the changed buffer, with `object_hash`
49+ /// being used accordingly.
50+ ///
51+ /// ### Deviation
52+ ///
53+ /// `Git` also tries to cleanup 'stray' substituted `$Id: <hex>$`, but we don't do that, sticking exactly to what ought to be done.
54+ /// The respective code is up to 16 years old and one might assume that `git` by now handles checking and checkout filters correctly.
55+ pub fn apply(mut input: Cow<'_, BStr>, object_hash: gix_hash::Kind) -> Cow<'_, BStr> {
56+ let mut buf: [u8; b": $".len() + gix_hash::Kind::longest().len_in_hex()] = std::array::from_fn(|_| 0);
57+ let mut id = None;
58+ let mut ofs = 0;
59+ while let Some(pos) = input[ofs..].find(b"$Id$") {
60+ let id = id.get_or_insert_with(|| gix_object::compute_hash(object_hash, gix_object::Kind::Blob, &input));
61+
62+ buf[..2].copy_from_slice(b": ");
63+ let _ = id.hex_to_buf(&mut buf[2..][..object_hash.len_in_hex()]);
64+ let replaced_id = &mut buf[..2 + object_hash.len_in_hex() + 1];
65+ *replaced_id.last_mut().expect("present") = b'$';
66+ input
67+ .to_mut()
68+ .replace_range((ofs + pos + 3)..(ofs + pos + 4), &*replaced_id);
69+ ofs += pos + 3 + replaced_id.len();
70+ }
71+ input
72+ }
4773 }
gix-filter/tests/ident/mod.rs+49−0
@@ -56,3 +56,52 @@ mod undo { · 55 unmodified lines
5656 );
5757 }
5858 }
59+
60+mod apply {
61+ use crate::ident::cowstr;
62+ use gix_filter::ident;
63+ use std::borrow::Cow;
64+
65+ #[test]
66+ fn no_change() {
67+ for input_no_match in [
68+ "",
69+ "nothing",
70+ "$ID$ case sensitive matching",
71+ "$Id: expanded is ignored$",
72+ ] {
73+ let res = ident::apply(cowstr(input_no_match), gix_hash::Kind::Sha1);
74+ assert!(
75+ matches!(res, Cow::Borrowed(_)),
76+ "no substitution happens, so no mutable version of the Cow is created"
77+ );
78+ assert_eq!(res.as_ref(), input_no_match, "there definitely is no change");
79+ }
80+ }
81+
82+ #[test]
83+ fn simple() {
84+ assert_eq!(
85+ ident::apply(cowstr("$Id$"), gix_hash::Kind::Sha1).as_ref(),
86+ "$Id: b3f5ebfb5843bc43ceecff6d4f26bb37c615beb1$"
87+ );
88+
89+ assert_eq!(
90+ ident::apply(cowstr("$Id$ $Id$"), gix_hash::Kind::Sha1).as_ref(),
91+ "$Id: f6f3176060328ef7030a8b8eeda57fbf0587b2f9$ $Id: f6f3176060328ef7030a8b8eeda57fbf0587b2f9$"
92+ );
93+ }
94+
95+ #[test]
96+ fn round_trips() {
97+ for input in [
98+ "hi\n$Id$\nho\n\t$Id$$Id$$Id$",
99+ "$Id$",
100+ "$Id$ and one more $Id$ and done",
101+ ] {
102+ let res = ident::apply(cowstr(input), gix_hash::Kind::Sha1);
103+ assert_ne!(res.as_ref(), input, "the input was rewritten");
104+ assert_eq!(ident::undo(res).as_ref(), input, "the filter can be undone perfectly");
105+ }
106+ }
107+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t