pull: make pull verbose
[ugit.git] / ugit / controllers / createbranch.py
blob0351af5d269200b70dd13cdf8eec1a49d46c7281
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 self.add_observables('revision', 'local_branch')
20 self.add_callbacks(
21 branch_list = self.item_changed,
22 create_button = self.create_branch,
23 local_radio = self.display_model,
24 remote_radio = self.display_model,
25 tag_radio = self.display_model,
27 self.display_model()
29 #+--------------------------------------------------------------------
30 #+ Qt callbacks
31 def create_branch(self):
32 '''This callback is called when the "Create Branch"
33 button is called.'''
35 revision = self.model.get_revision()
36 branch = self.model.get_local_branch()
37 existing_branches = self.model.get_local_branches()
39 if not branch or not revision:
40 qtutils.information(
41 self.tr('Missing Data'),
42 self.tr('Please provide both a branch'
43 + ' name and revision expression.' ))
44 return
46 check_branch = False
47 if branch in existing_branches:
49 if self.view.no_update_radio.isChecked():
50 msg = self.tr("Branch '%s' already exists.")
51 msg = unicode(msg) % branch
52 qtutils.information( self.tr('warning'), msg )
53 return
55 # Whether we should prompt the user for lost commits
56 commits = self.model.rev_list_range(revision, branch)
57 check_branch = bool(commits)
59 if check_branch:
60 msg = self.tr("Resetting '%s' to '%s' will "
61 "lose the following commits:")
62 lines = [ unicode(msg) % (branch, revision) ]
64 for idx, commit in enumerate(commits):
65 subject = commit[1][0:min(len(commit[1]),16)]
66 if len(subject) < len(commit[1]):
67 subject += '...'
68 lines.append('\t' + commit[0][:8]
69 +'\t' + subject)
70 if idx >= 5:
71 skip = len(commits) - 5
72 lines.append('\t(%d skipped)' % skip)
73 break
75 lines.extend([
76 unicode(self.tr("Recovering lost commits may "
77 "not be easy.")),
78 unicode(self.tr("Reset '%s'?")) % branch
81 result = qtutils.question(self.view,
82 self.tr('warning'), '\n'.join(lines))
84 if not result: return
86 # TODO: Settings for git branch
87 track = self.view.remote_radio.isChecked()
88 fetch = self.view.fetch_checkbox.isChecked()
89 ffwd = self.view.ffwd_only_radio.isChecked()
90 reset = self.view.reset_radio.isChecked()
92 output = self.model.create_branch(branch, revision, track=track)
93 qtutils.show_output(output)
94 self.view.accept()
96 def item_changed(self, *rest):
97 '''This callback is called when the item selection changes
98 in the branch_list.'''
100 qlist = self.view.branch_list
101 ( row, selected ) = qtutils.get_selected_row(qlist)
102 if not selected: return
104 sources = self.__get_branch_sources()
105 rev = sources[row]
107 # Update the model with the selection
108 self.model.set_revision(rev)
110 # Only set the branch name field if we're
111 # branching from a remote branch.
112 if not self.view.remote_radio.isChecked():
113 return
115 branch = utils.basename(rev)
116 if branch == 'HEAD': return
118 self.model.set_local_branch(branch)
120 ######################################################################
122 def display_model(self):
123 branches = self.__get_branch_sources()
124 qtutils.set_items(self.view.branch_list, branches)
126 def __get_branch_sources(self):
127 '''Get the list of items for populating the branch root list.'''
128 if self.view.local_radio.isChecked():
129 return self.model.get_local_branches()
130 elif self.view.remote_radio.isChecked():
131 return self.model.get_remote_branches()
132 elif self.view.tag_radio.isChecked():
133 return self.model.get_tags()