@@ -170,26 +170,59 @@ pub fn get_git_untracked_files(
170
170
pub fn warn_old_master_branch (
171
171
config : & GitConfig < ' _ > ,
172
172
git_dir : & Path ,
173
- ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
174
- use std :: time :: Duration ;
175
- const WARN_AFTER : Duration = Duration :: from_secs ( 60 * 60 * 24 * 10 ) ;
176
- let updated_master = updated_master_branch ( config , Some ( git_dir ) ) ? ;
177
- let branch_path = git_dir . join ( ".git/refs/remotes" ) . join ( & updated_master ) ;
178
- match std :: fs :: metadata ( branch_path ) {
179
- Ok ( meta ) => {
180
- if meta . modified ( ) ? . elapsed ( ) ? > WARN_AFTER {
181
- eprintln ! ( "warning: {updated_master} has not been updated in 10 days" ) ;
182
- } else {
183
- return Ok ( ( ) ) ;
184
- }
173
+ ) {
174
+ if crate :: ci :: CiEnv :: is_ci ( ) {
175
+ // this warning is useless in CI,
176
+ // and CI probably won't have the right branches anyway.
177
+ return ;
178
+ }
179
+ // this will be overwritten by the actual name, if possible
180
+ let mut updated_master = "the upstream master branch" . to_string ( ) ;
181
+ match warn_old_master_branch_ ( config , git_dir , & mut updated_master ) {
182
+ Ok ( branch_is_old ) => {
183
+ if !branch_is_old { return }
184
+ // otherwise fall through and print the rest of the warning
185
185
}
186
186
Err ( err) => {
187
187
eprintln ! ( "warning: unable to check if {updated_master} is old due to error: {err}" )
188
188
}
189
189
}
190
190
eprintln ! (
191
191
"warning: {updated_master} is used to determine if files have been modified\n \
192
- warning: if it is not updated, this may cause files to be needlessly reformatted"
192
+ warning: if it is not updated, this may cause files to be needlessly reformatted"
193
193
) ;
194
- Ok ( ( ) )
194
+ }
195
+
196
+ pub fn warn_old_master_branch_ (
197
+ config : & GitConfig < ' _ > ,
198
+ git_dir : & Path ,
199
+ updated_master : & mut String ,
200
+ ) -> Result < bool , Box < dyn std:: error:: Error > > {
201
+ use std:: time:: Duration ;
202
+ * updated_master = updated_master_branch ( config, Some ( git_dir) ) ?;
203
+ let branch_path = git_dir. join ( ".git/refs/remotes" ) . join ( & updated_master) ;
204
+ const WARN_AFTER : Duration = Duration :: from_secs ( 60 * 60 * 24 * 10 ) ;
205
+ let meta = match std:: fs:: metadata ( & branch_path) {
206
+ Ok ( meta) => meta,
207
+ Err ( err) => {
208
+ let gcd = git_common_dir ( & git_dir) ?;
209
+ if branch_path. starts_with ( & gcd) {
210
+ return Err ( Box :: new ( err) ) ;
211
+ }
212
+ std:: fs:: metadata ( Path :: new ( & gcd) . join ( "refs/remotes" ) . join ( & updated_master) ) ?
213
+ } ,
214
+ } ;
215
+ if meta. modified ( ) ?. elapsed ( ) ? > WARN_AFTER {
216
+ eprintln ! ( "warning: {updated_master} has not been updated in 10 days" ) ;
217
+ Ok ( true )
218
+ } else {
219
+ Ok ( false )
220
+ }
221
+ }
222
+
223
+ fn git_common_dir ( dir : & Path ) -> Result < String , String > {
224
+ output_result ( Command :: new ( "git" )
225
+ . arg ( "-C" ) . arg ( dir)
226
+ . arg ( "rev-parse" ) . arg ( "--git-common-dir" ) )
227
+ . map ( |x| x. trim ( ) . to_string ( ) )
195
228
}
0 commit comments