From patchwork Fri Jan 17 19:17:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Dufresne X-Patchwork-Id: 2664 Return-Path: Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A10A260783 for ; Fri, 17 Jan 2020 20:17:59 +0100 (CET) Received: from nicolas-tpx395.localdomain (unknown [IPv6:2610:98:8005::127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: nicolas) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 2CBEA2949D5; Fri, 17 Jan 2020 19:17:59 +0000 (GMT) From: Nicolas Dufresne To: libcamera-devel@lists.libcamera.org Cc: Nicolas Dufresne Date: Fri, 17 Jan 2020 14:17:31 -0500 Message-Id: <20200117191733.198897-5-nicolas@ndufresne.ca> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200117191733.198897-1-nicolas@ndufresne.ca> References: <20200117191733.198897-1-nicolas@ndufresne.ca> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/6] checkstyle: Add support for checking style on indexed changes X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jan 2020 19:18:00 -0000 From: Nicolas Dufresne This introduce a new command line "--staged" and a new special type of commit "Index". It will check the style of changes that are in the index, so the changes that would be committed by "git commit". "--staged" was chosen to match with "git diff --staged" command line. Other valid name could have been "--index" or "--cached". This was my personal preference, alias can be added later. Note that we must not confuse this with working tree changes, as these changes are not picked by "git commit". This feature is needed to implement pre-commit hook. Signed-off-by: Nicolas Dufresne --- utils/checkstyle.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index fb865c8..8e456cd 100644 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -482,6 +482,25 @@ class Commit: stdout=subprocess.PIPE).stdout.decode('utf-8') +class Index(Commit): + def __init__(self): + Commit.__init__(self, None) + + def get_info(self, top_level): + ret = subprocess.run(['git', 'diff', '--staged', '--name-only'], + stdout=subprocess.PIPE).stdout.decode('utf-8') + return "Staged changes", ret.splitlines() + + def get_diff(self, top_level, filename): + return subprocess.run(['git', 'diff', '--staged', '--', + '%s/%s' % (top_level, filename)], + stdout=subprocess.PIPE).stdout.decode('utf-8') + + def get_file(self, filename): + return subprocess.run(['git', 'show', ':%s' % (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) @@ -612,7 +631,9 @@ def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--formatter', '-f', type=str, choices=['astyle', 'clang-format'], help='Code formatter. Default to clang-format if not specified.') - parser.add_argument('revision_range', type=str, default='HEAD', nargs='?', + parser.add_argument('--staged', '-s', action='store_true', + help='Include the changes in the index. 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:]) @@ -647,7 +668,16 @@ def main(argv): if top_level is None: return 1 - revlist = extract_revlist(args.revision_range) + revlist = [] + if args.staged: + revlist.append(Index()) + + # If nothing of --staged or --amend was passed, defaults to HEAD + if len(revlist) == 0 and not args.revision_range: + args.revision_range = 'HEAD' + + if args.revision_range: + revlist += extract_revlist(args.revision_range) issues = 0 for commit in revlist: