From patchwork Fri Jan 17 19:17:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nicolas Dufresne X-Patchwork-Id: 2663 Return-Path: Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8B9FA60787 for ; Fri, 17 Jan 2020 20:17:58 +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 15DC52949D4; Fri, 17 Jan 2020 19:17:58 +0000 (GMT) From: Nicolas Dufresne To: libcamera-devel@lists.libcamera.org Cc: Nicolas Dufresne Date: Fri, 17 Jan 2020 14:17:30 -0500 Message-Id: <20200117191733.198897-4-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 3/6] checkstyle: Introduce a Commit class 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:17:58 -0000 From: Nicolas Dufresne This introduce a Commit class used in the final revlist list. All the git command are moved into that class. This class will be used to introduce new type of commit (index and amendment) needed to implement pre-commit hook support. Signed-off-by: Nicolas Dufresne Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- utils/checkstyle.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index e7375b3..fb865c8 100644 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -458,12 +458,34 @@ class StripTrailingSpaceFormatter(Formatter): # Style checking # +class Commit: + commit = None + + def __init__(self, commit): + self.commit = commit + + def get_info(self, top_level): + # Get the commit title and list of files. + ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-only', + self.commit], + stdout=subprocess.PIPE).stdout.decode('utf-8') + files = ret.splitlines() + return files[0], files[1:] + + def get_diff(self, top_level, filename): + return subprocess.run(['git', 'diff', '%s~..%s' % (self.commit, self.commit), + '--', '%s/%s' % (top_level, filename)], + stdout=subprocess.PIPE).stdout.decode('utf-8') + + def get_file(self, filename): + return subprocess.run(['git', 'show', '%s:%s' % (self.commit, filename)], + stdout=subprocess.PIPE).stdout.decode('utf-8') + + def check_file(top_level, commit, filename): # Extract the line numbers touched by the commit. - 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) + diff = commit.get_diff(top_level, filename) + diff = diff.splitlines(True) commit_diff = parse_diff(diff) lines = [] @@ -476,9 +498,7 @@ def check_file(top_level, commit, filename): # Format the file after the commit with all formatters and compute the diff # between the unformatted and formatted contents. - after = subprocess.run(['git', 'show', '%s:%s' % (commit, filename)], - stdout=subprocess.PIPE).stdout - after = after.decode('utf-8') + after = commit.get_file(filename) formatted = after for formatter in Formatter.formatters(filename): @@ -522,12 +542,7 @@ def check_file(top_level, commit, filename): 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) - files = ret.stdout.decode('utf-8').splitlines() - title = files[0] - files = files[1:] + title, files = commit.get_info(top_level) separator = '-' * len(title) print(separator) @@ -576,7 +591,7 @@ def extract_revlist(revs): revlist = ret.stdout.decode('utf-8').splitlines() revlist.reverse() - return revlist + return [Commit(x) for x in revlist] def git_top_level():