cola: add more documentation strings to the cola modules
[git-cola.git] / cola / controllers / stash.py
blob333329a7971f5784c03edf6fd7190a2e5ac99f12
1 """This controller handles the stash dialog."""
4 import os
6 from cola import utils
7 from cola import qtutils
8 from cola.qobserver import QObserver
9 from cola.views import StashView
11 def stash(model, parent):
12 model = model.clone()
13 model.create( stash_list=[], stash_revids=[] )
14 view = StashView(parent)
15 ctl = StashController(model, view)
16 view.show()
18 class StashController(QObserver):
19 def init (self, model, view):
20 self.add_observables('stash_list')
21 self.add_callbacks(button_stash_show = self.stash_show,
22 button_stash_apply = self.stash_apply,
23 button_stash_drop = self.stash_drop,
24 button_stash_clear = self.stash_clear,
25 button_stash_save = self.stash_save)
26 self.update_model()
28 def update_model(self):
29 self.model.set_stash_list(self.model.parse_stash_list())
30 self.model.set_stash_revids(self.model.parse_stash_list(revids=True))
31 self.refresh_view()
33 def get_selected_stash(self):
34 list_widget = self.view.stash_list
35 stash_list = self.model.get_stash_revids()
36 return qtutils.get_selected_item(list_widget, stash_list)
38 def stash_save(self):
39 if not qtutils.question(self.view,
40 self.tr('Stash Changes?'),
41 self.tr('This will stash your current '
42 'changes away for later use.\n'
43 'Continue?')):
44 return
46 stash_name, ok = qtutils.input(self.tr('Enter a name for this stash'))
47 if not ok:
48 return
49 while stash_name in self.model.get_stash_list():
50 qtutils.information(self.tr("Oops!"),
51 self.tr('That name already exists. '
52 'Please enter another name.'))
53 stash_name, ok = qtutils.input(self.tr("Enter a name for this stash"))
54 if not ok:
55 return
57 if not stash_name:
58 return
60 # Sanitize our input, just in case
61 stash_name = utils.sanitize_input(stash_name)
62 qtutils.log(self.model.git.stash('save', stash_name),
63 quiet=False,
64 doraise=True)
65 self.view.accept()
67 def stash_show(self):
68 """Shows the current stash in the main view."""
69 selection = self.get_selected_stash()
70 if not selection:
71 return
72 diffstat = self.model.git.stash('show', selection)
73 diff = self.model.stash('show', '-p', selection)
74 self.view.parent_view.display('%s\n\n%s' % (diffstat, diff))
76 def stash_apply(self):
77 selection = self.get_selected_stash()
78 if not selection:
79 return
80 (status, stdout, stderr) =\
81 self.model.git.stash('apply', selection,
82 with_extended_output=True)
83 qtutils.log(stdout + stderr,
84 quiet=False,
85 doraise=True)
86 self.view.accept()
88 def stash_drop(self):
89 selection = self.get_selected_stash()
90 if not selection:
91 return
92 if not qtutils.question(self.view,
93 self.tr('Drop Stash?'),
94 self.tr('This will permanently remove the '
95 'selected stash.\n'
96 'Recovering these changes may not '
97 'be possible.\n\n'
98 'Continue?')):
99 return
100 qtutils.log(self.model.git.stash('drop', selection),
101 quiet=False,
102 doraise=True)
103 self.update_model()
105 def stash_clear(self):
106 if not qtutils.question(self.view,
107 self.tr('Drop All Stashes?'),
108 self.tr('This will permanently remove '
109 'ALL stashed changes.\n'
110 'Recovering these changes may not '
111 'be possible.\n\n'
112 'Continue?')):
113 return
114 self.model.git.stash('clear'),
115 self.update_model()