@@ -182,16 +182,21 @@ def find(
182
182
return result
183
183
184
184
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 :
186
188
"""Compute a hash on a filetree to reflect its current state.
187
189
188
190
:param path: root path of the file tree to be checked
189
191
:param ignore_hidden: if True (default) then files and directories
190
192
tarting with a dot are ignored.
193
+ :param hash_content: if True, include the content in the hash.
194
+
191
195
:return: a hash as a string
192
196
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.
195
200
This case is quite uncommon. By ignoring it we can compute efficiently a
196
201
hash representing the state of the file tree without having to read the
197
202
content of all files.
@@ -205,6 +210,10 @@ def compute_state(file_path: str) -> bytes:
205
210
)
206
211
return state .encode ("utf-8" )
207
212
213
+ def get_content (file_path : str ) -> bytes :
214
+ with open (file_path , "rb" ) as f :
215
+ return f .read ()
216
+
208
217
path = os .path .abspath (path )
209
218
result = hashlib .sha1 ()
210
219
if os .path .isdir (path ):
@@ -225,8 +234,15 @@ def compute_state(file_path: str) -> bytes:
225
234
full_path = os .path .join (root , path )
226
235
result .update (compute_state (full_path ))
227
236
237
+ if hash_content :
238
+ result .update (get_content (full_path ))
239
+
228
240
else :
229
241
result .update (compute_state (path ))
242
+
243
+ if hash_content :
244
+ result .update (get_content (path ))
245
+
230
246
return result .hexdigest ()
231
247
232
248
0 commit comments