From b9b8d1b522009d9028749743c3046c564cbde918 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Fri, 4 Apr 2008 03:40:58 -0700 Subject: [PATCH] git.py: use a dispatcher to allow for more dynamism The goal of these changes is to allow runtime creation of arbitrary git commands. e.g. git.branch() should return the output of "git branch." We do this by using a module dispatcher/__getattr__ to intersept attributes. Eventually, the git.py module should be very tiny as the hand-coded methods are phased out. Signed-off-by: David Aguilar --- ugit/git.py | 27 +++++++++++++++++---------- ugit/models.py | 4 ++-- ugit/utils.py | 1 + 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ugit/git.py b/ugit/git.py index 4199832..d7b56f5 100644 --- a/ugit/git.py +++ b/ugit/git.py @@ -1,6 +1,6 @@ -'''TODO: "import stgit"''' import os import re +import sys import types import utils import defaults @@ -341,13 +341,6 @@ def rev_list_range(start, end): revs.append((rev_id, summary,) ) return revs -def show(sha1): - return git('show',sha1) - -def show_cdup(): - '''Returns a relative path to the git project root.''' - return git('rev-parse','--show-cdup') - def status(): '''RETURNS: A tuple of staged, unstaged and untracked files. ( array(staged), array(unstaged), array(untracked) )''' @@ -407,5 +400,19 @@ def status(): return( staged, unstaged, untracked ) -def tag(): - return git('tag').splitlines() + +class GitModuleDispatcher(object): + """This class wraps the git.py so that arbitrary git commands + can be supported at runtime.""" + + def __init__(self, module): + self.module = module + + def __getattr__(self, name): + if hasattr(self.module, name): + return getattr(self.module, name) + def run_git_cmd(*args, **kwargs): + return git(name.replace('_','-'), *args, **kwargs) + return run_git_cmd + +sys.modules[__name__] = GitModuleDispatcher(sys.modules[__name__]) diff --git a/ugit/models.py b/ugit/models.py index 8252535..a7d4985 100644 --- a/ugit/models.py +++ b/ugit/models.py @@ -18,7 +18,7 @@ class Model(model.Model): model.Model.__init__(self) # chdir to the root of the git tree. # This keeps paths relative. - cdup = git.show_cdup() + cdup = git.rev_parse(show_cdup=True) if cdup: os.chdir(cdup) @@ -349,7 +349,7 @@ class Model(model.Model): self.set_remotes(git.remote()) self.set_remote_branches(git.branch(remote=True)) self.set_local_branches(git.branch(remote=False)) - self.set_tags(git.tag()) + self.set_tags(git.tag().splitlines()) self.set_revision('') self.set_local_branch('') self.set_remote_branch('') diff --git a/ugit/utils.py b/ugit/utils.py index 6d86357..903c14d 100644 --- a/ugit/utils.py +++ b/ugit/utils.py @@ -81,6 +81,7 @@ def run_cmd(cmd, *args, **kwargs): kwarglist = [] for k,v in kwargs.iteritems(): if len(k) > 1: + k = k.replace('_','-') if v is True: kwarglist.append("--%s" % k) elif v is not None and type(v) is not bool: -- 2.11.4.GIT