Skip to content

Commit

Permalink
feat: Adding codeowners in the git blame
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Sep 18, 2024
1 parent 79963f2 commit efa9be0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions installations/golang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ install() {
go install "github.com/derailed/popeye@latest"
go install "github.com/go-delve/delve/cmd/dlv@latest"
go install "github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
go install "github.com/hmarr/codeowners/cmd/codeowners@latest"
go install "github.com/tsenart/vegeta@latest"
go install "go.uber.org/mock/mockgen@latest"
go install "golang.org/x/tools/cmd/goimports@latest"
Expand Down
14 changes: 11 additions & 3 deletions sublime/plugins/SublimeGit/.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
[
{
"caption": "Copy git web url",
"caption": "SublimeGit: Copy git web url",
"command": "sublime_git_web"
},
{
"caption": "Copy line path",
"caption": "SublimeGit: Copy line path",
"command": "sublime_git_linepath"
}
},
{
"caption": "SublimeGit: Disable show git blame",
"command": "sublime_git_disable_blame"
},
{
"caption": "SublimeGit: Enable show git blame",
"command": "sublime_git_enable_blame"
},
]
3 changes: 3 additions & 0 deletions sublime/plugins/SublimeGit/SublimeGit.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"show_blame": ""
}
47 changes: 46 additions & 1 deletion sublime/plugins/SublimeGit/blame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import sublime


PLUGIN_NAME = "SublimeGit"
PLUGIN_SETTINGS = "{}.sublime-settings".format(PLUGIN_NAME)

new_file_regex = re.compile("fatal: no such path '.*' in HEAD")
not_git_regex = re.compile(
"fatal: not a git repository \\(or any of the parent directories\\): .git"
Expand All @@ -15,6 +19,12 @@
summary_regex = re.compile("summary\\s(.*)")


def plugin_loaded() -> None:
global _settings_obj
loaded_settings_obj = sublime.load_settings(PLUGIN_SETTINGS)
_settings_obj = loaded_settings_obj


# From https://gist.github.com/jonlabelle/7d306575cbbd34b154f87b1853d532cc
def relative_time(date):
def formatn(n, s):
Expand Down Expand Up @@ -47,6 +57,17 @@ def format(self):
return FormatDelta(date).format()


class SublimeGitDisableBlame(sublime_plugin.WindowCommand):
def run(self):
_settings_obj.set("show_blame", "")
self.window.active_view().erase_status("git_blame")


class SublimeGitEnableBlame(sublime_plugin.WindowCommand):
def run(self):
_settings_obj.set("show_blame", "true")


class SublimeGitBlame(sublime_plugin.EventListener):
_blame_key = "git_blame"

Expand All @@ -60,6 +81,9 @@ def print_blame(self, view, value):
view.set_status(self._blame_key, value)

def on_selection_modified_async(self, view):
if not _settings_obj.get("show_blame", ""):
return

file_name = view.file_name()
if not file_name or len(file_name) == 0:
return
Expand Down Expand Up @@ -91,6 +115,8 @@ def on_selection_modified_async(self, view):
self.clear_blame(view)
return

variables = window.extract_variables()

try:
git_blame = subprocess.check_output(
[
Expand All @@ -104,7 +130,7 @@ def on_selection_modified_async(self, view):
file_name,
],
stderr=subprocess.STDOUT,
cwd=window.extract_variables()["file_path"],
cwd=variables["file_path"],
)
except subprocess.CalledProcessError as e:
err_content = e.output.decode("utf8")
Expand All @@ -129,4 +155,23 @@ def on_selection_modified_async(self, view):
description = summary_regex.findall(blame_content)[0]
blame = "{}|{}({}): {}".format(author, relative_time(moment), moment.strftime("%Y-%m-%dT%H:%M:%S%z"), description)

try:
folder = variables.get("folder")
file = variables.get("file")

filename_from_root = file.replace(folder, "").lstrip("/")
owners = subprocess.check_output(
[
"codeowners",
"--",
filename_from_root,
],
stderr=subprocess.STDOUT,
cwd=folder,
)

blame += ", {}".format(owners.decode("utf8").replace(filename_from_root, "").strip())
except subprocess.CalledProcessError as e:
print(e.output.decode("utf8"), end="")

self.print_blame(view, blame)

0 comments on commit efa9be0

Please sign in to comment.