diff --git a/common.py b/common.py index f1498a4..4ea2956 100644 --- a/common.py +++ b/common.py @@ -2,6 +2,7 @@ import os import subprocess import logging +from threading import Thread import bpy @@ -51,6 +52,18 @@ def ui_refresh(): time.sleep(0.1) +def stash_save(msg, background=True): + def stash(): + if not check_repo_exists(): + return + do_git("stash", "save", "-u", msg) + if not background: + stash() + return + thread = Thread(target=stash) + thread.start() + + def do_git(*args): """Common routine for invoking various Git functions.""" env = dict(os.environ) diff --git a/extensions.py b/extensions.py index 90c7c14..676cef2 100644 --- a/extensions.py +++ b/extensions.py @@ -29,6 +29,10 @@ class VersionProperties(PropertyGroup): @register_wrap class BranchProperties(PropertyGroup): """Properties for branches section""" + stash: BoolProperty( + name="Stash before load") + stash_message: StringProperty( + name="Stash message") branch: bpy.props.EnumProperty( name="The local branches of the repo", items=list_branches) diff --git a/tools/branches.py b/tools/branches.py index f6562b4..7e47107 100644 --- a/tools/branches.py +++ b/tools/branches.py @@ -3,7 +3,8 @@ from ..common import (do_git, doc_saved, working_dir_clean, - check_repo_exists) + check_repo_exists, + stash_save) from .register import register_wrap @@ -22,6 +23,16 @@ def execute(self, context: bpy.types.Context): {"ERROR"}, "Working directory must be clean (try saving)") return {"CANCELLED"} + if context.window_manager.branches.stash \ + and context.window_manager.branches.stash_message: + print("Doing stash for", + context.window_manager.branches.stash_message) + stash_save(context.window_manager.branches.stash_message, + background=False) + elif not context.window_manager.branches.stash_message: + self.report({"ERROR"}, "Please enter a stash message") + return {"CANCELLED"} + if len(context.window_manager.branches.branch) == 0: return {"CANCELLED"} do_git("checkout", context.window_manager.branches.branch) diff --git a/tools/lfs.py b/tools/lfs.py index 472d811..cb6dd3e 100644 --- a/tools/lfs.py +++ b/tools/lfs.py @@ -49,7 +49,7 @@ def check_lfs_initialized() -> bool: return False -def initialize_lfs(context, extra_filetypes=()): +def initialize_lfs(self=None, context=None, extra_filetypes=()): """Initializes LFS with default binary filetypes""" global lfs_initialized filetypes = { diff --git a/tools/loading.py b/tools/loading.py index 61ac96a..42c6849 100644 --- a/tools/loading.py +++ b/tools/loading.py @@ -8,6 +8,7 @@ from ..common import (do_git, working_dir_clean, check_repo_exists, + stash_save, ui_refresh) commits_list = [] @@ -32,6 +33,8 @@ def format_compact_datetime(timestamp: int) -> str: def get_main_branch() -> str: """Returns the main branch of the repo""" + if not check_repo_exists(): + return None branches = do_git("branch").split('\n') branches = [branch.strip() for branch in branches] if 'main' in branches: @@ -42,6 +45,8 @@ def get_main_branch() -> str: def which_branch() -> str: """Returns the current branch (if not in a commit)""" branch = None + if not check_repo_exists(): + return None for line in do_git("branch").split("\n"): if "*" in line and "(" not in line: branch = line[2:].rstrip() @@ -131,13 +136,3 @@ def execute(self, context: bpy.types.Context): result = {"CANCELLED"} return result - - -def stash_save(msg, background=True): - def stash(): - do_git("stash", "save", "-u", msg) - if not background: - stash() - return - thread = Thread(target=stash) - thread.start() diff --git a/tools/saving.py b/tools/saving.py index 476bfdb..5a3dcef 100644 --- a/tools/saving.py +++ b/tools/saving.py @@ -6,7 +6,7 @@ from ..common import do_git, check_repo_exists, get_work_dir from .register import register_wrap -from .lfs import initialize_lfs_async +from .lfs import initialize_lfs from .loading import refresh_commit_list_async @@ -22,7 +22,8 @@ def execute(self, context: bpy.types.Context): if msg.strip(): if not check_repo_exists(): do_git("init") - initialize_lfs_async() + do_git("config", "--local", "core.autocrlf", "false") + initialize_lfs() create_gitignore() if context.window_manager.versions.restore_stash: @@ -93,8 +94,10 @@ def create_gitignore(): "*.gltf", ) work_dir = get_work_dir() - with open(os.path.join(work_dir, '.gitignore')) as f: + gitignore_path = os.path.join(work_dir, '.gitignore') + with open(gitignore_path, 'w', newline='\n') as f: f.write("\n".join(gitignore)) + do_git("add", '.gitignore') def stash_pop(background=True): diff --git a/ui/branches.py b/ui/branches.py index c6a0861..7acf62f 100644 --- a/ui/branches.py +++ b/ui/branches.py @@ -16,8 +16,15 @@ def draw(self, context): row = box.row(align=True) row.prop(context.window_manager.branches, "branch", text='') row = box.row(align=True) + row.prop(context.window_manager.branches, "stash") + if context.window_manager.branches.stash: + row = box.row(align=True) + row.prop(context.window_manager.branches, + "stash_message", text="") + row = box.row(align=True) row.operator(SwitchBranch.bl_idname) + layout.separator() box = layout.box() row = box.row(align=True) row.prop(context.window_manager.branches, "new_branch", text='') diff --git a/ui/versions.py b/ui/versions.py index 60e60c9..ede80de 100644 --- a/ui/versions.py +++ b/ui/versions.py @@ -1,6 +1,7 @@ import bpy from ..templates import ToolPanel +from ..common import check_repo_exists from ..tools.register import register_wrap from ..tools import lfs from ..tools.saving import SaveCommit @@ -28,7 +29,11 @@ def draw(self, context: bpy.types.Context): save_commit_button_row = box.row(align=True) save_commit_button_row.operator(SaveCommit.bl_idname) - if not lfs.lfs_installed: + if not check_repo_exists(): + row = box.row(align=True) + row.label(text="New repo will be created", + icon="INFO") + elif not lfs.lfs_installed: # Tell user to install git-lfs, disable commit button save_commit_button_row.enabled = False row = box.row(align=True) @@ -44,6 +49,7 @@ def draw(self, context: bpy.types.Context): row.operator(lfs.InitLfs.bl_idname) # LOADING + layout.separator() box = layout.box() row = box.row(align=True) row.prop(context.window_manager.versions, "commits", text="")