chore: more robust git on add new service
This commit is contained in:
+17
-12
@@ -6,23 +6,22 @@ import git
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
def git_push_service_directory(
|
||||
repo_root: str | Path, branch_name: str, service_dir_rel_path: str, commit_msg: str
|
||||
def git_push_changes(
|
||||
repo: git.Repo,
|
||||
branch_name: str,
|
||||
service_dir_rel_path: str,
|
||||
registry_file_rel_path: str,
|
||||
commit_msg: str,
|
||||
):
|
||||
"""
|
||||
Push newly created service to Gitea
|
||||
"""
|
||||
repo = git.Repo(repo_root)
|
||||
original_branch = repo.active_branch.name
|
||||
|
||||
try:
|
||||
switch_branch(repo, branch_name)
|
||||
|
||||
logger.info(f"Staging files inside: {service_dir_rel_path}...")
|
||||
repo.index.add([service_dir_rel_path])
|
||||
logger.info(f"Staging files inside: {service_dir_rel_path} and {registry_file_rel_path}...")
|
||||
repo.index.add([service_dir_rel_path, registry_file_rel_path])
|
||||
|
||||
# Check if there are changes to avoid empty commit crashes
|
||||
if not repo.is_dirty(index=True, working_tree=False, path=service_dir_rel_path):
|
||||
if not repo.is_dirty(index=True, working_tree=False):
|
||||
logger.info("No changes detected. Skipping commit.")
|
||||
return
|
||||
|
||||
@@ -34,11 +33,9 @@ def git_push_service_directory(
|
||||
origin.push(refspec=f"{branch_name}:{branch_name}").raise_if_error()
|
||||
|
||||
logger.info("Successfully pushed!")
|
||||
switch_branch(repo, original_branch)
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"Git Operation Failed: {e}")
|
||||
switch_branch(repo, original_branch)
|
||||
|
||||
|
||||
def switch_branch(repo: git.Repo, branch_name: str):
|
||||
@@ -51,3 +48,11 @@ def switch_branch(repo: git.Repo, branch_name: str):
|
||||
else:
|
||||
new_branch = repo.create_head(branch_name)
|
||||
new_branch.checkout()
|
||||
|
||||
|
||||
def assert_clean_repo(repo: git.Repo):
|
||||
if repo.is_dirty(index=True, working_tree=True):
|
||||
raise RuntimeError(
|
||||
"Your Git repository has uncommitted modifications to tracked files.\n"
|
||||
"Please commit, stash, or discard them before proceeding."
|
||||
)
|
||||
|
||||
@@ -69,7 +69,9 @@ class ServiceRegistry(BaseModel):
|
||||
svc = Service(id=self.next_available_id, name=name, status=ServiceStatus.ACTIVE)
|
||||
self.services.update({svc.id: svc.to_dict()})
|
||||
self.next_available_id += 1
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.safe_dump(self.model_dump(), f)
|
||||
|
||||
return svc
|
||||
|
||||
def write_to_config(self) -> None:
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.safe_dump(self.model_dump(), f)
|
||||
|
||||
Reference in New Issue
Block a user