utils: checkstyle.py: Make title and files properties of commit class

Make the API of the Commit class more explicit by exposing the title and
files as properties instead of through a get_info() method.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2020-12-24 12:34:55 +02:00
parent 097720840a
commit 4f5d17f3a4

View file

@ -197,15 +197,24 @@ def parse_diff(diff):
class Commit: class Commit:
def __init__(self, commit): def __init__(self, commit):
self.commit = commit self.commit = commit
self.__parse()
def get_info(self): def __parse(self):
# Get the commit title and list of files. # Get the commit title and list of files.
ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-only', ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-only',
self.commit], self.commit],
stdout=subprocess.PIPE).stdout.decode('utf-8') stdout=subprocess.PIPE).stdout.decode('utf-8')
files = ret.splitlines() files = ret.splitlines()
# Returning title and files list as a tuple self.__files = files[1:]
return files[0], files[1:] self.__title = files[0]
@property
def files(self):
return self.__files
@property
def title(self):
return self.__title
def get_diff(self, top_level, filename): def get_diff(self, top_level, filename):
return subprocess.run(['git', 'diff', '%s~..%s' % (self.commit, self.commit), return subprocess.run(['git', 'diff', '%s~..%s' % (self.commit, self.commit),
@ -221,10 +230,11 @@ class StagedChanges(Commit):
def __init__(self): def __init__(self):
Commit.__init__(self, '') Commit.__init__(self, '')
def get_info(self): def __parse(self):
ret = subprocess.run(['git', 'diff', '--staged', '--name-only'], ret = subprocess.run(['git', 'diff', '--staged', '--name-only'],
stdout=subprocess.PIPE).stdout.decode('utf-8') stdout=subprocess.PIPE).stdout.decode('utf-8')
return "Staged changes", ret.splitlines() self.__title = "Staged changes"
self.__files = ret.splitlines()
def get_diff(self, top_level, filename): def get_diff(self, top_level, filename):
return subprocess.run(['git', 'diff', '--staged', '--', return subprocess.run(['git', 'diff', '--staged', '--',
@ -236,15 +246,15 @@ class Amendment(StagedChanges):
def __init__(self): def __init__(self):
StagedChanges.__init__(self) StagedChanges.__init__(self)
def get_info(self): def __parse(self):
# Create a title using HEAD commit # Create a title using HEAD commit
ret = subprocess.run(['git', 'show', '--pretty=oneline', '--no-patch'], ret = subprocess.run(['git', 'show', '--pretty=oneline', '--no-patch'],
stdout=subprocess.PIPE).stdout.decode('utf-8') stdout=subprocess.PIPE).stdout.decode('utf-8')
title = 'Amendment of ' + ret.strip() self.__title = 'Amendment of ' + ret.strip()
# Extract the list of modified files # Extract the list of modified files
ret = subprocess.run(['git', 'diff', '--staged', '--name-only', 'HEAD~'], ret = subprocess.run(['git', 'diff', '--staged', '--name-only', 'HEAD~'],
stdout=subprocess.PIPE).stdout.decode('utf-8') stdout=subprocess.PIPE).stdout.decode('utf-8')
return title, ret.splitlines() self.__files = ret.splitlines()
def get_diff(self, top_level, filename): def get_diff(self, top_level, filename):
return subprocess.run(['git', 'diff', '--staged', 'HEAD~', '--', return subprocess.run(['git', 'diff', '--staged', 'HEAD~', '--',
@ -686,18 +696,16 @@ def check_file(top_level, commit, filename):
def check_style(top_level, commit): def check_style(top_level, commit):
title, files = commit.get_info() separator = '-' * len(commit.title)
separator = '-' * len(title)
print(separator) print(separator)
print(title) print(commit.title)
print(separator) print(separator)
# Filter out files we have no checker for. # Filter out files we have no checker for.
patterns = set() patterns = set()
patterns.update(StyleChecker.all_patterns()) patterns.update(StyleChecker.all_patterns())
patterns.update(Formatter.all_patterns()) patterns.update(Formatter.all_patterns())
files = [f for f in files if len([p for p in patterns if fnmatch.fnmatch(os.path.basename(f), p)])] files = [f for f in commit.files if len([p for p in patterns if fnmatch.fnmatch(os.path.basename(f), p)])]
if len(files) == 0: if len(files) == 0:
print("Commit doesn't touch source files, skipping") print("Commit doesn't touch source files, skipping")
return 0 return 0