From f12c03f13a2f5dbc61a85034f76ade168f9f24d0 Mon Sep 17 00:00:00 2001 From: "Amlesh Sivanantham (zamlz)" Date: Thu, 27 Dec 2018 18:29:45 -0800 Subject: [PATCH] Added the git listener functions for the events that were prewritten --- pubs/plugs/git/git.py | 45 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/pubs/plugs/git/git.py b/pubs/plugs/git/git.py index 3ee3fb8..f761adb 100644 --- a/pubs/plugs/git/git.py +++ b/pubs/plugs/git/git.py @@ -3,14 +3,18 @@ import subprocess from pipes import quote as shell_quote from ...plugins import PapersPlugin +from ...events import RemoveEvent, RenameEvent, AddEvent class GitPlugin(PapersPlugin): name = 'git' + pubsdir = None def __init__(self, conf): self.description = "Run git commands in the pubs directory" + # Needed for the event listening + GitPlugin.pubsdir = conf['main']['pubsdir'] def update_parser(self, subparsers, conf): git_parser = self.parser(subparsers) @@ -24,9 +28,40 @@ class GitPlugin(PapersPlugin): def command(self, conf, args): """Runs the git program in a shell""" - subprocess.call( - 'pubs_git() {{\ngit -C {} $@\n}}\npubs_git {}'.format( - conf['main']['pubsdir'], - ' '.join([shell_quote(a) for a in args.arguments]) - ), shell=True) + for a in args.arguments: + print(a) + GitPlugin.shell(' '.join([shell_quote(a) for a in args.arguments])) + + @classmethod + def shell(cls, cmd): + subprocess.call('git -C {} {}'.format(cls.pubsdir, cmd), shell=True) + + +@RenameEvent.listen() +def git_rename(RenameEventInstance): + new_key = RenameEventInstance.paper.citekey + old_key = RenameEventInstance.old_citekey + + # Stage the changes and commit + GitPlugin.shell("add \*/{}.\*".format(old_key)) + GitPlugin.shell("add \*/{}.\*".format(new_key)) + GitPlugin.shell('commit -m "Renamed {} to {}"'.format(old_key, new_key) + + +@RemoveEvent.listen() +def git_remove(RemoveEventInstance): + citekey = RemoveEventInstance.old_citekey + + # Stage the changes and commit + GitPlugin.shell("add \*/{}.\*".format(citekey)) + GitPlugin.shell('commit -m "Removed {}"'.format(citekey)) + + +@AddEvent.listen() +def git_add(AddEventInstance): + citekey = AddEventInstance.citekey + + # Stage the changes and commit + GitPlugin.shell("add \*/{}.\*".format(citekey)) + GitPlugin.shell('commit -m "Added {}"'.format(citekey))