@@ -110,7 +110,7 @@ use std::fs::{self, File};
110110use std:: io;
111111use std:: io:: ErrorKind ;
112112use std:: path:: { Path , PathBuf } ;
113- use std:: process:: Command ;
113+ use std:: process:: { Command , Stdio } ;
114114use std:: str;
115115
116116use build_helper:: ci:: CiEnv ;
@@ -662,12 +662,32 @@ impl Build {
662662
663663 // Try passing `--progress` to start, then run git again without if that fails.
664664 let update = |progress : bool | {
665- let mut git = Command :: new ( "git" ) ;
665+ // Git is buggy and will try to fetch submodules from the tracking branch for *this* repository,
666+ // even though that has no relation to the upstream for the submodule.
667+ let current_branch = {
668+ let output = self
669+ . config
670+ . git ( )
671+ . args ( [ "symbolic-ref" , "--short" , "HEAD" ] )
672+ . stderr ( Stdio :: inherit ( ) )
673+ . output ( ) ;
674+ let output = t ! ( output) ;
675+ if output. status . success ( ) {
676+ Some ( String :: from_utf8 ( output. stdout ) . unwrap ( ) . trim ( ) . to_owned ( ) )
677+ } else {
678+ None
679+ }
680+ } ;
681+
682+ let mut git = self . config . git ( ) ;
683+ if let Some ( branch) = current_branch {
684+ git. arg ( "-c" ) . arg ( format ! ( "branch.{branch}.remote=origin" ) ) ;
685+ }
666686 git. args ( & [ "submodule" , "update" , "--init" , "--recursive" , "--depth=1" ] ) ;
667687 if progress {
668688 git. arg ( "--progress" ) ;
669689 }
670- git. arg ( relative_path) . current_dir ( & self . config . src ) ;
690+ git. arg ( relative_path) ;
671691 git
672692 } ;
673693 // NOTE: doesn't use `try_run` because this shouldn't print an error if it fails.
0 commit comments