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 @@
1//! Interact with git revisions by parsing them from rev-specs and turning them into rev-specs.1//! Interact with git revisions by parsing them from rev-specs and turning them into rev-specs.
2//!2//!
3//! One can also describe revisions using a different algorithm.3//! One can also describe revisions using a different algorithm.
4#![forbid(unsafe_code)]4#![deny(unsafe_code, missing_docs, rust_2018_idioms)]
5#![deny(missing_docs, rust_2018_idioms)]
6 5
7/// Access to collections optimized for keys that are already a hash.6/// Access to collections optimized for keys that are already a hash.
8pub use hash_hasher;7pub 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
54 let rest = substr.get(1..)?;54 let rest = substr.get(1..)?;
55 rest.iter().all(|b| b.is_ascii_hexdigit()).then(|| rest.as_bstr())55 rest.iter().all(|b| b.is_ascii_hexdigit()).then(|| rest.as_bstr())
56 })?;56 })?;
57 iter.any(|token| !token.is_empty())57
58 .then(|| (candidate, PrefixHint::MustBeCommit))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
61fn short_describe_prefix(name: &BStr) -> Option<&BStr> {82fn short_describe_prefix(name: &BStr) -> Option<&BStr> {
git-revision/tests/spec/parse/anchor/describe.rs+22−2
@@ -1,12 +1,32 @@
1use crate::spec::parse::{parse, try_parse_opts, Options, PrefixHintOwned};1use crate::spec::parse::{parse, try_parse_opts, Options, PrefixHintOwned};
2 2
3fn anchor_hint() -> Option<PrefixHintOwned> {
4 Some(PrefixHintOwned::DescribeAnchor {
5 ref_name: "cargo-smart-release".into(),
6 generation: 679,
7 })
8}
9
3#[test]10#[test]
4fn full_format_parses_hash_portion_as_prefix() {11fn full_format_parses_hash_portion_as_prefix() {
5 let rec = parse("cargo-smart-release-679-g3bee7fb");12 let rec = parse("cargo-smart-release-679-g3bee7fb");
6 assert!(rec.kind.is_none());13 assert!(rec.kind.is_none());
7 assert_eq!(rec.find_ref[0], None, "references are not resolved in describe output");14 assert_eq!(rec.find_ref[0], None, "references are not resolved in describe output");
8 assert_eq!(rec.prefix[0], Some(git_hash::Prefix::from_hex("3bee7fb").unwrap()));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 assert_eq!(rec.calls, 1);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 assert!(rec.kind.is_none());82 assert!(rec.kind.is_none());
63 assert_eq!(rec.find_ref[0], None, "git does not see this as prefix, we do");83 assert_eq!(rec.find_ref[0], None, "git does not see this as prefix, we do");
64 assert_eq!(rec.prefix[0], Some(git_hash::Prefix::from_hex("3bee7fb").unwrap()),);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 assert_eq!(rec.calls, 1);86 assert_eq!(rec.calls, 1);
67}87}
68 88

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