Skip to content

Commit

Permalink
Add stash option to Branches panel, fix git init functionality + 2 more
Browse files Browse the repository at this point in the history
- Use check_repo_exists where possible
- Turn off git autocrlf for Windows users
  • Loading branch information
dragos240 committed May 24, 2022
1 parent e0bba24 commit 797125b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 16 deletions.
13 changes: 13 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import subprocess
import logging
from threading import Thread

import bpy

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion tools/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tools/lfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
15 changes: 5 additions & 10 deletions tools/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..common import (do_git,
working_dir_clean,
check_repo_exists,
stash_save,
ui_refresh)

commits_list = []
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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()
9 changes: 6 additions & 3 deletions tools/saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions ui/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='')
Expand Down
8 changes: 7 additions & 1 deletion ui/versions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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="")
Expand Down

0 comments on commit 797125b

Please sign in to comment.