66import os
77from io import open
88
9- from git import Repo , InvalidGitRepositoryError
9+ from git import Repo , InvalidGitRepositoryError , Diffable
1010
1111# [M]odified, [A]dded, [C]opied, [T]ype changed, [R]enamed (R092 should be R according to
1212# https://gitpython.readthedocs.io/en/stable/reference.html#git.diff.DiffIndex, but testing it locally gave R092)
@@ -74,6 +74,9 @@ def filter_changed_files(changed_files, path_to_repository, file_encoding):
7474 filtered_files = []
7575 for changed_file in changed_files :
7676 file_is_valid = True
77+ if (os .path .isdir (os .path .join (path_to_repository , changed_file ))):
78+ #ignore directories (e.g., git submodule folders)
79+ continue
7780 if os .path .getsize (os .path .join (path_to_repository , changed_file )) > 1 * 1024 * 1024 :
7881 print ('File too large for precommit analysis. Ignoring: %s' % changed_file )
7982 file_is_valid = False
@@ -97,7 +100,7 @@ def filter_changed_files(changed_files, path_to_repository, file_encoding):
97100 return filtered_files
98101
99102
100- def get_changed_files_and_content (path_to_repository , file_encoding ):
103+ def get_changed_files_and_content (path_to_repository , file_encoding , ignore_subrepositories ):
101104 """Utility method for getting the currently changed files from a Git repository.
102105
103106 Filters the changed files using `filter_changed_files`.
@@ -108,13 +111,15 @@ def get_changed_files_and_content(path_to_repository, file_encoding):
108111
109112 Returns:
110113 dict: Mapping of filename to file content for all changed files in the provided repository.
114+ :param ignore_subrepositories:
111115 """
112- changed_files = filter_changed_files (get_changed_files (path_to_repository ), path_to_repository , file_encoding )
116+ changed_files = filter_changed_files (get_changed_files (path_to_repository , ignore_subrepositories ),
117+ path_to_repository , file_encoding )
113118 return {filename : open (os .path .join (path_to_repository , filename ), encoding = file_encoding ).read () for filename in
114119 changed_files }
115120
116121
117- def get_changed_files (path_to_repository ):
122+ def get_changed_files (path_to_repository , ignore_subrepositories ):
118123 """Utility method for getting the currently changed files from a Git repository.
119124
120125 Args:
@@ -123,11 +128,11 @@ def get_changed_files(path_to_repository):
123128 Returns:
124129 List(str): List of filenames of all changed files in the provided repository.
125130 """
126- diff = _get_diff_to_last_commit (path_to_repository )
131+ diff = _get_diff_to_last_commit (path_to_repository , ignore_subrepositories )
127132 return [item .b_path for item in diff if item .change_type in _CHANGE_TYPES_CONSIDERED_FOR_PRECOMMIT ]
128133
129134
130- def get_deleted_files (path_to_repository ):
135+ def get_deleted_files (path_to_repository , ignore_subrepositories ):
131136 """Utility method for getting the deleted files from a Git repository.
132137
133138 Args:
@@ -136,11 +141,11 @@ def get_deleted_files(path_to_repository):
136141 Returns:
137142 List(str): List of filenames of all deleted files in the provided repository.
138143 """
139- diff = _get_diff_to_last_commit (path_to_repository )
144+ diff = _get_diff_to_last_commit (path_to_repository , ignore_subrepositories )
140145 return [item .b_path for item in diff if item .change_type == _CHANGE_TYPE_DELETED ]
141146
142147
143- def _get_diff_to_last_commit (path_to_repository ):
148+ def _get_diff_to_last_commit (path_to_repository , ignore_subrepositories ):
144149 """ Utility method for getting a diff between the working copy and the HEAD commit
145150
146151 Args:
@@ -150,6 +155,11 @@ def _get_diff_to_last_commit(path_to_repository):
150155 List(git.diff.Diff): List of Diff objects for every file
151156 """
152157 repo = Repo (path_to_repository )
153- unstaged_diff = repo .index .diff (None )
154- staged_diff = repo .head .commit .diff ()
158+ if ignore_subrepositories == True :
159+ unstaged_diff = repo .index .diff (other = None , paths = None , create_patch = False , ignore_submodules = "all" )
160+ staged_diff = repo .head .commit .diff (other = Diffable .Index , paths = None , create_patch = False , ignore_submodules = "all" )
161+ else :
162+ unstaged_diff = repo .index .diff (other = None , paths = None , create_patch = False )
163+ staged_diff = repo .head .commit .diff (other = Diffable .Index , paths = None , create_patch = False )
164+
155165 return unstaged_diff + staged_diff
0 commit comments