@@ -42,6 +42,9 @@ pub enum PathFreshness {
4242 /// `upstream` is the latest upstream merge commit that made modifications to the
4343 /// set of paths.
4444 HasLocalModifications { upstream : String } ,
45+ /// No upstream commit was found.
46+ /// This should not happen in most reasonable circumstances, but one never knows.
47+ MissingUpstream ,
4548}
4649
4750/// This function figures out if a set of paths was last modified upstream or
@@ -102,21 +105,29 @@ pub fn check_path_modifications(
102105 // artifacts.
103106
104107 // Do not include HEAD, as it is never an upstream commit
105- get_closest_upstream_commit ( git_dir, config, ci_env) ?
108+ // If we do not find an upstream commit in CI, something is seriously wrong.
109+ Some (
110+ get_closest_upstream_commit ( git_dir, config, ci_env) ?
111+ . expect ( "No upstream commit was found on CI" ) ,
112+ )
106113 } else {
107- // Outside CI, we have to find the most recent upstream commit that
108- // modified the set of paths, to have an upstream reference.
109- let upstream_sha = get_latest_commit_that_modified_files (
114+ // Outside CI, we want to find the most recent upstream commit that
115+ // modified the set of paths, to have an upstream reference that does not change
116+ // unnecessarily often.
117+ // However, if such commit is not found, we can fall back to the latest upstream commit
118+ let upstream_with_modifications = get_latest_commit_that_modified_files (
110119 git_dir,
111120 target_paths,
112121 config. git_merge_commit_email ,
113122 ) ?;
114- let Some ( upstream_sha) = upstream_sha else {
115- eprintln ! ( "No upstream commit that modified paths {target_paths:?} found." ) ;
116- eprintln ! ( "Try to fetch more upstream history." ) ;
117- return Err ( "No upstream commit with modifications found" . to_string ( ) ) ;
118- } ;
119- upstream_sha
123+ match upstream_with_modifications {
124+ Some ( sha) => Some ( sha) ,
125+ None => get_closest_upstream_commit ( git_dir, config, ci_env) ?,
126+ }
127+ } ;
128+
129+ let Some ( upstream_sha) = upstream_sha else {
130+ return Ok ( PathFreshness :: MissingUpstream ) ;
120131 } ;
121132
122133 // For local environments, we want to find out if something has changed
@@ -178,7 +189,7 @@ fn get_closest_upstream_commit(
178189 git_dir : Option < & Path > ,
179190 config : & GitConfig < ' _ > ,
180191 env : CiEnv ,
181- ) -> Result < String , String > {
192+ ) -> Result < Option < String > , String > {
182193 let mut git = Command :: new ( "git" ) ;
183194
184195 if let Some ( git_dir) = git_dir {
@@ -189,7 +200,7 @@ fn get_closest_upstream_commit(
189200 CiEnv :: None => "HEAD" ,
190201 CiEnv :: GitHubActions => {
191202 // On CI, we always have a merge commit at the tip.
192- // We thus skip it, because although it can be creatd by
203+ // We thus skip it, because although it can be created by
193204 // `config.git_merge_commit_email`, it should not be upstream.
194205 "HEAD^1"
195206 }
@@ -202,7 +213,8 @@ fn get_closest_upstream_commit(
202213 & base,
203214 ] ) ;
204215
205- Ok ( output_result ( & mut git) ?. trim ( ) . to_owned ( ) )
216+ let output = output_result ( & mut git) ?. trim ( ) . to_owned ( ) ;
217+ if output. is_empty ( ) { Ok ( None ) } else { Ok ( Some ( output) ) }
206218}
207219
208220/// Returns the files that have been modified in the current branch compared to the master branch.
@@ -216,7 +228,9 @@ pub fn get_git_modified_files(
216228 git_dir : Option < & Path > ,
217229 extensions : & [ & str ] ,
218230) -> Result < Vec < String > , String > {
219- let merge_base = get_closest_upstream_commit ( git_dir, config, CiEnv :: None ) ?;
231+ let Some ( merge_base) = get_closest_upstream_commit ( git_dir, config, CiEnv :: None ) ? else {
232+ return Err ( "No upstream commit was found" . to_string ( ) ) ;
233+ } ;
220234
221235 let mut git = Command :: new ( "git" ) ;
222236 if let Some ( git_dir) = git_dir {
0 commit comments