Skip to content

Commit c0905ce

Browse files
Byronlthms
andcommitted
refactor
- more possible inputs for '@' in branches - assure ranges are also handled - remove error variant which should not be possible anymore Co-authored-by: Thomas Letan <lthms@soap.coffee> Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
1 parent 1c27e7a commit c0905ce

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

gix-revision/src/spec/parse/function.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,19 @@ where
344344
{
345345
let mut cursor = input;
346346
let mut ofs = 0;
347+
const SEPARATORS: &[u8] = b"~^:.";
347348
while let Some((pos, b)) = cursor.iter().enumerate().find(|(pos, b)| {
348349
if **b == b'@' {
349-
cursor.get(pos + 1) == Some(&b'{')
350-
|| cursor
351-
.get(pos + 1)
352-
.and_then(|b| Some(b"~^:.".contains(b)))
353-
.unwrap_or(false)
354-
|| (*pos == 0 && cursor.get(1) == None)
355-
} else if b"~^:.".contains(b) {
350+
if cursor.len() == 1 {
351+
return true;
352+
}
353+
let next = cursor.get(pos + 1);
354+
let next_next = cursor.get(pos + 2);
355+
if *pos != 0 && (next, next_next) == (Some(&b'.'), Some(&b'.')) {
356+
return false;
357+
}
358+
next == Some(&b'{') || next.map_or(false, |b| SEPARATORS.contains(b))
359+
} else if SEPARATORS.contains(b) {
356360
true
357361
} else {
358362
if let Some(num) = consecutive_hex_chars.as_mut() {

gix-revision/tests/spec/parse/anchor/refnames.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use gix_revision::spec;
2-
31
use crate::spec::parse::{parse, try_parse};
42

53
#[test]
@@ -11,9 +9,36 @@ fn at_by_itself_is_shortcut_for_head() {
119

1210
#[test]
1311
fn at_is_allowed() {
14-
let rec = parse("a@b");
15-
assert!(rec.kind.is_none());
16-
assert_eq!(rec.get_ref(0), "a@b");
12+
for name in ["a@b", "@branch", "branch@", "@@", "@inner@"] {
13+
let rec = parse(name);
14+
assert!(rec.kind.is_none());
15+
assert_eq!(rec.get_ref(0), name);
16+
assert_eq!(rec.find_ref[1], None);
17+
}
18+
}
19+
20+
#[test]
21+
fn at_in_ranges_is_allowed() {
22+
let input = "@@@..";
23+
let rec = parse(input);
24+
assert_eq!(rec.kind, Some(gix_revision::spec::Kind::RangeBetween));
25+
assert_eq!(rec.get_ref(0), "@@@");
26+
assert_eq!(rec.get_ref(1), "HEAD");
27+
28+
let input = "@@...@@";
29+
let rec = parse(input);
30+
assert_eq!(rec.kind, Some(gix_revision::spec::Kind::ReachableToMergeBase));
31+
assert_eq!(rec.get_ref(0), "@@");
32+
assert_eq!(rec.get_ref(1), "@@");
33+
}
34+
35+
#[test]
36+
fn strange_revspecs_do_not_panic() {
37+
let err = try_parse(".@.").unwrap_err();
38+
assert!(matches!(
39+
err,
40+
gix_revision::spec::parse::Error::AtNeedsCurlyBrackets { input } if input == "@."
41+
));
1742
}
1843

1944
#[test]

0 commit comments

Comments
 (0)