ifcgit.git_diff - save diff to temp file on alt-click

This commit is contained in:
Andrej730
2025-06-20 12:50:04 +05:00
parent 98145fbbd9
commit 946cae4065
3 changed files with 30 additions and 8 deletions
@@ -4,6 +4,7 @@ import bpy
import bonsai.core.ifcgit as core
import bonsai.tool as tool
from bonsai.bim.module.ifcgit.data import IfcGitData, refresh
from typing import TYPE_CHECKING
class CreateRepo(bpy.types.Operator):
@@ -410,12 +411,20 @@ class InstallGit(bpy.types.Operator):
class RunGitDiff(bpy.types.Operator):
"""Run `git diff` for the current version of IFC file and the last saved one."""
bl_label = "Git Diff"
bl_idname = "ifcgit.git_diff"
bl_description = (
"Run `git diff` for the current version of IFC file and the last saved one.\n\n"
"ALT+click to save output to temp directory."
)
bl_options = set()
save_to_temp: bpy.props.BoolProperty(options={"SKIP_SAVE"}) # pyright: ignore[reportRedeclaration]
if TYPE_CHECKING:
save_to_temp: bool
@classmethod
def poll(cls, context):
if not tool.Ifc.get():
@@ -426,6 +435,11 @@ class RunGitDiff(bpy.types.Operator):
return False
return True
def invoke(self, context, event):
if event.alt:
self.save_to_temp = True
return self.execute(context)
def execute(self, context):
core.run_git_diff(tool.IfcGit, self)
core.run_git_diff(tool.IfcGit, self, self.save_to_temp)
return {"FINISHED"}
+2 -2
View File
@@ -144,5 +144,5 @@ def install_git(ifcgit: type[tool.IfcGit], operator: bpy.types.Operator) -> None
print("install_git() not implemented")
def run_git_diff(ifcgit: type[tool.IfcGit], operator: bpy.types.Operator) -> None:
ifcgit.run_git_diff(operator)
def run_git_diff(ifcgit: type[tool.IfcGit], operator: bpy.types.Operator, save_to_temp: bool) -> None:
ifcgit.run_git_diff(operator, save_to_temp)
+12 -4
View File
@@ -22,9 +22,11 @@ import re
import subprocess
import bpy
import logging
import tempfile
from bonsai.bim import import_ifc
from bonsai.bim.ifc import IfcStore
import bonsai.tool as tool
from pathlib import Path
from typing import TYPE_CHECKING, Union, Literal, Any
# allows git import even if git executable isn't found
@@ -582,21 +584,27 @@ class IfcGit:
operator.report({"ERROR"}, "Winget is not available. Make sure Windows Package Manager is installed.")
@classmethod
def run_git_diff(cls, operator: bpy.types.Operator) -> None:
def run_git_diff(cls, operator: bpy.types.Operator, save_to_temp: bool) -> None:
path = tool.Ifc.get_path()
ifc_file = tool.Ifc.get()
ifc_str = ifc_file.to_string()
color = "never" if save_to_temp else "always"
# Avoid `text=True` as it's causing issues with colorful output.
try:
subprocess.check_output(
("git", "diff", "--no-index", "--color=always", "--", path, "-"),
("git", "diff", "--no-index", f"--color={color}", "--", path, "-"),
input=ifc_str.encode(),
)
except subprocess.CalledProcessError as e:
if e.returncode == 1:
print(e.stdout.decode())
operator.report({"INFO"}, "See system console for git diff output.")
if save_to_temp:
temp_path = Path(tempfile.gettempdir()) / "bonsai.diff"
temp_path.write_bytes(e.stdout)
operator.report({"INFO"}, f"Git diff output is saved to {temp_path}.")
else:
print(e.stdout.decode())
operator.report({"INFO"}, "See system console for git diff output.")
return
print(e.output)
raise Exception("Error running git diff, see system console.")