1#[derive(PartialOrd, Ord, PartialEq, Eq, Copy, Clone, Hash, Debug)]
3pub enum Operation {
4 Push,
6 Fetch,
8}
9
10pub(crate) mod function {
11 use crate::{RefSpecRef, parse::Operation, types::Mode};
12 use bstr::{BStr, ByteSlice};
13 use gix_error::{Result, ResultExt, bail, message, validation};
14
15 pub fn parse(mut spec: &BStr, operation: Operation) -> Result<RefSpecRef<'_>> {
19 fn fetch_head_only(mode: Mode) -> RefSpecRef<'static> {
20 RefSpecRef {
21 mode,
22 op: Operation::Fetch,
23 src: Some("HEAD".into()),
24 dst: None,
25 }
26 }
27
28 let mode = match spec.first() {
29 Some(&b'^') => {
30 spec = &spec[1..];
31 Mode::Negative
32 }
33 Some(&b'+') => {
34 spec = &spec[1..];
35 Mode::Force
36 }
37 Some(_) => Mode::Normal,
38 None => {
39 return match operation {
40 Operation::Push => Err(message("Empty refspecs are invalid").validation_error()),
41 Operation::Fetch => Ok(fetch_head_only(Mode::Normal)),
42 };
43 }
44 };
45
46 let (mut src, dst) = match spec.rfind_byte(b':') {
50 Some(pos) => {
51 if mode == Mode::Negative {
52 bail!(validation(
53 "Negative refspecs cannot have destinations as they exclude sources"
54 ));
55 }
56
57 let (src, dst) = spec.split_at(pos);
58 let dst = &dst[1..];
59 let src = (!src.is_empty()).then(|| src.as_bstr());
60 let dst = (!dst.is_empty()).then(|| dst.as_bstr());
61 match (src, dst) {
62 (None, None) => match operation {
63 Operation::Push => (None, None),
64 Operation::Fetch => (Some("HEAD".into()), None),
65 },
66 (None, Some(dst)) => match operation {
67 Operation::Push => (None, Some(dst)),
68 Operation::Fetch => (Some("HEAD".into()), Some(dst)),
69 },
70 (Some(src), None) => match operation {
71 Operation::Push => {
72 bail!(validation("Cannot push into an empty destination"));
73 }
74 Operation::Fetch => (Some(src), None),
75 },
76 (Some(src), Some(dst)) => (Some(src), Some(dst)),
77 }
78 }
79 None => {
80 let src = (!spec.is_empty()).then_some(spec);
81 if Operation::Fetch == operation && mode != Mode::Negative && src.is_none() {
82 return Ok(fetch_head_only(mode));
83 } else {
84 (src, None)
85 }
86 }
87 };
88
89 if let Some(spec) = src.as_mut()
90 && *spec == "@"
91 {
92 *spec = "HEAD".into();
93 }
94 let (src, src_had_pattern) = validated(src, operation == Operation::Push && dst.is_some())?;
95 let (dst, dst_had_pattern) = validated(dst, false)?;
96 if mode != Mode::Negative
97 && src_had_pattern != dst_had_pattern
98 && !(operation == Operation::Push && dst.is_none())
99 {
100 bail!(validation(
101 "Both sides of a two-sided specification need a pattern, like 'a/*:b/*'"
102 ));
103 }
104
105 if mode == Mode::Negative {
106 match src {
107 Some(spec) => {
108 if looks_like_object_hash(spec) {
109 bail!(validation("Negative specs must not be object hashes"));
110 }
111 }
112 None => bail!(validation("Negative specs must not be empty")),
113 }
114 }
115
116 Ok(RefSpecRef {
117 op: operation,
118 mode,
119 src,
120 dst,
121 })
122 }
123
124 fn looks_like_object_hash(spec: &BStr) -> bool {
125 spec.len() >= gix_hash::Kind::shortest().len_in_hex() && spec.iter().all(u8::is_ascii_hexdigit)
126 }
127
128 fn validate_partial_name_with_single_glob(
129 spec: &BStr,
130 ) -> std::result::Result<(), gix_validate::reference::name::Error> {
131 let mut buf = smallvec::SmallVec::<[u8; 256]>::with_capacity(spec.len());
132 buf.extend_from_slice(spec);
133 let glob_pos = buf.find_byte(b'*').expect("glob present");
134 buf[glob_pos] = b'a';
135 gix_validate::reference::name_partial(buf.as_bstr())?;
136 Ok(())
137 }
138
139 fn validated(spec: Option<&BStr>, any_name: bool) -> Result<(Option<&BStr>, bool)> {
143 match spec {
144 Some(spec) => {
145 let glob_count = spec.iter().filter(|b| **b == b'*').take(2).count();
146 if glob_count > 1 {
147 bail!(validation("refspec patterns may only contain a single '*' character").with_input(spec));
148 }
149 let has_globs = glob_count > 0;
150 if has_globs {
151 validate_partial_name_with_single_glob(spec).or_error()?;
152 } else if !any_name {
153 gix_validate::reference::name_partial(spec).or_error()?;
154 }
155 Ok((Some(spec), has_globs))
156 }
157 None => Ok((None, false)),
158 }
159 }
160}