forked from titoBouzout/SideBarGit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SideBarGitGutterDiff.txt
executable file
·227 lines (193 loc) · 5.78 KB
/
SideBarGitGutterDiff.txt
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
import sublime
import sublime_plugin
import threading
import subprocess
import sys
import re
import tempfile
import codecs
import os
import _thread as thread
import time
from .SideBarAPI import SideBarItem
from .SideBarGit import SideBarGit
running = False
timing = time.time()
loaded = False
modified = True
def plugin_loaded():
global loaded
loaded = True
# threads listening scroll, and waiting to set data on focus change
if not 'running_sg_git_gutter_loop' in globals():
global running_sg_git_gutter_loop
running_sg_git_gutter_loop = True
thread.start_new_thread(sg_git_gutter_loop, ())
def sg_git_gutter_loop():
global running
while True:
if not running:
sublime.set_timeout(lambda:SideBarGitGutterDiff().run(sublime.active_window().active_view()), 0)
time.sleep(0.3)
class SideBarGitGutterDiff(sublime_plugin.EventListener):
def on_load(self, view):
global modified
if loaded:
modified = True
self.run(view)
def on_modified(self, view):
global timing, modified
modified = True
now = time.time()
if now - timing > 0.3:
timing = now
if loaded:
self.run(view)
else:
timing = now
def on_post_save(self, view):
self.run(view)
def run(self, view):
global running, modified
if running == False and modified and view.file_name() != None and view.file_name() != '':
modified = False
running = True
settings = view.settings()
# cache file repository ( if any )
if not settings.has('SideBarGitGutterRepository'):
#print('not has')
item = SideBarItem(view.file_name(), False)
_item = SideBarItem(view.file_name(), False)
repos = SideBarGit().getSelectedRepos([_item])
#print(repos)
#print(len(repos))
#print(repos[0].repository.path())
if len(repos) > 0:
#print('set')
settings.set('SideBarGitGutterRepository', str(repos[0].repository.path()))
settings.set('SideBarGitGutterCWD', str(repos[0].repository.path()))
settings.set('SideBarGitGutterPath', str(item.forCwdSystemPathRelativeFrom(repos[0].repository.path())))
else:
#print('set to empty')
settings.set('SideBarGitGutterRepository', '')
# if in a repo check for modifications
repo = settings.get('SideBarGitGutterRepository')
#print(repo)
if repo != '':
#print('running thread')
SideBarGitGutterDiffThread(
view,
repo,
settings.get('SideBarGitGutterCWD'),
settings.get('SideBarGitGutterPath'),
view.substr(sublime.Region(0, view.size()))
).start()
else:
#print('repo empty')
running = False
class SideBarGitGutterDiffThread(threading.Thread):
def __init__(self, view, repo, cwd, path, content):
threading.Thread.__init__(self)
self.view = view
self.repo = repo
self.cwd = cwd
self.path = path
self.content = content
def run(self):
tmp = tempfile.NamedTemporaryFile(delete=False)
codecs.open(tmp.name, 'w+', 'utf-8').write(self.content)
comand = ['git', 'diff', '-p', '--unified=0', '--no-color', '--ignore-all-space', '--ignore-space-at-eol', '--ignore-space-change', self.path, tmp.name]
process = subprocess.Popen(
comand,
cwd=self.cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=sys.platform == 'win32',
universal_newlines=True)
stdout, stderr = process.communicate()
if stdout != '' and stdout.find('fatal:') != 0:
hunk = re.finditer('\n@@ -([0-9]+),?([0-9]*) \+([0-9]+),?([0-9]*) @@', stdout)
additions = []
deletions = []
changes = []
# print('------------')
# print(additions)
# print(deletions)
# print(changes)
for change in hunk:
g = []
for group in change.groups():
if group == '':
g.append(1)
else:
g.append(int(group))
deleted = g[1]
added = g[3]
if deleted == added and added == 1:
changes.append([g[2]-1, g[2]]);
else:
if deleted > 0:
if deleted == 1:
if added > deleted:
deletions.append([g[2], g[2]+1]);
else:
deletions.append([g[2], g[2]+1]);
else:
deletions.append([g[2]-1, g[2]+1])
if added > 0:
if added == 1:
additions.append([g[2]-1, g[2]+added-1]);
else:
additions.append([g[2]-1, g[2]+added-1])
tmp.close();
try:
os.remove(tmp.name)
except:
pass
sublime.set_timeout(lambda:self.add_regions(self.view, additions, deletions, changes), 0)
else:
tmp.close();
try:
os.remove(tmp.name)
except:
pass
if stdout.find('fatal:') != -1:
self.view.settings().set('SideBarGitGutterRepository', '')
sublime.set_timeout(lambda:self.erase_regions(self.view), 0)
def add_regions(self, view, additions, deletions, changes):
global running
# print('------------')
# print(additions)
# print(deletions)
# print(changes)
view.erase_regions("git.diff.additions")
view.erase_regions("git.diff.deletions")
view.erase_regions("git.diff.changes")
rs = []
for r in additions:
while r[0] < r[1]:
rs.append(view.line(view.text_point(r[0], 0)))
r[0] = r[0]+1
if len(rs):
view.add_regions("git.diff.additions", rs, "entity.name.function", "dot", sublime.DRAW_OUTLINED)
rs = []
for r in deletions:
while r[0] < r[1]:
rs.append(view.line(view.text_point(r[0], 0)))
r[0] = r[0]+1
if len(rs):
view.add_regions("git.diff.deletions", rs, "entity.class.name", "dot", sublime.DRAW_OUTLINED)
rs = []
for r in changes:
while r[0] < r[1]:
rs.append(view.line(view.text_point(r[0], 0)))
r[0] = r[0]+1
if len(rs):
view.add_regions("git.diff.changes", rs, "entity.name.function", "dot", sublime.DRAW_OUTLINED)
running = False
def erase_regions(self, view):
global running
view.erase_regions("git.diff.additions")
view.erase_regions("git.diff.deletions")
view.erase_regions("git.diff.changes")
running = False