From patchwork Tue Dec 18 19:46:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 54 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E8A3260B0C for ; Tue, 18 Dec 2018 20:45:35 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 421B153A for ; Tue, 18 Dec 2018 20:45:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1545162335; bh=24BDaEBW8tjobi0Ae5suyHrpsYClv+HRjjuMTjQ7zYo=; h=From:To:Subject:Date:From; b=i+qTWZ7WKc2pjEPgpPBvb0euoaGPz1ntIak5D/BfBVHa34UEpzCYB6ZiHsq0jAluv drd88URE4+Xmk1sIRE+nsihaPCLpuDuVU1WooEi+fEWICLaLNpnuwlUQzzFCY4RyRu rSRjM+A+8E9Ux3mpt/CUdjR/hb/1pyGed1Dde1UE= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 18 Dec 2018 21:46:24 +0200 Message-Id: <20181218194624.21444-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] utils: checkstyle.py: Support execution from non-root directories X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Dec 2018 19:45:37 -0000 The git diff command is invoked with relative paths, which causes git to fail to locate files when the checkstyle.py script is run from subdirectories of the git tree. Fix this by prepending the absolute path to the git tree root directory to the file names. Signed-off-by: Laurent Pinchart --- utils/checkstyle.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index 6e07ffdc19e6..b7c4d876a145 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -144,10 +144,11 @@ def parse_diff(diff): return hunks -def check_file(commit, filename): +def check_file(top_level, commit, filename): # Extract the line numbers touched by the commit. - diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--', filename], - stdout=subprocess.PIPE).stdout + diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--', + '%s/%s' % (top_level, filename)], + stdout=subprocess.PIPE).stdout diff = diff.decode('utf-8').splitlines(True) commit_diff = parse_diff(diff) @@ -187,7 +188,7 @@ def check_file(commit, filename): return len(formatted_diff) -def check_style(commit): +def check_style(top_level, commit): # Get the commit title and list of files. ret = subprocess.run(['git', 'show', '--pretty=oneline','--name-only', commit], stdout=subprocess.PIPE) @@ -208,7 +209,7 @@ def check_style(commit): issues = 0 for f in files: - issues += check_file(commit, f) + issues += check_file(top_level, commit, f) if issues == 0: print("No style issue detected") @@ -240,6 +241,18 @@ def extract_revlist(revs): return revlist +def git_top_level(): + """Get the absolute path of the git top-level directory.""" + ret = subprocess.run(['git', 'rev-parse', '--show-toplevel'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + if ret.returncode != 0: + print(ret.stderr.decode('utf-8').splitlines()[0]) + return None + + return ret.stdout.decode('utf-8').strip() + + def main(argv): # Parse command line arguments @@ -256,12 +269,21 @@ def main(argv): print("Executable %s not found" % dependency) return 1 + # Get the top level directory to pass absolute file names to git diff + # commands, in order to support execution from subdirectories of the git + # tree. + top_level = git_top_level() + if top_level is None: + return 1 + revlist = extract_revlist(args.revision_range) for commit in revlist: - check_style(commit) + check_style(top_level, commit) print('') + return 0 + if __name__ == '__main__': sys.exit(main(sys.argv))