forked from claytonlemons/Subforce
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubforce.py
More file actions
855 lines (673 loc) · 31.5 KB
/
Subforce.py
File metadata and controls
855 lines (673 loc) · 31.5 KB
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# Useful documentation:
# Sublime Plugin Framework: http://docs.sublimetext.info/en/latest/reference/plugins.html
# Sublime Plugin Python API: http://www.sublimetext.com/docs/3/api_reference.html
# Perforce API: https://www.perforce.com/perforce/r16.1/manuals/cmdref
# Perforce Python API: https://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html
# Example Plugin: https://github.com/SideBarEnhancements-org/SideBarEnhancements/blob/st3/SideBar.py
import sublime
import sublime_plugin
import P4
import os
import sys
import threading
import subprocess
import re
import tempfile
from .utilities import \
getAllViewsForPath, \
coercePathsToActiveViewIfNeeded, \
getRevisionQualifiedDepotPath, \
checkForAndGetSinglePath, \
ellipsizeIfDirectory, \
createRevision
NEW_CHANGELIST_NAME = "new"
NEW_CHANGELIST_DESCRIPTION = "Creates a new changelist."
DEFAULT_CHANGELIST_NAME = "default"
DEFAULT_CHANGELIST_DESCRIPTION = "The default changelist."
HAVE_REVISION_NAME = "have"
HAVE_REVISION_DESCRIPTION = "The currently synced revision."
HEAD_REVISION_NAME = "head"
HEAD_REVISION_DESCRIPTION = "The most recently checked-in revision."
FILE_CHECKED_OUT_SETTING_KEY = "subforce_file_checked_out"
FILE_NOT_IN_DEPOT_SETTING_KEY = "subforce_file_not_in_depot"
CHANGELIST_NUMBER_STATUS_KEY = "subforce_changelist_number"
CURRENT_WORKING_DIRECTORY_SETTING_KEY = 'current_working_directory'
DISPLAY_WARNINGS_SETTING_KEY = 'display_warnings'
USE_CONNECTION_INFO_SETTINGS_KEY = 'use_connection_info'
CONNECTION_INFO_PORT_SETTINGS_KEY = 'connection_info_port'
CONNECTION_INFO_USER_SETTINGS_KEY = 'connection_info_user'
CONNECTION_INFO_CLIENT_SETTINGS_KEY = 'connection_info_client'
DISABLE_AUTO_CHECKOUT_SETTINGS_KEY = 'disable_auto_checkout'
class SettingsWrapper(object):
def __init__(self):
self._settings = sublime.load_settings("Subforce.sublime-settings")
def __getattr__(self, name):
return getattr(self._settings, name)
def getOrThrow(self, name):
setting = self._settings.get(name)
if setting is None:
raise P4.P4Exception("Subforce: You must set the {} setting!".format(name))
return setting
class PerforceWrapper(object):
def __init__(self, squelchErrorAndWarninMessages=False):
self._p4 = P4.P4()
self._settings = SettingsWrapper()
currentWorkingDirectorySetting = self._settings.get(CURRENT_WORKING_DIRECTORY_SETTING_KEY, None)
projectPath = sublime.active_window().extract_variables()['folder']
self._p4.cwd = currentWorkingDirectorySetting if currentWorkingDirectorySetting else projectPath
self._p4.exception_level = 1 # Only errors are raised as exceptions. Warnings are accessed through p4.warnings
self._p4.api_level = 79 # Lock to 2015.2 format
self._contextManagerEnterLevel = 0
self._squelchErrorAndWarninMessages = squelchErrorAndWarninMessages
def __getattr__(self, name):
attribute = getattr(self._p4, name)
return attribute
def __enter__(self):
if self._contextManagerEnterLevel == 0:
try:
if self._settings.get(USE_CONNECTION_INFO_SETTINGS_KEY, False):
self._p4.port = self._settings.getOrThrow(CONNECTION_INFO_PORT_SETTINGS_KEY)
self._p4.user = self._settings.getOrThrow(CONNECTION_INFO_USER_SETTINGS_KEY)
self._p4.client = self._settings.getOrThrow(CONNECTION_INFO_CLIENT_SETTINGS_KEY)
self._p4.connect()
except:
if self.__exit__(*sys.exc_info()):
pass
else:
raise
self._contextManagerEnterLevel += 1
return self
def __exit__(self, type, value, traceback):
noErrors = True
if self._contextManagerEnterLevel == 1:
self.handleWarnings()
try:
self._p4.disconnect()
except P4.P4Exception:
print("Subforce: failed to disconnect!")
noErrors = self.handleErrors(type, value, traceback)
self._contextManagerEnterLevel -= 1
return noErrors
def login(self, password):
self._p4.password = password
with self as p4:
p4.run_login()
print("Subforce: sucessfully logged in!")
def handleWarnings(self):
displayWarningsSetting = self._settings.get(DISPLAY_WARNINGS_SETTING_KEY, True)
if not self._squelchErrorAndWarninMessages and displayWarningsSetting:
for warning in self._p4.warnings:
sublime.message_dialog(str(warning))
def handleErrors(self, type, value, traceback):
noErrors = True
if type is P4.P4Exception:
if not self._squelchErrorAndWarninMessages:
sublime.error_message(str(value))
noErrors = False
elif type is not None:
noErrors = False
else:
noErrors = True
return noErrors
def pathUnderRoot(self, path):
try:
stat = self._p4.run_fstat(path)
# Sometimes an empty array is returned instead of an exception
if stat:
return True
else:
return False
except P4.P4Exception:
return False
def plugin_loaded():
print("Subforce: plugin loaded!")
def plugin_unloaded():
print("Subforce: plugin unloaded!")
class SubforceDisplayDescriptionCommand(sublime_plugin.TextCommand):
def run(self, edit, description = ""):
# Enable editing momentarily to set description
self.view.set_read_only(False)
self.view.replace(edit, sublime.Region(0, self.view.size()), description)
self.view.sel().clear()
self.view.set_read_only(True)
class DescriptionOutputPanel(object):
_outputPanelName = 'description_output_panel'
_qualifiedOutputPanelName = 'output.description_output_panel'
_outputPanelCreationLock = threading.Lock()
def __init__(self, window):
self._outputPanelCreationLock.acquire(blocking=True, timeout=1)
self._window = window
self._descriptionOutputPanel = self._window.find_output_panel(self._outputPanelName)
if not self._descriptionOutputPanel:
self._descriptionOutputPanel = self._window.create_output_panel(self._outputPanelName, True)
self._descriptionOutputPanel.settings().set("is_description_output_panel", True)
self._outputPanelCreationLock.release()
def show(self, description):
self._window.run_command(
"show_panel",
{
"panel": self._qualifiedOutputPanelName
}
)
self._descriptionOutputPanel.run_command(
"subforce_display_description",
{
"description": description
}
)
def hide(self):
self._window.run_command(
"hide_panel",
{
"panel": self._qualifiedOutputPanelName,
"cancel": True
}
)
class ChangelistManager(object):
def __init__(self, window, perforceWrapper):
self._window = window
self._perforceWrapper = perforceWrapper
self._changelistDescriptionOutputPanel = DescriptionOutputPanel(self._window)
def viewAllChangelists(self, onDoneCallback, includeNew=False, includeDefault=False):
with self._perforceWrapper as p4:
changelists = []
if includeNew:
changelists.append({"change": NEW_CHANGELIST_NAME, "desc": NEW_CHANGELIST_DESCRIPTION})
if includeDefault:
changelists.append({"change": DEFAULT_CHANGELIST_NAME, "desc": DEFAULT_CHANGELIST_DESCRIPTION})
changelists.extend(p4.run_changes("-c", p4.client, "-s", "pending", "-l"))
def onCreatedChangelist(number):
if onDoneCallback and number:
onDoneCallback(number)
SubforceStatusUpdatingEventListener.updateStatus(self._window.active_view())
def onDone(selectedIndex):
self._changelistDescriptionOutputPanel.hide()
selectedChangelistNumber = changelists[selectedIndex]['change'] if selectedIndex >= 0 else None
if selectedChangelistNumber == NEW_CHANGELIST_NAME:
self.createChangelist(onCreatedChangelist)
else:
if onDoneCallback and selectedChangelistNumber:
onDoneCallback(selectedChangelistNumber)
SubforceStatusUpdatingEventListener.updateStatus(self._window.active_view())
def onHighlighted(selectedIndex):
self._changelistDescriptionOutputPanel.show(changelists[selectedIndex]['desc'])
changelistItems = [[changelist['change'], changelist['desc'][:250]] for changelist in changelists]
self._window.show_quick_panel(
changelistItems,
onDone,
sublime.KEEP_OPEN_ON_FOCUS_LOST,
0,
onHighlighted
)
def createChangelist(self, onDoneCallback=None):
return self.editChangelist(None, onDoneCallback)
def editChangelist(self, changelistNumber, onDoneCallback=None):
with self._perforceWrapper as p4:
caption = 'New Changelist Description'
description = '<new changelist>'
if changelistNumber:
changeResult = p4.fetch_change(changelistNumber)
caption = 'Changelist {} Description'.format(changelistNumber)
description = changeResult['Description'].strip()
def onDescriptionEntered(desc):
with self._perforceWrapper as p4:
if changelistNumber:
changeResult = p4.fetch_change(changelistNumber)
changeResult['Description'] = desc
changeResult = p4.save_change(changeResult)[0]
else:
changeResult = p4.fetch_change()
changeResult['Description'] = desc
changeResult['Files'] = []
changeResult = p4.save_change(changeResult)[0]
changeResultRE = r'Change (\d+) (updated|created).'
changeResultMatch = re.match(changeResultRE, changeResult)
assert changeResultMatch and changeResultMatch.group(1).isdigit()
newChangeNumber = changeResultMatch.group(1)
if onDoneCallback:
onDoneCallback(newChangeNumber)
if changelistNumber:
self._window.status_message('Edited changelist ' +
newChangeNumber)
else:
self._window.status_message('Created new changelist ' +
newChangeNumber)
self._window.show_input_panel(caption, description,
onDescriptionEntered, None, None)
def deleteChangelist(self, changelistNumber):
with self._perforceWrapper as p4:
p4.run_change("-d", changelistNumber)
def moveToChangelist(self, changelistNumber, file):
with self._perforceWrapper as p4:
p4.run_reopen("-c", changelistNumber, file)
def checkoutInChangelist(self, changelistNumber, path):
with self._perforceWrapper as p4:
if changelistNumber == DEFAULT_CHANGELIST_NAME:
p4.run_edit(path)
else:
p4.run_edit("-c", changelistNumber, path)
def revertFilesInChangelist(self, changelistNumber):
with self._perforceWrapper as p4:
p4.run_revert("-c", changelistNumber, "//...")
def addToChangelist(self, changelistNumber, file):
with self._perforceWrapper as p4:
if changelistNumber == DEFAULT_CHANGELIST_NAME:
p4.run_add(changelistNumber, file)
else:
p4.run_add("-c", changelistNumber, file)
class SubforceAutoCheckoutEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
if SettingsWrapper().get(DISABLE_AUTO_CHECKOUT_SETTINGS_KEY, False):
return
with PerforceWrapper() as p4:
fileName = view.file_name()
settings = view.settings()
if not fileName or \
settings.get(FILE_NOT_IN_DEPOT_SETTING_KEY, False) or \
settings.get(FILE_CHECKED_OUT_SETTING_KEY, False):
return
# check if file is in depot
if p4.pathUnderRoot(fileName):
stat = p4.run_fstat(fileName)
# check if file is already checked out
if len(stat) > 0 and 'action' in stat[0]:
# Cache this setting, so we don't run fstat unnecessarily
settings.set(FILE_CHECKED_OUT_SETTING_KEY, True)
return
else:
# More caching!
settings.set(FILE_NOT_IN_DEPOT_SETTING_KEY, True)
return
checkoutFile = sublime.ok_cancel_dialog(
"You are saving a file in your depot. Do you want to check it out first?",
"Checkout"
)
if checkoutFile:
# Because Sublime's show_quick_panel is non-blocking, we cannot use it to acquire the user's desired
# changelist before checking out the actual file. Instead, we check out the file first and then move it to
# the user's desired changelist.
p4.run_edit(fileName)
view.settings().set(FILE_CHECKED_OUT_SETTING_KEY, True)
else:
return
moveToChangelist = sublime.ok_cancel_dialog(
"You're file has been checked out in the default changelist. Do you want to move it to another changelist?",
"Move"
)
if moveToChangelist:
view.window().run_command(
"subforce_move_to_changelist",
{
"paths": [fileName]
}
)
@classmethod
def eraseAutoCheckoutEventListenerSettings(self, view):
settings = view.settings()
settings.erase(FILE_CHECKED_OUT_SETTING_KEY)
settings.erase(FILE_NOT_IN_DEPOT_SETTING_KEY)
def on_load(self, view):
self.eraseAutoCheckoutEventListenerSettings(view)
class SubforceStatusUpdatingEventListener(sublime_plugin.EventListener):
# Some of these may be redundant. Meh.
def on_activated_async(self, view):
self.updateStatus(view)
def on_deactivated_async(self, view):
self.updateStatus(view)
def on_post_window_command(self, window, commandName, args):
if commandName.startswith("subforce"):
self.updateStatus(window.active_view())
@classmethod
def updateStatus(self, view):
settings = view.settings()
try:
with PerforceWrapper(squelchErrorAndWarninMessages=True) as p4:
stat = None
# check if file is in depot
if p4.pathUnderRoot(view.file_name()):
stat = p4.run_fstat(view.file_name())
stat = stat[0]
else:
return
if "change" in stat:
view.set_status(
CHANGELIST_NUMBER_STATUS_KEY,
"Changelist Number: {}".format(stat['change'])
)
else:
view.erase_status(CHANGELIST_NUMBER_STATUS_KEY)
except P4.P4Exception: # Squelch all Perforce exceptions
pass
class SubforceLoginCommand(sublime_plugin.WindowCommand):
savedPasswordCharacters = []
def run(self):
def onDone(password):
PerforceWrapper().login("".join(self.savedPasswordCharacters))
def onChange(password):
nextPasswordCharacter = password[len(self.savedPasswordCharacters):]
if len(password) < len(self.savedPasswordCharacters):
self.savedPasswordCharacters.pop()
elif len(password) > len(self.savedPasswordCharacters):
self.savedPasswordCharacters.append(nextPasswordCharacter)
else:
return
hiddenPassword = '*' * len(password)
self.window.show_input_panel(
"Password",
hiddenPassword,
onDone,
onChange,
None
)
self.window.show_input_panel(
"Password",
"",
onDone,
onChange,
None
)
class SubforceSyncCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
with PerforceWrapper() as p4:
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
dirtyOpenFiles = (view.file_name() for window in sublime.windows() for view in window.views() if view.is_dirty())
dirtyFileInSyncPath = False
for dirtyOpenFile in dirtyOpenFiles:
for path in paths:
if os.path.commonprefix([path, dirtyOpenFile]) == path:
dirtyFileInSyncPath = True
break
performSync = not dirtyFileInSyncPath or \
sublime.ok_cancel_dialog("You are about to sync over one or more files with unsaved modifications. Are you sure you want to proceed?")
paths = [ellipsizeIfDirectory(path) for path in paths]
if performSync:
# @TODO: Add a configurable logging system
print("Subforce: syncing\n\t{}".format("\n\t".join(paths)))
p4.run_sync(paths)
class SubforceAddCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
paths = [ellipsizeIfDirectory(path) for path in paths]
def onDoneCallback(selectedChangelistNumber):
print("Subforce: adding\n\t{}\nto changelist {}: ".format("\n\t".join(paths), selectedChangelistNumber))
changelistManager.addToChangelist(selectedChangelistNumber, paths)
changelistManager.viewAllChangelists(onDoneCallback, includeNew=True, includeDefault=True)
class SubforceGetRevisionCommand(sublime_plugin.WindowCommand):
def run(self, paths):
perforceWrapper = PerforceWrapper()
revisionManager = RevisionManager(self.window, perforceWrapper)
with perforceWrapper as p4:
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
path = checkForAndGetSinglePath(paths)
if not path:
return
path = ellipsizeIfDirectory(path)
def onDoneCallback(selectedRevision):
revisionManager.getRevision(selectedRevision, path)
revisionManager.showHaveHeadAndFileRevisions(path, onDoneCallback)
class SubforceCheckoutCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
paths = [ellipsizeIfDirectory(path) for path in paths]
def onDoneCallback(selectedChangelistNumber):
print("Subforce: checking out\n\t{}\nin changelist {}: ".format("\n\t".join(paths), selectedChangelistNumber))
changelistManager.checkoutInChangelist(selectedChangelistNumber, paths)
changelistManager.viewAllChangelists(onDoneCallback, includeNew=True, includeDefault=True)
class SubforceRevertCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
with PerforceWrapper() as p4:
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
ellipsizedPaths = [ellipsizeIfDirectory(path) for path in paths]
print("Subforce: reverting\n\t{}".format("\n\t".join(ellipsizedPaths)))
p4.run_revert(ellipsizedPaths)
self._resetAutoCheckoutEventListenerSettingsForAllViews(paths)
def _resetAutoCheckoutEventListenerSettingsForAllViews(self, paths):
for path in paths:
for view in getAllViewsForPath(path):
SubforceAutoCheckoutEventListener.eraseAutoCheckoutEventListenerSettings(view)
class SubforceRenameCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
with perforceWrapper as p4:
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
path = checkForAndGetSinglePath(paths)
if not path:
return
path = ellipsizeIfDirectory(path)
if not p4.pathUnderRoot(path):
requiresCheckout = True
else:
requiresCheckout = False
if requiresCheckout and not \
sublime.ok_cancel_dialog(
"File must be checked out before it can be renamed. Do you want to check it out now?",
"Checkout"
):
return
def renameFile(file):
def onDoneRenameCallback(newFileName):
with perforceWrapper as p4: # necessary because the callback runs in a different thread
p4.run_rename(file, newFileName)
self.window.show_input_panel(
"New File Name",
file,
onDoneRenameCallback,
None,
None
)
if requiresCheckout:
def onDoneViewingChangelistsCallback(selectedChangelistNumber):
changelistManager.checkoutInChangelist(selectedChangelistNumber, path)
renameFile(path)
changelistManager.viewAllChangelists(onDoneViewingChangelistsCallback, includeNew=True, includeDefault=True)
else:
renameFile(path)
class SubforceViewChangelistsCommand(sublime_plugin.WindowCommand):
def run(self):
perforceWrapper = PerforceWrapper()
ChangelistManager(self.window, perforceWrapper).viewAllChangelists(None)
class SubforceCreateChangelistCommand(sublime_plugin.WindowCommand):
def run(self):
perforceWrapper = PerforceWrapper()
ChangelistManager(self.window, perforceWrapper).createChangelist()
class SubforceEditChangelistCommand(sublime_plugin.WindowCommand):
def run(self):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
def onDoneCallback(selectedChangelistNumber):
print("Subforce: editing {}".format(selectedChangelistNumber))
changelistManager.editChangelist(selectedChangelistNumber)
changelistManager.viewAllChangelists(onDoneCallback)
class SubforceDeleteChangelistCommand(sublime_plugin.WindowCommand):
def run(self):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
def onDoneCallback(selectedChangelistNumber):
print("Subforce: deleting {}".format(selectedChangelistNumber))
changelistManager.deleteChangelist(selectedChangelistNumber)
changelistManager.viewAllChangelists(onDoneCallback)
class SubforceMoveToChangelistCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
paths = [ellipsizeIfDirectory(path) for path in paths]
def onDoneCallback(selectedChangelistNumber):
print("Subforce: moving\n\t{}\nto changelist {}".format("\n\t".join(paths), selectedChangelistNumber))
changelistManager.moveToChangelist(selectedChangelistNumber, paths)
changelistManager.viewAllChangelists(onDoneCallback, includeNew=True, includeDefault=True)
class SubforceRevertFilesInChangelistCommand(sublime_plugin.WindowCommand):
def run(self):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
def onDoneCallback(selectedChangelistNumber):
print("Subforce: reverting files in {}".format(selectedChangelistNumber))
changelistManager.revertFilesInChangelist(selectedChangelistNumber)
changelistManager.viewAllChangelists(onDoneCallback)
def executeP4VCCommand(command, *args):
with PerforceWrapper() as p4:
command = " ".join(["p4vc.exe", command] + list(args))
print("Subforce: executing p4vc command '{}'".format(command))
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=p4.cwd)
stdout, stderr = process.communicate()
if stdout:
print(stdout)
if stderr:
print(stderr)
def executeP4VCommand(command, *args):
# See: https://www.perforce.com/blog/vcs/p4v-secrets-calling-p4v-command-line
with PerforceWrapper() as p4:
command = 'p4v.exe -p {} -c {} -u {} -cmd "{}"'.format(
p4.port,
p4.client,
p4.user,
' '.join([command] + list(args))
)
print("Subforce: executing p4v command '{}'".format(command))
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=p4.cwd)
class SubforceViewRevisionHistoryCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
for path in paths:
executeP4VCommand("history", path)
class SubforceViewTimelapseCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
for path in paths:
executeP4VCommand("annotate", path)
class SubforceSubmitChangelistCommand(sublime_plugin.WindowCommand):
def run(self):
perforceWrapper = PerforceWrapper()
changelistManager = ChangelistManager(self.window, perforceWrapper)
def onDoneCallback(selectedChangelistNumber):
if selectedChangelistNumber:
executeP4VCCommand("submit", "-c", selectedChangelistNumber)
changelistManager.viewAllChangelists(onDoneCallback)
class SubforceResolveCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
executeP4VCCommand("resolve", " ".join(paths))
class RevisionManager:
def __init__(self, window, perforceWrapper):
self._window = window
self._perforceWrapper = perforceWrapper
self._revisionDescriptionOutputPanel = DescriptionOutputPanel(self._window)
self._callbackDepth = 0
def diffClientFileAgainstDepotRevision(self, revision, file):
with self._perforceWrapper as p4:
depotFilePath = p4.run_fstat(file)[0]['depotFile']
temporaryDepotFilePath = self._createTemporaryDepotFile(depotFilePath, revision)
self._startP4MergeThread(
temporaryDepotFilePath,
file,
getRevisionQualifiedDepotPath(depotFilePath, revision),
"{} (workspace file)".format(file)
)
def diffDepotRevisions(self, revision1, revision2, file):
with self._perforceWrapper as p4:
(revision1, revision2) = sorted([revision1, revision2]) # ensures the most recent revision is on the right
depotFilePath = p4.run_fstat(file)[0]['depotFile']
temporaryDepotFilePath1 = self._createTemporaryDepotFile(depotFilePath, revision1)
temporaryDepotFilePath2 = self._createTemporaryDepotFile(depotFilePath, revision2)
self._startP4MergeThread(
temporaryDepotFilePath1,
temporaryDepotFilePath2,
getRevisionQualifiedDepotPath(depotFilePath, revision1),
getRevisionQualifiedDepotPath(depotFilePath, revision2)
)
def showHaveHeadRevisions(self, onDoneCallback):
revisions = [{'revision': HAVE_REVISION_NAME, 'desc': HAVE_REVISION_DESCRIPTION}, {'revision': HEAD_REVISION_NAME, 'desc': HEAD_REVISION_DESCRIPTION}]
self._showRevisions(revisions, onDoneCallback)
def showHaveHeadAndFileRevisions(self, file, onDoneCallback):
with self._perforceWrapper as p4:
revisions = [createRevision(HAVE_REVISION_NAME, HAVE_REVISION_DESCRIPTION), createRevision(HEAD_REVISION_NAME, HEAD_REVISION_DESCRIPTION)]
revisions.extend(
[
createRevision(str(revision.rev), revision.desc)
for revision in p4.run_filelog("-l", file)[0].revisions
]
)
self._showRevisions(revisions, onDoneCallback)
def getRevision(self, revision, file):
with self._perforceWrapper as p4:
depotFilePath = p4.run_fstat(file)[0]['depotFile']
p4.run_sync(getRevisionQualifiedDepotPath(depotFilePath, revision))
def _showRevisions(self, revisions, onDoneCallback):
self._callbackDepth += 1
def onDone(selectedIndex):
selectedRevision = revisions[selectedIndex]['revision'] if selectedIndex >= 0 else None
if onDoneCallback and selectedRevision:
onDoneCallback(selectedRevision)
if self._callbackDepth == 1: # last one out turns off the lights.
self._revisionDescriptionOutputPanel.hide()
self._callbackDepth -= 1
def onHighlighted(selectedIndex):
self._revisionDescriptionOutputPanel.show(revisions[selectedIndex]['desc'])
revisionItems = [[revision['revision'], revision['desc'][:250]] for revision in revisions]
self._window.show_quick_panel(
revisionItems,
onDone,
sublime.KEEP_OPEN_ON_FOCUS_LOST,
0,
onHighlighted
)
def _startP4MergeThread(self, leftFile, rightFile, leftFileAlias, rightFileAlias):
def target():
command = ["p4merge.exe", '-nl', leftFileAlias, '-nr', rightFileAlias, leftFile, rightFile]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if stdout:
print(stdout)
if stderr:
print(stderr)
threading.Thread(target=target).start()
def _createTemporaryDepotFile(self, file, revision):
with self._perforceWrapper as p4:
# @TODO: At some point in time, we may want to create temporary files with the same naming convention as p4v.
with tempfile.NamedTemporaryFile(prefix="subforce_", delete=False) as temporaryFile:
depotFilePath = getRevisionQualifiedDepotPath(file, revision)
depotFileText = p4.run_print(depotFilePath)[1]
temporaryFile.write(bytes(depotFileText, 'UTF-8'))
return temporaryFile.name
class SubforceViewGraphicalDiffWorkspaceFileCommand(sublime_plugin.WindowCommand):
'''
Diffs one or more files against a depot revision.
A single file may be diffed against any revision.
Multiple files may only be diffed against the have or head revisions.
'''
def run(self, paths=[]):
perforceWrapper = PerforceWrapper()
revisionManager = RevisionManager(self.window, perforceWrapper)
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
if len(paths) == 1:
path = paths[0]
def onDoneCallback(selectedRevision):
revisionManager.diffClientFileAgainstDepotRevision(selectedRevision, path)
revisionManager.showHaveHeadAndFileRevisions(path, onDoneCallback)
else:
def onDoneCallback(selectedRevision):
for path in paths:
revisionManager.diffClientFileAgainstDepotRevision(selectedRevision, path)
revisionManager.showHaveHeadRevisions(onDoneCallback)
class SubforceViewGraphicalDiffDepotRevisionsCommand(sublime_plugin.WindowCommand):
'''
Diffs two depot revisions of a given file.
Only a single file may be diffed at a time.
'''
def run(self, paths=[]):
perforceWrapper = PerforceWrapper()
revisionManager = RevisionManager(self.window, perforceWrapper)
paths = coercePathsToActiveViewIfNeeded(paths, self.window)
path = checkForAndGetSinglePath(paths)
if not path:
return
def onDoneCallback1(selectedRevision1):
def onDoneCallback2(selectedRevision2):
revisionManager.diffDepotRevisions(selectedRevision1, selectedRevision2, path)
revisionManager.showHaveHeadAndFileRevisions(path, onDoneCallback2)
revisionManager.showHaveHeadAndFileRevisions(path, onDoneCallback1)