[libcamera-devel,5/8] utils: checkstyle.py: Add ability to filter files by status in a commit
diff mbox series

Message ID 20201224122855.22200-6-laurent.pinchart@ideasonboard.com
State Accepted
Delegated to: Laurent Pinchart
Headers show
Series
  • checkstyle.py: Ensure meson.build is updated when adding header
Related show

Commit Message

Laurent Pinchart Dec. 24, 2020, 12:28 p.m. UTC
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 <laurent.pinchart@ideasonboard.com>
---
 utils/checkstyle.py | 39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)

Comments

Niklas Söderlund Dec. 27, 2020, 10:37 a.m. UTC | #1
Hi Laurent,

Thanks for your work.

On 2020-12-24 14:28:52 +0200, Laurent Pinchart wrote:
> 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 <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  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
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch
diff mbox series

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