A few more i18n fixes
[ugit.git] / ugitlibs / createbranchcontroller.py
blob256130845c77399960c5e9072f82874b3f7cbaea
1 #!/usr/bin/env python
2 import os
3 import cmds
4 import utils
5 import qtutils
6 from qobserver import QObserver
8 class GitCreateBranchController(QObserver):
9 def __init__(self, model, view):
10 QObserver.__init__(self, model, view)
12 self.model_to_view('revision', 'revisionLine')
13 self.model_to_view('local_branch', 'branchLine')
14 self.add_signals('textChanged(const QString&)',
15 view.revisionLine, view.branchLine)
17 self.add_signals('itemSelectionChanged()', view.branchRootList)
18 self.add_signals('released()',
19 view.createBranchButton,
20 view.localBranchRadio, view.remoteBranchRadio,
21 view.tagRadio)
23 self.add_callbacks({
24 'branchRootList': self.item_changed,
25 'createBranchButton': self.create_branch,
26 'localBranchRadio': self.__display_model,
27 'remoteBranchRadio': self.__display_model,
28 'tagRadio': self.__display_model,
31 model.init_branch_data()
32 self.__display_model()
34 ######################################################################
35 # Qt callbacks
37 def create_branch(self):
38 '''This callback is called when the "Create Branch"
39 button is called.'''
41 revision = self.model.get_revision()
42 branch = self.model.get_local_branch()
43 existing_branches = cmds.git_branch()
45 if not branch or not revision:
46 qtutils.information(self.view,
47 self.tr('Missing Data'),
48 self.tr('Please provide both a branch'
49 + ' name and revision expression.' ))
50 return
52 check_branch = False
53 if branch in existing_branches:
55 if self.view.noUpdateRadio.isChecked():
56 msg = self.tr("Branch '%s' already exists.")
57 msg = unicode(msg) % branch
58 qtutils.information(self.view,
59 self.tr('warning'), msg)
60 return
62 # Whether we should prompt the user for lost commits
63 commits = cmds.git_rev_list_range(revision, branch)
64 check_branch = bool(commits)
66 if check_branch:
67 msg = self.tr("Resetting '%s' to '%s' will lose the following commits:")
68 lines = [ unicode(msg) % (branch, revision) ]
70 for idx, commit in enumerate(commits):
71 subject = commit[1][0:min(len(commit[1]),16)]
72 if len(subject) < len(commit[1]):
73 subject += '...'
74 lines.append('\t' + commit[0][:8]
75 +'\t' + subject)
76 if idx >= 5:
77 skip = len(commits) - 5
78 lines.append('\t(%d skipped)' % skip)
79 break
81 lines.extend([
82 unicode(self.tr("Recovering lost commits may not be easy.")),
83 unicode(self.tr("Reset '%s'?")) % branch
86 result = qtutils.question(self.view,
87 self.tr('warning'), '\n'.join(lines))
89 if not result: return
91 # TODO: Settings for git branch
92 track = self.view.remoteBranchRadio.isChecked()
93 fetch = self.view.fetchCheckBox.isChecked()
94 ffwd = self.view.fastForwardUpdateRadio.isChecked()
95 reset = self.view.resetRadio.isChecked()
97 output = cmds.git_create_branch(branch, revision, track=track)
98 qtutils.show_command(self.view, output)
99 self.view.accept()
101 def item_changed(self):
102 '''This callback is called when the item selection changes
103 in the branchRootList.'''
105 qlist = self.view.branchRootList
106 ( row, selected ) = qtutils.get_selected_row(qlist)
107 if not selected: return
109 sources = self.__get_branch_sources()
110 rev = sources[row]
112 # Update the model with the selection
113 self.model.set_revision(rev)
115 # Only set the branch name field if we're
116 # branching from a remote branch.
117 if not self.view.remoteBranchRadio.isChecked():
118 return
120 branch = utils.basename(rev)
121 if branch == 'HEAD': return
123 self.model.set_local_branch(branch)
125 ######################################################################
127 def __display_model(self):
128 '''Visualize the current state of the model.'''
129 branch_sources = self.__get_branch_sources()
130 self.view.branchRootList.clear()
131 for branch_source in branch_sources:
132 self.view.branchRootList.addItem(branch_source)
134 def __get_branch_sources(self):
135 '''Get the list of items for populating the branch root list.'''
137 if self.view.localBranchRadio.isChecked():
138 return self.model.get_local_branches()
140 elif self.view.remoteBranchRadio.isChecked():
141 return self.model.get_remote_branches()
143 elif self.view.tagRadio.isChecked():
144 return self.model.get_tags()