oops: fixed git reset usage
[ugit.git] / ugit / controllers / createbranch.py
blob0d36594d0a9a4d25183232ac6d01d12e12558563
1 #!/usr/bin/env python
2 import os
3 from PyQt4.QtGui import QDialog
5 from ugit import utils
6 from ugit import qtutils
7 from ugit.views import CreateBranchView
8 from ugit.qobserver import QObserver
10 def create_new_branch(model,parent):
11 model = model.clone()
12 view = CreateBranchView(parent)
13 ctl = CreateBranchController(model, view)
14 view.show()
15 return view.exec_() == QDialog.Accepted
17 class CreateBranchController(QObserver):
18 def __init__(self, model, view):
19 QObserver.__init__(self, model, view)
20 self.add_observables('revision', 'local_branch')
21 self.add_callbacks(
22 branch_list = self.item_changed,
23 create_button = self.create_branch,
24 local_radio = self.display_model,
25 remote_radio = self.display_model,
26 tag_radio = self.display_model,
28 self.display_model()
30 ######################################################################
31 # Qt callbacks
33 def create_branch(self):
34 '''This callback is called when the "Create Branch"
35 button is called.'''
37 revision = self.model.get_revision()
38 branch = self.model.get_local_branch()
39 existing_branches = self.model.get_local_branches()
41 if not branch or not revision:
42 qtutils.information(self.view,
43 self.tr('Missing Data'),
44 self.tr('Please provide both a branch'
45 + ' name and revision expression.' ))
46 return
48 check_branch = False
49 if branch in existing_branches:
51 if self.view.no_update_radio.isChecked():
52 msg = self.tr("Branch '%s' already exists.")
53 msg = unicode(msg) % branch
54 qtutils.information(self.view,
55 self.tr('warning'), msg)
56 return
58 # Whether we should prompt the user for lost commits
59 commits = self.model.rev_list_range(revision, branch)
60 check_branch = bool(commits)
62 if check_branch:
63 msg = self.tr("Resetting '%s' to '%s' will "
64 "lose the following commits:")
65 lines = [ unicode(msg) % (branch, revision) ]
67 for idx, commit in enumerate(commits):
68 subject = commit[1][0:min(len(commit[1]),16)]
69 if len(subject) < len(commit[1]):
70 subject += '...'
71 lines.append('\t' + commit[0][:8]
72 +'\t' + subject)
73 if idx >= 5:
74 skip = len(commits) - 5
75 lines.append('\t(%d skipped)' % skip)
76 break
78 lines.extend([
79 unicode(self.tr("Recovering lost commits may "
80 "not be easy.")),
81 unicode(self.tr("Reset '%s'?")) % branch
84 result = qtutils.question(self.view,
85 self.tr('warning'), '\n'.join(lines))
87 if not result: return
89 # TODO: Settings for git branch
90 track = self.view.remote_radio.isChecked()
91 fetch = self.view.fetch_checkbox.isChecked()
92 ffwd = self.view.ffwd_only_radio.isChecked()
93 reset = self.view.reset_radio.isChecked()
95 output = self.model.create_branch(branch, revision, track=track)
96 qtutils.show_output(output)
97 self.view.accept()
99 def item_changed(self, *rest):
100 '''This callback is called when the item selection changes
101 in the branch_list.'''
103 qlist = self.view.branch_list
104 ( row, selected ) = qtutils.get_selected_row(qlist)
105 if not selected: return
107 sources = self.__get_branch_sources()
108 rev = sources[row]
110 # Update the model with the selection
111 self.model.set_revision(rev)
113 # Only set the branch name field if we're
114 # branching from a remote branch.
115 if not self.view.remote_radio.isChecked():
116 return
118 branch = utils.basename(rev)
119 if branch == 'HEAD': return
121 self.model.set_local_branch(branch)
123 ######################################################################
125 def display_model(self):
126 branches = self.__get_branch_sources()
127 qtutils.set_items(self.view.branch_list, branches)
129 def __get_branch_sources(self):
130 '''Get the list of items for populating the branch root list.'''
131 if self.view.local_radio.isChecked():
132 return self.model.get_local_branches()
133 elif self.view.remote_radio.isChecked():
134 return self.model.get_remote_branches()
135 elif self.view.tag_radio.isChecked():
136 return self.model.get_tags()