forked from kkabt/blendgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
487 lines (367 loc) · 14.2 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import bpy
from bpy.types import Menu, UIList, Panel
from .backend_git import Git
from .image_util import get_icon
from . import common
from .common import (
alert,
get_git_context as g,
get_addon_prefs as p
)
ICON_STATUS = {
Git.STATUS_UNMODIFIED: 'FILE_HIDDEN',
Git.STATUS_IGNORED: 'GHOST_DISABLED',
Git.STATUS_UNTRACKED: 'UNLINKED',
Git.STATUS_UNMERGED: 'LIBRARY_DATA_BROKEN',
Git.STATUS_NOTSTAGED: 'UNPINNED',
Git.STATUS_STAGED: 'PINNED'
}
ICON_COMMAND = {
'stage': 'PINNED',
'unstage': 'UNPINNED',
'untrack': 'UNLINKED',
'ignore': 'GHOST_DISABLED',
'notice': 'HIDE_OFF'
}
class GitPanel(Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Git"
@classmethod
def poll(cls, context):
show = p(context).show_v3d_panels
gcon = g(context)
return show and gcon.version and gcon.is_repository
class GIT_PT_context(GitPanel):
bl_label = ""
@classmethod
def poll(cls, context):
return p(context).show_v3d_panels
def draw_header(self, context):
layout = self.layout
gcon = g(context)
if gcon.version:
layout.label(
text=gcon.version,
icon_value=get_icon('LOGO')
)
else:
alert(layout, "git.exe is NOT found")
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
gcon = g(context)
grid = layout.grid_flow(even_columns=True)
grid.operator("preferences.addon_show", text="Open Setting", icon='PREFERENCES').module = "blendgit"
grid.operator("git.reload", text="Reload", icon='FILE_REFRESH', translate=False)
row = grid.row()
row.operator("git.open_folder", text="Open Root", icon='FILE_FOLDER', translate=False).dirpath = gcon.rootdir
row.enabled = bool(gcon.rootdir)
row = grid.row()
row.operator("git.load", text="Unload Repository", translate=False, icon='TRASH').unload = True
row.enabled = gcon.is_repository
class GIT_PT_init(GitPanel):
bl_label = ""
def draw_header(self, context):
self.layout.label(text="Init", translate=False)
@classmethod
def poll(cls, context):
show = p(context).show_v3d_panels
gcon = g(context)
return show and gcon.version and not gcon.is_repository
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
grid = layout.grid_flow(even_columns=True)
grid.operator("git.load", text="Load Repository", icon='NEWFOLDER')
grid.operator("git.init", text="Initialize Repository", icon='NEWFOLDER')
class GIT_PT_shortcut(GitPanel):
bl_label = "Shortcut"
def draw_header(self, context):
layout = self.layout
layout.operator("git.command_popup", text="", icon='CONSOLE', emboss=False)
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
prefs = p(context)
if len(prefs.shortcuts) == 0:
layout.label(text="No shortcuts.")
else:
grid = layout.grid_flow(even_columns=True)
for sc in prefs.shortcuts:
if sc.name and sc.command:
op = grid.operator("git.command_popup", text=sc.name, translate=False)
op.pre_command = sc.command
op.show_popup = sc.show_popup
op.need_confirm = sc.need_confirm
class GIT_MT_switch(Menu):
bl_label = "Switch"
def draw(self, context):
layout = self.layout
gcon = context.window_manager.git_context
active = gcon.active_branch
for i, branch in enumerate(gcon.branches):
if i==active:
continue
layout.operator("git.switch", text=branch.name).branchname = branch.name
class GIT_MT_branch(Menu):
bl_label = "Branch"
def draw(self, context):
gcon = context.window_manager.git_context
layout = self.layout
layout.operator("git.branch_add", text="Add", translate=False)
layout.operator("git.branch_rename", text="Rename", translate=False)
layout.operator("git.branch_copy", text="Copy", translate=False)
layout.operator("git.branch_delete", text="Delete", translate=False)
layout.separator()
layout.operator("git.merge", text="Merge", translate=False)
class GIT_PT_branch(GitPanel):
bl_label = "Branch"
def draw_header(self, context):
gcon = g(context)
if len(gcon.branches)>0:
branchname = gcon.branches[gcon.active_branch].name
self.bl_label = "Branch * "+branchname
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
gcon = g(context)
if len(gcon.branches)>0:
if gcon.is_dirty:
alert(layout, "Your local changes to the following files would be overwritten by checkout")
if gcon.status & Git.STATUS_UNTRACKED:
alert(layout, "The following untracked working tree files would be overwritten by checkout")
row = layout.row()
col = row.column()
branch = gcon.branches[gcon.active_branch]
col.menu("GIT_MT_switch", text=branch.name)
col.enabled = not gcon.is_dirty and len(gcon.branches) > 1
row.menu("GIT_MT_branch", icon='DOWNARROW_HLT', text="")
else:
layout.label(text="No branches.")
class GIT_MT_file(Menu):
bl_label = "File"
def draw(self, context):
layout = self.layout
layout.operator("git.write_ignore", icon=ICON_STATUS[Git.STATUS_IGNORED])
class GIT_UL_file(UIList):
"""For FileEntry"""
show_unmodified: bpy.props.BoolProperty(default=True)
show_ignored: bpy.props.BoolProperty(default=True)
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
gcon = data
file = item
status = file.status
row = layout.row()
row.label(text=file.name, icon=ICON_STATUS[status])
row_op = row.row(align=True)
if status == Git.STATUS_IGNORED:
row_op.operator("git.notice", text="", icon=ICON_COMMAND['notice']).target_ref = file.ref
else:
if status in (
Git.STATUS_UNTRACKED,
Git.STATUS_UNMERGED,
Git.STATUS_NOTSTAGED
):
row_op.operator("git.stage", text="", icon=ICON_COMMAND['stage']).target_ref = file.ref
if status == Git.STATUS_UNTRACKED:
row_op.operator("git.ignore", text="", icon=ICON_COMMAND['ignore']).target_ref = file.ref
# elif status == Git.STATUS_UNMERGED:
# op = row_op.operator("git.unstage", text="", icon='FILE_TICK')
# op.target_ref = file.ref
# op.resolve_unmerge = True
elif status in (
Git.STATUS_UNMODIFIED,
Git.STATUS_NOTSTAGED
):
row_op.operator("git.untrack", text="", icon=ICON_COMMAND['untrack']).target_ref = file.ref
elif status == Git.STATUS_STAGED:
if len(gcon.logs) == 0:
row_op.operator("git.untrack", text="", icon=ICON_COMMAND['untrack']).target_ref = file.ref
else:
row_op.operator("git.unstage", text="", icon=ICON_COMMAND['unstage']).target_ref = file.ref
def draw_filter(self, context, layout):
row = layout.row()
subrow = row.row(align=True)
subrow.prop(self, "filter_name", text="")
subrow.prop(self, "use_filter_invert", text="", icon='ARROW_LEFTRIGHT')
subrow = row.row(align=True)
subrow.prop(self, "show_unmodified", text="", toggle=True, icon=ICON_STATUS[Git.STATUS_UNMODIFIED])
subrow.prop(self, "show_ignored", text="", toggle=True, icon=ICON_STATUS[Git.STATUS_IGNORED])
def filter_items(self, context, data, propname):
files = getattr(data, propname)
helper_funcs = bpy.types.UI_UL_list
# Default return values.
flt_flags = []
flt_neworder = []
# Filtering by name
if self.filter_name:
flt_flags = helper_funcs.filter_items_by_name(
self.filter_name,
self.bitflag_filter_item,
files,
'name',
reverse=self.use_filter_invert
)
if not flt_flags:
flt_flags = [self.bitflag_filter_item] * len(files)
# Filtering by show-ignored.
for idx, file in enumerate(files):
status = file.status
if not self.show_unmodified and status==Git.STATUS_UNMODIFIED:
flt_flags[idx] &= False
elif not self.show_ignored and status==Git.STATUS_IGNORED:
flt_flags[idx] &= False
return flt_flags, flt_neworder
class GIT_PT_file(GitPanel):
bl_label = ""
def draw_header(self, context):
layout = self.layout
layout.label(text="File", translate=False)
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
gcon = g(context)
row = layout.row()
row.template_list(
'GIT_UL_file',
'',
gcon, "files",
gcon, "active_file",
# item_dyntip_propname="status",
rows=2
)
col = row.column()
col.menu("GIT_MT_file", icon='DOWNARROW_HLT', text="")
class GIT_MT_stash(Menu):
bl_label = "Stash"
def draw(self, context):
layout = self.layout
layout.operator("git.stash_apply", text="Apply", translate=False)
layout.operator("git.stash_drop", text="Drop", translate=False)
layout.operator("git.stash_clear", text="Clear", translate=False)
class GIT_PT_stash(GitPanel):
bl_label = "Stash"
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
gcon = g(context)
if len(gcon.stashes)>0:
row = layout.row()
row.template_list(
"UI_UL_list",
"stashes",
gcon,
"stashes",
gcon,
"active_stash",
rows=2
)
col = row.column()
col.menu("GIT_MT_stash", icon='DOWNARROW_HLT', text="", translate=False)
else:
layout.label(text="No stashes.")
layout.separator()
if not gcon.is_dirty:
alert(layout, "No local changes to save.")
row = layout.row()
row.operator("git.stash_save", text="Save Stash", icon='NEWFOLDER', translate=False)
row.enabled = gcon.is_dirty
class GIT_UL_log(UIList):
"""For LogEntry"""
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
gcon = data
log = item
col = layout.column()
for line in log.logline.split('\n'):
col.label(text=line)
def draw_filter(self, context, layout):
row = layout.row()
subrow = row.row(align=True)
subrow.prop(self, "filter_name", text="")
subrow.prop(self, "use_filter_invert", text="", icon='ARROW_LEFTRIGHT')
subrow = row.row(align=True)
prefs = p(context)
subrow.prop_menu_enum(prefs, 'log_command', text="", icon='COLLAPSEMENU')
class GIT_MT_log(Menu):
bl_label = "Log"
def draw(self, context):
gcon = context.window_manager.git_context
layout = self.layout
# prefs = p(context)
# layout.prop_menu_enum(prefs, 'log_command', text="Log Command")
# layout.separator()
layout.operator("git.thumbnail_edit", icon='IMAGE_REFERENCE')
layout.operator("git.checkout_file", icon='FILE_NEW')
layout.operator("git.pick_library", icon='IMPORT')
layout.separator()
layout.operator("git.archive", icon='FILE_ARCHIVE')
layout.operator("git.open_folder", text="Open Archive Folder", translate=False, icon='FILE_FOLDER').dirpath = common.get_archive_path(context)
class GIT_PT_log(GitPanel):
bl_label = "Log"
def draw(self, context):
self._draw(self.layout, context)
@staticmethod
def _draw(layout, context):
gcon = g(context)
if len(gcon.logs)>0:
row = layout.row()
row.template_list(
'GIT_UL_log',
"logs",
gcon, "logs",
gcon, "active_log",
item_dyntip_propname="name",
sort_lock=True
)
col = row.column()
col.operator("git.show", text="", icon='INFO')
col.operator("git.reset", text="", icon='LOOP_BACK')
col.operator("git.revert", text="", icon='FORWARD')
col.menu("GIT_MT_log", icon='DOWNARROW_HLT', text="", translate=False)
else:
layout.label(text="No logs.")
layout.separator()
if not gcon.is_commit_ready:
status = gcon.status
if status & Git.STATUS_NOTSTAGED:
alert(layout, "There are changes not staged for commit.")
if status & Git.STATUS_UNTRACKED:
alert(layout, "There are untracked files.")
if status & Git.STATUS_UNMERGED:
alert(layout, "There are unmerged files.")
if not gcon.is_dirty:
alert(layout, "Nothing to commit, working tree clean.")
text = "Commit" if gcon.is_dirty else "Empty Commit"
layout.operator("git.commit", text=text, icon='FOLDER_REDIRECT')
panels = (
GIT_PT_context,
GIT_PT_init,
GIT_PT_shortcut,
GIT_PT_branch,
GIT_PT_file,
GIT_PT_stash,
GIT_PT_log
)
classes = (
GIT_PT_context,
GIT_PT_init,
GIT_PT_shortcut,
GIT_MT_switch,
GIT_MT_branch,
GIT_PT_branch,
GIT_MT_file,
GIT_UL_file,
GIT_PT_file,
GIT_MT_stash,
GIT_PT_stash,
GIT_UL_log,
GIT_MT_log,
GIT_PT_log
)