@@ -78,13 +78,22 @@ pub fn rev_exists(rev: &str, git_dir: Option<&Path>) -> Result<bool, String> {
78
78
/// We will then fall back to origin/master in the hope that at least this exists.
79
79
pub fn updated_master_branch ( git_dir : Option < & Path > ) -> Result < String , String > {
80
80
let upstream_remote = get_rust_lang_rust_remote ( git_dir) ?;
81
- let upstream_master = format ! ( "{upstream_remote}/master" ) ;
82
- if rev_exists ( & upstream_master, git_dir) ? {
83
- return Ok ( upstream_master) ;
81
+ for upstream_master in [ format ! ( "{upstream_remote}/master" ) , format ! ( "origin/master" ) ] {
82
+ if rev_exists ( & upstream_master, git_dir) ? {
83
+ return Ok ( upstream_master) ;
84
+ }
84
85
}
85
86
86
- // We could implement smarter logic here in the future.
87
- Ok ( "origin/master" . into ( ) )
87
+ Err ( format ! ( "Cannot find any suitable upstream master branch" ) )
88
+ }
89
+
90
+ pub fn get_git_merge_base ( git_dir : Option < & Path > ) -> Result < String , String > {
91
+ let updated_master = updated_master_branch ( git_dir) ?;
92
+ let mut git = Command :: new ( "git" ) ;
93
+ if let Some ( git_dir) = git_dir {
94
+ git. current_dir ( git_dir) ;
95
+ }
96
+ Ok ( output_result ( git. arg ( "merge-base" ) . arg ( & updated_master) . arg ( "HEAD" ) ) ?. trim ( ) . to_owned ( ) )
88
97
}
89
98
90
99
/// Returns the files that have been modified in the current branch compared to the master branch.
@@ -94,20 +103,13 @@ pub fn get_git_modified_files(
94
103
git_dir : Option < & Path > ,
95
104
extensions : & Vec < & str > ,
96
105
) -> Result < Option < Vec < String > > , String > {
97
- let Ok ( updated_master) = updated_master_branch ( git_dir) else {
98
- return Ok ( None ) ;
99
- } ;
100
-
101
- let git = || {
102
- let mut git = Command :: new ( "git" ) ;
103
- if let Some ( git_dir) = git_dir {
104
- git. current_dir ( git_dir) ;
105
- }
106
- git
107
- } ;
106
+ let merge_base = get_git_merge_base ( git_dir) ?;
108
107
109
- let merge_base = output_result ( git ( ) . arg ( "merge-base" ) . arg ( & updated_master) . arg ( "HEAD" ) ) ?;
110
- let files = output_result ( git ( ) . arg ( "diff-index" ) . arg ( "--name-only" ) . arg ( merge_base. trim ( ) ) ) ?
108
+ let mut git = Command :: new ( "git" ) ;
109
+ if let Some ( git_dir) = git_dir {
110
+ git. current_dir ( git_dir) ;
111
+ }
112
+ let files = output_result ( git. arg ( "diff-index" ) . arg ( "--name-only" ) . arg ( merge_base. trim ( ) ) ) ?
111
113
. lines ( )
112
114
. map ( |s| s. trim ( ) . to_owned ( ) )
113
115
. filter ( |f| {
0 commit comments