@@ -277,6 +277,18 @@ pub(crate) fn path_relative_to(from: &Path, to: &Path) -> Result<PathBuf> {
277277 return Ok ( final_path) ;
278278}
279279
280+ /// Converts an ImageReference to a container reference string suitable for use with container storage APIs.
281+ /// For registry transport, returns just the image name. For other transports, prepends the transport.
282+ pub ( crate ) fn imageref_to_container_ref ( imgref : & crate :: spec:: ImageReference ) -> String {
283+ if imgref. transport == "registry" {
284+ // For registry transport, the image name is already in the right format
285+ imgref. image . clone ( )
286+ } else {
287+ // For other transports (containers-storage, oci, etc.), prepend the transport
288+ format ! ( "{}:{}" , imgref. transport, imgref. image)
289+ }
290+ }
291+
280292#[ cfg( test) ]
281293mod tests {
282294 use super :: * ;
@@ -337,4 +349,39 @@ mod tests {
337349 assert ! ( have_executable( "true" ) . unwrap( ) ) ;
338350 assert ! ( !have_executable( "someexethatdoesnotexist" ) . unwrap( ) ) ;
339351 }
352+
353+ #[ test]
354+ fn test_imageref_to_container_ref ( ) {
355+ use crate :: spec:: ImageReference ;
356+
357+ // Test registry transport (should return only the image name)
358+ let registry_ref = ImageReference {
359+ transport : "registry" . to_string ( ) ,
360+ image : "quay.io/example/foo:latest" . to_string ( ) ,
361+ signature : None ,
362+ } ;
363+ assert_eq ! (
364+ imageref_to_container_ref( & registry_ref) ,
365+ "quay.io/example/foo:latest"
366+ ) ;
367+
368+ // Test containers-storage transport
369+ let storage_ref = ImageReference {
370+ transport : "containers-storage" . to_string ( ) ,
371+ image : "localhost/bootc" . to_string ( ) ,
372+ signature : None ,
373+ } ;
374+ assert_eq ! (
375+ imageref_to_container_ref( & storage_ref) ,
376+ "containers-storage:localhost/bootc"
377+ ) ;
378+
379+ // Test oci transport
380+ let oci_ref = ImageReference {
381+ transport : "oci" . to_string ( ) ,
382+ image : "/path/to/image" . to_string ( ) ,
383+ signature : None ,
384+ } ;
385+ assert_eq ! ( imageref_to_container_ref( & oci_ref) , "oci:/path/to/image" ) ;
386+ }
340387}
0 commit comments