From 8bbe9caa17a4fe6d93798a0cd9a5db2da92b3418 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Fri, 5 Jun 2020 08:34:03 +0200 Subject: [PATCH] Add line number to blame and browse When opening a file, retrieve the current line number to open the file or blame at the exact position --- github.py | 28 ++++++++++++++++++++-------- github_blame.py | 7 ++++++- github_open.py | 7 ++++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/github.py b/github.py index 8241867..d276b75 100644 --- a/github.py +++ b/github.py @@ -150,18 +150,20 @@ def parse_heads(self, heads): f = lambda l: tuple(re.split("\s", l.replace('refs/heads/', ''))[::-1]) return dict(map(f, heads.splitlines())) - def browse_file_url(self, filename): + def browse_file_url(self, filename, linenumber=False): return git_browse_file_url(self.info['web_uri'], - self.path_from_rootdir(filename), self.branch) + self.path_from_rootdir(filename), + self.branch, + linenumber) def file_history_url(self, filename): return git_file_history_url(self.info['web_uri'], self.path_from_rootdir(filename), self.branch) - def blame_file_url(self, filename): + def blame_file_url(self, filename, linenumber): return git_blame_file_url( self.info['web_uri'], self.path_from_rootdir(filename), - self.revision) + self.revision, linenumber) def repository_url(self): return git_repository_url(self.info['web_uri']) @@ -257,14 +259,24 @@ def wrapper(self): return wrapper -def git_browse_file_url(repository, filepath, branch='master'): - return "https://%s/blob/%s%s" % (repository, urllib.quote(branch), filepath) +def git_browse_file_url(repository, filepath, branch='master', linenumber=False): + return "https://%s/blob/%s%s%s" % ( + repository, + urllib.quote(branch), + filepath, + "#L"+str(linenumber) if linenumber else '', + ) def git_file_history_url(repository, filepath, branch='master'): return "https://%s/commits/%s%s" % (repository, branch, filepath) -def git_blame_file_url(repository, filepath, revision): - return "https://%s/blame/%s%s" % (repository, revision, filepath) +def git_blame_file_url(repository, filepath, revision, linenumber=False): + return "https://%s/blame/%s%s%s" % ( + repository, + revision, + filepath, + "#L"+str(linenumber) if linenumber else '', + ) def git_issues_url(repository): diff --git a/github_blame.py b/github_blame.py index 13bb2cf..ffa6670 100644 --- a/github_blame.py +++ b/github_blame.py @@ -9,4 +9,9 @@ class GithubBlameCommand(GithubWindowCommand): @require_file @with_repo def run(self, repo): - open_url(repo.blame_file_url(self.relative_filename())) + open_url( + repo.blame_file_url( + self.relative_filename(), + self.current_line_number(), + ) + ) diff --git a/github_open.py b/github_open.py index bfedb70..b9c7937 100644 --- a/github_open.py +++ b/github_open.py @@ -9,4 +9,9 @@ class GithubOpenCommand(GithubWindowCommand): @require_file @with_repo def run(self, repo): - open_url(repo.browse_file_url(self.relative_filename())) + open_url( + repo.browse_file_url( + self.relative_filename(), + self.current_line_number(), + ) + )