@@ -4,7 +4,7 @@ use std::{
44 path:: { Path , PathBuf } ,
55} ;
66
7- use anyhow:: { Context , Result } ;
7+ use anyhow:: { Context , Result , bail } ;
88use serde:: { Deserialize , Serialize } ;
99use serde_json:: Value ;
1010
@@ -99,6 +99,7 @@ impl Cli {
9999#[ derive( Deserialize ) ]
100100struct LockPackage {
101101 name : String ,
102+ version : String ,
102103 source : Option < String > ,
103104}
104105
@@ -107,7 +108,10 @@ struct CargoLock {
107108 package : Vec < LockPackage > ,
108109}
109110
110- /// Parse `Cargo.lock` to find the git commit SHA for the `vrl` package.
111+ /// Parse `Cargo.lock` to find a git ref for the `vrl` package.
112+ ///
113+ /// Returns the commit SHA for git-sourced dependencies, or a version tag (e.g. `v0.31.0`) for
114+ /// registry-sourced dependencies.
111115fn get_vrl_commit_sha ( repo_root : & Path ) -> Result < String > {
112116 let lock_path = repo_root. join ( "Cargo.lock" ) ;
113117 let lock_text = fs:: read_to_string ( & lock_path)
@@ -119,22 +123,20 @@ fn get_vrl_commit_sha(repo_root: &Path) -> Result<String> {
119123 let pkg = lock
120124 . package
121125 . iter ( )
122- . find ( |p| {
123- p. name == VRL_PACKAGE_NAME
124- && p. source
125- . as_deref ( )
126- . is_some_and ( |s| s. contains ( "github.com/vectordotdev/vrl" ) )
127- } )
128- . context ( "Could not find VRL package with git source in Cargo.lock" ) ?;
129-
130- // Source format: "git+https://github.com/vectordotdev/vrl.git?branch=doc-generation#5316c01b..."
131- let source = pkg. source . as_deref ( ) . unwrap ( ) ;
132- let sha = source
133- . rsplit_once ( '#' )
134- . map ( |( _, sha) | sha)
135- . context ( "Could not extract commit SHA from VRL source string" ) ?;
136-
137- Ok ( sha. to_string ( ) )
126+ . find ( |p| p. name == VRL_PACKAGE_NAME )
127+ . context ( "Could not find VRL package in Cargo.lock" ) ?;
128+
129+ match pkg. source . as_deref ( ) {
130+ // Git source: "git+https://github.com/vectordotdev/vrl.git?branch=main#5316c01b..."
131+ Some ( source) if source. starts_with ( "git+" ) => source
132+ . rsplit_once ( '#' )
133+ . map ( |( _, sha) | sha. to_string ( ) )
134+ . context ( "Could not extract commit SHA from VRL git source string" ) ,
135+ // Registry source (crates.io): use the version as a tag
136+ Some ( source) if source. starts_with ( "registry+" ) => Ok ( format ! ( "v{}" , pkg. version) ) ,
137+ Some ( source) => bail ! ( "Unrecognized VRL package source in Cargo.lock: {source}" ) ,
138+ None => bail ! ( "VRL package in Cargo.lock has no source field" ) ,
139+ }
138140}
139141
140142/// Read all `*.json` files from a directory into a name->value map.
0 commit comments