@@ -42,6 +42,9 @@ pub enum PathFreshness {
42
42
/// `upstream` is the latest upstream merge commit that made modifications to the
43
43
/// set of paths.
44
44
HasLocalModifications { upstream : String } ,
45
+ /// No upstream commit was found.
46
+ /// This should not happen in most reasonable circumstances, but one never knows.
47
+ MissingUpstream ,
45
48
}
46
49
47
50
/// This function figures out if a set of paths was last modified upstream or
@@ -102,21 +105,29 @@ pub fn check_path_modifications(
102
105
// artifacts.
103
106
104
107
// 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
+ )
106
113
} 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 (
110
119
git_dir,
111
120
target_paths,
112
121
config. git_merge_commit_email ,
113
122
) ?;
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 ) ;
120
131
} ;
121
132
122
133
// For local environments, we want to find out if something has changed
@@ -178,7 +189,7 @@ fn get_closest_upstream_commit(
178
189
git_dir : Option < & Path > ,
179
190
config : & GitConfig < ' _ > ,
180
191
env : CiEnv ,
181
- ) -> Result < String , String > {
192
+ ) -> Result < Option < String > , String > {
182
193
let mut git = Command :: new ( "git" ) ;
183
194
184
195
if let Some ( git_dir) = git_dir {
@@ -189,7 +200,7 @@ fn get_closest_upstream_commit(
189
200
CiEnv :: None => "HEAD" ,
190
201
CiEnv :: GitHubActions => {
191
202
// 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
193
204
// `config.git_merge_commit_email`, it should not be upstream.
194
205
"HEAD^1"
195
206
}
@@ -202,7 +213,8 @@ fn get_closest_upstream_commit(
202
213
& base,
203
214
] ) ;
204
215
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) ) }
206
218
}
207
219
208
220
/// 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(
216
228
git_dir : Option < & Path > ,
217
229
extensions : & [ & str ] ,
218
230
) -> 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
+ } ;
220
234
221
235
let mut git = Command :: new ( "git" ) ;
222
236
if let Some ( git_dir) = git_dir {
0 commit comments