@@ -113,6 +113,9 @@ pub enum PathFreshness {
113113 /// `upstream` is the latest upstream merge commit that made modifications to the
114114 /// set of paths.
115115 HasLocalModifications { upstream : String } ,
116+ /// No upstream commit was found.
117+ /// This should not happen in most reasonable circumstances, but one never knows.
118+ MissingUpstream ,
116119}
117120
118121/// This function figures out if a set of paths was last modified upstream or
@@ -173,21 +176,29 @@ pub fn check_path_modifications(
173176 // artifacts.
174177
175178 // Do not include HEAD, as it is never an upstream commit
176- get_closest_upstream_commit ( git_dir, config, ci_env) ?
179+ // If we do not find an upstream commit in CI, something is seriously wrong.
180+ Some (
181+ get_closest_upstream_commit ( git_dir, config, ci_env) ?
182+ . expect ( "No upstream commit was found on CI" ) ,
183+ )
177184 } else {
178- // Outside CI, we have to find the most recent upstream commit that
179- // modified the set of paths, to have an upstream reference.
180- let upstream_sha = get_latest_commit_that_modified_files (
185+ // Outside CI, we want to find the most recent upstream commit that
186+ // modified the set of paths, to have an upstream reference that does not change
187+ // unnecessarily often.
188+ // However, if such commit is not found, we can fall back to the latest upstream commit
189+ let upstream_with_modifications = get_latest_commit_that_modified_files (
181190 git_dir,
182191 target_paths,
183192 config. git_merge_commit_email ,
184193 ) ?;
185- let Some ( upstream_sha) = upstream_sha else {
186- eprintln ! ( "No upstream commit that modified paths {target_paths:?} found." ) ;
187- eprintln ! ( "Try to fetch more upstream history." ) ;
188- return Err ( "No upstream commit with modifications found" . to_string ( ) ) ;
189- } ;
190- upstream_sha
194+ match upstream_with_modifications {
195+ Some ( sha) => Some ( sha) ,
196+ None => get_closest_upstream_commit ( git_dir, config, ci_env) ?,
197+ }
198+ } ;
199+
200+ let Some ( upstream_sha) = upstream_sha else {
201+ return Ok ( PathFreshness :: MissingUpstream ) ;
191202 } ;
192203
193204 // For local environments, we want to find out if something has changed
@@ -249,7 +260,7 @@ fn get_closest_upstream_commit(
249260 git_dir : Option < & Path > ,
250261 config : & GitConfig < ' _ > ,
251262 env : CiEnv ,
252- ) -> Result < String , String > {
263+ ) -> Result < Option < String > , String > {
253264 let mut git = Command :: new ( "git" ) ;
254265
255266 if let Some ( git_dir) = git_dir {
@@ -260,7 +271,7 @@ fn get_closest_upstream_commit(
260271 CiEnv :: None => "HEAD" ,
261272 CiEnv :: GitHubActions => {
262273 // On CI, we always have a merge commit at the tip.
263- // We thus skip it, because although it can be creatd by
274+ // We thus skip it, because although it can be created by
264275 // `config.git_merge_commit_email`, it should not be upstream.
265276 "HEAD^1"
266277 }
@@ -273,7 +284,8 @@ fn get_closest_upstream_commit(
273284 & base,
274285 ] ) ;
275286
276- Ok ( output_result ( & mut git) ?. trim ( ) . to_owned ( ) )
287+ let output = output_result ( & mut git) ?. trim ( ) . to_owned ( ) ;
288+ if output. is_empty ( ) { Ok ( None ) } else { Ok ( Some ( output) ) }
277289}
278290
279291/// Returns the files that have been modified in the current branch compared to the master branch.
@@ -287,7 +299,9 @@ pub fn get_git_modified_files(
287299 git_dir : Option < & Path > ,
288300 extensions : & [ & str ] ,
289301) -> Result < Vec < String > , String > {
290- let merge_base = get_closest_upstream_commit ( git_dir, config, CiEnv :: None ) ?;
302+ let Some ( merge_base) = get_closest_upstream_commit ( git_dir, config, CiEnv :: None ) ? else {
303+ return Err ( "No upstream commit was found" . to_string ( ) ) ;
304+ } ;
291305
292306 let mut git = Command :: new ( "git" ) ;
293307 if let Some ( git_dir) = git_dir {
0 commit comments