I doubt these are going to be used anytime soon, but they are there.
Unified · Split
| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | //! Interact with git revisions by parsing them from rev-specs and turning them into rev-specs. |
| 2 | 2 | //! |
| 3 | 3 | //! One can also describe revisions using a different algorithm. |
| 4 | −#![forbid(unsafe_code)] | |
| 5 | −#![deny(missing_docs, rust_2018_idioms)] | |
| 4 | +#![deny(unsafe_code, missing_docs, rust_2018_idioms)] | |
| 6 | 5 | |
| 7 | 6 | /// Access to collections optimized for keys that are already a hash. |
| 8 | 7 | pub use hash_hasher; |
| @@ -54,8 +54,29 @@ fn long_describe_prefix(name: &BStr) -> Option<(&BStr, PrefixHint<'_>)> { · 53 unmodified lines | ||
| 54 | 54 | let rest = substr.get(1..)?; |
| 55 | 55 | rest.iter().all(|b| b.is_ascii_hexdigit()).then(|| rest.as_bstr()) |
| 56 | 56 | })?; |
| 57 | − iter.any(|token| !token.is_empty()) | |
| 58 | − .then(|| (candidate, PrefixHint::MustBeCommit)) | |
| 57 | + | |
| 58 | + let candidate = iter.clone().any(|token| !token.is_empty()).then(|| candidate); | |
| 59 | + let hint = iter | |
| 60 | + .next() | |
| 61 | + .and_then(|gen| gen.to_str().ok().and_then(|gen| usize::from_str(gen).ok())) | |
| 62 | + .and_then(|generation| { | |
| 63 | + iter.next().map(|token| { | |
| 64 | + let last_token_len = token.len(); | |
| 65 | + let first_token_ptr = iter.last().map(|token| token.as_ptr()).unwrap_or(token.as_ptr()); | |
| 66 | + // SAFETY: both pointers are definitely part of the same object | |
| 67 | + #[allow(unsafe_code)] | |
| 68 | + let prior_tokens_len: usize = unsafe { token.as_ptr().offset_from(first_token_ptr) } | |
| 69 | + .try_into() | |
| 70 | + .expect("positive value"); | |
| 71 | + PrefixHint::DescribeAnchor { | |
| 72 | + ref_name: name[..prior_tokens_len + last_token_len].as_bstr(), | |
| 73 | + generation, | |
| 74 | + } | |
| 75 | + }) | |
| 76 | + }) | |
| 77 | + .unwrap_or(PrefixHint::MustBeCommit); | |
| 78 | + | |
| 79 | + candidate.map(|c| (c, hint)) | |
| 59 | 80 | } |
| 60 | 81 | |
| 61 | 82 | fn short_describe_prefix(name: &BStr) -> Option<&BStr> { |
| @@ -1,12 +1,32 @@ | ||
| 1 | 1 | use crate::spec::parse::{parse, try_parse_opts, Options, PrefixHintOwned}; |
| 2 | 2 | |
| 3 | +fn anchor_hint() -> Option<PrefixHintOwned> { | |
| 4 | + Some(PrefixHintOwned::DescribeAnchor { | |
| 5 | + ref_name: "cargo-smart-release".into(), | |
| 6 | + generation: 679, | |
| 7 | + }) | |
| 8 | +} | |
| 9 | + | |
| 3 | 10 | #[test] |
| 4 | 11 | fn full_format_parses_hash_portion_as_prefix() { |
| 5 | 12 | let rec = parse("cargo-smart-release-679-g3bee7fb"); |
| 6 | 13 | assert!(rec.kind.is_none()); |
| 7 | 14 | assert_eq!(rec.find_ref[0], None, "references are not resolved in describe output"); |
| 8 | 15 | assert_eq!(rec.prefix[0], Some(git_hash::Prefix::from_hex("3bee7fb").unwrap())); |
| 9 | − assert_eq!(rec.prefix_hint[0], Some(PrefixHintOwned::MustBeCommit)); | |
| 16 | + assert_eq!(rec.prefix_hint[0], anchor_hint()); | |
| 17 | + assert_eq!(rec.calls, 1); | |
| 18 | + | |
| 19 | + let rec = parse("v1.0-0-g3bee7fb"); | |
| 20 | + assert!(rec.kind.is_none()); | |
| 21 | + assert_eq!(rec.find_ref[0], None, "references are not resolved in describe output"); | |
| 22 | + assert_eq!(rec.prefix[0], Some(git_hash::Prefix::from_hex("3bee7fb").unwrap())); | |
| 23 | + assert_eq!( | |
| 24 | + rec.prefix_hint[0], | |
| 25 | + Some(PrefixHintOwned::DescribeAnchor { | |
| 26 | + ref_name: "v1.0".into(), | |
| 27 | + generation: 0, | |
| 28 | + }) | |
| 29 | + ); | |
| 10 | 30 | assert_eq!(rec.calls, 1); |
| 11 | 31 | } |
| 12 | 32 | |
| @@ -62,7 +82,7 @@ fn full_format_with_dirty_suffix_is_recognized() { · 49 unmodified lines | ||
| 62 | 82 | assert!(rec.kind.is_none()); |
| 63 | 83 | assert_eq!(rec.find_ref[0], None, "git does not see this as prefix, we do"); |
| 64 | 84 | assert_eq!(rec.prefix[0], Some(git_hash::Prefix::from_hex("3bee7fb").unwrap()),); |
| 65 | − assert_eq!(rec.prefix_hint[0], Some(PrefixHintOwned::MustBeCommit)); | |
| 85 | + assert_eq!(rec.prefix_hint[0], anchor_hint()); | |
| 66 | 86 | assert_eq!(rec.calls, 1); |
| 67 | 87 | } |
| 68 | 88 | |