11use super :: project_git_exec:: {
2- build_git_auth_config, clean_branch, run_git, validate_clone_url , GitAuthConfig ,
2+ build_git_auth_config, clean_branch, run_git, validate_workspace_clone_url , GitAuthConfig ,
33} ;
44use super :: project_repo_paths:: { canonical_repos_roots, find_local_repo_dir} ;
55use crate :: app_state:: AppState ;
@@ -57,6 +57,7 @@ pub struct ProjectRepoSyncStatusInfo {
5757 pub remote_branch : Option < String > ,
5858 pub remote_head : Option < String > ,
5959 pub remote_short_head : Option < String > ,
60+ pub merge_base : Option < String > ,
6061 pub ahead_count : usize ,
6162 pub behind_count : usize ,
6263 pub has_uncommitted_changes : bool ,
@@ -70,6 +71,9 @@ pub struct ProjectRepoSyncStatusInfo {
7071pub struct ProjectRepoPushResult {
7172 pub pushed : bool ,
7273 pub message : String ,
74+ pub branch : String ,
75+ pub commit : String ,
76+ pub merge_base : Option < String > ,
7377}
7478#[ derive( Serialize ) ]
7579pub struct ProjectRepoPullResult {
@@ -104,7 +108,7 @@ fn short_hash(hash: &str) -> String {
104108 hash. chars ( ) . take ( 7 ) . collect ( )
105109}
106110
107- fn first_output_line ( output : & str ) -> Option < String > {
111+ pub ( crate ) fn first_output_line ( output : & str ) -> Option < String > {
108112 output
109113 . lines ( )
110114 . next ( )
@@ -492,6 +496,7 @@ fn compare_local_remote_status(
492496 repo_dir : & std:: path:: Path ,
493497 clone_url : & str ,
494498 branch_name : Option < & str > ,
499+ base_branch : Option < & str > ,
495500 auth : & GitAuthConfig ,
496501) -> ProjectRepoSyncStatusInfo {
497502 let local_branch = run_git ( & [ "branch" , "--show-current" ] , Some ( repo_dir) , auth)
@@ -518,18 +523,20 @@ fn compare_local_remote_status(
518523 auth,
519524 ) ;
520525 }
521- let _ = run_git (
522- & [
523- "fetch" ,
524- "--quiet" ,
525- "--depth=100" ,
526- "--end-of-options" ,
527- "origin" ,
528- branch. as_str ( ) ,
529- ] ,
530- Some ( repo_dir) ,
531- auth,
532- ) ;
526+ let base_branch =
527+ normalize_branch_option ( base_branch) . filter ( |base_branch| * base_branch != branch) ;
528+ let mut fetch_args = vec ! [
529+ "fetch" ,
530+ "--quiet" ,
531+ "--depth=100" ,
532+ "--end-of-options" ,
533+ "origin" ,
534+ branch. as_str( ) ,
535+ ] ;
536+ if let Some ( base_branch) = base_branch. as_deref ( ) {
537+ fetch_args. push ( base_branch) ;
538+ }
539+ let _ = run_git ( & fetch_args, Some ( repo_dir) , auth) ;
533540
534541 let local_head = run_git ( & [ "rev-parse" , "HEAD" ] , Some ( repo_dir) , auth)
535542 . ok ( )
@@ -542,6 +549,19 @@ fn compare_local_remote_status(
542549 )
543550 . ok ( )
544551 . and_then ( |output| first_output_line ( & output) ) ;
552+ let merge_base = base_branch. as_deref ( ) . and_then ( |base_branch| {
553+ run_git (
554+ & [
555+ "merge-base" ,
556+ "HEAD" ,
557+ format ! ( "origin/{base_branch}" ) . as_str ( ) ,
558+ ] ,
559+ Some ( repo_dir) ,
560+ auth,
561+ )
562+ . ok ( )
563+ . and_then ( |output| first_output_line ( & output) )
564+ } ) ;
545565 let status = run_git ( & [ "status" , "--porcelain" ] , Some ( repo_dir) , auth) . unwrap_or_default ( ) ;
546566 let has_uncommitted_changes = has_uncommitted_changes ( & status) ;
547567 let has_untracked_files = has_untracked_files ( & status) ;
@@ -576,6 +596,10 @@ fn compare_local_remote_status(
576596
577597 let push_block_reason = if local_head. is_none ( ) {
578598 Some ( "No local commits to push." . to_string ( ) )
599+ } else if local_branch. as_deref ( ) != Some ( branch. as_str ( ) ) {
600+ Some ( format ! (
601+ "Local checkout is on a different branch than {branch}."
602+ ) )
579603 } else if has_uncommitted_changes || has_untracked_files {
580604 Some ( "Commit or discard local changes before pushing." . to_string ( ) )
581605 } else if behind_count > 0 {
@@ -615,6 +639,7 @@ fn compare_local_remote_status(
615639 remote_branch : Some ( branch) ,
616640 remote_head : remote_head. clone ( ) ,
617641 remote_short_head : remote_head. as_deref ( ) . map ( short_hash) ,
642+ merge_base,
618643 ahead_count,
619644 behind_count,
620645 has_uncommitted_changes,
@@ -657,7 +682,7 @@ pub async fn get_project_repo_snapshot(
657682 target_commit : Option < String > ,
658683 state : State < ' _ , AppState > ,
659684) -> Result < ProjectRepoSnapshotInfo , String > {
660- validate_clone_url ( & clone_url) ?;
685+ validate_workspace_clone_url ( & clone_url, & state ) ?;
661686 let auth = build_git_auth_config ( & state) ?;
662687 let branch = clean_branch ( default_branch) ;
663688 let base_branch = clean_branch ( base_branch) ;
@@ -793,10 +818,11 @@ pub async fn get_project_repo_sync_status(
793818 repos_dir : Option < String > ,
794819 project_dtag : String ,
795820 clone_url : String ,
796- default_branch : Option < String > ,
821+ branch_name : Option < String > ,
822+ base_branch : Option < String > ,
797823 state : State < ' _ , AppState > ,
798824) -> Result < ProjectRepoSyncStatusInfo , String > {
799- validate_clone_url ( & clone_url) ?;
825+ validate_workspace_clone_url ( & clone_url, & state ) ?;
800826 let auth = build_git_auth_config ( & state) ?;
801827
802828 tauri:: async_runtime:: spawn_blocking ( move || {
@@ -808,11 +834,12 @@ pub async fn get_project_repo_sync_status(
808834 local_branch : None ,
809835 local_head : None ,
810836 local_short_head : None ,
811- remote_branch : default_branch
837+ remote_branch : branch_name
812838 . as_deref ( )
813839 . and_then ( |branch| normalize_branch_option ( Some ( branch) ) ) ,
814840 remote_head : None ,
815841 remote_short_head : None ,
842+ merge_base : None ,
816843 ahead_count : 0 ,
817844 behind_count : 0 ,
818845 has_uncommitted_changes : false ,
@@ -827,7 +854,8 @@ pub async fn get_project_repo_sync_status(
827854 Ok ( compare_local_remote_status (
828855 & repo_dir,
829856 & clone_url,
830- default_branch. as_deref ( ) ,
857+ branch_name. as_deref ( ) ,
858+ base_branch. as_deref ( ) ,
831859 & auth,
832860 ) )
833861 } )
@@ -840,10 +868,11 @@ pub async fn push_project_local_repository(
840868 repos_dir : Option < String > ,
841869 project_dtag : String ,
842870 clone_url : String ,
843- default_branch : Option < String > ,
871+ branch_name : Option < String > ,
872+ base_branch : Option < String > ,
844873 state : State < ' _ , AppState > ,
845874) -> Result < ProjectRepoPushResult , String > {
846- validate_clone_url ( & clone_url) ?;
875+ validate_workspace_clone_url ( & clone_url, & state ) ?;
847876 let auth = build_git_auth_config ( & state) ?;
848877
849878 tauri:: async_runtime:: spawn_blocking ( move || {
@@ -852,17 +881,26 @@ pub async fn push_project_local_repository(
852881 else {
853882 return Err ( "No local checkout found." . to_string ( ) ) ;
854883 } ;
855- let status =
856- compare_local_remote_status ( & repo_dir, & clone_url, default_branch. as_deref ( ) , & auth) ;
884+ let status = compare_local_remote_status (
885+ & repo_dir,
886+ & clone_url,
887+ branch_name. as_deref ( ) ,
888+ base_branch. as_deref ( ) ,
889+ & auth,
890+ ) ;
857891 if !status. can_push {
858892 return Err ( status
859893 . push_block_reason
860894 . unwrap_or_else ( || "Local checkout cannot be pushed." . to_string ( ) ) ) ;
861895 }
862896 let branch = status
863897 . remote_branch
864- . as_deref ( )
898+ . clone ( )
865899 . 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 ( ) ) ?;
866904 run_git (
867905 & [
868906 "push" ,
@@ -877,6 +915,9 @@ pub async fn push_project_local_repository(
877915 Ok ( ProjectRepoPushResult {
878916 pushed : true ,
879917 message : format ! ( "Pushed {branch} to remote." ) ,
918+ branch,
919+ commit,
920+ merge_base : status. merge_base ,
880921 } )
881922 } )
882923 . await
@@ -890,10 +931,10 @@ pub async fn pull_project_local_repository(
890931 repos_dir : Option < String > ,
891932 project_dtag : String ,
892933 clone_url : String ,
893- default_branch : Option < String > ,
934+ branch_name : Option < String > ,
894935 state : State < ' _ , AppState > ,
895936) -> Result < ProjectRepoPullResult , String > {
896- validate_clone_url ( & clone_url) ?;
937+ validate_workspace_clone_url ( & clone_url, & state ) ?;
897938 let auth = build_git_auth_config ( & state) ?;
898939
899940 tauri:: async_runtime:: spawn_blocking ( move || {
@@ -903,7 +944,7 @@ pub async fn pull_project_local_repository(
903944 return Err ( "No local checkout found." . to_string ( ) ) ;
904945 } ;
905946 let status =
906- compare_local_remote_status ( & repo_dir, & clone_url, default_branch . as_deref ( ) , & auth) ;
947+ compare_local_remote_status ( & repo_dir, & clone_url, branch_name . as_deref ( ) , None , & auth) ;
907948 if !status. can_pull {
908949 return Err ( status
909950 . pull_block_reason
0 commit comments