@@ -167,29 +167,59 @@ pub fn get_git_untracked_files(
167167///
168168/// This can result in formatting thousands of files instead of a dozen,
169169/// so we should warn the user something is wrong.
170- pub fn warn_old_master_branch (
171- config : & GitConfig < ' _ > ,
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 ( ( ) ) ;
170+ pub fn warn_old_master_branch ( config : & GitConfig < ' _ > , git_dir : & Path ) {
171+ if crate :: ci:: CiEnv :: is_ci ( ) {
172+ // this warning is useless in CI,
173+ // and CI probably won't have the right branches anyway.
174+ return ;
175+ }
176+ // this will be overwritten by the actual name, if possible
177+ let mut updated_master = "the upstream master branch" . to_string ( ) ;
178+ match warn_old_master_branch_ ( config, git_dir, & mut updated_master) {
179+ Ok ( branch_is_old) => {
180+ if !branch_is_old {
181+ return ;
184182 }
183+ // otherwise fall through and print the rest of the warning
185184 }
186185 Err ( err) => {
187186 eprintln ! ( "warning: unable to check if {updated_master} is old due to error: {err}" )
188187 }
189188 }
190189 eprintln ! (
191190 "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"
191+ warning: if it is not updated, this may cause files to be needlessly reformatted"
193192 ) ;
194- Ok ( ( ) )
193+ }
194+
195+ pub fn warn_old_master_branch_ (
196+ config : & GitConfig < ' _ > ,
197+ git_dir : & Path ,
198+ updated_master : & mut String ,
199+ ) -> Result < bool , Box < dyn std:: error:: Error > > {
200+ use std:: time:: Duration ;
201+ * updated_master = updated_master_branch ( config, Some ( git_dir) ) ?;
202+ let branch_path = git_dir. join ( ".git/refs/remotes" ) . join ( & updated_master) ;
203+ const WARN_AFTER : Duration = Duration :: from_secs ( 60 * 60 * 24 * 10 ) ;
204+ let meta = match std:: fs:: metadata ( & branch_path) {
205+ Ok ( meta) => meta,
206+ Err ( err) => {
207+ let gcd = git_common_dir ( & git_dir) ?;
208+ if branch_path. starts_with ( & gcd) {
209+ return Err ( Box :: new ( err) ) ;
210+ }
211+ std:: fs:: metadata ( Path :: new ( & gcd) . join ( "refs/remotes" ) . join ( & updated_master) ) ?
212+ }
213+ } ;
214+ if meta. modified ( ) ?. elapsed ( ) ? > WARN_AFTER {
215+ eprintln ! ( "warning: {updated_master} has not been updated in 10 days" ) ;
216+ Ok ( true )
217+ } else {
218+ Ok ( false )
219+ }
220+ }
221+
222+ fn git_common_dir ( dir : & Path ) -> Result < String , String > {
223+ output_result ( Command :: new ( "git" ) . arg ( "-C" ) . arg ( dir) . arg ( "rev-parse" ) . arg ( "--git-common-dir" ) )
224+ . map ( |x| x. trim ( ) . to_string ( ) )
195225}
0 commit comments