summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Dufresne <nicolas.dufresne@collabora.com>2020-01-18 15:00:14 -0500
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-01-18 22:40:55 +0200
commit17b3c794095eb6f73ef83061bd49aeef5ad11507 (patch)
tree53df0c5b2a7b8a88d5db80854fafd57d8aec50ff
parent8bf84425652df7972085f3e1aaf43c9f84da9bd8 (diff)
checkstyle: Add support for checking style on amendments
This introduces a new argument "--amend" and a new special type of commit "Amendment". It will check the style of changes that are in the index combined with the changes of the last commit. So this is the changes that would be applied by "git commit --amend" hence the name of the argument. This is needed to implement pre-commit hook. Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rwxr-xr-xutils/checkstyle.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index e7c3ae27..5ac14508 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -496,6 +496,26 @@ class StagedChanges(Commit):
stdout=subprocess.PIPE).stdout.decode('utf-8')
+class Amendment(StagedChanges):
+ def __init__(self):
+ StagedChanges.__init__(self)
+
+ def get_info(self):
+ # Create a title using HEAD commit
+ ret = subprocess.run(['git', 'show', '--pretty=oneline', '--no-patch'],
+ stdout=subprocess.PIPE).stdout.decode('utf-8')
+ title = 'Amendment of ' + ret.strip()
+ # Extract the list of modified files
+ ret = subprocess.run(['git', 'diff', '--staged', '--name-only', 'HEAD~'],
+ stdout=subprocess.PIPE).stdout.decode('utf-8')
+ return title, ret.splitlines()
+
+ def get_diff(self, top_level, filename):
+ return subprocess.run(['git', 'diff', '--staged', 'HEAD~', '--',
+ '%s/%s' % (top_level, filename)],
+ stdout=subprocess.PIPE).stdout.decode('utf-8')
+
+
def check_file(top_level, commit, filename):
# Extract the line numbers touched by the commit.
diff = commit.get_diff(top_level, filename)
@@ -628,6 +648,8 @@ def main(argv):
help='Code formatter. Default to clang-format if not specified.')
parser.add_argument('--staged', '-s', action='store_true',
help='Include the changes in the index. Defaults to False')
+ parser.add_argument('--amend', '-a', action='store_true',
+ help='Include changes in the index and the previous patch combined. Defaults to False')
parser.add_argument('revision_range', type=str, default=None, nargs='?',
help='Revision range (as defined by git rev-parse). Defaults to HEAD if not specified.')
args = parser.parse_args(argv[1:])
@@ -666,8 +688,10 @@ def main(argv):
commits = []
if args.staged:
commits.append(StagedChanges())
+ if args.amend:
+ commits.append(Amendment())
- # If not --staged
+ # If none of --staged or --amend was passed
if len(commits) == 0:
# And no revisions were passed, then default to HEAD
if not args.revision_range: