forked from titoBouzout/SideBarGit
/
SideBarGit.py
executable file
·281 lines (254 loc) · 8.25 KB
/
SideBarGit.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
# coding=utf8
import sublime
import os
import subprocess
from SideBarItem import SideBarItem
from Utils import Object
class SideBarGit:
def run(
self,
object,
modal = False,
background = False,
refresh_funct_view = False,
refresh_funct_command = False,
refresh_funct_item = False,
refresh_funct_to_status_bar = False,
refresh_funct_title = False,
refresh_funct_no_results = False,
refresh_funct_syntax_file = False
):
if not refresh_funct_view:
pass
else:
object = Object()
object.command = refresh_funct_command
object.item = SideBarItem(refresh_funct_item, os.path.isdir(refresh_funct_item))
object.to_status_bar = refresh_funct_to_status_bar
object.title = refresh_funct_title
object.no_results = refresh_funct_no_results
object.syntax_file = refresh_funct_syntax_file
debug = True
if debug:
print '----------------------------------------------------------'
print 'GIT:'
print object.command
print 'CWD:'
print object.item.forCwdSystemPath()
print 'PATH:'
print object.item.forCwdSystemName()
failed = False
import sys
if sys.platform == 'win32':
object.command = map(self.escapeCMDWindows, object.command)
cwd = object.item.forCwdSystemPath()
try:
if not modal :
process = subprocess.Popen(
object.command,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell= sys.platform == 'win32',
universal_newlines=True)
else:
if sys.platform == 'win32':
process = subprocess.Popen(
#" ".join(object.command),
object.command,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True)
else:
process = subprocess.Popen(
object.command,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
universal_newlines=True)
if background:
if debug:
print 'SUCCESS'
print '----------------------------------------------------------'
return True
stdout, stderr = process.communicate()
stdout = stdout.strip()
if stdout.find('fatal:') == 0 or stdout.find('error:') == 0 or stdout.find('Permission denied') == 0 or stderr:
print 'FAILED'
failed = True
else:
if debug:
print 'SUCCESS'
if stdout:
if debug:
print 'STDOUT'
print stdout
if stderr:
print 'STDERR'
print stderr
except OSError as (errno, strerror):
print 'FAILED'
failed = True
print errno
print strerror
except IOError as (errno, strerror):
print 'FAILED'
failed = True
print errno
print strerror
if debug:
print '----------------------------------------------------------'
try:
object.to_status_bar
except:
object.to_status_bar = False
if failed:
try:
strerror
if errno == 2:
self.alert(strerror+'\nPossible error:\n'+object.command[0]+' not found on $PATH')
else:
self.alert(strerror)
return False
except:
if not stdout and not stderr:
return False
if stdout.find('Permission denied') == 0 or stdout.find('fatal: The remote end hung up unexpectedly') == 0:
self.alert((stdout or '')+'\n'+(stderr or '')+'\nPossible error:\nssh keys not in .ssh/ directory or keys not opened')
else:
self.alert((stdout or '')+'\n'+(stderr or ''))
return False
else:
if stdout != '' and refresh_funct_view == False and (object.to_status_bar or " ".join(object.command).find('git push') == 0 or stdout.find('nothing to commit') == 0):
self.status(stdout)
else:
if stdout == '' and refresh_funct_view == False:
try:
self.status(object.no_results)
except:
self.status('No output to show')
return True
if stdout == '' and refresh_funct_view != False:
try:
stdout = object.no_results
except:
stdout = 'No output to show'
if stdout == '':
return True
if refresh_funct_view == False:
view = sublime.active_window().new_file()
else:
view = refresh_funct_view
try:
view.set_name(object.title.decode('utf-8'))
except:
view.set_name('No Title')
try:
if object.syntax_file != False:
view.set_syntax_file(object.syntax_file)
except:
pass
try:
object.word_wrap
view.settings().set('word_wrap', False)
except:
pass
view.settings().set('fallback_encoding', 'UTF-8')
view.settings().set('encoding', 'UTF-8')
view.settings().set('default_dir', object.item.dirname())
view.set_scratch(True)
if refresh_funct_view == False:
view.settings().set('SideBarGitIsASideBarGitTab', True)
view.settings().set('SideBarGitCommand', object.command)
view.settings().set('SideBarGitModal', modal)
view.settings().set('SideBarGitBackground', background)
view.settings().set('SideBarGitItem', object.item.path())
try:
view.settings().set('SideBarGitToStatusBar', object.to_status_bar)
except:
view.settings().set('SideBarGitToStatusBar', False)
try:
view.settings().set('SideBarGitTitle', object.title)
except:
view.settings().set('SideBarGitTitle', 'No Title')
try:
view.settings().set('SideBarGitNoResults', object.no_results)
except:
view.settings().set('SideBarGitNoResults', 'No output to show')
try:
view.settings().set('SideBarGitSyntaxFile', object.syntax_file)
except:
view.settings().set('SideBarGitSyntaxFile', False)
content = "[SideBarGit@SublimeText "
content += object.item.name().decode('utf-8')
content += "/] "
content += (" ".join(object.command)).decode('utf-8')
content += "\n\n"
content += "# Improve this command, the output or the tab title by posting here:"
content += "\n"
content += "# http://www.sublimetext.com/forum/viewtopic.php?f=5&t=3405"
content += "\n"
content += "# Tip: F5 will run the command again and refresh the contents of this tab"
content += "\n\n"
content += stdout.decode('utf-8')
edit = view.begin_edit()
view.replace(edit, sublime.Region(0, view.size()), content);
view.sel().clear()
view.sel().add(sublime.Region(0))
view.end_edit(edit)
return True
def confirm(self, message, function, arg1):
import functools
sublime.active_window().run_command('hide_panel');
sublime.active_window().show_input_panel("Confirmation Required:", message.decode('utf-8'), functools.partial(function, arg1, True), None, None)
def prompt(self, message, default, function, arg1):
import functools
sublime.active_window().run_command('hide_panel');
sublime.active_window().show_input_panel(message.decode('utf-8'), default.decode('utf-8'), functools.partial(function, arg1, True), None, None)
def alert(self, message):
try:
sublime.error_message('Git : '+(message.decode('utf-8')))
except:
try:
sublime.error_message('Git : '+message)
except:
print message
def status(self, message):
message = message[:200] + (message[200:] and '…')
message = message.replace('\n', ' ')
try:
v = sublime.active_window().active_view()
v.set_status('SideBarGit', 'Git : '+(message.decode('utf-8')))
sublime.set_timeout(lambda: SideBarGit().statusRemove(v), 16000)
except:#there is no tabs opened
sublime.status_message('Git : '+(message.decode('utf-8')))
def statusRemove(self, v):
try:
v.erase_status('SideBarGit')
except:#this view is not there
pass
def getSelectedRepos(self, items):
repos = []
reposTemp = []
for item in items:
original = item.path()
while not os.path.exists(item.join('.git')):
if item.dirname() == item.path():
break;
item.path(item.dirname())
if os.path.exists(item.join('.git')):
try:
index = reposTemp.index(item.path())
except ValueError:
reposTemp.append(item.path())
index = reposTemp.index(item.path())
repos.append(Object())
repos[index].repository = item
repos[index].items = []
repos[index].items.append(SideBarItem(original, os.path.isdir(original)))
return repos
def escapeCMDWindows(self, string):
return string.replace('^', '^^')