From 580bc554bac1b2bdd1828b6dd822a68093b8b309 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 20 Jan 2019 14:09:42 +0100 Subject: [PATCH] [modules] Add new module to display git information This module uses xcwd to retrieve the directory of the currently focused window and, if any of the parent directories is a git repository, displays basic information about the repository. Right now, only the current branch name is displayed. --- bumblebee/modules/git.py | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bumblebee/modules/git.py diff --git a/bumblebee/modules/git.py b/bumblebee/modules/git.py new file mode 100644 index 0000000..29840f5 --- /dev/null +++ b/bumblebee/modules/git.py @@ -0,0 +1,50 @@ +# pylint: disable=C0111,R0903 + +"""Print the branch and git status for the +currently focused window. + +Requires: + * xcwd + * Python module 'pygit2' +""" + +import os +import string +import pygit2 + +import bumblebee.input +import bumblebee.output +import bumblebee.engine +import bumblebee.util + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.gitinfo) + ) + self._fmt = self.parameter("format", "{branch}") + + def gitinfo(self, widget): + info = "" + directory = None + data = { + "branch": "n/a", + } + try: + directory = bumblebee.util.execute("xcwd").strip() + directory = self._get_git_root(directory) + repo = pygit2.Repository(directory) + data["branch"] = repo.head.shorthand + except Exception as e: + return e + + return string.Formatter().vformat(self._fmt, (), data) + + def _get_git_root(self, directory): + while directory != "/": + if os.path.exists(os.path.join(directory, ".git")): + return directory + directory = "/".join(directory.split("/")[0:-1]) + return None + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4