@@ -48,15 +48,22 @@ def applies_to_file(filename):
4848# obtain list of files in repo according to INCLUDE and EXCLUDE
4949################################################################################
5050
51- GIT_LS_CMD = 'git ls-files'
51+ GIT_LS_CMD = 'git ls-files --full-name' .split (' ' )
52+ GIT_TOPLEVEL_CMD = 'git rev-parse --show-toplevel' .split (' ' )
5253
53- def call_git_ls ():
54- out = subprocess .check_output (GIT_LS_CMD . split ( ' ' ) )
54+ def call_git_ls (base_directory ):
55+ out = subprocess .check_output ([ * GIT_LS_CMD , base_directory ] )
5556 return [f for f in out .decode ("utf-8" ).split ('\n ' ) if f != '' ]
5657
57- def get_filenames_to_examine ():
58- filenames = call_git_ls ()
59- return sorted ([filename for filename in filenames if
58+ def call_git_toplevel ():
59+ "Returns the absolute path to the project root"
60+ return subprocess .check_output (GIT_TOPLEVEL_CMD ).strip ().decode ("utf-8" )
61+
62+ def get_filenames_to_examine (base_directory ):
63+ "Returns an array of absolute paths to any project files in the base_directory that pass the include/exclude filters"
64+ root = call_git_toplevel ()
65+ filenames = call_git_ls (base_directory )
66+ return sorted ([os .path .join (root , filename ) for filename in filenames if
6067 applies_to_file (filename )])
6168
6269################################################################################
@@ -146,7 +153,7 @@ def file_has_without_c_style_copyright_for_holder(contents, holder_name):
146153################################################################################
147154
148155def read_file (filename ):
149- return open (os . path . abspath ( filename ) , 'r' , encoding = "utf8" ).read ()
156+ return open (filename , 'r' , encoding = "utf8" ).read ()
150157
151158def gather_file_info (filename ):
152159 info = {}
@@ -260,12 +267,9 @@ def print_report(file_infos, verbose):
260267 print (SEPARATOR )
261268
262269def exec_report (base_directory , verbose ):
263- original_cwd = os .getcwd ()
264- os .chdir (base_directory )
265- filenames = get_filenames_to_examine ()
270+ filenames = get_filenames_to_examine (base_directory )
266271 file_infos = [gather_file_info (f ) for f in filenames ]
267272 print_report (file_infos , verbose )
268- os .chdir (original_cwd )
269273
270274################################################################################
271275# report cmd
@@ -325,13 +329,13 @@ def get_most_recent_git_change_year(filename):
325329################################################################################
326330
327331def read_file_lines (filename ):
328- f = open (os . path . abspath ( filename ) , 'r' , encoding = "utf8" )
332+ f = open (filename , 'r' , encoding = "utf8" )
329333 file_lines = f .readlines ()
330334 f .close ()
331335 return file_lines
332336
333337def write_file_lines (filename , file_lines ):
334- f = open (os . path . abspath ( filename ) , 'w' , encoding = "utf8" )
338+ f = open (filename , 'w' , encoding = "utf8" )
335339 f .write ('' .join (file_lines ))
336340 f .close ()
337341
@@ -399,11 +403,8 @@ def update_updatable_copyright(filename):
399403 "Copyright updated! -> %s" % last_git_change_year )
400404
401405def exec_update_header_year (base_directory ):
402- original_cwd = os .getcwd ()
403- os .chdir (base_directory )
404- for filename in get_filenames_to_examine ():
406+ for filename in get_filenames_to_examine (base_directory ):
405407 update_updatable_copyright (filename )
406- os .chdir (original_cwd )
407408
408409################################################################################
409410# update cmd
0 commit comments