Skip to content

Commit

Permalink
Better handling of special cases (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 6, 2022
1 parent e4227d6 commit c99f575
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
17 changes: 15 additions & 2 deletions git-refspec/src/parse.rs
Expand Up @@ -29,7 +29,17 @@ pub(crate) mod function {
Mode::Force
}
Some(_) => Mode::Normal,
None => return Err(Error::Empty),
None => {
return match operation {
Operation::Push => Err(Error::Empty),
Operation::Fetch => Ok(RefSpecRef {
mode: Mode::Normal,
op: operation,
src: Some("HEAD".into()),
dst: None,
}),
}
}
};

let (src, dst) = match spec.find_byte(b':') {
Expand All @@ -43,7 +53,10 @@ pub(crate) mod function {
let src = (!src.is_empty()).then(|| src.as_bstr());
let dst = (!dst.is_empty()).then(|| dst.as_bstr());
match (src, dst) {
(None, None) => (None, None),
(None, None) => match operation {
Operation::Push => (None, None),
Operation::Fetch => (Some("HEAD".into()), None),
},
(None, Some(dst)) => match operation {
Operation::Push => (None, Some(dst)),
Operation::Fetch => (Some("HEAD".into()), Some(dst)),
Expand Down
5 changes: 0 additions & 5 deletions git-refspec/src/spec.rs
Expand Up @@ -34,11 +34,6 @@ impl RefSpecRef<'_> {
(Operation::Push, Mode::Normal | Mode::Force, None, None) => Instruction::Push(Push::AllMatchingBranches {
allow_non_fast_forward: matches!(self.mode, Mode::Force),
}),
(Operation::Fetch, Mode::Normal | Mode::Force, None, None) => {
Instruction::Fetch(Fetch::AllMatchingBranches {
allow_non_fast_forward: matches!(self.mode, Mode::Force),
})
}
(Operation::Push, Mode::Normal | Mode::Force, Some(src), Some(dst)) if has_pattern(src) => {
Instruction::Push(Push::MultipleWithGlob {
src,
Expand Down
5 changes: 0 additions & 5 deletions git-refspec/src/types.rs
Expand Up @@ -70,11 +70,6 @@ pub enum Push<'a> {

#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Fetch<'a> {
/// TODO: figure out what this actually does - it's valid for sure and only fetches HEAD -> FETCH_HEAD apparently
AllMatchingBranches {
/// Unclear what this does, but it's allowed
allow_non_fast_forward: bool,
},
Only {
/// The ref name to fetch on the remote side, without updating the local side.
src: &'a BStr,
Expand Down
Git LFS file not shown
1 change: 1 addition & 0 deletions git-refspec/tests/fixtures/make_baseline.sh
Expand Up @@ -64,6 +64,7 @@ baseline push ':'

baseline fetch ''
baseline fetch ':'
baseline fetch '+'
baseline push 'refs/heads/main:refs/remotes/frotz/xyzzy'
baseline push 'refs/heads/*:refs/remotes/frotz/*'

Expand Down
25 changes: 10 additions & 15 deletions git-refspec/tests/parse/mod.rs
Expand Up @@ -33,7 +33,7 @@ fn baseline() {
Ok(res) => match (res.is_ok(), err_code == 0) {
(true, true) | (false, false) => {}
_ => {
eprintln!("{res:?} {err_code} {} {:?}", kind.as_bstr(), spec.as_bstr());
eprintln!("{err_code} {res:?} {} {:?}", kind.as_bstr(), spec.as_bstr());
mismatch += 1;
}
},
Expand All @@ -59,7 +59,6 @@ mod invalid {

#[test]
fn empty() {
assert!(matches!(try_parse("", Operation::Fetch).unwrap_err(), Error::Empty));
assert!(matches!(try_parse("", Operation::Push).unwrap_err(), Error::Empty));
}

Expand Down Expand Up @@ -142,19 +141,15 @@ mod fetch {
}

#[test]
fn colon_alone_is_for_fetching_into_fetchhead() {
assert_parse(
":",
Instruction::Fetch(Fetch::AllMatchingBranches {
allow_non_fast_forward: false,
}),
);
assert_parse(
"+:",
Instruction::Fetch(Fetch::AllMatchingBranches {
allow_non_fast_forward: true,
}),
);
fn colon_alone_is_for_fetching_head_into_fetchhead() {
assert_parse(":", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
let spec = assert_parse("+:", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
assert_eq!(spec.mode(), Mode::Force, "it's set even though it's not useful");
}

#[test]
fn empty_refspec_is_enough_for_fetching_head_into_fetchhead() {
assert_parse("", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
}
}

Expand Down

0 comments on commit c99f575

Please sign in to comment.