Recover external git repo changes, fixes #5311

IfcGit didn't cope gracefully when commits and/or repositories vanished
unexpectedly
This commit is contained in:
Bruno Postle
2024-09-15 17:24:36 +01:00
parent c487643d76
commit 71de39ea7c
3 changed files with 16 additions and 6 deletions
+5 -3
View File
@@ -154,15 +154,17 @@ class IfcGitData:
@classmethod @classmethod
def commit(cls): def commit(cls):
props = bpy.context.scene.IfcGitProperties props = bpy.context.scene.IfcGitProperties
if len(props.ifcgit_commits) > 0: if cls.repo() and len(props.ifcgit_commits) > 0:
item = props.ifcgit_commits[props.commit_index] item = props.ifcgit_commits[props.commit_index]
if cls.repo(): try:
return cls.repo().commit(rev=item.hexsha) return cls.repo().commit(rev=item.hexsha)
except ValueError:
return
@classmethod @classmethod
def current_revision(cls): def current_revision(cls):
props = bpy.context.scene.IfcGitProperties props = bpy.context.scene.IfcGitProperties
if len(props.ifcgit_commits) > 0: if cls.repo() and cls.repo().head.is_valid() and len(props.ifcgit_commits) > 0:
return tool.IfcGitRepo.repo.commit() return tool.IfcGitRepo.repo.commit()
@classmethod @classmethod
+8 -2
View File
@@ -1,5 +1,7 @@
import bpy import bpy
import time import time
import os
from bonsai.bim.module.ifcgit.data import IfcGitData from bonsai.bim.module.ifcgit.data import IfcGitData
@@ -32,7 +34,7 @@ class IFCGIT_PT_panel(bpy.types.Panel):
row = layout.row() row = layout.row()
if path_ifc: if path_ifc:
if IfcGitData.data["repo"]: if IfcGitData.data["repo"] and os.path.exists(IfcGitData.data["repo"].git_dir):
name_ifc = IfcGitData.data["name_ifc"] name_ifc = IfcGitData.data["name_ifc"]
row.label(text=IfcGitData.data["working_dir"], icon="SYSTEM") row.label(text=IfcGitData.data["working_dir"], icon="SYSTEM")
if name_ifc in IfcGitData.data["untracked_files"]: if name_ifc in IfcGitData.data["untracked_files"]:
@@ -44,6 +46,7 @@ class IFCGIT_PT_panel(bpy.types.Panel):
else: else:
row.label(text=name_ifc, icon="FILE") row.label(text=name_ifc, icon="FILE")
else: else:
IfcGitData.load()
row.operator( row.operator(
"ifcgit.createrepo", "ifcgit.createrepo",
text="Create '" + IfcGitData.data["dir_name"] + "' repository", text="Create '" + IfcGitData.data["dir_name"] + "' repository",
@@ -193,7 +196,10 @@ class COMMIT_UL_List(bpy.types.UIList):
# TODO Figure how this "item" can be acesse in "data.py" # TODO Figure how this "item" can be acesse in "data.py"
# so it's possible to move the ".commit" # so it's possible to move the ".commit"
commit = IfcGitData.data["repo"].commit(rev=item.hexsha) try:
commit = IfcGitData.data["repo"].commit(rev=item.hexsha)
except ValueError:
return
lookup = IfcGitData.data["branches_by_hexsha"] lookup = IfcGitData.data["branches_by_hexsha"]
refs = "" refs = ""
+3 -1
View File
@@ -79,7 +79,7 @@ class IfcGit:
else: else:
return None return None
if IfcGitRepo.repo is not None and IfcGitRepo.repo.working_dir == path_dir: if IfcGitRepo.repo is not None and os.path.exists(IfcGitRepo.repo.git_dir) and IfcGitRepo.repo.working_dir == path_dir:
return IfcGitRepo.repo return IfcGitRepo.repo
try: try:
@@ -88,9 +88,11 @@ class IfcGit:
parentdir_path = os.path.abspath(os.path.join(path_dir, os.pardir)) parentdir_path = os.path.abspath(os.path.join(path_dir, os.pardir))
if parentdir_path == path_dir: if parentdir_path == path_dir:
# root folder # root folder
IfcGitRepo.repo = None
return None return None
return cls.repo_from_path(parentdir_path) return cls.repo_from_path(parentdir_path)
except git.exc.NoSuchPathError: except git.exc.NoSuchPathError:
IfcGitRepo.repo = None
return None return None
if repo: if repo:
IfcGitRepo.repo = repo IfcGitRepo.repo = repo