File: git.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (76 lines) | stat: -rw-r--r-- 2,138 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# pylint: disable=C0111,R0903

"""Print the branch and git status for the
currently focused window.

Requires:
    * xcwd
    * Python module 'pygit2'
"""

import os
import pygit2

import core.module

import util.cli


class Module(core.module.Module):
    def __init__(self, config, theme):
        super().__init__(config, theme, [])

        self.__error = False

    def hidden(self):
        return self.__error

    def update(self):
        state = {}
        self.clear_widgets()
        try:
            directory = util.cli.execute("xcwd").strip()
            directory = self.__get_git_root(directory)
            repo = pygit2.Repository(directory)

            self.add_widget(name="git.main", full_text=repo.head.shorthand)

            for filepath, flags in repo.status().items():
                if (
                    flags == pygit2.GIT_STATUS_WT_NEW
                    or flags == pygit2.GIT_STATUS_INDEX_NEW
                ):
                    state["new"] = True
                if (
                    flags == pygit2.GIT_STATUS_WT_DELETED
                    or flags == pygit2.GIT_STATUS_INDEX_DELETED
                ):
                    state["deleted"] = True
                if (
                    flags == pygit2.GIT_STATUS_WT_MODIFIED
                    or flags == pygit2.GIT_STATUS_INDEX_MODIFIED
                ):
                    state["modified"] = True
            self.__error = False
            if "new" in state:
                self.add_widget(name="git.new")
            if "modified" in state:
                self.add_widget(name="git.modified")
            if "deleted" in state:
                self.add_widget(name="git.deleted")

        except Exception as e:
            self.__error = True

    def state(self, widget):
        return widget.name.split(".")[1]

    def __get_git_root(self, directory):
        while len(directory) > 1:
            if os.path.exists(os.path.join(directory, ".git")):
                return directory
            directory = "/".join(directory.split("/")[0:-1])
        return "/"


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4