Kiln » TortoiseHg » TortoiseHg
Clone URL:  
tag.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
# tag.py - Tag dialog for TortoiseHg # # Copyright 2010 Yuki KODAMA <endflow.net@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import os import traceback from PyQt4.QtCore import * from PyQt4.QtGui import * from mercurial import hg, ui, error from tortoisehg.util import hglib, paths, i18n, thgrepo from tortoisehg.hgqt.qtlib import getpixmap from tortoisehg.hgqt.i18n import _ from tortoisehg.hgqt import qtlib keep = i18n.keepgettext() class TagDialog(QDialog): tagChanged = pyqtSignal() localTagChanged = pyqtSignal() showMessage = pyqtSignal(str) def __init__(self, repo=None, tag='', rev='tip', parent=None, opts={}): super(TagDialog, self).__init__(parent) self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) self.ui = ui.ui() if repo: self.repo = repo else: root = paths.find_root() if root: self.repo = thgrepo.repository(self.ui, path=root) else: raise 'no repository found' if not tag and rev and rev != 'tip': bmarks = hglib.get_repo_bookmarks(self.repo) for t in self.repo.nodetags(self.repo[rev].node()): if t != 'tip' \ and ((not bmarks) or (bmarks and t not in bmarks)): tag = t break else: tag = '' # base layout box base = QVBoxLayout() base.setSpacing(0) base.setContentsMargins(*(0,)*4) # main layout box box = QVBoxLayout() box.setSpacing(6) box.setContentsMargins(*(6,)*4) base.addLayout(box) ## main layout grid grid = QGridLayout() grid.setSpacing(6) box.addLayout(grid) ### tag combo self.tag_combo = QComboBox() self.tag_combo.setEditable(True) self.tag_combo.setMinimumWidth(180) self.tag_combo.setEditText(tag) self.tag_combo.editTextChanged.connect(self.tag_changed) grid.addWidget(QLabel(_('Tag:')), 0, 0) grid.addWidget(self.tag_combo, 0, 1) ### revision input self.initial_rev = rev self.rev_text = QLineEdit() self.rev_text.setMaximumWidth(100) self.rev_text.setText(rev) self.rev_text.textEdited.connect(lambda s: self.update_sensitives()) grid.addWidget(QLabel(_('Revision:')), 1, 0) grid.addWidget(self.rev_text, 1, 1) ### options expander = qtlib.ExpanderLabel(_('Options'), False) expander.expanded.connect(self.show_options) grid.addWidget(expander, 2, 0, 1, 2, Qt.AlignLeft | Qt.AlignTop) optbox = QVBoxLayout() optbox.setSpacing(6) grid.addLayout(optbox, 3, 0, 1, 2) hbox = QHBoxLayout() hbox.setSpacing(0) optbox.addLayout(hbox) self.local_chk = QCheckBox(_('Local tag')) self.local_chk.toggled.connect(self.local_toggled) self.replace_chk = QCheckBox(_('Replace existing tag (-f/--force)')) self.replace_chk.toggled.connect(lambda b: self.update_sensitives()) optbox.addWidget(self.local_chk) optbox.addWidget(self.replace_chk) self.eng_chk = QCheckBox(_('Use English commit message')) engmsg = self.repo.ui.configbool('tortoisehg', 'engmsg', False) self.eng_chk.setChecked(engmsg) optbox.addWidget(self.eng_chk) self.custom_chk = QCheckBox(_('Use custom commit message:')) self.custom_chk.toggled.connect( lambda e: self.toggle_enabled(e, self.custom_text)) self.custom_text = QLineEdit() optbox.addWidget(self.custom_chk) optbox.addWidget(self.custom_text) ## bottom buttons buttons = QDialogButtonBox() self.close_btn = buttons.addButton(QDialogButtonBox.Close) self.close_btn.clicked.connect(self.reject) self.add_btn = buttons.addButton(_('&Add'), QDialogButtonBox.ActionRole) self.add_btn.clicked.connect(self.add_tag) self.remove_btn = buttons.addButton(_('&Remove'), QDialogButtonBox.ActionRole) self.remove_btn.clicked.connect(self.remove_tag) box.addWidget(buttons) ## horizontal separator self.sep = QFrame() self.sep.setFrameShadow(QFrame.Sunken) self.sep.setFrameShape(QFrame.HLine) base.addWidget(self.sep) ## status line self.status = qtlib.StatusLabel() self.status.setContentsMargins(4, 2, 4, 4) base.addWidget(self.status) # dialog setting self.setLayout(base) self.layout().setSizeConstraint(QLayout.SetFixedSize) reponame = hglib.get_reponame(self.repo) self.setWindowTitle(_('Tag - %s') % hglib.tounicode(reponame)) self.setWindowIcon(qtlib.geticon('tag')) # prepare to show self.custom_text.setDisabled(True) self.clear_statue() self.update_tagcombo(clear=False) self.update_sensitives(affectlocal=True) self.show_options(False) self.tag_combo.setFocus() self.replace_chk.setChecked(bool(opts.get('force'))) self.local_chk.setChecked(bool(opts.get('local'))) if not opts.get('local') and opts.get('message'): self.custom_chk.setChecked(True) self.custom_text.setText(opts.get('message', '')) ### Private Methods ### def update_tagcombo(self, clear=True): """ update display on dialog with recent repo data """ self.repo.thginvalidate() tag_name = self.tag_combo.currentText() self.tag_combo.clear() # add tags to drop-down list tags = list(self.repo.tags()) tags.sort() tags.reverse() for tag in tags: if tag == 'tip': continue self.tag_combo.addItem(hglib.tounicode(tag)) self.tag_combo.clearEditText() # restore tag name if not clear and tag_name: self.tag_combo.setEditText(tag_name) def update_sensitives(self, affectlocal=False): """ update bottom button sensitives based on rev and tag """ tag = self.tag_combo.currentText() rev = self.rev_text.text() if not rev or not tag: self.add_btn.setDisabled(True) self.remove_btn.setDisabled(True) return # check if valid revision try: self.repo[hglib.fromunicode(rev)] except (error.LookupError, error.RepoLookupError, error.RepoError): self.add_btn.setDisabled(True) self.remove_btn.setDisabled(True) return # check tag existence force = self.replace_chk.isChecked() is_exist = hglib.fromunicode(tag) in self.repo.tags() self.add_btn.setEnabled(not is_exist or force) self.remove_btn.setEnabled(is_exist) # check if local is_local = self.repo.tagtype(hglib.fromunicode(tag)) if affectlocal and is_local is not None: self.local_chk.setChecked(is_local == 'local') self.update_revision() def update_revision(self): """ update revision entry based on tag """ tagmap = self.repo.tags() tag = self.tag_combo.currentText() replace = self.replace_chk.isChecked() if not tag or hglib.fromunicode(tag) not in tagmap or replace: if self.initial_rev: self.rev_text.setText(self.initial_rev) return node = tagmap[hglib.fromunicode(tag)] ctx = self.repo[node] self.rev_text.setText(unicode(ctx.rev())) def show_options(self, visible): self.local_chk.setVisible(visible) self.replace_chk.setVisible(visible) self.eng_chk.setVisible(visible) self.custom_chk.setVisible(visible) self.custom_text.setVisible(visible) def set_status(self, text, icon=None): self.status.setShown(True) self.sep.setShown(True) self.status.set_status(text, icon) self.showMessage.emit(text) def clear_statue(self): self.status.setHidden(True) self.sep.setHidden(True) def add_tag(self): local = self.local_chk.isChecked() name = self.tag_combo.currentText() lname = hglib.fromunicode(name) rev = hglib.fromunicode(self.rev_text.text()) force = self.replace_chk.isChecked() english = self.eng_chk.isChecked() message = self.custom_text.text() try: # tagging if lname in self.repo.tags() and not force: raise util.Abort(_("Tag '%s' already exist") % name) ctx = self.repo[rev] node = ctx.node() if not message: msgset = keep._('Added tag %s for changeset %s') message = (english and msgset['id'] or msgset['str']) \ % (name, str(ctx)) if not isinstance(message, str): message = hglib.fromunicode(message) self.repo.tag(lname, node, message, local, None, None) # update UI self.set_status(_("Tag '%s' has been added") % name, True) self.update_tagcombo() self.close_btn.setFocus() self.repo.thginvalidate() if local: self.localTagChanged.emit() else: self.tagChanged.emit() except: self.set_status(_('Error in tagging'), False) print traceback.format_exc() def remove_tag(self): local = self.local_chk.isChecked() name = self.tag_combo.currentText() lname = hglib.fromunicode(name) english = self.eng_chk.isChecked() message = hglib.fromunicode(self.custom_text.text()) try: # tagging tagtype = self.repo.tagtype(lname) if local: if tagtype != 'local': raise util.Abort(_('tag \'%s\' is not a local tag') % lname) else: if tagtype != 'global': raise util.Abort(_('tag \'%s\' is not a global tag') % lname) if not message: msgset = keep._('Removed tag %s') message = (english and msgset['id'] or msgset['str']) % name node = self.repo[-1].node() self.repo.tag(lname, node, message, local, None, None) # update UI self.set_status(_("Tag '%s' has been removed") % name, True) self.update_tagcombo() self.close_btn.setFocus() self.repo.thginvalidate() if local: self.localTagChanged.emit() else: self.tagChanged.emit() except: self.set_status(_('Error in tagging'), False) print traceback.format_exc() ### Signal Handlers ### def local_toggled(self, checked): self.eng_chk.setEnabled(not checked) self.custom_chk.setEnabled(not checked) custom = self.custom_chk.isChecked() self.custom_text.setEnabled(not checked and custom) def tag_changed(self, combo): self.update_revision() self.update_sensitives(True) def toggle_enabled(self, checked, target): target.setEnabled(checked) if checked: target.setFocus() def run(ui, *pats, **opts): kargs = {} tag = len(pats) > 0 and pats[0] or None if tag: kargs['tag'] = tag rev = opts.get('rev') if rev: kargs['rev'] = rev return TagDialog(opts=opts, **kargs)