Skip to content

Commit

Permalink
utils: checkstyle.py: Extract title and trailers with one command
Browse files Browse the repository at this point in the history
The Amendment class calls `git show` twice, once to extract the commit
title, and a second time to extract the trailers. This can be combined
in a single command, which is more efficient. Do so.

While at it, centralize initialization of self._trailers in the
Commit.__init__() function.

Signed-off-by: Laurent Pinchart <[email protected]>
Reviewed-by: Kieran Bingham <[email protected]>
  • Loading branch information
pinchartl committed Jul 28, 2023
1 parent 5322b7b commit 4694e44
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions utils/checkstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ def status(self):
class Commit:
def __init__(self, commit):
self.commit = commit
self._trailers = []
self._parse()

def _parse_trailers(self, lines):
self._trailers = []
for index in range(1, len(lines)):
line = lines[index]
if not line:
Expand Down Expand Up @@ -257,9 +257,6 @@ class StagedChanges(Commit):
def __init__(self):
Commit.__init__(self, '')

# There are no trailers to parse on a Staged Change.
self._trailers = []

def _parse(self):
ret = subprocess.run(['git', 'diff', '--staged', '--name-status'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
Expand All @@ -278,21 +275,21 @@ def __init__(self):
Commit.__init__(self, '')

def _parse(self):
# Create a title using HEAD commit
ret = subprocess.run(['git', 'show', '--pretty=oneline', '--no-patch'],
# Create a title using HEAD commit and parse the trailers.
ret = subprocess.run(['git', 'show', '--format=%H %s%n%(trailers:only,unfold)',
'--no-patch'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
self._title = 'Amendment of ' + ret.strip()
lines = ret.splitlines()

self._title = 'Amendment of ' + lines[0].strip()

self._parse_trailers(lines)

# Extract the list of modified files
ret = subprocess.run(['git', 'diff', '--staged', '--name-status', 'HEAD~'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
self._files = [CommitFile(f) for f in ret.splitlines()]

# Parse trailers from the existing commit only.
ret = subprocess.run(['git', 'show', '--format=%n%(trailers:only,unfold)',
'--no-patch'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
self._parse_trailers(ret.splitlines())

def get_diff(self, top_level, filename):
diff = subprocess.run(['git', 'diff', '--staged', 'HEAD~', '--',
'%s/%s' % (top_level, filename)],
Expand Down

0 comments on commit 4694e44

Please sign in to comment.