aboutsummaryrefslogtreecommitdiffstats
path: root/alfred/git.py
blob: 107a3b1b6506ef92fb4335c1ccb84ec778fd3fb1 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Sopel plugin: bare git repo management

  !git list                   list all repos
  !git desc <repo> <text>     set repo description
  !git hook <repo> <type>     install a named post-receive hook
  !git remove <repo>          remove a repo (confirms first)
"""

import os
import shutil
import subprocess

from sopel import plugin

REPOS  = "/home/git/repos"
HOOKS  = "/home/ahmed/scripts/git-hooks"

_pending_remove = {}


def _owner(bot, trigger):
    return trigger.nick == bot.settings.core.owner


def _repos():
    try:
        return sorted(
            e for e in os.listdir(REPOS)
            if os.path.isdir(os.path.join(REPOS, e))
        )
    except OSError:
        return []


def _desc(name):
    path = os.path.join(REPOS, name, "description")
    try:
        text = open(path).read().strip()
        if text.startswith("Unnamed repository"):
            return ""
        return text
    except OSError:
        return ""


def _last_commit(name):
    try:
        r = subprocess.run(
            ["git", "-C", os.path.join(REPOS, name), "log", "-1", "--format=%cr"],
            capture_output=True, text=True, timeout=5,
        )
        return r.stdout.strip() or "no commits"
    except Exception:
        return "?"


@plugin.command("git")
def cmd_git(bot, trigger):
    if not _owner(bot, trigger):
        return

    args = (trigger.group(2) or "").split()
    if not args:
        bot.say("usage: !git <list|desc|hook|remove>")
        return

    action = args[0]

    if action == "list":
        repos = _repos()
        if not repos:
            bot.say("no repos")
            return
        for name in repos:
            desc = _desc(name)
            last = _last_commit(name)
            line = f"{name}  ({last})"
            if desc:
                line += f"  {desc}"
            bot.say(line)

    elif action == "desc":
        if len(args) < 3:
            bot.say("usage: !git desc <repo> <description>")
            return
        name, text = args[1], " ".join(args[2:])
        path = os.path.join(REPOS, name)
        if not os.path.isdir(path):
            bot.say(f"{name}: not found")
            return
        with open(os.path.join(path, "description"), "w") as f:
            f.write(text + "\n")
        bot.say(f"{name}: description updated")

    elif action == "hook":
        if len(args) < 3:
            available = [
                f[len("post-receive-"):-len(".sh")]
                for f in os.listdir(HOOKS)
                if f.startswith("post-receive-") and f.endswith(".sh")
            ] if os.path.isdir(HOOKS) else []
            bot.say(f"usage: !git hook <repo> <type>  available: {', '.join(available) or 'none'}")
            return
        name, hooktype = args[1], args[2]
        repo_path = os.path.join(REPOS, name)
        if not os.path.isdir(repo_path):
            bot.say(f"{name}: not found")
            return
        src = os.path.join(HOOKS, f"post-receive-{hooktype}.sh")
        if not os.path.isfile(src):
            bot.say(f"{hooktype}: hook template not found")
            return
        dst = os.path.join(repo_path, "hooks", "post-receive")
        shutil.copy2(src, dst)
        os.chmod(dst, 0o755)
        bot.say(f"{name}: {hooktype} hook installed")

    elif action == "remove":
        if len(args) < 2:
            bot.say("usage: !git remove <repo>")
            return
        name = args[1]
        path = os.path.join(REPOS, name)
        if not os.path.isdir(path):
            bot.say(f"{name}: not found")
            return
        channel = str(trigger.sender)
        _pending_remove[channel] = path
        bot.say(f"remove {name}? reply !git yes or !git no")

    elif action == "yes":
        channel = str(trigger.sender)
        path = _pending_remove.pop(channel, None)
        if not path:
            bot.say("nothing pending")
            return
        name = os.path.basename(path)
        shutil.rmtree(path)
        bot.say(f"{name}: removed")

    elif action == "no":
        channel = str(trigger.sender)
        _pending_remove.pop(channel, None)
        bot.say("cancelled")

    else:
        bot.say("usage: !git <list|desc|hook|remove>")