@@ -48,15 +48,22 @@ def applies_to_file(filename):
48
48
# obtain list of files in repo according to INCLUDE and EXCLUDE
49
49
################################################################################
50
50
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 (' ' )
52
53
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 ] )
55
56
return [f for f in out .decode ("utf-8" ).split ('\n ' ) if f != '' ]
56
57
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
60
67
applies_to_file (filename )])
61
68
62
69
################################################################################
@@ -146,7 +153,7 @@ def file_has_without_c_style_copyright_for_holder(contents, holder_name):
146
153
################################################################################
147
154
148
155
def read_file (filename ):
149
- return open (os . path . abspath ( filename ) , 'r' , encoding = "utf8" ).read ()
156
+ return open (filename , 'r' , encoding = "utf8" ).read ()
150
157
151
158
def gather_file_info (filename ):
152
159
info = {}
@@ -260,12 +267,9 @@ def print_report(file_infos, verbose):
260
267
print (SEPARATOR )
261
268
262
269
def 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 )
266
271
file_infos = [gather_file_info (f ) for f in filenames ]
267
272
print_report (file_infos , verbose )
268
- os .chdir (original_cwd )
269
273
270
274
################################################################################
271
275
# report cmd
@@ -325,13 +329,13 @@ def get_most_recent_git_change_year(filename):
325
329
################################################################################
326
330
327
331
def read_file_lines (filename ):
328
- f = open (os . path . abspath ( filename ) , 'r' , encoding = "utf8" )
332
+ f = open (filename , 'r' , encoding = "utf8" )
329
333
file_lines = f .readlines ()
330
334
f .close ()
331
335
return file_lines
332
336
333
337
def write_file_lines (filename , file_lines ):
334
- f = open (os . path . abspath ( filename ) , 'w' , encoding = "utf8" )
338
+ f = open (filename , 'w' , encoding = "utf8" )
335
339
f .write ('' .join (file_lines ))
336
340
f .close ()
337
341
@@ -399,11 +403,8 @@ def update_updatable_copyright(filename):
399
403
"Copyright updated! -> %s" % last_git_change_year )
400
404
401
405
def 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 ):
405
407
update_updatable_copyright (filename )
406
- os .chdir (original_cwd )
407
408
408
409
################################################################################
409
410
# update cmd
0 commit comments