Unified · Split
| @@ -1650,6 +1650,8 @@ name = "gix-filter" · 1649 unmodified lines | |||
| 1650 | version = "0.0.0" | 1650 | version = "0.0.0" |
| 1651 | dependencies = [ | 1651 | dependencies = [ |
| 1652 | "bstr", | 1652 | "bstr", |
| 1653 | "gix-hash 0.11.3", | ||
| 1654 | "gix-object 0.31.0", | ||
| 1653 | ] | 1655 | ] |
| 1654 | 1656 | ||
| 1655 | [[package]] | 1657 | [[package]] |
| @@ -12,4 +12,7 @@ rust-version = "1.65" · 11 unmodified lines | |||
| 12 | doctest = false | 12 | doctest = false |
| 13 | 13 | ||
| 14 | [dependencies] | 14 | [dependencies] |
| 15 | gix-hash = { version = "^0.11.3", path = "../gix-hash" } | ||
| 16 | gix-object = { version = "^0.31.0", path = "../gix-object" } | ||
| 17 | |||
| 15 | bstr = { version = "1.5.0", default-features = false, features = ["std"] } | 18 | bstr = { version = "1.5.0", default-features = false, features = ["std"] } |
| @@ -44,4 +44,30 @@ pub mod ident { · 43 unmodified lines | |||
| 44 | } | 44 | } |
| 45 | input | 45 | input |
| 46 | } | 46 | } |
| 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 | } | ||
| 47 | } | 73 | } |
| @@ -56,3 +56,52 @@ mod undo { · 55 unmodified lines | |||
| 56 | ); | 56 | ); |
| 57 | } | 57 | } |
| 58 | } | 58 | } |
| 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 | } | ||