Make things look nicer on MacOS
[ugit.git] / py / createbranchcontroller.py
blobe387a021cd239dfec75ab8fa5881ed8a890f14e1
1 #!/usr/bin/env python
2 import os
3 import re
4 import cmds
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 (model, 'revision', 'revisionLine')
13 self.model_to_view (model, 'branch', 'branchNameLine')
15 self.add_signals ('textChanged (const QString&)',
16 view.revisionLine,
17 view.branchNameLine)
19 self.add_signals ('itemSelectionChanged()',
20 view.branchRootList)
22 self.add_signals ('released()',
23 view.createBranchButton,
24 view.localBranchRadio,
25 view.remoteBranchRadio,
26 view.tagRadio)
28 self.add_callbacks (model, {
29 'branchRootList': self.cb_item_changed,
30 'createBranchButton': self.cb_create_branch,
31 'localBranchRadio':
32 lambda(m): self.__display_model (m),
33 'remoteBranchRadio':
34 lambda(m): self.__display_model (m),
35 'tagRadio':
36 lambda(m): self.__display_model (m),
39 self.__display_model (model)
41 ######################################################################
42 # CALLBACKS
43 ######################################################################
45 def cb_create_branch (self, model):
46 '''This callback is called when the "Create Branch"
47 button is called.'''
49 revision = model.get_revision()
50 branch = model.get_branch()
51 existing_branches = cmds.git_branch()
53 if not branch or not revision:
54 qtutils.information (self.view,
55 'Missing Data',
56 ('Please provide both a branch name and '
57 + 'revision expression.' ))
58 return
60 check_branch = False
61 if branch in existing_branches:
63 if self.view.noUpdateRadio.isChecked():
64 qtutils.information (self.view,
65 'Warning: Branch Already Exists...',
66 ('The "' + branch + '"'
67 + ' branch already exists and '
68 + '"Update Existing Branch?" = "No."'))
69 return
71 # Whether we should prompt the user for lost commits
72 commits = cmds.git_rev_list_range (revision, branch)
73 check_branch = bool (commits)
75 if check_branch:
76 lines = []
77 for commit in commits:
78 lines.append (commit[0][:8] +'\t'+ commit[1])
80 lost_commits = '\n\t'.join (lines)
82 result = qtutils.question (self.view,
83 'Warning: Commits Will Be Lost...',
84 ('Updating the '
85 + branch + ' branch will lose the '
86 + 'following commits:\n\n\t'
87 + lost_commits + '\n\n'
88 + 'Continue anyways?'))
90 if not result: return
92 # TODO: Settings for git branch
93 track = self.view.remoteBranchRadio.isChecked()
94 fetch = self.view.fetchCheckBox.isChecked()
95 ffwd = self.view.fastForwardUpdateRadio.isChecked()
96 reset = self.view.resetRadio.isChecked()
98 output = cmds.git_create_branch (branch, revision, track=track)
99 qtutils.show_command (self.view, output)
100 self.view.accept()
102 def cb_item_changed (self, model):
103 '''This callback is called when the item selection changes
104 in the branchRootList.'''
106 qlist = self.view.branchRootList
107 ( row, selected ) = qtutils.get_selected_row (qlist)
108 if not selected: return
110 sources = self.__get_branch_sources (model)
111 rev = sources[row]
113 # Update the model with the selection
114 model.set_revision (rev)
116 # Only set the branch name field if we're
117 # branching from a remote branch.
118 # Assume that we only want to use
119 # the last part of the branch name so that "origin/master"
120 # becomes just "master." Avoid creating branches named HEAD.
121 if not self.view.remoteBranchRadio.isChecked():
122 return
124 base_regex = re.compile ('(.*?/)?([^/]+)$')
125 match = base_regex.match (rev)
126 if match:
127 branch = match.group (2)
128 #branch = os.path.basename (rev)
129 if branch == 'HEAD': return
130 model.set_branch (branch)
132 ######################################################################
133 # PRIVATE HELPER METHODS
134 ######################################################################
136 def __display_model (self, model):
137 '''Visualize the current state of the model.'''
138 branch_sources = self.__get_branch_sources (model)
139 self.view.branchRootList.clear()
140 for branch_source in branch_sources:
141 self.view.branchRootList.addItem (branch_source)
143 def __get_branch_sources (self, model):
144 '''Get the list of items for populating the branch root list.'''
146 if self.view.localBranchRadio.isChecked():
147 return model.get_local_branches()
149 elif self.view.remoteBranchRadio.isChecked():
150 return model.get_remote_branches()
152 elif self.view.tagRadio.isChecked():
153 return model.get_tags()