Skip to content
Clone
68129bc
70 lines 1.8 KB Rust
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub enum GitOperation {
UploadPack { bare: PathBuf },
ReceivePack { bare: PathBuf },
UploadArchive { bare: PathBuf },
}
impl GitOperation {
pub fn bare(&self) -> &PathBuf {
match self {
Self::UploadPack { bare }
| Self::ReceivePack { bare }
| Self::UploadArchive { bare } => bare,
}
}
pub fn is_write(&self) -> bool {
matches!(self, Self::ReceivePack { .. })
}
pub fn service_arg(&self) -> &'static str {
match self {
Self::UploadPack { .. } => "upload-pack",
Self::ReceivePack { .. } => "receive-pack",
Self::UploadArchive { .. } => "upload-archive",
}
}
pub fn is_upload_archive(&self) -> bool {
matches!(self, Self::UploadArchive { .. })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::git_service::GitService;
#[test]
fn service_to_operation_is_not_write_by_default() {
assert!(!GitService::UploadPack.is_write());
assert!(!GitService::UploadArchive.is_write());
assert!(GitService::ReceivePack.is_write());
}
#[test]
fn upload_archive_is_read_only_operation() {
let op = GitOperation::UploadArchive {
bare: PathBuf::from("/tmp/nonexistent-repo.git"),
};
assert!(!op.is_write());
assert_eq!(op.service_arg(), "upload-archive");
assert!(op.is_upload_archive());
}
#[test]
fn receive_pack_is_the_only_write_operation() {
let rp = GitOperation::ReceivePack {
bare: PathBuf::from("/tmp/r.git"),
};
let up = GitOperation::UploadPack {
bare: PathBuf::from("/tmp/u.git"),
};
assert!(rp.is_write());
assert!(!up.is_write());
assert!(!up.is_upload_archive());
}
}