@@ -182,16 +182,21 @@ def find(
182182 return result
183183
184184
185- def get_filetree_state (path : str , ignore_hidden : bool = True ) -> str :
185+ def get_filetree_state (
186+ path : str , ignore_hidden : bool = True , hash_content : bool = False
187+ ) -> str :
186188 """Compute a hash on a filetree to reflect its current state.
187189
188190 :param path: root path of the file tree to be checked
189191 :param ignore_hidden: if True (default) then files and directories
190192 tarting with a dot are ignored.
193+ :param hash_content: if True, include the content in the hash.
194+
191195 :return: a hash as a string
192196
193- The function will not report changes in the hash if a file is modified
194- and its attributes (size, modification time and mode) are not changed.
197+ By default, the function will not report changes in the hash if a file is
198+ modified and its attributes (size, modification time and mode) are not
199+ changed.
195200 This case is quite uncommon. By ignoring it we can compute efficiently a
196201 hash representing the state of the file tree without having to read the
197202 content of all files.
@@ -205,6 +210,10 @@ def compute_state(file_path: str) -> bytes:
205210 )
206211 return state .encode ("utf-8" )
207212
213+ def get_content (file_path : str ) -> bytes :
214+ with open (file_path , "rb" ) as f :
215+ return f .read ()
216+
208217 path = os .path .abspath (path )
209218 result = hashlib .sha1 ()
210219 if os .path .isdir (path ):
@@ -225,8 +234,15 @@ def compute_state(file_path: str) -> bytes:
225234 full_path = os .path .join (root , path )
226235 result .update (compute_state (full_path ))
227236
237+ if hash_content :
238+ result .update (get_content (full_path ))
239+
228240 else :
229241 result .update (compute_state (path ))
242+
243+ if hash_content :
244+ result .update (get_content (path ))
245+
230246 return result .hexdigest ()
231247
232248
0 commit comments