-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathggu.py
180 lines (134 loc) · 5 KB
/
ggu.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
import sublime, sublime_plugin
import itertools
import os
import re
import random, sys
class GguCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Generate file path and position
(row,col) = self.view.rowcol(self.view.sel()[0].begin())
row = row + 1
path = os.path.realpath(self.view.file_name())
path = path + "#L" + str(row)
# Get remotes
settings = sublime.load_settings("ggu.sublime-settings")
URL = MakeURL().getremotes(path, settings, remote = False)
if URL:
if URL == "panelcall":
self.view.run_command("ggur")
else:
sublime.set_clipboard(URL)
sublime.status_message('Copied %s to clipboard.' % URL)
print('Copied %s to clipboard.' % URL)
class GgurCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.items = []
self.fremotes = []
# Generate file path and position
(row,col) = self.view.rowcol(self.view.sel()[0].begin())
row = row + 1
path = os.path.realpath(self.view.file_name())
path = path + "#L" + str(row)
# Get remotes
settings = sublime.load_settings("ggu.sublime-settings")
remotes_stuff = MakeURL().getremotes(path, settings, remote = True)
if remotes_stuff is None:
return
remotes = remotes_stuff[0]
alises = remotes_stuff[1]
self.b = remotes_stuff[2]
for a in alises:
self.items.append(a)
for r in remotes:
self.fremotes.append("https://github.com/"+r[0]+"/"+r[1])
# show quick panel
self.view.window().show_quick_panel(self.items, self.on_done)
def on_done(self, index):
"""
called when the remote is selected
"""
if index >= 0:
URL = self.fremotes[index]
URL = URL + "/blob/master"
URL = URL + self.b
self.paste_url(URL)
else:
print "you canceled it."
def paste_url(self, URL):
"""
Paste URL to clipboard
"""
if URL:
sublime.set_clipboard(URL)
sublime.status_message('Copied %s to clipboard.' % URL)
print('Copied %s to clipboard.' % URL)
class MakeURL(object):
def find_dir(self, path, folder):
items = os.listdir(path)
if folder in items and os.path.isdir(os.path.join(path, folder)):
return path
dirname = os.path.dirname(path)
if dirname == path:
return None
return self.find_dir(dirname, folder)
def getremotes (self, path, settings, remote = None):
"""
Generate the url
"""
folder_name, file_name = os.path.split(path)
git_path = self.find_dir(folder_name, '.git')
if not git_path:
sublime.error_message('Could not find .git directory.')
print('Could not find .git directory.')
return
new_path = folder_name[len(git_path):]
username = ""
username = settings.get("username")
if username == "":
sublime.error_message("Please edit your ggu.sublime-settings file")
URL_path = new_path+"/"+file_name
branch = self.get_branch(git_path)
remote_alias, remotes = self.get_remote_branch(git_path, folder_name)
if remote is True :
return (remotes, remote_alias, URL_path)
if "origin" in remote_alias:
index = remote_alias.index("origin")
r = remotes[index]
username = r[0]
else:
for u in remotes:
if u[0] == username:
match = username
break;
else:
match = None
username = match
if username is None:
return "panelcall"
repo = path[:len(path)-len(URL_path)].split("/")
repo = repo[len(repo)-1:][0]
URL = "https://github.com/%s/%s/blob/%s%s"%(username, repo,branch,URL_path)
return URL
def get_remote_branch(self, git_path, folder_name):
"""
Get remote branches
"""
gitc_path = os.path.join(git_path, '.git', 'config')
with open(gitc_path, "r") as git_config_file:
config = git_config_file.read()
r1 = r'(?:remote\s\")(.*?)\"\]'
raliases = re.findall(r1,config)
host = 'github.com'
r2 = r'url\s=\s(?:https://%s/|%s:|git://%s|git@%s:)(.*)/(.*?)(?:\.git)'%(host, host, host, host)
remotes = re.findall(r2,config)
if remotes is None:
self.error_message("No remotes found")
return None
return raliases, remotes
def get_branch(self, git_path):
"""
get current branch of the required repo
"""
ref = open(os.path.join(git_path, '.git', 'HEAD'), "r").read().replace('ref: ', '')[:-1]
branch = ref.replace('refs/heads/','')
return branch