summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xgitnoti.py86
1 files changed, 68 insertions, 18 deletions
diff --git a/gitnoti.py b/gitnoti.py
index 7817182..149493b 100755
--- a/gitnoti.py
+++ b/gitnoti.py
@@ -37,12 +37,24 @@ if root[-1] == '/':
repos = None
-def repo_commit_msg(repo, commit):
+def repo_commit_msg(repo, commits):
reponame = os.path.splitext(os.path.basename(repo.path))[0]
- stat = '%d files,' % commit.stats.total['files']
- stat += ' \00305--%d\017' % commit.stats.total['deletions']
- stat += ' \00303++%d\017' % commit.stats.total['insertions']
+ deletions = insertions = 0
+ files = []
+
+ for c in commits:
+ deletions += c.stats.total['deletions']
+ insertions += c.stats.total['insertions']
+ files.extend((f for f in c.stats.files.iterkeys() if not f in files))
+
+ stat = '%d files,' % len(files)
+ if len(commits) > 1:
+ stat = '%d commits, %s' % (len(commits), stat)
+ stat += ' \00305--%d\017' % deletions
+ stat += ' \00303++%d\017' % insertions
+
+ commit = commits[-1]
msg = '\002%s\002 pushed to \002%s\002 by \002%s\002 (%s) %s' % (
reponame,
commit.id_abbrev,
@@ -102,7 +114,8 @@ class ReposNotifyEvent(pyinotify.ProcessEvent):
last = l[1]
nlast = repo.heads[0].commit
if nlast.id != last:
- msg = repo_commit_msg(repo, nlast)
+ commits = list(repo.commits_between(last, 'HEAD'))
+ msg = repo_commit_msg(repo, commits)
self.bot.gitmsg(msg)
l[1] = nlast.id
@@ -160,6 +173,32 @@ class Bot(irc.IRCClient):
self.repeater = LoopingCall(check_notifies, self)
self.repeater.start(options.interval)
+ def get_repo(self, name):
+ repo = [(k, v) for k, v in repos.iteritems() if os.path.basename(k).startswith(name)]
+ if len(repo) == 1:
+ path = repo[0][0]
+ repo = repo[0][1]
+ r = repo[0] or git.Repo(path)
+ return r
+ elif len(repo) == 0:
+ self.msg(target, 'No repo found.')
+ else:
+ self.msg(target, 'Ambiguous name: %s' % (', '.join([os.path.basename(x[0].path) for x in repos])))
+
+ return None
+
+ def get_commits(self, repo, name):
+ try:
+ if '..' in name:
+ frm, to = name.split('..')
+ commits = list(repo.commits_between(frm, to))
+ else:
+ commits = repo.commits(name)
+ except git.errors.GitCommandError:
+ return []
+
+ return commits
+
def privmsg(self, user, channel, message):
private = channel == self.nickname
nick = user.split('!')[0]
@@ -173,21 +212,32 @@ class Bot(irc.IRCClient):
s = 'Repos: %s' % ', '.join([os.path.splitext(os.path.basename(x))[0] for x in repos.keys()])
self.msg(target, s)
elif cmd == 'last':
- repo = messagelist[2].lower() if len(messagelist) > 2 else None
+ if not len(messagelist) == 3:
+ self.msg(target, 'Usage: %s last REPO' % self.nickname)
+ return
+
+ repo = self.get_repo(messagelist[2].lower())
if not repo:
- self.msg(target, 'Which repo?')
return
- repo = [(k, v) for k, v in repos.iteritems() if os.path.basename(k).startswith(repo)]
- if len(repo) == 1:
- path = repo[0][0]
- repo = repo[0][1]
- r = repo[0] or git.Repo(path)
- msg = repo_commit_msg(r, r.heads[0].commit)
- self.msg(target, msg)
- elif len(repo) == 0:
- self.msg(target, 'No repo found.')
- else:
- self.msg(target, 'Ambiguous name: %s' % (', '.join([os.path.basename(x[0].path) for x in repos])))
+
+ msg = repo_commit_msg(repo, [repo.heads[0].commit])
+ self.msg(target, msg)
+ elif cmd == 'show':
+ if not len(messagelist) == 4:
+ self.msg(target, 'Usage: %s show REPO COMMIT' % self.nickname)
+ return
+
+ repo = self.get_repo(messagelist[2].lower())
+ if not repo:
+ return
+
+ commits = self.get_commits(repo, messagelist[3])
+ if not commits:
+ self.msg(target, 'No commits found.')
+ return
+
+ msg = repo_commit_msg(repo, commits)
+ self.msg(target, msg)
class BotFactory(protocol.ReconnectingClientFactory):
protocol = Bot