Skip to content

Commit

Permalink
Support <reference> as the digest of manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn111 committed May 9, 2024
1 parent 8c30a0d commit 167870a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ocipkg/src/distribution/reference.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::Digest;
use anyhow::{bail, Result};
use regex::Regex;
use std::fmt;

/// Reference of container image stored in the repository
///
/// In [OCI distribution spec](https://github.com/opencontainers/distribution-spec/blob/main/spec.md):
/// > `<reference>` MUST be either (a) the digest of the manifest or (b) a tag
/// > `<reference>` as a tag MUST be at most 128 characters
/// > in length and MUST match the following regular expression:
/// > ```text
Expand Down Expand Up @@ -39,6 +41,9 @@ impl Reference {
pub fn new(name: &str) -> Result<Self> {
if REF_RE.is_match(name) {
Ok(Reference(name.to_string()))
} else if name.contains(":") {
_ = Digest::new(name)?;
Ok(Reference(name.to_string()))
} else {
bail!("Invalid reference {name}");
}
Expand All @@ -52,6 +57,10 @@ mod tests {
#[test]
fn reference() {
assert_eq!(Reference::new("latest").unwrap().as_str(), "latest");
assert_eq!(
Reference::new("sha256:a1b2c3").unwrap().as_str(),
"sha256:a1b2c3"
);
// @ is not allowed
assert!(Reference::new("my_super_tag@2").is_err());
}
Expand Down

0 comments on commit 167870a

Please sign in to comment.