From patchwork Thu Dec 24 12:28:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10741 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B6FC9C0F1A for ; Thu, 24 Dec 2020 12:29:14 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 740E361FF6; Thu, 24 Dec 2020 13:29:14 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="V3SzuRIV"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9E56F61FF3 for ; Thu, 24 Dec 2020 13:29:09 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 485B1DFC for ; Thu, 24 Dec 2020 13:29:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1608812949; bh=uKANu3/kKLScJOKlHXIhE61UJF0R/3MmN5xzJ/AGvNE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=V3SzuRIVHIgOZbx6iHyyYoJ/q0Ps8/1QSCmF3Zvk+juNKk7/QQRASYnSlaC2SvC5S F6G/omlRrCDTbd6EptmGfHcVa3XXOvgPlEHiOoba1hVe2MHMDnHXot0vkKVFL88Y2M AVtBjEXq+cPAyDngXciOC21sJHuuREJch9Vo2Wn4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 24 Dec 2020 14:28:52 +0200 Message-Id: <20201224122855.22200-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201224122855.22200-1-laurent.pinchart@ideasonboard.com> References: <20201224122855.22200-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 5/8] utils: checkstyle.py: Add ability to filter files by status in a commit 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" A commit can perform different operations on a file. Record the file status (added, modified, renamed, deleted, ...) and add the ability to filter files by status when listing the files touched by a commit. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- utils/checkstyle.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index 77e635dc5154..07e896d9819f 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -194,6 +194,26 @@ def parse_diff(diff): # Commit, Staged Changes & Amendments # +class CommitFile: + def __init__(self, name): + info = name.split() + self.__status = info[0][0] + + # For renamed files, store the new name + if self.__status == 'R': + self.__filename = info[2] + else: + self.__filename = info[1] + + @property + def filename(self): + return self.__filename + + @property + def status(self): + return self.__status + + class Commit: def __init__(self, commit): self.commit = commit @@ -201,16 +221,15 @@ class Commit: def __parse(self): # Get the commit title and list of files. - ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-only', + ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-status', self.commit], stdout=subprocess.PIPE).stdout.decode('utf-8') files = ret.splitlines() - self.__files = files[1:] + self.__files = [CommitFile(f) for f in files[1:]] self.__title = files[0] - @property - def files(self): - return self.__files + def files(self, filter='AM'): + return [f.filename for f in self.__files if f.status in filter] @property def title(self): @@ -231,10 +250,10 @@ class StagedChanges(Commit): Commit.__init__(self, '') def __parse(self): - ret = subprocess.run(['git', 'diff', '--staged', '--name-only'], + ret = subprocess.run(['git', 'diff', '--staged', '--name-status'], stdout=subprocess.PIPE).stdout.decode('utf-8') self.__title = "Staged changes" - self.__files = ret.splitlines() + self.__files = [CommitFile(f) for f in ret.splitlines()] def get_diff(self, top_level, filename): return subprocess.run(['git', 'diff', '--staged', '--', @@ -252,9 +271,9 @@ class Amendment(StagedChanges): stdout=subprocess.PIPE).stdout.decode('utf-8') self.__title = 'Amendment of ' + ret.strip() # Extract the list of modified files - ret = subprocess.run(['git', 'diff', '--staged', '--name-only', 'HEAD~'], + ret = subprocess.run(['git', 'diff', '--staged', '--name-status', 'HEAD~'], stdout=subprocess.PIPE).stdout.decode('utf-8') - self.__files = ret.splitlines() + self.__files = [CommitFile(f) for f in ret.splitlines()] def get_diff(self, top_level, filename): return subprocess.run(['git', 'diff', '--staged', 'HEAD~', '--', @@ -705,7 +724,7 @@ def check_style(top_level, commit): patterns = set() patterns.update(StyleChecker.all_patterns()) patterns.update(Formatter.all_patterns()) - files = [f for f in commit.files if len([p for p in patterns if fnmatch.fnmatch(os.path.basename(f), p)])] + files = [f for f in commit.files() if len([p for p in patterns if fnmatch.fnmatch(os.path.basename(f), p)])] if len(files) == 0: print("Commit doesn't touch source files, skipping") return 0