Skip to content

Commit 32d98e9

Browse files
committed
support for @ shortcut. (#450)
1 parent b62ee56 commit 32d98e9

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

git-refspec/src/parse.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) mod function {
5050
}
5151
};
5252

53-
let (src, dst) = match spec.find_byte(b':') {
53+
let (mut src, dst) = match spec.find_byte(b':') {
5454
Some(pos) => {
5555
let (src, dst) = spec.split_at(pos);
5656
let dst = &dst[1..];
@@ -86,6 +86,11 @@ pub(crate) mod function {
8686
}
8787
};
8888

89+
if let Some(spec) = src.as_mut() {
90+
if *spec == "@" {
91+
*spec = "HEAD".into();
92+
}
93+
}
8994
let (src, src_had_pattern) = validated(src, operation == Operation::Push)?;
9095
let (dst, dst_had_pattern) = validated(dst, false)?;
9196
if mode != Mode::Negative && src_had_pattern != dst_had_pattern {
@@ -103,10 +108,11 @@ pub(crate) mod function {
103108
match spec {
104109
Some(spec) => {
105110
let glob_count = spec.iter().filter(|b| **b == b'*').take(2).count();
106-
if glob_count == 2 {
111+
if glob_count > 1 {
107112
return Err(Error::PatternUnsupported { pattern: spec.into() });
108113
}
109-
if glob_count == 1 {
114+
let has_globs = glob_count == 1;
115+
if has_globs {
110116
let mut buf = smallvec::SmallVec::<[u8; 256]>::with_capacity(spec.len());
111117
buf.extend_from_slice(spec);
112118
let glob_pos = buf.find_byte(b'*').expect("glob present");
@@ -132,7 +138,7 @@ pub(crate) mod function {
132138
}
133139
})?;
134140
}
135-
Ok((Some(spec), glob_count == 1))
141+
Ok((Some(spec), has_globs))
136142
}
137143
None => Ok((None, false)),
138144
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:d68f02fcf17b72dfb953ab02f3d448d7b859d822aa71835856045a291a645d5a
3-
size 9344
2+
oid sha256:ace43ca904298dc76951863d41485ae0c21ee9b88abf7d604eb6c6e7efa18ab2
3+
size 9352

git-refspec/tests/fixtures/make_baseline.sh

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ baseline fetch 'HEAD'
8080
baseline push '@'
8181
baseline fetch '@'
8282

83+
baseline push '^@'
84+
baseline fetch '^@'
85+
86+
baseline push '+@'
87+
baseline fetch '+@'
88+
8389
baseline fetch 'HEAD:'
8490

8591
baseline push ':refs/remotes/frotz/deleteme'

git-refspec/tests/parse/fetch.rs

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ fn exclude() {
77
assert_parse("^a*", Instruction::Fetch(Fetch::Exclude { src: b("a*") }));
88
}
99

10+
#[test]
11+
fn ampersand_is_resolved_to_head() {
12+
assert_parse("@", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
13+
assert_parse("+@", Instruction::Fetch(Fetch::Only { src: b("HEAD") }));
14+
assert_parse("^@", Instruction::Fetch(Fetch::Exclude { src: b("HEAD") }));
15+
}
16+
1017
#[test]
1118
fn lhs_colon_empty_fetches_only() {
1219
assert_parse("src:", Instruction::Fetch(Fetch::Only { src: b("src") }));

git-refspec/tests/parse/push.rs

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ fn exclude() {
77
assert_parse("^a*", Instruction::Push(Push::Exclude { src: b("a*") }));
88
}
99

10+
#[test]
11+
fn ampersand_is_resolved_to_head() {
12+
assert_parse(
13+
"@",
14+
Instruction::Push(Push::Matching {
15+
src: b("HEAD"),
16+
dst: b("HEAD"),
17+
allow_non_fast_forward: false,
18+
}),
19+
);
20+
21+
assert_parse(
22+
"+@",
23+
Instruction::Push(Push::Matching {
24+
src: b("HEAD"),
25+
dst: b("HEAD"),
26+
allow_non_fast_forward: true,
27+
}),
28+
);
29+
assert_parse("^@", Instruction::Push(Push::Exclude { src: b("HEAD") }));
30+
}
31+
1032
#[test]
1133
fn lhs_colon_rhs_pushes_single_ref() {
1234
assert_parse(

0 commit comments

Comments
 (0)