@@ -123,16 +123,36 @@ pub fn get_closest_merge_commit(
123123 config : & GitConfig < ' _ > ,
124124 target_paths : & [ PathBuf ] ,
125125) -> Result < String , String > {
126- let mut git = Command :: new ( "git" ) ;
126+ // The need of `--first-parent` is inconsistent. It's sometimes required, sometimes not.
127+ // See https://github.com/rust-lang/rust/issues/101907#issuecomment-2677860377 for more
128+ // context.
129+ //
130+ // FIXME: This is so hacky, is there a more sane way to fix this problem?
131+ let result = match get_closest_merge_commit_impl ( git_dir, config, target_paths, false ) {
132+ Ok ( commit) if commit. is_empty ( ) => {
133+ get_closest_merge_commit_impl ( git_dir, config, target_paths, true )
134+ }
135+ result => result,
136+ } ;
127137
128- if let Some ( git_dir) = git_dir {
129- git. current_dir ( git_dir) ;
130- }
138+ return result;
139+
140+ fn get_closest_merge_commit_impl (
141+ git_dir : Option < & Path > ,
142+ config : & GitConfig < ' _ > ,
143+ target_paths : & [ PathBuf ] ,
144+ follow_first_parent_only : bool ,
145+ ) -> Result < String , String > {
146+ let mut git = Command :: new ( "git" ) ;
147+
148+ if let Some ( git_dir) = git_dir {
149+ git. current_dir ( git_dir) ;
150+ }
131151
132- let channel = include_str ! ( "../../ci/channel" ) ;
152+ let channel = include_str ! ( "../../ci/channel" ) ;
133153
134- let merge_base = {
135- if CiEnv :: is_ci ( ) &&
154+ let merge_base = {
155+ if CiEnv :: is_ci ( ) &&
136156 // FIXME: When running on rust-lang managed CI and it's not a nightly build,
137157 // `git_upstream_merge_base` fails with an error message similar to this:
138158 // ```
@@ -141,28 +161,29 @@ pub fn get_closest_merge_commit(
141161 // ```
142162 // Investigate and resolve this issue instead of skipping it like this.
143163 ( channel == "nightly" || !CiEnv :: is_rust_lang_managed_ci_job ( ) )
144- {
145- git_upstream_merge_base ( config, git_dir) . unwrap ( )
146- } else {
147- // For non-CI environments, ignore rust-lang/rust upstream as it usually gets
148- // outdated very quickly.
149- "HEAD" . to_string ( )
164+ {
165+ git_upstream_merge_base ( config, git_dir) . unwrap ( )
166+ } else {
167+ // For non-CI environments, ignore rust-lang/rust upstream as it usually gets
168+ // outdated very quickly.
169+ "HEAD" . to_string ( )
170+ }
171+ } ;
172+
173+ git. args ( [ "rev-list" , & format ! ( "--author={}" , config. git_merge_commit_email) , "-n1" ] ) ;
174+
175+ if follow_first_parent_only {
176+ git. arg ( "--first-parent" ) ;
150177 }
151- } ;
152178
153- git. args ( [
154- "rev-list" ,
155- & format ! ( "--author={}" , config. git_merge_commit_email) ,
156- "-n1" ,
157- "--first-parent" ,
158- & merge_base,
159- ] ) ;
179+ git. arg ( merge_base) ;
160180
161- if !target_paths. is_empty ( ) {
162- git. arg ( "--" ) . args ( target_paths) ;
163- }
181+ if !target_paths. is_empty ( ) {
182+ git. arg ( "--" ) . args ( target_paths) ;
183+ }
164184
165- Ok ( output_result ( & mut git) ?. trim ( ) . to_owned ( ) )
185+ Ok ( output_result ( & mut git) ?. trim ( ) . to_owned ( ) )
186+ }
166187}
167188
168189/// Returns the files that have been modified in the current branch compared to the master branch.
0 commit comments