Skip to content

Commit

Permalink
Use configparser to parse the .git/config
Browse files Browse the repository at this point in the history
This allows for a more accurate parsing of the file since we known the
sections and values within those sections.

Fixes ehamiter#62
  • Loading branch information
Fryguy committed Mar 27, 2018
1 parent 9847e3d commit 07c1e83
Showing 1 changed file with 51 additions and 53 deletions.
104 changes: 51 additions & 53 deletions githubinator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import codecs
import configparser
import os
import re
import sublime
Expand All @@ -22,10 +23,6 @@ class GithubinatorCommand(sublime_plugin.TextCommand):
def load_config(self):
s = sublime.load_settings("Githubinator.sublime-settings")
self.default_remote = s.get("default_remote") or self.DEFAULT_GIT_REMOTE

if not isinstance(self.default_remote, list):
self.default_remote = [self.default_remote]

self.default_host = s.get("default_host") or self.DEFAULT_HOST

def run(self, edit, copyonly=False, permalink=False, mode="blob", branch=None, open_repo=False):
Expand Down Expand Up @@ -53,21 +50,8 @@ def run(self, edit, copyonly=False, permalink=False, mode="blob", branch=None, o

# Read the config file in .git
git_config_path = os.path.join(git_path, ".git", "config")
with codecs.open(git_config_path, "r", "utf-8") as git_config_file:
config = git_config_file.read()

# Figure out the host
scheme = "https"
result = re.search(r"url.*?=.*?((https?)://([^/]*)/)|(git@([^:]*):)", config)
if result:
matches = result.groups()
if matches[0]:
scheme = matches[1]
self.default_host = matches[2]
else:
self.default_host = matches[4]

re_host = re.escape(self.default_host)
config = configparser.ConfigParser()
config.read(git_config_path)

sha, current_branch = self.get_git_status(git_path)
if not branch:
Expand All @@ -76,51 +60,65 @@ def run(self, edit, copyonly=False, permalink=False, mode="blob", branch=None, o
target = sha if permalink else branch
target = quote_plus(target, safe="/")

detected_remote = None
regex = r".*\s.*(?:remote = )(\w+?)\r?\n"
result = re.search(branch + regex, config)
print(branch)
print(target)

if result:
matches = result.groups()
detected_remote = [matches[0]]
branch_section = "branch \"%s\"" % branch
if config.has_section(branch_section):
remote = config.get(branch_section, "remote") or self.default_remote
else:
remote = self.default_remote

for remote in (detected_remote or self.default_remote):
remote_section = "remote \"%s\"" % remote
url = config.get(remote_section, "url")

regex = r".*\s.*(?:https?://%s/|%s:|git://%s/)(.*)/(.*?)(?:\.git)?\r?\n" % (re_host, re_host, re_host)
result = re.search(remote + regex, config)
if not result:
continue
result = re.search(r"((https?)://([^/]*)/)|(git@([^:]*):)", url)
if not result:
return

matches = result.groups()
username = matches[0]
project = matches[1]
matches = result.groups()
if matches[0]:
scheme = matches[1]
self.default_host = matches[2]
else:
scheme = "https"
self.default_host = matches[4]

lines = self.get_selected_line_nums()
re_host = re.escape(self.default_host)

regex = r"(?:https?://%s/|%s:|git://%s/)(.*)/(.*?)(?:\.git)" % (re_host, re_host, re_host)
result = re.search(regex, url)
if not result:
return

repo_link = scheme + "://%s/%s/%s/" % (self.default_host, username, project)
matches = result.groups()
username = matches[0]
project = matches[1]

if open_repo:
full_link = repo_link
else:
if "bitbucket" in self.default_host:
mode = "src" if mode == "blob" else "annotate"
lines = ":".join([str(l) for l in lines])
full_link = repo_link + "%s/%s%s/%s#cl-%s" % \
(mode, sha, new_git_path, file_name, lines)
else:
lines = "-".join("L%s" % line for line in lines)
full_link = repo_link + "%s/%s%s/%s#%s" % \
(mode, target, new_git_path, file_name, lines)
lines = self.get_selected_line_nums()

full_link = quote(full_link, safe=':/#@')
repo_link = scheme + "://%s/%s/%s/" % (self.default_host, username, project)

if open_repo:
full_link = repo_link
else:
if "bitbucket" in self.default_host:
mode = "src" if mode == "blob" else "annotate"
lines = ":".join([str(l) for l in lines])
full_link = repo_link + "%s/%s%s/%s#cl-%s" % \
(mode, sha, new_git_path, file_name, lines)
else:
lines = "-".join("L%s" % line for line in lines)
full_link = repo_link + "%s/%s%s/%s#%s" % \
(mode, target, new_git_path, file_name, lines)

sublime.set_clipboard(full_link)
sublime.status_message("Copied %s to clipboard." % full_link)
full_link = quote(full_link, safe=':/#@')

if not copyonly:
self.view.window().run_command("open_url", {"url": full_link})
sublime.set_clipboard(full_link)
sublime.status_message("Copied %s to clipboard." % full_link)

break
if not copyonly:
self.view.window().run_command("open_url", {"url": full_link})

def get_selected_line_nums(self):
"""Get the line number of selections."""
Expand Down

0 comments on commit 07c1e83

Please sign in to comment.