Skip to content

Commit c99f575

Browse files
committed
Better handling of special cases (#450)
1 parent e4227d6 commit c99f575

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed

git-refspec/src/parse.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ pub(crate) mod function {
2929
Mode::Force
3030
}
3131
Some(_) => Mode::Normal,
32-
None => return Err(Error::Empty),
32+
None => {
33+
return match operation {
34+
Operation::Push => Err(Error::Empty),
35+
Operation::Fetch => Ok(RefSpecRef {
36+
mode: Mode::Normal,
37+
op: operation,
38+
src: Some("HEAD".into()),
39+
dst: None,
40+
}),
41+
}
42+
}
3343
};
3444

3545
let (src, dst) = match spec.find_byte(b':') {
@@ -43,7 +53,10 @@ pub(crate) mod function {
4353
let src = (!src.is_empty()).then(|| src.as_bstr());
4454
let dst = (!dst.is_empty()).then(|| dst.as_bstr());
4555
match (src, dst) {
46-
(None, None) => (None, None),
56+
(None, None) => match operation {
57+
Operation::Push => (None, None),
58+
Operation::Fetch => (Some("HEAD".into()), None),
59+
},
4760
(None, Some(dst)) => match operation {
4861
Operation::Push => (None, Some(dst)),
4962
Operation::Fetch => (Some("HEAD".into()), Some(dst)),

git-refspec/src/spec.rs

-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ impl RefSpecRef<'_> {
3434
(Operation::Push, Mode::Normal | Mode::Force, None, None) => Instruction::Push(Push::AllMatchingBranches {
3535
allow_non_fast_forward: matches!(self.mode, Mode::Force),
3636
}),
37-
(Operation::Fetch, Mode::Normal | Mode::Force, None, None) => {
38-
Instruction::Fetch(Fetch::AllMatchingBranches {
39-
allow_non_fast_forward: matches!(self.mode, Mode::Force),
40-
})
41-
}
4237
(Operation::Push, Mode::Normal | Mode::Force, Some(src), Some(dst)) if has_pattern(src) => {
4338
Instruction::Push(Push::MultipleWithGlob {
4439
src,

git-refspec/src/types.rs

-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ pub enum Push<'a> {
7070

7171
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
7272
pub enum Fetch<'a> {
73-
/// TODO: figure out what this actually does - it's valid for sure and only fetches HEAD -> FETCH_HEAD apparently
74-
AllMatchingBranches {
75-
/// Unclear what this does, but it's allowed
76-
allow_non_fast_forward: bool,
77-
},
7873
Only {
7974
/// The ref name to fetch on the remote side, without updating the local side.
8075
src: &'a BStr,
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:515045d1eade39e1aeee1f171216ac389b8adc54af541b2ff49bf1b1ab68eae2
3-
size 9340
2+
oid sha256:d68f02fcf17b72dfb953ab02f3d448d7b859d822aa71835856045a291a645d5a
3+
size 9344

git-refspec/tests/fixtures/make_baseline.sh

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ baseline push ':'
6464

6565
baseline fetch ''
6666
baseline fetch ':'
67+
baseline fetch '+'
6768
baseline push 'refs/heads/main:refs/remotes/frotz/xyzzy'
6869
baseline push 'refs/heads/*:refs/remotes/frotz/*'
6970

git-refspec/tests/parse/mod.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn baseline() {
3333
Ok(res) => match (res.is_ok(), err_code == 0) {
3434
(true, true) | (false, false) => {}
3535
_ => {
36-
eprintln!("{res:?} {err_code} {} {:?}", kind.as_bstr(), spec.as_bstr());
36+
eprintln!("{err_code} {res:?} {} {:?}", kind.as_bstr(), spec.as_bstr());
3737
mismatch += 1;
3838
}
3939
},
@@ -59,7 +59,6 @@ mod invalid {
5959

6060
#[test]
6161
fn empty() {
62-
assert!(matches!(try_parse("", Operation::Fetch).unwrap_err(), Error::Empty));
6362
assert!(matches!(try_parse("", Operation::Push).unwrap_err(), Error::Empty));
6463
}
6564

@@ -142,19 +141,15 @@ mod fetch {
142141
}
143142

144143
#[test]
145-
fn colon_alone_is_for_fetching_into_fetchhead() {
146-
assert_parse(
147-
":",
148-
Instruction::Fetch(Fetch::AllMatchingBranches {
149-
allow_non_fast_forward: false,
150-
}),
151-
);
152-
assert_parse(
153-
"+:",
154-
Instruction::Fetch(Fetch::AllMatchingBranches {
155-
allow_non_fast_forward: true,
156-
}),
157-
);
144+
fn colon_alone_is_for_fetching_head_into_fetchhead() {
145+
assert_parse(":", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
146+
let spec = assert_parse("+:", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
147+
assert_eq!(spec.mode(), Mode::Force, "it's set even though it's not useful");
148+
}
149+
150+
#[test]
151+
fn empty_refspec_is_enough_for_fetching_head_into_fetchhead() {
152+
assert_parse("", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
158153
}
159154
}
160155

0 commit comments

Comments
 (0)