@@ -113,6 +113,9 @@ pub enum PathFreshness {
113
113
/// `upstream` is the latest upstream merge commit that made modifications to the
114
114
/// set of paths.
115
115
HasLocalModifications { upstream : String } ,
116
+ /// No upstream commit was found.
117
+ /// This should not happen in most reasonable circumstances, but one never knows.
118
+ MissingUpstream ,
116
119
}
117
120
118
121
/// This function figures out if a set of paths was last modified upstream or
@@ -177,21 +180,29 @@ pub fn check_path_modifications(
177
180
// artifacts.
178
181
179
182
// Do not include HEAD, as it is never an upstream commit
180
- get_closest_upstream_commit ( git_dir, config, ci_env) ?
183
+ // If we do not find an upstream commit in CI, something is seriously wrong.
184
+ Some (
185
+ get_closest_upstream_commit ( git_dir, config, ci_env) ?
186
+ . expect ( "No upstream commit was found on CI" ) ,
187
+ )
181
188
} else {
182
- // Outside CI, we have to find the most recent upstream commit that
183
- // modified the set of paths, to have an upstream reference.
184
- let upstream_sha = get_latest_commit_that_modified_files (
189
+ // Outside CI, we want to find the most recent upstream commit that
190
+ // modified the set of paths, to have an upstream reference that does not change
191
+ // unnecessarily often.
192
+ // However, if such commit is not found, we can fall back to the latest upstream commit
193
+ let upstream_with_modifications = get_latest_commit_that_modified_files (
185
194
git_dir,
186
195
target_paths,
187
196
config. git_merge_commit_email ,
188
197
) ?;
189
- let Some ( upstream_sha) = upstream_sha else {
190
- eprintln ! ( "No upstream commit that modified paths {target_paths:?} found." ) ;
191
- eprintln ! ( "Try to fetch more upstream history." ) ;
192
- return Err ( "No upstream commit with modifications found" . to_string ( ) ) ;
193
- } ;
194
- upstream_sha
198
+ match upstream_with_modifications {
199
+ Some ( sha) => Some ( sha) ,
200
+ None => get_closest_upstream_commit ( git_dir, config, ci_env) ?,
201
+ }
202
+ } ;
203
+
204
+ let Some ( upstream_sha) = upstream_sha else {
205
+ return Ok ( PathFreshness :: MissingUpstream ) ;
195
206
} ;
196
207
197
208
// For local environments, we want to find out if something has changed
@@ -253,7 +264,7 @@ fn get_closest_upstream_commit(
253
264
git_dir : Option < & Path > ,
254
265
config : & GitConfig < ' _ > ,
255
266
env : CiEnv ,
256
- ) -> Result < String , String > {
267
+ ) -> Result < Option < String > , String > {
257
268
let mut git = Command :: new ( "git" ) ;
258
269
259
270
if let Some ( git_dir) = git_dir {
@@ -264,7 +275,7 @@ fn get_closest_upstream_commit(
264
275
CiEnv :: None => "HEAD" ,
265
276
CiEnv :: GitHubActions => {
266
277
// On CI, we always have a merge commit at the tip.
267
- // We thus skip it, because although it can be creatd by
278
+ // We thus skip it, because although it can be created by
268
279
// `config.git_merge_commit_email`, it should not be upstream.
269
280
"HEAD^1"
270
281
}
@@ -277,7 +288,8 @@ fn get_closest_upstream_commit(
277
288
& base,
278
289
] ) ;
279
290
280
- Ok ( output_result ( & mut git) ?. trim ( ) . to_owned ( ) )
291
+ let output = output_result ( & mut git) ?. trim ( ) . to_owned ( ) ;
292
+ if output. is_empty ( ) { Ok ( None ) } else { Ok ( Some ( output) ) }
281
293
}
282
294
283
295
/// Returns the files that have been modified in the current branch compared to the master branch.
@@ -291,7 +303,9 @@ pub fn get_git_modified_files(
291
303
git_dir : Option < & Path > ,
292
304
extensions : & [ & str ] ,
293
305
) -> Result < Vec < String > , String > {
294
- let merge_base = get_closest_upstream_commit ( git_dir, config, CiEnv :: None ) ?;
306
+ let Some ( merge_base) = get_closest_upstream_commit ( git_dir, config, CiEnv :: None ) ? else {
307
+ return Err ( "No upstream commit was found" . to_string ( ) ) ;
308
+ } ;
295
309
296
310
let mut git = Command :: new ( "git" ) ;
297
311
if let Some ( git_dir) = git_dir {
0 commit comments