From 447d3728b7933e93ac00949c8735f8cf416a4c06 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 1 Dec 2010 11:59:42 +0100 Subject: Merged the show and last commands with branch and tag completion, defaulting to 'master'. --- gitnoti.py | 80 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/gitnoti.py b/gitnoti.py index 6ffcec2..e1f2de3 100755 --- a/gitnoti.py +++ b/gitnoti.py @@ -53,6 +53,8 @@ if root[-1] == '/': repos = None +class GitnoitpyAmbiguousException(Exception): pass + class NotifyRepo(object): def __init__(self, bot, path): self.path = path @@ -241,33 +243,46 @@ class Bot(irc.IRCClient): return None def get_commits(self, repo, name): + branch = None try: if '..' in name: commits = list(repo.iter_commits(name)) else: - commits = [repo.commit(name)] + try: + commits = [repo.commit(name)] + branches = [b.name for b in repo.heads if b.name == name] + if len(branches) == 1: + branch = branches[0] + except git.exc.BadObject: + commits = [] + if not len(commits): + branches = self.get_branch(repo, name) + tags = self.get_tags(repo, name) + if len(branches) and len(tags): + raise GitnoitpyAmbiguousException('Ambiguous name: %s (\002branches\002 %s tags \002%s\002' % ( + name, ', '.join((x.name for x in branches)), ', '.join((x.name for x in tags)))) + elif len(branches) > 1: + raise GitnoitpyAmbiguousException('Ambiguous name: %s' % (', '.join((x.name for x in branches)))) + elif len(tags) > 1: + raise GitnoitpyAmbiguousException('Ambiguous name: %s' % (', '.join((x.name for x in tags)))) + elif len(branches): + commits, branch = [branches[0].commit], branches[0].name + elif len(tags): + commits = [tags[0].commit] except ValueError: - return [] + return [], None except git.exc.BadObject: - return [] + return [], None except git.exc.GitCommandError: - return [] - - return commits + return [], None - 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)) + return commits, branch - if not len(heads): - self.msg(target, 'No branches matches "%s".' % branch) + def get_branch(self, repo, branch): + return sorted((h for h in repo.heads if h.name.startswith(branch)), cmp = lambda a, b: cmp(a.name, b.name)) - if heads[0].name == branch: - return heads[0] - - if len(heads) > 1: - self.msg(target, 'Ambiguous name: %s' % (', '.join([h.name for h in heads]))) - else: - return heads[0] + def get_tags(self, repo, tag): + return sorted((t for t in repo.tags if t.name.startswith(tag)), cmp = lambda a, b: cmp(a.name, b.name)) def privmsg(self, user, channel, message): private = channel == self.nickname @@ -281,36 +296,29 @@ class Bot(irc.IRCClient): if cmd == 'list': 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 [BRANCH]' % self.nickname) - return - - repo = self.get_repo(messagelist[2].lower(), target) - if not repo: - return - - 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: - self.msg(target, 'Usage: %s show REPO COMMIT' % self.nickname) + if len(messagelist) < 3: + self.msg(target, 'Usage: %s show REPO [COMMIT|BRANCH|TAG|RANGE]' % self.nickname) return repo = self.get_repo(messagelist[2].lower(), target) if not repo: return - commits = self.get_commits(repo, messagelist[3]) + if len(messagelist) > 3: + try: + commits, branch = self.get_commits(repo, messagelist[3]) + except GitnoitpyAmbiguousException as e: + self.msg(target, e.message) + return + else: + commits, branch = self.get_commits(repo, 'master') + if not commits: self.msg(target, 'No commits found.') return - msg = repo_commit_msg(repo, None, commits) + msg = repo_commit_msg(repo, branch, commits) self.msg(target, msg) elif cmd == 'branches': if not len(messagelist) == 3: -- cgit v1.2.3