use bstr::{BStr, BString, ByteSlice};
use crate::{
instruction::{Fetch, Push},
parse::Operation,
types::Mode,
Instruction, RefSpec, RefSpecRef,
};
impl RefSpec {
pub fn to_ref(&self) -> RefSpecRef<'_> {
RefSpecRef {
mode: self.mode,
op: self.op,
src: self.src.as_ref().map(AsRef::as_ref),
dst: self.dst.as_ref().map(AsRef::as_ref),
}
}
pub fn allow_non_fast_forward(&self) -> bool {
matches!(self.mode, Mode::Force)
}
}
mod impls {
use std::{
cmp::Ordering,
hash::{Hash, Hasher},
};
use crate::{RefSpec, RefSpecRef};
impl From<RefSpecRef<'_>> for RefSpec {
fn from(v: RefSpecRef<'_>) -> Self {
v.to_owned()
}
}
impl Hash for RefSpec {
fn hash<H: Hasher>(&self, state: &mut H) {
self.to_ref().hash(state)
}
}
impl Hash for RefSpecRef<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.instruction().hash(state)
}
}
impl PartialEq for RefSpec {
fn eq(&self, other: &Self) -> bool {
self.to_ref().eq(&other.to_ref())
}
}
impl PartialEq for RefSpecRef<'_> {
fn eq(&self, other: &Self) -> bool {
self.instruction().eq(&other.instruction())
}
}
impl PartialOrd for RefSpecRef<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialOrd for RefSpec {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.to_ref().cmp(&other.to_ref()))
}
}
impl Ord for RefSpecRef<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.instruction().cmp(&other.instruction())
}
}
impl Ord for RefSpec {
fn cmp(&self, other: &Self) -> Ordering {
self.to_ref().cmp(&other.to_ref())
}
}
}
impl<'a> RefSpecRef<'a> {
pub fn source(&self) -> Option<&BStr> {
self.src
}
pub fn destination(&self) -> Option<&BStr> {
self.dst
}
pub fn remote(&self) -> Option<&BStr> {
match self.op {
Operation::Push => self.dst,
Operation::Fetch => self.src,
}
}
pub fn local(&self) -> Option<&BStr> {
match self.op {
Operation::Push => self.src,
Operation::Fetch => self.dst,
}
}
pub fn prefix(&self) -> Option<&BStr> {
if self.mode == Mode::Negative {
return None;
}
let source = match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
}?;
if source == "HEAD" {
return source.into();
}
let suffix = source.strip_prefix(b"refs/")?;
let slash_pos = suffix.find_byte(b'/')?;
let prefix = source[..="refs/".len() + slash_pos].as_bstr();
(!prefix.contains(&b'*')).then_some(prefix)
}
pub fn expand_prefixes(&self, out: &mut Vec<BString>) {
match self.prefix() {
Some(prefix) => out.push(prefix.into()),
None => {
let source = match match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
} {
Some(source) => source,
None => return,
};
if let Some(rest) = source.strip_prefix(b"refs/") {
if !rest.contains(&b'/') {
out.push(source.into());
}
return;
} else if gix_hash::ObjectId::from_hex(source).is_ok() {
return;
}
expand_partial_name(source, |expanded| {
out.push(expanded.into());
None::<()>
});
}
}
}
pub fn instruction(&self) -> Instruction<'a> {
match self.op {
Operation::Fetch => match (self.mode, self.src, self.dst) {
(Mode::Normal | Mode::Force, Some(src), None) => Instruction::Fetch(Fetch::Only { src }),
(Mode::Normal | Mode::Force, Some(src), Some(dst)) => Instruction::Fetch(Fetch::AndUpdate {
src,
dst,
allow_non_fast_forward: matches!(self.mode, Mode::Force),
}),
(Mode::Negative, Some(src), None) => Instruction::Fetch(Fetch::Exclude { src }),
(mode, src, dest) => {
unreachable!(
"BUG: fetch instructions with {:?} {:?} {:?} are not possible",
mode, src, dest
)
}
},
Operation::Push => match (self.mode, self.src, self.dst) {
(Mode::Normal | Mode::Force, Some(src), None) => Instruction::Push(Push::Matching {
src,
dst: src,
allow_non_fast_forward: matches!(self.mode, Mode::Force),
}),
(Mode::Normal | Mode::Force, None, Some(dst)) => {
Instruction::Push(Push::Delete { ref_or_pattern: dst })
}
(Mode::Normal | Mode::Force, None, None) => Instruction::Push(Push::AllMatchingBranches {
allow_non_fast_forward: matches!(self.mode, Mode::Force),
}),
(Mode::Normal | Mode::Force, Some(src), Some(dst)) => Instruction::Push(Push::Matching {
src,
dst,
allow_non_fast_forward: matches!(self.mode, Mode::Force),
}),
(mode, src, dest) => {
unreachable!(
"BUG: push instructions with {:?} {:?} {:?} are not possible",
mode, src, dest
)
}
},
}
}
}
impl RefSpecRef<'_> {
pub fn to_owned(&self) -> RefSpec {
RefSpec {
mode: self.mode,
op: self.op,
src: self.src.map(ToOwned::to_owned),
dst: self.dst.map(ToOwned::to_owned),
}
}
}
pub(crate) fn expand_partial_name<T>(name: &BStr, mut cb: impl FnMut(&BStr) -> Option<T>) -> Option<T> {
use bstr::ByteVec;
let mut buf = BString::from(Vec::with_capacity(128));
for (base, append_head) in [
("", false),
("refs/", false),
("refs/tags/", false),
("refs/heads/", false),
("refs/remotes/", false),
("refs/remotes/", true),
] {
buf.clear();
buf.push_str(base);
buf.push_str(name);
if append_head {
buf.push_str("/HEAD");
}
if let Some(res) = cb(buf.as_ref()) {
return Some(res);
}
}
None
}