Skip to content

Commit 9b2f2e9

Browse files
spoorccben-edna
authored andcommitted
Check for local changes on per-file basis
1 parent d44017c commit 9b2f2e9

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

dfetch/project/metadata.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ def branch(self) -> str:
127127
"""Branch as stored in the metadata."""
128128
return self._version.branch
129129

130+
@property
131+
def files(self) -> Iterable[FileInfo]:
132+
"""File info as stored in the metadata."""
133+
return self._files
134+
130135
@property
131136
def tag(self) -> str:
132137
"""Tag as stored in the metadata."""

dfetch/project/vcs.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import pathlib
66
from abc import ABC, abstractmethod
7-
from typing import List, Optional, Sequence, Tuple
7+
from typing import Iterable, List, Optional, Sequence, Tuple
88

99
from halo import Halo
1010
from patch_ng import fromfile
@@ -318,23 +318,24 @@ def on_disk_version(self) -> Optional[Version]:
318318
)
319319
return None
320320

321-
def _on_disk_hash(self) -> Optional[str]:
321+
def _on_disk_hash(self) -> Tuple[Iterable[FileInfo], Optional[str]]:
322322
"""Get the hash of the project on disk.
323323
324324
Returns:
325325
Str: Could be None if no on disk version
326326
"""
327327
if not os.path.exists(self.__metadata.path):
328-
return None
328+
return [], None
329329

330330
try:
331-
return Metadata.from_file(self.__metadata.path).hash
331+
metadata = Metadata.from_file(self.__metadata.path)
332+
return metadata.files, metadata.hash
332333
except TypeError:
333334
logger.warning(
334335
f"{pathlib.Path(self.__metadata.path).relative_to(os.getcwd()).as_posix()}"
335336
" is an invalid metadata file, not checking local hash!"
336337
)
337-
return None
338+
return [], None
338339

339340
def _check_for_newer_version(self) -> Optional[Version]:
340341
"""Check if a newer version is available on the given branch.
@@ -374,11 +375,24 @@ def _are_there_local_changes(self) -> bool:
374375
Bool: True if there are local changes, false if no were detected or no hash was found.
375376
"""
376377
logger.debug(f"Checking if there were local changes in {self.local_path}")
377-
on_disk_hash = self._on_disk_hash()
378378

379-
return bool(on_disk_hash) and on_disk_hash != hash_directory(
380-
self.local_path, skiplist=[self.__metadata.FILENAME]
381-
)
379+
file_info, on_disk_hash = self._on_disk_hash()
380+
381+
if not file_info:
382+
return bool(on_disk_hash) and on_disk_hash != hash_directory(
383+
self.local_path, skiplist=[self.__metadata.FILENAME]
384+
)
385+
386+
for file in file_info:
387+
full_path = os.path.join(self.local_path, file.path)
388+
if hash_file_normalized(full_path).hexdigest() != file.hash:
389+
logger.debug(f"The hash of {full_path} changed!")
390+
return True
391+
if oct(os.stat(full_path).st_mode)[-3:] != file.permissions:
392+
logger.debug(f"The file permissions of {full_path} changed!")
393+
return True
394+
395+
return False
382396

383397
@abstractmethod
384398
def _fetch_impl(self, version: Version) -> Version:

0 commit comments

Comments
 (0)