@@ -17,27 +17,41 @@ pub(super) fn git_remote_revision(
1717 }
1818
1919 let ref_name = ref_name. unwrap_or ( "HEAD" ) ;
20+ let peeled_ref_name = format ! ( "{ref_name}^{{}}" ) ;
2021 let output = run_git_command_with_timeout (
21- git_command ( ) . arg ( "ls-remote" ) . arg ( source) . arg ( ref_name) ,
22+ git_command ( )
23+ . arg ( "ls-remote" )
24+ . arg ( source)
25+ . arg ( ref_name)
26+ . arg ( peeled_ref_name) ,
2227 "git ls-remote marketplace source" ,
2328 timeout,
2429 ) ?;
2530 ensure_git_success ( & output, "git ls-remote marketplace source" ) ?;
2631
2732 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
28- let Some ( first_line) = stdout. lines ( ) . next ( ) else {
29- return Err ( "git ls-remote returned empty output for marketplace source" . to_string ( ) ) ;
30- } ;
31- let Some ( ( revision, _) ) = first_line. split_once ( '\t' ) else {
32- return Err ( format ! (
33- "unexpected git ls-remote output for marketplace source: {first_line}"
34- ) ) ;
35- } ;
36- let revision = revision. trim ( ) ;
37- if revision. is_empty ( ) {
38- return Err ( "git ls-remote returned empty revision for marketplace source" . to_string ( ) ) ;
33+ parse_git_ls_remote_revision ( & stdout)
34+ }
35+
36+ fn parse_git_ls_remote_revision ( stdout : & str ) -> Result < String , String > {
37+ let mut first_revision = None ;
38+ for line in stdout. lines ( ) {
39+ let Some ( ( revision, reference) ) = line. split_once ( '\t' ) else {
40+ return Err ( format ! (
41+ "unexpected git ls-remote output for marketplace source: {line}"
42+ ) ) ;
43+ } ;
44+ let revision = revision. trim ( ) ;
45+ if revision. is_empty ( ) {
46+ return Err ( "git ls-remote returned empty revision for marketplace source" . to_string ( ) ) ;
47+ }
48+ first_revision. get_or_insert_with ( || revision. to_string ( ) ) ;
49+ if reference. trim ( ) . ends_with ( "^{}" ) {
50+ return Ok ( revision. to_string ( ) ) ;
51+ }
3952 }
40- Ok ( revision. to_string ( ) )
53+ first_revision
54+ . ok_or_else ( || "git ls-remote returned empty output for marketplace source" . to_string ( ) )
4155}
4256
4357pub ( super ) fn clone_git_source (
@@ -225,6 +239,7 @@ fn ensure_git_success(output: &Output, context: &str) -> Result<(), String> {
225239mod tests {
226240 use super :: git_command;
227241 use super :: is_full_git_sha;
242+ use super :: parse_git_ls_remote_revision;
228243 use super :: strip_windows_verbatim_path_prefix;
229244 use pretty_assertions:: assert_eq;
230245 use std:: ffi:: OsStr ;
@@ -236,6 +251,29 @@ mod tests {
236251 assert ! ( !is_full_git_sha( "0123456" ) ) ;
237252 }
238253
254+ #[ test]
255+ fn remote_revision_prefers_peeled_annotated_tag ( ) {
256+ let output = concat ! (
257+ "0c7a0df5d351306392958f37ba80d590070a318d\t refs/tags/electric/v1.6.10-electric.2\n " ,
258+ "e690cabf19dea108fced6ee1a7ef17e9e623ec54\t refs/tags/electric/v1.6.10-electric.2^{}\n " ,
259+ ) ;
260+
261+ assert_eq ! (
262+ parse_git_ls_remote_revision( output) ,
263+ Ok ( "e690cabf19dea108fced6ee1a7ef17e9e623ec54" . to_string( ) )
264+ ) ;
265+ }
266+
267+ #[ test]
268+ fn remote_revision_uses_direct_ref_when_no_peeled_ref_exists ( ) {
269+ let output = "e690cabf19dea108fced6ee1a7ef17e9e623ec54\t refs/heads/main\n " ;
270+
271+ assert_eq ! (
272+ parse_git_ls_remote_revision( output) ,
273+ Ok ( "e690cabf19dea108fced6ee1a7ef17e9e623ec54" . to_string( ) )
274+ ) ;
275+ }
276+
239277 #[ test]
240278 fn git_command_uses_path_lookup_with_stable_noninteractive_env ( ) {
241279 let command = git_command ( ) ;
0 commit comments