git_deploy: improve pristine detection

Detect untracked source files and modified files for each
configured repo.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-07-19 07:40:16 -04:00
parent ac876b9271
commit 3e1919b46e
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 31 additions and 3 deletions

View File

@ -238,6 +238,7 @@ GIT_REF_FMT = (
"'%(if)%(*objecttype)%(then)%(*objecttype) %(*objectname)" "'%(if)%(*objecttype)%(then)%(*objecttype) %(*objectname)"
"%(else)%(objecttype) %(objectname)%(end) %(refname)'" "%(else)%(objecttype) %(objectname)%(end) %(refname)'"
) )
SRC_EXTS = (".py", ".c", ".cpp")
class GitRepo: class GitRepo:
def __init__( def __init__(
@ -308,6 +309,8 @@ class GitRepo:
self.commits_behind_count: int = storage.get('cbh_count', 0) self.commits_behind_count: int = storage.get('cbh_count', 0)
self.diverged: bool = storage.get("diverged", False) self.diverged: bool = storage.get("diverged", False)
self.repo_corrupt: bool = storage.get('corrupt', False) self.repo_corrupt: bool = storage.get('corrupt', False)
self.modified_files: List[str] = storage.get("modified_files", [])
self.untracked_files: List[str] = storage.get("untracked_files", [])
def_rbs = self.capture_state_for_rollback() def_rbs = self.capture_state_for_rollback()
self.rollback_commit: str = storage.get('rollback_commit', self.current_commit) self.rollback_commit: str = storage.get('rollback_commit', self.current_commit)
self.rollback_branch: str = storage.get('rollback_branch', def_rbs["branch"]) self.rollback_branch: str = storage.get('rollback_branch', def_rbs["branch"])
@ -341,7 +344,9 @@ class GitRepo:
'commits_behind': self.commits_behind, 'commits_behind': self.commits_behind,
'cbh_count': self.commits_behind_count, 'cbh_count': self.commits_behind_count,
'diverged': self.diverged, 'diverged': self.diverged,
'corrupt': self.repo_corrupt 'corrupt': self.repo_corrupt,
'modified_files': self.modified_files,
'untracked_files': self.untracked_files
} }
async def refresh_repo_state(self, need_fetch: bool = True) -> None: async def refresh_repo_state(self, need_fetch: bool = True) -> None:
@ -455,6 +460,21 @@ class GitRepo:
break break
if resp is None: if resp is None:
return False return False
self.modified_files.clear()
self.untracked_files.clear()
for line in resp.splitlines():
parts = line.strip().split(maxsplit=1)
if len(parts) != 2:
continue
prefix, fname = [p.strip() for p in parts]
if prefix == "M":
# modified file
self.modified_files.append(fname)
elif prefix == "??":
# untracked file
ext = pathlib.Path(fname).suffix
if ext in SRC_EXTS:
self.untracked_files.append(fname)
self.valid_git_repo = True self.valid_git_repo = True
await self.set_current_instance() await self.set_current_instance()
return True return True
@ -714,7 +734,14 @@ class GitRepo:
if self.diverged: if self.diverged:
self.repo_warnings.append("Repo has diverged from remote") self.repo_warnings.append("Repo has diverged from remote")
if self.is_dirty(): if self.is_dirty():
self.repo_warnings.append("Repo has modified files (dirty)") self.repo_warnings.append(
"Repo is dirty. Detected the following modifed files: "
f"{self.modified_files}"
)
if self.untracked_files:
self.repo_warnings.append(
f"Repo has untracked source files: {self.untracked_files}"
)
if len(self.managing_instances) > 1: if len(self.managing_instances) > 1:
instances = "\n".join([f" {ins}" for ins in self.managing_instances]) instances = "\n".join([f" {ins}" for ins in self.managing_instances])
self.repo_warnings.append( self.repo_warnings.append(
@ -1026,6 +1053,7 @@ class GitRepo:
return f"{cur_name}@{cur_uuid}" return f"{cur_name}@{cur_uuid}"
def get_repo_status(self) -> Dict[str, Any]: def get_repo_status(self) -> Dict[str, Any]:
no_untrk_src = len(self.untracked_files) == 0
return { return {
'detected_type': "git_repo", 'detected_type': "git_repo",
'remote_alias': self.git_remote, 'remote_alias': self.git_remote,
@ -1045,7 +1073,7 @@ class GitRepo:
'commits_behind_count': self.commits_behind_count, 'commits_behind_count': self.commits_behind_count,
'git_messages': self.git_messages, 'git_messages': self.git_messages,
'full_version_string': self.current_version.full_version, 'full_version_string': self.current_version.full_version,
'pristine': not self.current_version.dirty, 'pristine': no_untrk_src and not self.current_version.dirty,
'corrupt': self.repo_corrupt, 'corrupt': self.repo_corrupt,
'warnings': self.repo_warnings 'warnings': self.repo_warnings
} }