From ff5e354ae3d45b424976fe2319b5b3cd6dd95c26 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 13 Apr 2010 20:54:48 +0200 Subject: Support branches, second part. --- gitnoti.py | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/gitnoti.py b/gitnoti.py index cc78c4b..0e51bd5 100755 --- a/gitnoti.py +++ b/gitnoti.py @@ -55,9 +55,9 @@ def repo_commit_msg(repo, branch, commits): stat += ' \00303++%d\017' % insertions commit = commits[-1] - msg = '\002%s/%s\002 pushed to \002%s\002 by \002%s\002 (%s) %s' % ( + msg = '\002%s%s\002 pushed to \002%s\002 by \002%s\002 (%s) %s' % ( reponame, - branch, + ('/'+branch) if branch else '', commit.sha[:7], commit.committer.name if commit.author.name == commit.committer.name else '%s/%s' % (commit.committer.name, commit.author.name), stat, @@ -196,14 +196,34 @@ class Bot(irc.IRCClient): try: if '..' in name: frm, to = name.split('..') - commits = list(repo.commits_between(frm, to)) + commits = list(repo.iter_commits('%s..%s' % (frm, to))) else: - commits = repo.commits(name)[:1] + commits = [repo.commit(name)] + except ValueError: + return [] except git.errors.GitCommandError: return [] return commits + def get_branch(self, repo, branch, target): + heads = sorted((h for h in repo.heads if h.name.startswith(branch)), cmp = lambda a, b: cmp(a.name, b.name)) + + if not len(heads): + self.msg(target, 'No branches matches "%s".' % branch) + + if heads[0].name == branch: + return heads[0] + + heads = [h for h in repo.heads if h.name.startswith(branch)] + + if not len(heads): + self.msg(target, 'No branches matches "%s".' % branch) + elif len(heads) > 1: + self.msg(target, 'Ambiguous name: %s' % (', '.join([h.name for h in heads]))) + else: + return heads[0] + def privmsg(self, user, channel, message): private = channel == self.nickname nick = user.split('!')[0] @@ -217,15 +237,19 @@ 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': - if not len(messagelist) == 3: - self.msg(target, 'Usage: %s last REPO' % self.nickname) + if not len(messagelist) >= 3: + self.msg(target, 'Usage: %s last REPO [BRANCH]' % self.nickname) return repo = self.get_repo(messagelist[2].lower(), target) if not repo: return - msg = repo_commit_msg(repo, [repo.heads[0].commit]) if repo.heads else ('No commits found for %s' % os.path.splitext(os.path.basename(repo.working_dir))[0]) + branch = self.get_branch(repo, messagelist[3] if len(messagelist) == 4 else 'master', target) + if not branch: + return + + msg = repo_commit_msg(repo, branch.name, [branch.commit]) if repo.heads else ('No commits found for %s' % os.path.splitext(os.path.basename(repo.working_dir))[0]) self.msg(target, msg) elif cmd == 'show': if not len(messagelist) == 4: @@ -241,8 +265,19 @@ class Bot(irc.IRCClient): self.msg(target, 'No commits found.') return - msg = repo_commit_msg(repo, commits) + msg = repo_commit_msg(repo, None, commits) self.msg(target, msg) + elif cmd == 'branches': + if not len(messagelist) == 3: + self.msg(target, 'Usage: %s branches REPO') + return + + repo = self.get_repo(messagelist[2].lower(), target) + if not repo: + return + + reponame = os.path.splitext(os.path.basename(repo.working_dir))[0] + self.msg(target, '\002%s\002 has branches %s' % (reponame, ', '.join(['\002%s\002' % h.name for h in repo.heads]))) class BotFactory(protocol.ReconnectingClientFactory): protocol = Bot -- cgit v1.2.3