Skip to content

Commit

Permalink
support for @ shortcut. (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 7, 2022
1 parent b62ee56 commit 32d98e9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
14 changes: 10 additions & 4 deletions git-refspec/src/parse.rs
Expand Up @@ -50,7 +50,7 @@ pub(crate) mod function {
}
};

let (src, dst) = match spec.find_byte(b':') {
let (mut src, dst) = match spec.find_byte(b':') {
Some(pos) => {
let (src, dst) = spec.split_at(pos);
let dst = &dst[1..];
Expand Down Expand Up @@ -86,6 +86,11 @@ pub(crate) mod function {
}
};

if let Some(spec) = src.as_mut() {
if *spec == "@" {
*spec = "HEAD".into();
}
}
let (src, src_had_pattern) = validated(src, operation == Operation::Push)?;
let (dst, dst_had_pattern) = validated(dst, false)?;
if mode != Mode::Negative && src_had_pattern != dst_had_pattern {
Expand All @@ -103,10 +108,11 @@ pub(crate) mod function {
match spec {
Some(spec) => {
let glob_count = spec.iter().filter(|b| **b == b'*').take(2).count();
if glob_count == 2 {
if glob_count > 1 {
return Err(Error::PatternUnsupported { pattern: spec.into() });
}
if glob_count == 1 {
let has_globs = glob_count == 1;
if has_globs {
let mut buf = smallvec::SmallVec::<[u8; 256]>::with_capacity(spec.len());
buf.extend_from_slice(spec);
let glob_pos = buf.find_byte(b'*').expect("glob present");
Expand All @@ -132,7 +138,7 @@ pub(crate) mod function {
}
})?;
}
Ok((Some(spec), glob_count == 1))
Ok((Some(spec), has_globs))
}
None => Ok((None, false)),
}
Expand Down
Git LFS file not shown
6 changes: 6 additions & 0 deletions git-refspec/tests/fixtures/make_baseline.sh
Expand Up @@ -80,6 +80,12 @@ baseline fetch 'HEAD'
baseline push '@'
baseline fetch '@'

baseline push '^@'
baseline fetch '^@'

baseline push '+@'
baseline fetch '+@'

baseline fetch 'HEAD:'

baseline push ':refs/remotes/frotz/deleteme'
Expand Down
7 changes: 7 additions & 0 deletions git-refspec/tests/parse/fetch.rs
Expand Up @@ -7,6 +7,13 @@ fn exclude() {
assert_parse("^a*", Instruction::Fetch(Fetch::Exclude { src: b("a*") }));
}

#[test]
fn ampersand_is_resolved_to_head() {
assert_parse("@", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
assert_parse("+@", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
assert_parse("^@", Instruction::Fetch(Fetch::Exclude { src: b("HEAD") }));
}

#[test]
fn lhs_colon_empty_fetches_only() {
assert_parse("src:", Instruction::Fetch(Fetch::Only { src: b("src") }));
Expand Down
22 changes: 22 additions & 0 deletions git-refspec/tests/parse/push.rs
Expand Up @@ -7,6 +7,28 @@ fn exclude() {
assert_parse("^a*", Instruction::Push(Push::Exclude { src: b("a*") }));
}

#[test]
fn ampersand_is_resolved_to_head() {
assert_parse(
"@",
Instruction::Push(Push::Matching {
src: b("HEAD"),
dst: b("HEAD"),
allow_non_fast_forward: false,
}),
);

assert_parse(
"+@",
Instruction::Push(Push::Matching {
src: b("HEAD"),
dst: b("HEAD"),
allow_non_fast_forward: true,
}),
);
assert_parse("^@", Instruction::Push(Push::Exclude { src: b("HEAD") }));
}

#[test]
fn lhs_colon_rhs_pushes_single_ref() {
assert_parse(
Expand Down

0 comments on commit 32d98e9

Please sign in to comment.