Added commit message menu, other fixes
[ugit.git] / ugitlibs / createbranchcontroller.py
blob1e7982cd71988108eb373bdae3dd537af857c4e1
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 model.init_branch_data()
40 self.__display_model(model)
42 ######################################################################
43 # CALLBACKS
44 ######################################################################
46 def cb_create_branch(self, model):
47 '''This callback is called when the "Create Branch"
48 button is called.'''
50 revision = model.get_revision()
51 branch = model.get_branch()
52 existing_branches = cmds.git_branch()
54 if not branch or not revision:
55 qtutils.information(self.view,
56 'Missing Data',
57 ('Please provide both a branch name and '
58 + 'revision expression.' ))
59 return
61 check_branch = False
62 if branch in existing_branches:
64 if self.view.noUpdateRadio.isChecked():
65 qtutils.information(self.view,
66 'Warning: Branch Already Exists...',
67 ('The "' + branch + '"'
68 + ' branch already exists and '
69 + '"Update Existing Branch?" = "No."'))
70 return
72 # Whether we should prompt the user for lost commits
73 commits = cmds.git_rev_list_range(revision, branch)
74 check_branch = bool(commits)
76 if check_branch:
77 lines = []
78 for commit in commits:
79 lines.append(commit[0][:8] +'\t'+ commit[1])
81 lost_commits = '\n\t'.join(lines)
83 result = qtutils.question(self.view,
84 'Warning: Commits Will Be Lost...',
85 ('Updating the '
86 + branch + ' branch will lose the '
87 + 'following commits:\n\n\t'
88 + lost_commits + '\n\n'
89 + 'Continue anyways?'))
91 if not result: return
93 # TODO: Settings for git branch
94 track = self.view.remoteBranchRadio.isChecked()
95 fetch = self.view.fetchCheckBox.isChecked()
96 ffwd = self.view.fastForwardUpdateRadio.isChecked()
97 reset = self.view.resetRadio.isChecked()
99 output = cmds.git_create_branch(branch, revision, track=track)
100 qtutils.show_command(self.view, output)
101 self.view.accept()
103 def cb_item_changed(self, model):
104 '''This callback is called when the item selection changes
105 in the branchRootList.'''
107 qlist = self.view.branchRootList
108 ( row, selected ) = qtutils.get_selected_row(qlist)
109 if not selected: return
111 sources = self.__get_branch_sources(model)
112 rev = sources[row]
114 # Update the model with the selection
115 model.set_revision(rev)
117 # Only set the branch name field if we're
118 # branching from a remote branch.
119 # Assume that we only want to use
120 # the last part of the branch name so that "origin/master"
121 # becomes just "master." Avoid creating branches named HEAD.
122 if not self.view.remoteBranchRadio.isChecked():
123 return
125 base_regex = re.compile('(.*?/)?([^/]+)$')
126 match = base_regex.match(rev)
127 if match:
128 branch = match.group(2)
129 #branch = os.path.basename(rev)
130 if branch == 'HEAD': return
131 model.set_branch(branch)
133 ######################################################################
134 # PRIVATE HELPER METHODS
135 ######################################################################
137 def __display_model(self, model):
138 '''Visualize the current state of the model.'''
139 branch_sources = self.__get_branch_sources(model)
140 self.view.branchRootList.clear()
141 for branch_source in branch_sources:
142 self.view.branchRootList.addItem(branch_source)
144 def __get_branch_sources(self, model):
145 '''Get the list of items for populating the branch root list.'''
147 if self.view.localBranchRadio.isChecked():
148 return model.get_local_branches()
150 elif self.view.remoteBranchRadio.isChecked():
151 return model.get_remote_branches()
153 elif self.view.tagRadio.isChecked():
154 return model.get_tags()