-
Notifications
You must be signed in to change notification settings - Fork 3
/
branch_commands.py
292 lines (249 loc) · 9.71 KB
/
branch_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import sublime
import sublime_plugin
import os
import re
from functools import partial
from . import svn_commands
from .lib import util, thread, settings, output, panels
CHERRYPICK_FORMAT = r'^(\-?\d+,?)+$'
REVISIONS_FORMAT = r'^(HEAD|BASE|COMMITTED|PREV|\d+):?(HEAD|BASE|COMMITTED|PREV|\d+)?$'
def nothing(nothing1=None, nothing2=None, nothing3=None, **args):
"""Does nothing, just a placeholder for things I don't handle"""
return
def get_branches():
"""Get the branches listed in the project"""
data = sublime.active_window().project_data()
if not data:
return []
return list(data.get('HypnotoadSVN', {}).get('branches', []))
def add_branch(branch):
"""Adds a branch to the project"""
if branch is None or not util.is_url(branch):
sublime.status_message('Invalid URL')
return False
data = sublime.active_window().project_data()
if data is None:
data = {
'HypnotoadSVN': {
'branches': [branch]
}
}
else:
hypno = data.get('HypnotoadSVN')
if hypno is None:
data['HypnotoadSVN'] = {
'branches': [branch]
}
else:
branches = hypno.get('branches')
if branches is None:
branches = [branch]
else:
if branch in branches:
branches.remove(branch)
branches.insert(0, branch)
hypno['branches'] = branches
sublime.active_window().set_project_data(data)
return True
def picked_branch(on_complete, branch):
"""Moves a branch to the top of the list after being picked"""
if not add_branch(branch):
sublime.error_message('Branch is not a valid URL')
return
on_complete(branch)
def pick_branch(current, on_complete):
"""Picks a branch from the project"""
add_branch(current)
branches = get_branches()
branches.remove(current)
picked = partial(picked_branch, on_complete)
if len(branches) > 0:
panels.SelectOrAdd(branches, picked, add_base=current, input_name='Branch...')
else:
sublime.active_window().show_input_panel('Branch...', current, picked, nothing, nothing)
class HypnoSvnMergeCommand(svn_commands.HypnoSvnCommand):
"""Merges changes from the repo to the working copy"""
def __init__(self, window):
"""Initialize the command object"""
super().__init__(window)
self.svn_name = 'Merge'
self.tests = {
'versionned': True,
'enabled': True,
'single': True
}
self.files = None
self.url = None
self.branch = None
def on_revisions_picked(self, value):
"""Verifies that the revisions are valid format then runs the merge"""
args = value.split(' ')
argserr = []
params = []
for a in args:
if re.match(CHERRYPICK_FORMAT, a):
params.append('-c ' + a)
elif re.match(REVISIONS_FORMAT, a):
params.append('-r ' + a)
elif (a != ''):
argserr.append(a)
if len(argserr) != 0:
sublime.error_message('These revisions argument are not in a valid format:\n ' + '\n '.join(argserr))
return
self.run_command('merge ' + ' '.join(params), [self.branch, self.files[0]])
def pick_revisions(self):
"""Prompts the user for revision numbers"""
sublime.active_window().show_input_panel('Revisions...', '', self.on_revisions_picked, self.nothing, self.nothing)
def on_branch_picked(self, value):
"""Handles picking the branch"""
self.branch = value
self.pick_revisions()
def verify_changes(self, files):
"""If the files are changed, checks with the user to confirm that they really want to merge."""
if self.is_changed(files):
message = 'There are changed files, are you sure you want to merge to this location?'
if self.is_file(files):
message = 'This file has been modified, are you sure you want to merge to this file?'
if not sublime.ok_cancel_dialog(message):
return False
return True
def run(self, paths=None, group=-1, index=-1):
"""Runs the command"""
util.debug(self.svn_name)
files = util.get_files(paths, group, index)
if util.prefer_tortoise('merge'):
self.run_tortoise('merge', files)
return
if not util.use_native():
return
if not self.verify_changes(files):
sublime.status_message('Merge cancelled by user.')
return
self.files = files
self.url = self.get_url(files[0])
pick_branch(self.url, self.on_branch_picked)
class HypnoSvnMergeReintegrateCommand(HypnoSvnMergeCommand):
"""Merges reintegrate changes from branch to its origin"""
def __init__(self, window):
"""Initialize the command object"""
super().__init__(window)
self.svn_name = 'Merge reintegrate'
self.tests = {
'versionned': True,
'native': True,
'single': True
}
self.native_only = 'merge'
self.files = None
self.url = None
def on_branch_picked(self, value):
"""Handles picking the branch"""
self.run_command('merge --reintegrate', [value, self.files[0]])
def run(self, paths=None, group=-1, index=-1):
"""Runs the command"""
util.debug(self.svn_name)
files = util.get_files(paths, group, index)
if not self.verify_changes(files):
sublime.status_message('Merge cancelled by user.')
return
self.files = files
self.url = self.get_url(files[0])
pick_branch(self.url, self.on_branch_picked)
class HypnoSvnSwitchCommand(svn_commands.HypnoSvnCommand):
"""Switches the working copy to a different branch"""
def __init__(self, window):
"""Initialize the command object"""
super().__init__(window)
self.svn_name = 'Switch'
self.tests = {
'versionned': True,
'enabled': True,
'single': True
}
self.files = None
self.url = None
def on_branch_picked(self, value):
"""Handles selecting a value"""
self.run_command('switch', [value, self.files[0]])
def run(self, paths=None, group=-1, index=-1):
"""Runs the command"""
util.debug(self.svn_name)
files = util.get_files(paths, group, index)
if util.prefer_tortoise('switch'):
self.run_tortoise('switch', files)
return
self.files = files
self.url = self.get_url(files[0])
pick_branch(self.url, self.on_branch_picked)
class HypnoSvnSwitchIgnoreAncestryCommand(svn_commands.HypnoSvnCommand):
"""Switches the working copy to a different branch"""
def __init__(self, window):
"""Initialize the command object"""
super().__init__(window)
self.svn_name = 'Switch ignore ancestry'
self.tests = {
'versionned': True,
'native': True,
'single': True
}
self.native_only = 'switch'
self.files = None
self.url = None
def on_branch_picked(self, value):
"""Handles selecting a value"""
self.run_command('switch --ignore-ancestry ', [value, self.files[0]])
def run(self, paths=None, group=-1, index=-1):
"""Runs the command"""
util.debug(self.svn_name)
files = util.get_files(paths, group, index)
self.files = files
self.url = self.get_url(files[0])
pick_branch(self.url, self.on_branch_picked)
class HypnoSvnBranchCommand(svn_commands.HypnoSvnCommand):
"""Creates a new branch"""
def __init__(self, window):
"""Initialize the command object"""
super().__init__(window)
self.svn_name = 'Branch'
self.tests = {
'versionned': True,
'enabled': True,
'single': True
}
self.url = None
self.branch = None
def escape(self, message):
"""Escapes quotes in a commit message."""
return message.replace('"', '\\"')
def on_complete(self, proc):
"""If the branch was successfully created, add it to the list of project branches"""
if proc.returncode != 0:
sublime.error_message('Branch creation failed')
return
add_branch(self.branch)
def on_message_input(self, value):
"""Handles completion of an input panel"""
self.message = value
minSize = settings.get_native('commitMessageSize', 0)
if minSize > 0 and len(value) < minSize:
sublime.status_message('Commit message too short')
return
items = [self.url, self.branch]
self.run_command('copy -m "' + self.escape(self.message) + '"', items, on_complete=self.on_complete)
def on_done_input(self, value):
"""Handles completion of an input panel"""
if value is None or not util.is_url(value):
sublime.status_message('Invalid URL')
return
self.branch = value
sublime.active_window().show_input_panel('Copy message', 'Create branch \'branches/name\'', self.on_message_input, self.nothing, self.nothing)
def run(self, paths=None, group=-1, index=-1):
"""Runs the command"""
util.debug(self.svn_name)
files = util.get_files(paths, group, index)
if util.prefer_tortoise('branch'):
self.run_tortoise('branch', files)
return
self.url = self.get_url(files[0])
add_branch(self.url)
sublime.active_window().show_input_panel('Branch to...', self.url, self.on_done_input, self.nothing, self.nothing)