Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git blame plugin #1302

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions porcupine/plugins/git_blame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from __future__ import annotations

import logging
import re
import subprocess
import sys
import time
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import NamedTuple

from porcupine import get_tab_manager, tabs, utils

log = logging.getLogger(__name__)

GIT_BLAME_REGEX = r"([0-9a-fA-F]+)\s\((.+?)\s([0-9]*)\s.{5}\s[0-9]+\)"


class Commit(NamedTuple):
revision: int
subject: str
author: str
date: datetime


def run_git(*args, cwd: Path) -> subprocess.CompletedProcess:
return subprocess.run(
["git"] + list(args),
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding=sys.getfilesystemencoding(),
timeout=(60 * 10), # 10min. Must be huge to avoid unnecessary killing (#885)
**utils.subprocess_kwargs,
)


def is_in_git_repo(path: Path) -> bool:
for parent in path.parents:
if (parent / ".git").is_dir():
return True
return False


def git_blame_get_commit(path: Path, line: int) -> Commit | None:
if not path or not is_in_git_repo(path):
# TODO: Do we have to check this every time?
return None

try:
start = time.perf_counter()
git_blame_result = run_git(
"blame", str(path), "-L", f"{line},{line}", "-t", cwd=path.parent
)
log.debug(
f"running git blame for {path} took" f" {round((time.perf_counter() - start)*1000)}ms"
)
except (OSError, UnicodeError, subprocess.TimeoutExpired):
log.warning("can't run git", exc_info=True)
return None

result = re.search(GIT_BLAME_REGEX, git_blame_result.stdout)
if not result:
return None

revision, author, timestamp = result.groups()
date = datetime.fromtimestamp(int(timestamp))

git_log_result = run_git("log", "-n", "1", "--pretty=format:%s", revision, cwd=path.parent)

return Commit(int(revision), git_log_result.stdout, author, date)
rdbende marked this conversation as resolved.
Show resolved Hide resolved


def show_git_blame_message(path: Path, event) -> None:
line_num = int(event.widget.index("insert").split(".")[0])

commit = git_blame_get_commit(path=path, line=line_num)
if commit is None:
return

formatted = f"Line {line_num}: by {commit.author} at {commit.date} - {commit.subject}"
print(formatted)


def on_new_filetab(tab: tabs.FileTab) -> None:
tab.textwidget.bind("<<CursorMoved>>", partial(show_git_blame_message, tab.path), add=True)


def setup() -> None:
get_tab_manager().add_filetab_callback(on_new_filetab)