-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.py
915 lines (748 loc) · 29.7 KB
/
cmds.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
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
import os
import sys
import platform
from cStringIO import StringIO
#from cola import i18n
import core
import errors
import gitcfg
import gitcmds
import utils
import signals
import cmdfactory
#from cola import difftool
#from cola.diffparse import DiffParser
#from cola.models import selection
import notification
_notifier = notification.notifier()
_factory = cmdfactory.factory()
_config = gitcfg.instance()
class BaseCommand(object):
"""Base class for all commands; provides the command pattern"""
def __init__(self):
self.undoable = False
def is_undoable(self):
"""Can this be undone?"""
return self.undoable
def name(self):
"""Return this command's name."""
return self.__class__.__name__
def do(self):
raise NotImplementedError('%s.do() is unimplemented' % self.name())
def undo(self):
raise NotImplementedError('%s.undo() is unimplemented' % self.name())
class Command(BaseCommand):
"""Base class for commands that modify the main model"""
def __init__(self):
"""Initialize the command and stash away values for use in do()"""
# These are commonly used so let's make it easier to write new commands.
BaseCommand.__init__(self)
self.model = git_repo.model()
self.old_diff_text = self.model.diff_text
self.old_filename = self.model.filename
self.old_mode = self.model.mode
self.old_head = self.model.head
self.new_diff_text = self.old_diff_text
self.new_filename = self.old_filename
self.new_head = self.old_head
self.new_mode = self.old_mode
def do(self):
"""Perform the operation."""
self.model.set_diff_text(self.new_diff_text)
self.model.set_filename(self.new_filename)
self.model.set_head(self.new_head)
self.model.set_mode(self.new_mode)
def undo(self):
"""Undo the operation."""
self.model.set_diff_text(self.old_diff_text)
self.model.set_filename(self.old_filename)
self.model.set_head(self.old_head)
self.model.set_mode(self.old_mode)
class AmendMode(Command):
"""Try to amend a commit."""
def __init__(self, amend):
Command.__init__(self)
self.undoable = True
self.skip = False
self.amending = amend
self.old_commitmsg = self.model.commitmsg
if self.amending:
self.new_mode = self.model.mode_amend
self.new_head = 'HEAD^'
self.new_commitmsg = self.model.prev_commitmsg()
return
# else, amend unchecked, regular commit
self.new_mode = self.model.mode_none
self.new_head = 'HEAD'
self.new_diff_text = ''
self.new_commitmsg = self.model.commitmsg
# If we're going back into new-commit-mode then search the
# undo stack for a previous amend-commit-mode and grab the
# commit message at that point in time.
if not _factory.undostack:
return
undo_count = len(_factory.undostack)
for i in xrange(undo_count):
# Find the latest AmendMode command
idx = undo_count - i - 1
cmdobj = _factory.undostack[idx]
if type(cmdobj) is not AmendMode:
continue
if cmdobj.amending:
self.new_commitmsg = cmdobj.old_commitmsg
break
def do(self):
"""Leave/enter amend mode."""
"""Attempt to enter amend mode. Do not allow this when merging."""
if self.amending:
if os.path.exists(self.model.git.git_path('MERGE_HEAD')):
self.skip = True
_notifier.broadcast(signals.amend, False)
_factory.prompt_user(signals.information,
'Oops! Unmerged',
'You are in the middle of a merge.\n'
'You cannot amend while merging.')
return
self.skip = False
_notifier.broadcast(signals.amend, self.amending)
self.model.set_commitmsg(self.new_commitmsg)
Command.do(self)
self.model.update_file_status()
def undo(self):
if self.skip:
return
self.model.set_commitmsg(self.old_commitmsg)
Command.undo(self)
self.model.update_file_status()
class ApplyDiffSelection(Command):
def __init__(self, staged, selected, offset, selection, apply_to_worktree):
Command.__init__(self)
self.staged = staged
self.selected = selected
self.offset = offset
self.selection = selection
self.apply_to_worktree = apply_to_worktree
def do(self):
# The normal worktree vs index scenario
parser = DiffParser(self.model,
filename=self.model.filename,
cached=self.staged,
reverse=self.apply_to_worktree)
status, output = \
parser.process_diff_selection(self.selected,
self.offset,
self.selection,
apply_to_worktree=self.apply_to_worktree)
_notifier.broadcast(signals.log_cmd, status, output)
# Redo the diff to show changes
if self.staged:
diffcmd = DiffStaged([self.model.filename])
else:
diffcmd = Diff([self.model.filename])
diffcmd.do()
self.model.update_file_status()
class ApplyPatches(Command):
def __init__(self, patches):
Command.__init__(self)
patches.sort()
self.patches = patches
def do(self):
diff_text = ''
num_patches = len(self.patches)
orig_head = self.model.git.rev_parse('HEAD')
for idx, patch in enumerate(self.patches):
status, output = self.model.git.am(patch,
with_status=True,
with_stderr=True)
# Log the git-am command
_notifier.broadcast(signals.log_cmd, status, output)
if num_patches > 1:
diff = self.model.git.diff('HEAD^!', stat=True)
diff_text += 'Patch %d/%d - ' % (idx+1, num_patches)
diff_text += '%s:\n%s\n\n' % (os.path.basename(patch), diff)
diff_text += 'Summary:\n'
diff_text += self.model.git.diff(orig_head, stat=True)
# Display a diffstat
self.model.set_diff_text(diff_text)
self.model.update_file_status()
_factory.prompt_user(signals.information,
'Patch(es) Applied',
'%d patch(es) applied:\n\n%s' %
(len(self.patches),
'\n'.join(map(os.path.basename, self.patches))))
class Checkout(Command):
"""
A command object for git-checkout.
'argv' is handed off directly to git.
"""
def __init__(self, argv, checkout_branch=False):
Command.__init__(self)
self.argv = argv
self.checkout_branch = checkout_branch
self.new_diff_text = ''
def do(self):
status, output = self.model.git.checkout(with_stderr=True,
with_status=True, *self.argv)
_notifier.broadcast(signals.log_cmd, status, output)
if self.checkout_branch:
self.model.update_status()
else:
self.model.update_file_status()
class CheckoutBranch(Checkout):
"""Checkout a branch."""
def __init__(self, branch, checkout_branch=True):
Checkout.__init__(self, [branch])
class CherryPick(Command):
"""Cherry pick commits into the current branch."""
def __init__(self, commits):
Command.__init__(self)
self.commits = commits
def do(self):
self.model.cherry_pick_list(self.commits)
self.model.update_file_status()
class ResetMode(Command):
"""Reset the mode and clear the model's diff text."""
def __init__(self):
Command.__init__(self)
self.new_mode = self.model.mode_none
self.new_head = 'HEAD'
self.new_diff_text = ''
def do(self):
Command.do(self)
self.model.update_file_status()
class Commit(ResetMode):
"""Attempt to create a new commit."""
def __init__(self, amend, msg):
ResetMode.__init__(self)
self.amend = amend
self.msg = core.encode(msg)
self.old_commitmsg = self.model.commitmsg
self.new_commitmsg = ''
def do(self):
tmpfile = utils.tmp_filename('commit-message')
status, output = self.model.commit_with_msg(self.msg, tmpfile, amend=self.amend)
if status == 0:
ResetMode.do(self)
self.model.set_commitmsg(self.new_commitmsg)
title = 'Commit: '
else:
title = 'Commit failed: '
_notifier.broadcast(signals.log_cmd, status, title+output)
class Ignore(Command):
"""Add files to .gitignore"""
def __init__(self, filenames):
Command.__init__(self)
self.filenames = filenames
def do(self):
new_additions = ''
for fname in self.filenames:
new_additions = new_additions + fname + '\n'
for_status = new_additions
if new_additions:
if os.path.exists('.gitignore'):
current_list = utils.slurp('.gitignore')
new_additions = new_additions + current_list
utils.write('.gitignore', new_additions)
_notifier.broadcast(signals.log_cmd,
0,
'Added to .gitignore:\n%s' % for_status)
self.model.update_file_status()
class Delete(Command):
"""Simply delete files."""
def __init__(self, filenames):
Command.__init__(self)
self.filenames = filenames
# We could git-hash-object stuff and provide undo-ability
# as an option. Heh.
def do(self):
rescan = False
for filename in self.filenames:
if filename:
try:
os.remove(filename)
rescan=True
except:
_factory.prompt_user(signals.information,
'Error'
'Deleting "%s" failed.' % filename)
if rescan:
self.model.update_file_status()
class DeleteBranch(Command):
"""Delete a git branch."""
def __init__(self, branch):
Command.__init__(self)
self.branch = branch
def do(self):
status, output = self.model.delete_branch(self.branch)
title = ''
if output.startswith('error:'):
output = 'E' + output[1:]
else:
title = 'Info: '
_notifier.broadcast(signals.log_cmd, status, title + output)
class Diff(Command):
"""Perform a diff and set the model's current text."""
def __init__(self, filenames, cached=False):
Command.__init__(self)
# Guard against the list of files being empty
if not filenames:
return
opts = {}
if cached:
opts['ref'] = self.model.head
self.new_filename = filenames[0]
self.old_filename = self.model.filename
self.new_mode = self.model.mode_worktree
self.new_diff_text = gitcmds.diff_helper(filename=self.new_filename,
cached=cached, **opts)
class Diffstat(Command):
"""Perform a diffstat and set the model's diff text."""
def __init__(self):
Command.__init__(self)
diff = self.model.git.diff(self.model.head,
unified=_config.get('diff.context', 3),
no_color=True,
M=True,
stat=True)
self.new_diff_text = core.decode(diff)
self.new_mode = self.model.mode_worktree
class DiffStaged(Diff):
"""Perform a staged diff on a file."""
def __init__(self, filenames):
Diff.__init__(self, filenames, cached=True)
self.new_mode = self.model.mode_index
class DiffStagedSummary(Command):
def __init__(self):
Command.__init__(self)
diff = self.model.git.diff(self.model.head,
cached=True,
no_color=True,
patch_with_stat=True,
M=True)
self.new_diff_text = core.decode(diff)
self.new_mode = self.model.mode_index
class Difftool(Command):
"""Run git-difftool limited by path."""
def __init__(self, staged, filenames):
Command.__init__(self)
self.staged = staged
self.filenames = filenames
def do(self):
if not self.filenames:
return
args = []
if self.staged:
args.append('--cached')
if self.model.head != 'HEAD':
args.append(self.model.head)
args.append('--')
args.extend(self.filenames)
difftool.launch(args)
class Edit(Command):
"""Edit a file using the configured gui.editor."""
def __init__(self, filenames, line_number=None):
Command.__init__(self)
self.filenames = filenames
self.line_number = line_number
def do(self):
filename = self.filenames[0]
if not os.path.exists(filename):
return
editor = self.model.editor()
if editor == 'gvim' and self.line_number:
utils.fork([editor, filename, '+'+self.line_number])
else:
utils.fork([editor, filename])
class FormatPatch(Command):
"""Output a patch series given all revisions and a selected subset."""
def __init__(self, to_export, revs):
Command.__init__(self)
self.to_export = to_export
self.revs = revs
def do(self):
status, output = gitcmds.format_patchsets(self.to_export, self.revs)
_notifier.broadcast(signals.log_cmd, status, output)
class GrepMode(Command):
def __init__(self, txt):
"""Perform a git-grep."""
Command.__init__(self)
self.new_mode = self.model.mode_grep
self.new_diff_text = core.decode(self.model.git.grep(txt, n=True))
class LoadCommitMessage(Command):
"""Loads a commit message from a path."""
def __init__(self, path):
Command.__init__(self)
self.undoable = True
self.path = path
self.old_commitmsg = self.model.commitmsg
self.old_directory = self.model.directory
def do(self):
path = self.path
if not path or not os.path.isfile(path):
raise errors.UsageError('Error: cannot find commit template',
'%s: No such file or directory.' % path)
self.model.set_directory(os.path.dirname(path))
self.model.set_commitmsg(utils.slurp(path))
def undo(self):
self.model.set_commitmsg(self.old_commitmsg)
self.model.set_directory(self.old_directory)
class LoadCommitTemplate(LoadCommitMessage):
"""Loads the commit message template specified by commit.template."""
def __init__(self):
LoadCommitMessage.__init__(self, _config.get('commit.template'))
def do(self):
if self.path is None:
raise errors.UsageError('Error: unconfigured commit template',
'A commit template has not been configured.\n'
'Use "git config" to define "commit.template"\n'
'so that it points to a commit template.')
return LoadCommitMessage.do(self)
class LoadPreviousMessage(Command):
"""Try to amend a commit."""
def __init__(self, sha1):
Command.__init__(self)
self.sha1 = sha1
self.old_commitmsg = self.model.commitmsg
self.new_commitmsg = self.model.prev_commitmsg(sha1)
self.undoable = True
def do(self):
self.model.set_commitmsg(self.new_commitmsg)
def undo(self):
self.model.set_commitmsg(self.old_commitmsg)
class Mergetool(Command):
"""Launch git-mergetool on a list of paths."""
def __init__(self, paths):
Command.__init__(self)
self.paths = paths
def do(self):
if not self.paths:
return
utils.fork(['git', 'mergetool', '--no-prompt', '--'] + self.paths)
class OpenRepo(Command):
"""Launches git-cola on a repo."""
def __init__(self, dirname):
Command.__init__(self)
self.new_directory = dirname
def do(self):
self.model.set_directory(self.new_directory)
utils.fork([sys.executable, sys.argv[0], '--repo', self.new_directory])
class Clone(Command):
"""Clones a repository and optionally spawns a new cola session."""
def __init__(self, url, destdir, spawn=True):
Command.__init__(self)
self.url = url
self.new_directory = destdir
self.spawn = spawn
def do(self):
self.model.git.clone(self.url, self.new_directory,
with_stderr=True, with_status=True)
if self.spawn:
utils.fork(['python', sys.argv[0], '--repo', self.new_directory])
rescan = 'rescan'
class Rescan(Command):
"""Rescans for changes."""
def do(self):
self.model.update_status()
rescan_and_refresh = 'rescan_and_refresh'
class RescanAndRefresh(Command):
"""Rescans for changes."""
def do(self):
self.model.update_status(update_index=True)
class RunConfigAction(Command):
"""Run a user-configured action, typically from the "Tools" menu"""
def __init__(self, name):
Command.__init__(self)
self.name = name
self.model = cola.model()
def do(self):
for env in ('FILENAME', 'REVISION', 'ARGS'):
try:
del os.environ[env]
except KeyError:
pass
rev = None
args = None
opts = _config.get_guitool_opts(self.name)
cmd = opts.get('cmd')
if 'title' not in opts:
opts['title'] = cmd
if 'prompt' not in opts or opts.get('prompt') is True:
prompt = i18n.gettext('Are you sure you want to run %s?') % cmd
opts['prompt'] = prompt
if opts.get('needsfile'):
filename = selection.filename()
if not filename:
_factory.prompt_user(signals.information,
'Please select a file',
'"%s" requires a selected file' % cmd)
return
os.environ['FILENAME'] = filename
if opts.get('revprompt') or opts.get('argprompt'):
while True:
ok = _factory.prompt_user(signals.run_config_action, cmd, opts)
if not ok:
return
rev = opts.get('revision')
args = opts.get('args')
if opts.get('revprompt') and not rev:
title = 'Invalid Revision'
msg = 'The revision expression cannot be empty.'
_factory.prompt_user(signals.critical, title, msg)
continue
break
elif opts.get('confirm'):
title = os.path.expandvars(opts.get('title'))
prompt = os.path.expandvars(opts.get('prompt'))
if not _factory.prompt_user(signals.question, title, prompt):
return
if rev:
os.environ['REVISION'] = rev
if args:
os.environ['ARGS'] = args
title = os.path.expandvars(cmd)
_notifier.broadcast(signals.log_cmd, 0, 'running: ' + title)
cmd = ['sh', '-c', cmd]
if opts.get('noconsole'):
status, out, err = utils.run_command(cmd, flag_error=False)
else:
status, out, err = _factory.prompt_user(signals.run_command,
title, cmd)
_notifier.broadcast(signals.log_cmd, status,
'stdout: %s\nstatus: %s\nstderr: %s' %
(out.rstrip(), status, err.rstrip()))
if not opts.get('norescan'):
self.model.update_status()
return status
class SetDiffText(Command):
def __init__(self, text):
Command.__init__(self)
self.undoable = True
self.new_diff_text = text
class ShowUntracked(Command):
"""Show an untracked file."""
# We don't actually do anything other than set the mode right now.
# TODO check the mimetype for the file and handle things
# generically.
def __init__(self, filenames):
Command.__init__(self)
self.new_mode = self.model.mode_untracked
# TODO new_diff_text = utils.file_preview(filenames[0])
class SignOff(Command):
def __init__(self):
Command.__init__(self)
self.undoable = True
self.old_commitmsg = self.model.commitmsg
def do(self):
signoff = self.signoff()
if signoff in self.model.commitmsg:
return
self.model.set_commitmsg(self.model.commitmsg + '\n' + signoff)
def undo(self):
self.model.set_commitmsg(self.old_commitmsg)
def signoff(self):
try:
import pwd
user = pwd.getpwuid(os.getuid()).pw_name
except ImportError:
user = os.getenv('USER', 'unknown')
name = _config.get('user.name', user)
email = _config.get('user.email', '%s@%s' % (user, platform.node()))
return '\nSigned-off-by: %s <%s>' % (name, email)
class Stage(Command):
"""Stage a set of paths."""
def __init__(self, paths):
Command.__init__(self)
self.paths = paths
def do(self):
msg = 'Staging: %s' % (', '.join(self.paths))
_notifier.broadcast(signals.log_cmd, 0, msg)
self.model.stage_paths(self.paths)
class StageModified(Stage):
"""Stage all modified files."""
def __init__(self):
Stage.__init__(self, None)
self.paths = self.model.modified
class StageUnmerged(Stage):
"""Stage all modified files."""
def __init__(self):
Stage.__init__(self, None)
self.paths = self.model.unmerged
class StageUntracked(Stage):
"""Stage all untracked files."""
def __init__(self):
Stage.__init__(self, None)
self.paths = self.model.untracked
class Tag(Command):
"""Create a tag object."""
def __init__(self, name, revision, sign=False, message=''):
Command.__init__(self)
self._name = name
self._message = core.encode(message)
self._revision = revision
self._sign = sign
def do(self):
log_msg = 'Tagging: "%s" as "%s"' % (self._revision, self._name)
opts = {}
if self._message:
opts['F'] = utils.tmp_filename('tag-message')
utils.write(opts['F'], self._message)
if self._sign:
log_msg += ', GPG-signed'
opts['s'] = True
status, output = self.model.git.tag(self._name,
self._revision,
with_status=True,
with_stderr=True,
**opts)
else:
opts['a'] = bool(self._message)
status, output = self.model.git.tag(self._name,
self._revision,
with_status=True,
with_stderr=True,
**opts)
if 'F' in opts:
os.unlink(opts['F'])
if output:
log_msg += '\nOutput:\n%s' % output
_notifier.broadcast(signals.log_cmd, status, log_msg)
if status == 0:
self.model.update_status()
class Unstage(Command):
"""Unstage a set of paths."""
def __init__(self, paths):
Command.__init__(self)
self.paths = paths
def do(self):
msg = 'Unstaging: %s' % (', '.join(self.paths))
_notifier.broadcast(signals.log_cmd, 0, msg)
self.model.unstage_paths(self.paths)
class UnstageAll(Command):
"""Unstage all files; resets the index."""
def do(self):
self.model.unstage_all()
class UnstageSelected(Unstage):
"""Unstage selected files."""
def __init__(self):
Unstage.__init__(self, cola.selection_model().staged)
class Untrack(Command):
"""Unstage a set of paths."""
def __init__(self, paths):
Command.__init__(self)
self.paths = paths
def do(self):
msg = 'Untracking: %s' % (', '.join(self.paths))
_notifier.broadcast(signals.log_cmd, 0, msg)
status, out = self.model.untrack_paths(self.paths)
_notifier.broadcast(signals.log_cmd, status, out)
class UntrackedSummary(Command):
"""List possible .gitignore rules as the diff text."""
def __init__(self):
Command.__init__(self)
untracked = self.model.untracked
suffix = len(untracked) > 1 and 's' or ''
io = StringIO()
io.write('# %s untracked file%s\n' % (len(untracked), suffix))
if untracked:
io.write('# possible .gitignore rule%s:\n' % suffix)
for u in untracked:
io.write('/'+core.encode(u))
self.new_diff_text = core.decode(io.getvalue())
self.new_mode = self.model.mode_untracked
class UpdateFileStatus(Command):
"""Rescans for changes."""
def do(self):
self.model.update_file_status()
class VisualizeAll(Command):
"""Visualize all branches."""
def do(self):
browser = self.model.history_browser()
utils.fork([browser, '--all'])
class VisualizeCurrent(Command):
"""Visualize all branches."""
def do(self):
browser = self.model.history_browser()
utils.fork([browser, self.model.currentbranch])
class VisualizePaths(Command):
"""Path-limited visualization."""
def __init__(self, paths):
Command.__init__(self)
browser = self.model.history_browser()
if paths:
self.argv = [browser] + paths
else:
self.argv = [browser]
def do(self):
utils.fork(self.argv)
visualize_revision = 'visualize_revision'
class VisualizeRevision(Command):
"""Visualize a specific revision."""
def __init__(self, revision, paths=None):
Command.__init__(self)
self.revision = revision
self.paths = paths
def do(self):
argv = [self.model.history_browser()]
if self.revision:
argv.append(self.revision)
if self.paths:
argv.append('--')
argv.extend(self.paths)
utils.fork(argv)
def register():
"""
Register signal mappings with the factory.
These commands are automatically created and run when
their corresponding signal is broadcast by the notifier.
"""
signal_to_command_map = {
signals.amend_mode: AmendMode,
signals.apply_diff_selection: ApplyDiffSelection,
signals.apply_patches: ApplyPatches,
signals.clone: Clone,
signals.checkout: Checkout,
signals.checkout_branch: CheckoutBranch,
signals.cherry_pick: CherryPick,
signals.commit: Commit,
signals.delete: Delete,
signals.delete_branch: DeleteBranch,
signals.diff: Diff,
signals.diff_staged: DiffStaged,
signals.diffstat: Diffstat,
signals.difftool: Difftool,
signals.edit: Edit,
signals.format_patch: FormatPatch,
signals.grep: GrepMode,
signals.ignore: Ignore,
signals.load_commit_message: LoadCommitMessage,
signals.load_commit_template: LoadCommitTemplate,
signals.load_previous_message: LoadPreviousMessage,
signals.modified_summary: Diffstat,
signals.mergetool: Mergetool,
signals.open_repo: OpenRepo,
signals.rescan: Rescan,
signals.rescan_and_refresh: RescanAndRefresh,
signals.reset_mode: ResetMode,
signals.run_config_action: RunConfigAction,
signals.set_diff_text: SetDiffText,
signals.show_untracked: ShowUntracked,
signals.signoff: SignOff,
signals.stage: Stage,
signals.stage_modified: StageModified,
signals.stage_unmerged: StageUnmerged,
signals.stage_untracked: StageUntracked,
signals.staged_summary: DiffStagedSummary,
signals.tag: Tag,
signals.untrack: Untrack,
signals.unstage: Unstage,
signals.unstage_all: UnstageAll,
signals.unstage_selected: UnstageSelected,
signals.untracked_summary: UntrackedSummary,
signals.update_file_status: UpdateFileStatus,
signals.visualize_all: VisualizeAll,
signals.visualize_current: VisualizeCurrent,
signals.visualize_paths: VisualizePaths,
signals.visualize_revision: VisualizeRevision,
}
for signal, cmd in signal_to_command_map.iteritems():
_factory.add_global_command(signal, cmd)