Created
March 10, 2016 22:58
-
-
Save ianoc/08c2e48ea0e24d54dbc3 to your computer and use it in GitHub Desktop.
Open current file/line number on github and copy to clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sublime, sublime_plugin | |
class GithubCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
file = self.view.file_name() | |
dir = "/".join(file.split("/")[:-1]) | |
gitRepo = os.popen('cd %s; git rev-parse --show-toplevel'%(dir)).read()[:-1] | |
branch = os.popen('cd %s; git rev-parse --abbrev-ref HEAD'%(dir)).read()[:-1] | |
originUrl = os.popen('cd %s; git config --get remote.origin.url'%(dir)).read()[:-1] | |
if len(branch) > 0: | |
branchOnRemote = len(os.popen('cd %s; git branch -r --list origin/%s'%(dir, branch)).read()[:-1]) > len(branch) | |
else: | |
branchOnRemote = False | |
if originUrl.startswith("git@"): | |
subPath = originUrl.split(":")[1] | |
sections = subPath.split("/") | |
orgName = sections[0] | |
repoName = sections[1].split(".")[0] | |
relPath = file[len(gitRepo):] | |
if len(branch)>0 and branchOnRemote: | |
branchParam = branch | |
else: | |
branchParam = "master" | |
sel = self.view.sel() | |
if len(sel)==1: | |
line, _ = self.view.rowcol(sel[0].a) | |
currentLine = "#L%s"%(line+1) | |
else: | |
currentLine = "" | |
githubLink = "https://github.com/" + orgName + "/" + repoName + "/blob/" + branchParam + relPath + currentLine | |
sublime.status_message(githubLink + " copied to clipboard") | |
os.system("echo '%s' | pbcopy"%(githubLink)) | |
os.system("open '%s'"%(githubLink)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment