11use super :: project_git_exec:: {
22 build_git_auth_config, clean_branch, run_git, validate_workspace_clone_url, GitAuthConfig ,
33} ;
4+ use super :: project_git_push:: push_project_local_repository_blocking;
45use super :: project_repo_paths:: { canonical_repos_roots, find_local_repo_dir} ;
56use crate :: app_state:: AppState ;
67use serde:: Serialize ;
@@ -52,6 +53,7 @@ pub struct ProjectLocalRepoInfo {
5253pub struct ProjectRepoSyncStatusInfo {
5354 pub local_path : Option < String > ,
5455 pub local_branch : Option < String > ,
56+ pub local_branches : Vec < String > ,
5557 pub local_head : Option < String > ,
5658 pub local_short_head : Option < String > ,
5759 pub remote_branch : Option < String > ,
@@ -492,7 +494,7 @@ pub(crate) fn normalize_branch_option(branch: Option<&str>) -> Option<String> {
492494 clean_branch ( branch. map ( str:: to_string) )
493495}
494496
495- fn compare_local_remote_status (
497+ pub ( crate ) fn compare_local_remote_status (
496498 repo_dir : & std:: path:: Path ,
497499 clone_url : & str ,
498500 branch_name : Option < & str > ,
@@ -502,6 +504,23 @@ fn compare_local_remote_status(
502504 let local_branch = run_git ( & [ "branch" , "--show-current" ] , Some ( repo_dir) , auth)
503505 . ok ( )
504506 . and_then ( |output| first_output_line ( & output) ) ;
507+ let local_branches = run_git (
508+ & [
509+ "for-each-ref" ,
510+ "--count=200" ,
511+ "--format=%(refname:short)" ,
512+ "refs/heads/" ,
513+ ] ,
514+ Some ( repo_dir) ,
515+ auth,
516+ )
517+ . map ( |output| {
518+ output
519+ . lines ( )
520+ . filter_map ( |branch| normalize_branch_option ( Some ( branch) ) )
521+ . collect ( )
522+ } )
523+ . unwrap_or_default ( ) ;
505524 // The local checkout's branch name is attacker-influencable (a hostile
506525 // remote can point HEAD at a flag-shaped refname), so it must pass the
507526 // same `clean_branch` validation as relay-supplied names before it is
@@ -549,6 +568,17 @@ fn compare_local_remote_status(
549568 )
550569 . ok ( )
551570 . and_then ( |output| first_output_line ( & output) ) ;
571+ // A legacy empty clone may have an unborn local `master` while the project
572+ // declares `main`. Permit that mismatch only when the remote has no branch
573+ // refs at all; any lookup failure is treated as non-empty (fail closed).
574+ let remote_has_branches = run_git (
575+ & [ "ls-remote" , "--heads" , "--end-of-options" , "origin" ] ,
576+ Some ( repo_dir) ,
577+ auth,
578+ )
579+ . map ( |output| !output. trim ( ) . is_empty ( ) )
580+ . unwrap_or ( true ) ;
581+ let is_first_publish = remote_head. is_none ( ) && !remote_has_branches;
552582 let merge_base = base_branch. as_deref ( ) . and_then ( |base_branch| {
553583 run_git (
554584 & [
@@ -596,7 +626,7 @@ fn compare_local_remote_status(
596626
597627 let push_block_reason = if local_head. is_none ( ) {
598628 Some ( "No local commits to push." . to_string ( ) )
599- } else if local_branch. as_deref ( ) != Some ( branch. as_str ( ) ) {
629+ } else if local_branch. as_deref ( ) != Some ( branch. as_str ( ) ) && !is_first_publish {
600630 Some ( format ! (
601631 "Local checkout is on a different branch than {branch}."
602632 ) )
@@ -634,6 +664,7 @@ fn compare_local_remote_status(
634664 ProjectRepoSyncStatusInfo {
635665 local_path : Some ( repo_dir. display ( ) . to_string ( ) ) ,
636666 local_branch,
667+ local_branches,
637668 local_head : local_head. clone ( ) ,
638669 local_short_head : local_head. as_deref ( ) . map ( short_hash) ,
639670 remote_branch : Some ( branch) ,
@@ -832,6 +863,7 @@ pub async fn get_project_repo_sync_status(
832863 return Ok ( ProjectRepoSyncStatusInfo {
833864 local_path : None ,
834865 local_branch : None ,
866+ local_branches : Vec :: new ( ) ,
835867 local_head : None ,
836868 local_short_head : None ,
837869 remote_branch : branch_name
@@ -881,44 +913,13 @@ pub async fn push_project_local_repository(
881913 else {
882914 return Err ( "No local checkout found." . to_string ( ) ) ;
883915 } ;
884- let status = compare_local_remote_status (
916+ push_project_local_repository_blocking (
885917 & repo_dir,
886- & clone_url,
887- branch_name. as_deref ( ) ,
888- base_branch. as_deref ( ) ,
918+ clone_url,
919+ branch_name,
920+ base_branch,
889921 & auth,
890- ) ;
891- if !status. can_push {
892- return Err ( status
893- . push_block_reason
894- . unwrap_or_else ( || "Local checkout cannot be pushed." . to_string ( ) ) ) ;
895- }
896- let branch = status
897- . remote_branch
898- . clone ( )
899- . ok_or_else ( || "No branch selected for push." . to_string ( ) ) ?;
900- let commit = status
901- . local_head
902- . clone ( )
903- . ok_or_else ( || "No local commit selected for push." . to_string ( ) ) ?;
904- run_git (
905- & [
906- "push" ,
907- "--end-of-options" ,
908- "origin" ,
909- format ! ( "HEAD:{branch}" ) . as_str ( ) ,
910- ] ,
911- Some ( & repo_dir) ,
912- & auth,
913- ) ?;
914-
915- Ok ( ProjectRepoPushResult {
916- pushed : true ,
917- message : format ! ( "Pushed {branch} to remote." ) ,
918- branch,
919- commit,
920- merge_base : status. merge_base ,
921- } )
922+ )
922923 } )
923924 . await
924925 . map_err ( |error| format ! ( "repo push task failed: {error}" ) ) ?
0 commit comments