-
Notifications
You must be signed in to change notification settings - Fork 13
/
easy_diff_version_control.py
536 lines (417 loc) · 14.7 KB
/
easy_diff_version_control.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
"""
Easy Diff Version Control.
Copyright (c) 2013 - 2017 Isaac Muse <isaacmuse@gmail.com>
License: MIT
"""
import sublime
import sublime_plugin
import os
from os.path import basename, splitext, join, exists
import EasyDiff.lib.svn as svn
import EasyDiff.lib.git as git
import EasyDiff.lib.hg as hg
from EasyDiff.lib.multiconf import get as multiget
from EasyDiff.easy_diff_global import load_settings, log, debug, get_encoding
from EasyDiff.easy_diff_global import get_external_diff, get_target, notify, get_group_view
import subprocess
import tempfile
SVN_ENABLED = None
GIT_ENABLED = None
HG_ENABLED = None
###############################
# Version Control Base
###############################
class _VersionControlDiff(object):
"""Version control diff base class."""
control_type = ""
temp_folder = None
@property
def control_enabled(self):
"""Get status."""
return False
def get_diff(self, name, **kwargs):
"""Get the diff."""
return None
def is_versioned(self, name):
"""Check if file is versioned."""
return False
def vc_is_enabled(self, name):
"""Check if version control command is enabled."""
enabled = False
if name is not None:
try:
if self.control_enabled is None:
self.check_vc()
enabled = (
self.control_enabled and
(
multiget(load_settings(), "skip_version_check_on_is_enabled", False) or
self.is_versioned(name)
)
)
except Exception:
pass
return enabled
def decode(self, result):
"""Decode with encoding."""
try:
debug("decoding with %s" % self.encoding)
return result.decode(self.encoding)
except Exception:
debug("fallback to utf-8 decode")
return result.decode('utf-8')
def get_encoding(self):
"""Get the encoding."""
return get_encoding(self.view)
def create_temp(self):
"""Create temp folder."""
if self.temp_folder is None:
self.temp_folder = tempfile.mkdtemp(prefix="easydiff")
def get_files(self, name, **kwargs):
"""Get files."""
return None, None
def revert_file(self, name):
"""Revert the file."""
pass
def revert(self, name):
"""Revert control."""
result = self.get_diff(name)
if result == "":
notify("Nothing to Revert")
result = None
if result is not None and sublime.ok_cancel_dialog("Are you sure you want to revert \"%s\"?" % basename(name)):
try:
self.revert_file(name)
except Exception as e:
debug(e)
sublime.error_message("Could not revert \"%s\"!" % basename(name))
def internal_diff(self, name, **kwargs):
"""Diff with internal diff."""
result = self.get_diff(name, **kwargs)
if result == "":
notify("No Difference")
result = None
if result is not None:
use_buffer = bool(load_settings().get("use_buffer", False))
win = sublime.active_window()
if use_buffer:
v = win.new_file()
v.set_name("EasyDiff: %s (%s)" % (self.control_type, basename(name)))
v.set_scratch(True)
v.assign_syntax('Packages/Diff/Diff.tmLanguage')
v.run_command('append', {'characters': result})
else:
v = win.create_output_panel('easy_diff')
v.assign_syntax('Packages/Diff/Diff.tmLanguage')
v.run_command('append', {'characters': result})
win.run_command("show_panel", {"panel": "output.easy_diff"})
def external_diff(self, name, **kwargs):
"""Diff with external diff command."""
self.create_temp()
f1, f2 = self.get_files(name, **kwargs)
ext_diff = get_external_diff()
if f1 is not None and f2 is not None:
subprocess.Popen(
[
ext_diff,
f1,
f2
]
)
def is_loaded(self):
"""Check if view is loaded."""
if self.view.is_loading():
sublime.set_timeout(self.is_loaded, 100)
else:
self.diff()
def vc_run(self, **kwargs):
"""Run version control command."""
self.kwargs = kwargs
sublime.set_timeout(self.is_loaded, 100)
def diff(self):
"""Diff."""
name = self.view.file_name() if self.view is not None else None
self.encoding = self.get_encoding()
if name is not None:
if self.kwargs.get("revert"):
self.revert(name)
else:
external = self.kwargs.get("external", False)
if not external:
self.internal_diff(name, **self.kwargs)
else:
self.external_diff(name, **self.kwargs)
class _VersionControlCommand(sublime_plugin.WindowCommand):
"""Version control base command."""
def run(self, paths=[], group=-1, index=-1, **kwargs):
"""Run command."""
if len(paths):
name = get_target(paths)
elif index != -1:
self.view = get_group_view(sublime.active_window(), group, index)
name = self.view.file_name()
else:
self.view = self.window.active_view()
if self.view is None:
return False
name = self.view.file_name() if self.view is not None else None
if name is None:
return False
if len(paths):
self.view = self.window.open_file(name)
self.vc_run(**kwargs)
def is_enabled(self, paths=[], group=-1, index=-1, **kwargs):
"""Check if command is enabled."""
if len(paths) or index != -1:
name = get_target(paths, group, index)
else:
self.view = self.window.active_view()
if self.view is None:
return False
name = self.view.file_name() if self.view is not None else None
if name is None:
return False
return self.vc_is_enabled(name)
###############################
# Version Control Specific Classes
###############################
class _EasyDiffSvn(_VersionControlDiff):
"""SVN command."""
def setup(self):
"""Setup important variables."""
self.control_type = "SVN"
@property
def control_enabled(self):
"""Check status."""
return SVN_ENABLED
def check_vc(self):
"""Check if version control is available."""
global SVN_ENABLED
location = svn.which()
log("svn %s" % location)
if location is not None:
SVN_ENABLED = True
else:
SVN_ENABLED = False
log("svn not found!")
def revert_file(self, name):
"""Revert file."""
svn.revert(name)
def get_files(self, name, **kwargs):
"""Get files."""
f1 = None
f2 = None
if self.is_versioned(name):
f2 = name
root, ext = splitext(basename(name))
rev = None
if kwargs.get("last", False):
rev = "PREV"
else:
rev = "BASE"
for x in range(1000):
file_path = join(self.temp_folder, "%s-r%s-LEFT-%03d%s" % (root, rev, x, ext))
if not os.path.exists(file_path):
f1 = file_path
break
if f1 is not None:
svn.export(f2, f1, rev=rev)
else:
log("View not versioned under SVN!", status=True)
return f1, f2
def is_versioned(self, name):
"""Check if file is versioned."""
disabled = multiget(load_settings(), "svn_disabled", False)
on_disk = exists(name)
return not disabled and on_disk and svn.is_versioned(name)
def get_diff(self, name, **kwargs):
"""Get the diff."""
result = None
if self.is_versioned(name):
result = self.decode(svn.diff(name, last=kwargs.get("last", False))).replace('\r', '')
else:
log("View not versioned under SVN!", status=True)
return result
class _EasyDiffGit(_VersionControlDiff):
"""Git command."""
def setup(self):
"""Setup important variables."""
self.control_type = "GIT"
@property
def control_enabled(self):
"""Check status."""
return GIT_ENABLED
def check_vc(self):
"""Check if version control is available."""
global GIT_ENABLED
location = git.which()
log("git %s" % location)
if location is not None:
GIT_ENABLED = True
else:
GIT_ENABLED = False
log("git not found!")
def revert_file(self, name):
"""Revert the file."""
git.checkout(name)
def get_files(self, name, **kwargs):
"""Get files."""
f1 = None
f2 = None
if self.is_versioned(name):
f2 = name
root, ext = splitext(basename(name))
rev = None
if kwargs.get("last", False):
revs = git.getrevision(f2, 2)
if revs is not None and len(revs) == 2:
rev = revs[1]
else:
rev = "HEAD"
if rev is not None:
for x in range(1000):
file_path = join(self.temp_folder, "%s-r%s-LEFT-%03d%s" % (root, rev, x, ext))
if not os.path.exists(file_path):
f1 = file_path
break
if f1 is not None:
with open(f1, "wb") as f:
bfr = git.show(f2, rev)
if bfr is not None:
f.write(bfr)
else:
f1 = None
else:
log("View not versioned under Git!", status=True)
return f1, f2
def is_versioned(self, name):
"""Check if file is versioned."""
disabled = multiget(load_settings(), "git_disabled", False)
on_disk = exists(name)
return not disabled and on_disk and git.is_versioned(name)
def get_diff(self, name, **kwargs):
"""Get the diff."""
result = None
if git.is_versioned(name):
result = self.decode(
git.diff(
name,
last=kwargs.get("last", False)
)
).replace('\r', '')
else:
log("View not versioned under Git!", status=True)
return result
class _EasyDiffHg(_VersionControlDiff):
"""Mercurial command."""
def setup(self):
"""Setup important variables."""
self.control_type = "HG"
@property
def control_enabled(self):
"""Check status."""
return HG_ENABLED
def check_vc(self):
"""Check if version control is available."""
global HG_ENABLED
location = hg.which()
log("hg %s" % location)
if location is not None:
HG_ENABLED = True
else:
HG_ENABLED = False
log("hg not found!")
def revert_file(self, name):
"""Revert file."""
hg.revert(name)
def get_files(self, name, **kwargs):
"""Get the files."""
f1 = None
f2 = None
if self.is_versioned(name):
f2 = name
root, ext = splitext(basename(name))
rev = None
if kwargs.get("last", False):
revs = hg.getrevision(f2, 2)
if revs is not None and len(revs) == 2:
rev = revs[1]
if not kwargs.get("last", False) or rev is not None:
for x in range(1000):
if rev is not None:
file_path = join(self.temp_folder, "%s-r%s-LEFT-%03d%s" % (root, rev, x, ext))
else:
file_path = join(self.temp_folder, "%s-rBASE-LEFT-%03d%s" % (root, x, ext))
if not os.path.exists(file_path):
f1 = file_path
break
if f1 is not None:
with open(f1, "wb") as f:
bfr = hg.cat(f2, rev)
if bfr is not None:
f.write(bfr)
else:
f1 = None
else:
log("View not versioned under Mercurial!", status=True)
return f1, f2
def is_versioned(self, name):
"""Check if file is versioned."""
disabled = multiget(load_settings(), "hg_disabled", False)
on_disk = exists(name)
return not disabled and on_disk and hg.is_versioned(name)
def get_diff(self, name, **kwargs):
"""Get the diff."""
result = None
if self.is_versioned(name):
result = self.decode(hg.diff(name, last=kwargs.get("last", False))).replace('\r', '')
else:
log("View not versioned under Mercurial!", status=True)
return result
###############################
# Version Control Commands
###############################
class EasyDiffSvnCommand(_VersionControlCommand, _EasyDiffSvn):
"""SVN diff command."""
def __init__(self, window):
"""Initialize."""
super().__init__(window)
self.setup()
class EasyDiffGitCommand(_VersionControlCommand, _EasyDiffGit):
"""Git diff command."""
def __init__(self, window):
"""Initialize."""
super().__init__(window)
self.setup()
class EasyDiffHgCommand(_VersionControlCommand, _EasyDiffHg):
"""Mercurial diff command."""
def __init__(self, window):
"""Initialize."""
super().__init__(window)
self.setup()
###############################
# Loaders
###############################
def setup_vc_binaries():
"""Setup version control binaries."""
global SVN_ENABLED
global GIT_ENABLED
global HG_ENABLED
settings = load_settings()
svn_path = multiget(settings, "svn", None)
git_path = multiget(settings, "git", None)
hg_path = multiget(settings, "hg", None)
if svn_path is not None and svn_path != "":
svn.set_svn_path(svn_path)
SVN_ENABLED = None
if git_path is not None and git_path != "":
git.set_git_path(git_path)
GIT_ENABLED = None
if hg_path is not None and hg_path != "":
hg.set_hg_path(hg_path)
HG_ENABLED = None
settings.clear_on_change('reload_vc')
settings.add_on_change('reload_vc', setup_vc_binaries)
def plugin_loaded():
"""Setup plugin."""
setup_vc_binaries()