From 0478d0934ec8eb9f741aef6141b0f30d7cc03ca8 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 15 Apr 2011 17:00:11 -0400 Subject: [PATCH] more sub-commands for git; quote info in help, and add a reply function to irc actions to automatically reply to a nick --- irc/actions.py | 4 ++++ irc/commands/git.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- irc/commands/help.py | 4 ++-- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/irc/actions.py b/irc/actions.py index e35ba60..7cd4930 100644 --- a/irc/actions.py +++ b/irc/actions.py @@ -23,6 +23,10 @@ class Actions: """send a message""" self.send("PRIVMSG %s :%s" % (target, msg)) + def reply(self, target, nick, msg): + """send a message as a reply""" + self.say(target, "%s%s%s: %s" % (chr(2), nick, chr(0x0f), msg)) + def action(self, target, msg): """send a message as an action""" self.say(target,"%sACTION %s%s" % (chr(1), msg, chr(1))) diff --git a/irc/commands/git.py b/irc/commands/git.py index 2f0f83e..ea9dadd 100644 --- a/irc/commands/git.py +++ b/irc/commands/git.py @@ -31,9 +31,15 @@ def call(a, d): elif data.args[0] == "checkout": do_checkout() + elif data.args[0] == "delete": + do_delete() + elif data.args[0] == "pull": do_pull() + elif data.args[0] == "status": + do_status() + else: # they asked us to do something we don't know actions.say(data.chan, "\x02%s\x0F: unknown argument: \x0303%s\x0301." % (data.nick, data.args[0])) @@ -45,11 +51,20 @@ def exec_shell(command): def do_help(): """display all commands""" - help = ["\x0303branch\x0301 (show current branch)", "\x0303branches\x0301 (show all branches)", - "\x0303checkout\x0301 (switch branches)", "\x0303pull\x0301 (update current branch)"] - help = ', '.join(help) - - actions.say(data.chan, "\x02%s\x0F: sub-commands are: %s" % (data.nick, help)) + help_dict = { + "branch": "get current branch", + "branches": "get all branches", + "checkout": "switch branches", + "delete": "delete an old branch", + "pull": "update everything from the remote server", + "status": "check if we are out of date" + } + help = "" + for key in help_dict.keys(): + help += "\x0303%s\x0301: (%s)," % (key, help_dict[key]) + help = help[:-1] # trim last comma + + actions.say(data.chan, "\x02%s\x0F: sub-commands are: %s." % (data.nick, help)) def do_branch(): """get our current branch""" @@ -88,6 +103,25 @@ def do_checkout(): except subprocess.CalledProcessError: # git couldn't switch branches actions.say(data.chan, "\x02%s\x0F: branch \x0302%s\x0301 does not exist!" % (data.nick, branch)) +def do_delete(): + """delete a branch, while making sure that we are not on it""" + try: + delete_branch = data.args[1] + except IndexError: # no branch name provided + actions.say(data.chan, "\x02%s\x0F: delete which branch?" % data.nick) + return + + current_branch = exec_shell("git name-rev --name-only HEAD") + current_branch = current_branch[:-1] # strip newline + + if current_branch == delete_branch: + actions.say(data.chan, "\x02%s\x0F: you're currently on this branch; please checkout to a different branch before deleting." % data.nick) + return + + exec_shell("git branch -d %s" % delete_branch) + + actions.say(data.chan, "\x02%s\x0F: branch \x0302%s\x0301 has been deleted locally." % (data.nick, delete_branch)) + def do_pull(): """pull from remote repository""" branch = exec_shell("git name-rev --name-only HEAD") @@ -100,3 +134,11 @@ def do_pull(): actions.say(data.chan, "\x02%s\x0F: done; no new changes." % data.nick) else: actions.say(data.chan, "\x02%s\x0F: done; new changes merged." % data.nick) + +def do_status(): + """check whether we have anything to pull""" + result = exec_shell("git fetch --dry-run") + if not result: + actions.say(data.chan, "\x02%s\x0F: local copy is up-to-date with remote." % data.nick) + else: + actions.say(data.chan, "\x02%s\x0F: remote is ahead of local copy." % (data.nick)) diff --git a/irc/commands/help.py b/irc/commands/help.py index 0365565..fb0cbaf 100644 --- a/irc/commands/help.py +++ b/irc/commands/help.py @@ -19,7 +19,7 @@ def do_general_help(): def do_command_help(): command = data.args[0] - + try: exec "from irc.commands import %s as this_command" % command except ImportError: @@ -29,6 +29,6 @@ def do_command_help(): info = this_command.__doc__ if info: - actions.say(data.chan, "\x02%s\x0F: Info for command \x0303%s\x0301: %s" % (data.nick, command, info)) + actions.say(data.chan, "\x02%s\x0F: Info for command \"\x0303%s\x0301: %s\"" % (data.nick, command, info)) else: actions.say(data.chan, "\x02%s\x0F: Sorry, no information for \x0303%s\x0301." % (data.nick, command))