History

provide better hints for parsing describe output (#427)

I doubt these are going to be used anytime soon, but they are there.

Sebastian Thiel committed 4 years ago

Commit fb0b8ca1 parent d9939923 files · +46 −6Browse files

Unified · Split

3 files
  1. git-revision/src/lib.rs +1 −2
  2. git-revision/src/spec/parse/function.rs +23 −2
  3. git-revision/tests/spec/parse/anchor/describe.rs +22 −2
git-revision/src/lib.rs+1−2
@@ -1,8 +1,7 @@
11 //! Interact with git revisions by parsing them from rev-specs and turning them into rev-specs.
22 //!
33 //! 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)]
65
76 /// Access to collections optimized for keys that are already a hash.
87 pub use hash_hasher;
git-revision/src/spec/parse/function.rs+23−2
@@ -54,8 +54,29 @@ fn long_describe_prefix(name: &BStr) -> Option<(&BStr, PrefixHint<'_>)> { · 53 unmodified lines
5454 let rest = substr.get(1..)?;
5555 rest.iter().all(|b| b.is_ascii_hexdigit()).then(|| rest.as_bstr())
5656 })?;
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))
5980 }
6081
6182 fn short_describe_prefix(name: &BStr) -> Option<&BStr> {
git-revision/tests/spec/parse/anchor/describe.rs+22−2
@@ -1,12 +1,32 @@
11 use crate::spec::parse::{parse, try_parse_opts, Options, PrefixHintOwned};
22
3+fn anchor_hint() -> Option<PrefixHintOwned> {
4+ Some(PrefixHintOwned::DescribeAnchor {
5+ ref_name: "cargo-smart-release".into(),
6+ generation: 679,
7+ })
8+}
9+
310 #[test]
411 fn full_format_parses_hash_portion_as_prefix() {
512 let rec = parse("cargo-smart-release-679-g3bee7fb");
613 assert!(rec.kind.is_none());
714 assert_eq!(rec.find_ref[0], None, "references are not resolved in describe output");
815 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+ );
1030 assert_eq!(rec.calls, 1);
1131 }
1232
@@ -62,7 +82,7 @@ fn full_format_with_dirty_suffix_is_recognized() { · 49 unmodified lines
6282 assert!(rec.kind.is_none());
6383 assert_eq!(rec.find_ref[0], None, "git does not see this as prefix, we do");
6484 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());
6686 assert_eq!(rec.calls, 1);
6787 }
6888

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