33# This file is part of cf-units and is released under the BSD license.
44# See LICENSE in the root of the repository for full licensing details.
55
6- from datetime import datetime
76from fnmatch import fnmatch
87from pathlib import Path
98import subprocess
2928
3029@pytest .mark .skipif (not IS_GIT_REPO , reason = "Not a git repository." )
3130class TestLicenseHeaders :
32- @staticmethod
33- def whatchanged_parse (whatchanged_output ):
34- """Returns a generator of tuples of data parsed from
35- "git whatchanged --pretty='TIME:%at'". The tuples are of the form
36- ``(filename, last_commit_datetime)``
37-
38- Sample input::
39-
40- ['TIME:1366884020', '',
41- ':000000 100644 0000000... 5862ced... A\t cf_units/cf_units.py']
42-
43- """
44- dt = None
45- for line in whatchanged_output :
46- if not line .strip ():
47- continue
48- elif line .startswith ("TIME:" ):
49- dt = datetime .fromtimestamp (int (line [5 :]))
50- else :
51- # Non blank, non date, line -> must be the lines
52- # containing the file info.
53- fname = " " .join (line .split ("\t " )[1 :])
54- yield fname , dt
55-
56- @staticmethod
57- def last_change_by_fname ():
58- """Return a dictionary of all the files under git which maps to
59- the datetime of their last modification in the git history.
60-
61- .. note::
62-
63- This function raises a ValueError if the repo root does
64- not have a ".git" folder. If git is not installed on the system,
65- or cannot be found by subprocess, an IOError may also be raised.
66-
67- """
68- # Call "git whatchanged" to get the details of all the files and when
69- # they were last changed.
31+ class NongitError (ValueError ):
32+ pass
33+
34+ @classmethod
35+ def all_git_filepaths (cls ):
36+ """Return a list of all the files under git."""
7037 output = subprocess .check_output (
71- ["git" , "whatchanged" , "--pretty=TIME:%ct" ], cwd = REPO_DIR
38+ ["git" , "ls-files" ],
39+ cwd = REPO_DIR ,
7240 )
73- output = str (output .decode ("ascii" ))
74- output = output .split ("\n " )
75- res = {}
76- for fname , dt in TestLicenseHeaders .whatchanged_parse (output ):
77- if fname not in res or dt > res [fname ]:
78- res [fname ] = dt
7941
80- return res
42+ # Result has one file-path per line.
43+ output_lines = output .decode ("ascii" ).split ("\n " )
44+ # Strip off any leading+trailing whitespace.
45+ output_lines = [line .strip () for line in output_lines ]
46+ # Ignore blank lines.
47+ output_lines = [line for line in output_lines if len (line ) > 0 ]
48+ return output_lines
8149
8250 def test_license_headers (self ):
8351 exclude_patterns = (
@@ -89,10 +57,10 @@ def test_license_headers(self):
8957 "cf_units/_udunits2_parser/_antlr4_runtime/*" ,
9058 )
9159
92- last_change_by_fname = self .last_change_by_fname ()
60+ file_paths = self .all_git_filepaths ()
9361
9462 failed = False
95- for fname in sorted (last_change_by_fname ):
63+ for fname in sorted (file_paths ):
9664 full_fname = REPO_DIR / fname
9765 if (
9866 full_fname .suffix == ".py"
0 commit comments