From 33ebe7acec14b25c5f84f35a664803fcab2f7781 Mon Sep 17 00:00:00 2001 From: Michael Trier Date: Wed, 7 May 2008 16:49:48 -0400 Subject: initial project --- lib/git_python/__init__.py | 19 ++ lib/git_python/actor.py | 33 ++++ lib/git_python/blob.py | 131 ++++++++++++ lib/git_python/commit.py | 235 ++++++++++++++++++++++ lib/git_python/diff.py | 75 +++++++ lib/git_python/errors.py | 5 + lib/git_python/git.py | 72 +++++++ lib/git_python/head.py | 107 ++++++++++ lib/git_python/lazy.py | 26 +++ lib/git_python/method_missing.py | 21 ++ lib/git_python/repo.py | 417 +++++++++++++++++++++++++++++++++++++++ lib/git_python/stats.py | 17 ++ lib/git_python/tag.py | 85 ++++++++ lib/git_python/tree.py | 86 ++++++++ lib/git_python/utils.py | 8 + 15 files changed, 1337 insertions(+) create mode 100644 lib/git_python/__init__.py create mode 100644 lib/git_python/actor.py create mode 100644 lib/git_python/blob.py create mode 100644 lib/git_python/commit.py create mode 100644 lib/git_python/diff.py create mode 100644 lib/git_python/errors.py create mode 100644 lib/git_python/git.py create mode 100644 lib/git_python/head.py create mode 100644 lib/git_python/lazy.py create mode 100644 lib/git_python/method_missing.py create mode 100644 lib/git_python/repo.py create mode 100644 lib/git_python/stats.py create mode 100644 lib/git_python/tag.py create mode 100644 lib/git_python/tree.py create mode 100644 lib/git_python/utils.py (limited to 'lib/git_python') diff --git a/lib/git_python/__init__.py b/lib/git_python/__init__.py new file mode 100644 index 00000000..404cea12 --- /dev/null +++ b/lib/git_python/__init__.py @@ -0,0 +1,19 @@ +import inspect + +from actor import Actor +from blob import Blob +from commit import Commit +from diff import Diff +from errors import InvalidGitRepositoryError, NoSuchPathError +from git import Git +from head import Head +from repo import Repo +from stats import Stats +from tag import Tag +from tree import Tree +from utils import shell_escape, dashify, touch + +__all__ = [ name for name, obj in locals().items() + if not (name.startswith('_') or inspect.ismodule(obj)) ] + +__version__ = 'svn' diff --git a/lib/git_python/actor.py b/lib/git_python/actor.py new file mode 100644 index 00000000..235c3e21 --- /dev/null +++ b/lib/git_python/actor.py @@ -0,0 +1,33 @@ +import re + +class Actor(object): + def __init__(self, name, email): + self.name = name + self.email = email + + def __str__(self): + return self.name + + def __repr__(self): + return '">' % (self.name, self.email) + + @classmethod + def from_string(cls, string): + """ + Create an Actor from a string. + + ``str`` + is the string, which is expected to be in regular git format + + Format + John Doe + + Returns + Actor + """ + if re.search(r'<.+>', string): + m = re.search(r'(.*) <(.+?)>', string) + name, email = m.groups() + return Actor(name, email) + else: + return Actor(string, None) diff --git a/lib/git_python/blob.py b/lib/git_python/blob.py new file mode 100644 index 00000000..c89c3c3f --- /dev/null +++ b/lib/git_python/blob.py @@ -0,0 +1,131 @@ +import mimetypes +import re +import time +from actor import Actor +from commit import Commit + +class Blob(object): + DEFAULT_MIME_TYPE = "text/plain" + + def __init__(self, repo, **kwargs): + """ + Create an unbaked Blob containing just the specified attributes + + ``repo`` + is the Repo + + ``atts`` + is a dict of instance variable data + + Returns + GitPython.Blob + """ + self.id = None + self.mode = None + self.name = None + self.size = None + self.data_stored = None + + self.repo = repo + for k, v in kwargs.items(): + setattr(self, k, v) + + def __len__(self): + """ + The size of this blob in bytes + + Returns + int + """ + self.size = self.size or int(self.repo.git.cat_file(self.id, **{'s': True}).rstrip()) + return self.size + + @property + def data(self): + """ + The binary contents of this blob. + + Returns + str + """ + self.data_stored = self.data_stored or self.repo.git.cat_file(self.id, **{'p': True}) + return self.data_stored + + @property + def mime_type(self): + """ + The mime type of this file (based on the filename) + + Returns + str + """ + guesses = None + if self.name: + guesses = mimetypes.guess_type(self.name) + return guesses and guesses[0] or self.DEFAULT_MIME_TYPE + + @property + def basename(self): + return os.path.basename(self.name) + + @classmethod + def blame(cls, repo, commit, file): + """ + The blame information for the given file at the given commit + + Returns + list: [GitPython.Commit, list: []] + """ + data = repo.git.blame(commit, '--', file, **{'p': True}) + commits = {} + blames = [] + info = None + + for line in data.splitlines(): + parts = re.split(r'\s+', line, 1) + if re.search(r'^[0-9A-Fa-f]{40}$', parts[0]): + if re.search(r'^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$', line): + m = re.search(r'^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$', line) + id, origin_line, final_line, group_lines = m.groups() + info = {'id': id} + blames.append([None, []]) + elif re.search(r'^([0-9A-Fa-f]{40}) (\d+) (\d+)$', line): + m = re.search(r'^([0-9A-Fa-f]{40}) (\d+) (\d+)$', line) + id, origin_line, final_line = m.groups() + info = {'id': id} + elif re.search(r'^(author|committer)', parts[0]): + if re.search(r'^(.+)-mail$', parts[0]): + m = re.search(r'^(.+)-mail$', parts[0]) + info["%s_email" % m.groups()[0]] = parts[-1] + elif re.search(r'^(.+)-time$', parts[0]): + m = re.search(r'^(.+)-time$', parts[0]) + info["%s_date" % m.groups()[0]] = time.gmtime(int(parts[-1])) + elif re.search(r'^(author|committer)$', parts[0]): + m = re.search(r'^(author|committer)$', parts[0]) + info[m.groups()[0]] = parts[-1] + elif re.search(r'^filename', parts[0]): + info['filename'] = parts[-1] + elif re.search(r'^summary', parts[0]): + info['summary'] = parts[-1] + elif parts[0] == '': + if info: + c = commits.has_key(info['id']) and commits[info['id']] + if not c: + c = Commit(repo, **{'id': info['id'], + 'author': Actor.from_string(info['author'] + ' ' + info['author_email']), + 'authored_date': info['author_date'], + 'committer': Actor.from_string(info['committer'] + ' ' + info['committer_email']), + 'committed_date': info['committer_date'], + 'message': info['summary']}) + commits[info['id']] = c + + m = re.search(r'^\t(.*)$', line) + text, = m.groups() + blames[-1][0] = c + blames[-1][1] += text + info = None + + return blames + + def __repr__(self): + return '' % self.id diff --git a/lib/git_python/commit.py b/lib/git_python/commit.py new file mode 100644 index 00000000..0cd7715e --- /dev/null +++ b/lib/git_python/commit.py @@ -0,0 +1,235 @@ +import re +import time + +from actor import Actor +from lazy import LazyMixin +import tree +import diff +import stats + +class Commit(LazyMixin): + def __init__(self, repo, **kwargs): + """ + Instantiate a new Commit + + ``id`` + is the id of the commit + + ``parents`` + is a list of commit ids (will be converted into Commit instances) + + ``tree`` + is the correspdonding tree id (will be converted into a Tree object) + + ``author`` + is the author string + + ``authored_date`` + is the authored DateTime + + ``committer`` + is the committer string + + ``committed_date`` + is the committed DateTime + + ``message`` + is the first line of the commit message + + Returns + GitPython.Commit + """ + LazyMixin.__init__(self) + + self.repo = repo + self.id = None + self.tree = None + self.author = None + self.authored_date = None + self.committer = None + self.committed_date = None + self.message = None + self.parents = None + + for k, v in kwargs.items(): + setattr(self, k, v) + + if self.id: + if 'parents' in kwargs: + self.parents = map(lambda p: Commit(repo, **{'id': p}), kwargs['parents']) + if 'tree' in kwargs: + self.tree = tree.Tree(repo, **{'id': kwargs['tree']}) + + def __bake__(self): + temp = Commit.find_all(self.repo, self.id, **{'max_count': 1})[0] + self.parents = temp.parents + self.tree = temp.tree + self.author = temp.author + self.authored_date = temp.authored_date + self.committer = temp.committer + self.committed_date = temp.committed_date + self.message = temp.message + + @property + def id_abbrev(self): + return self.id[0:7] + + @classmethod + def count(cls, repo, ref): + """ + Count the number of commits reachable from this ref + + ``repo`` + is the Repo + + ``ref`` + is the ref from which to begin (SHA1 or name) + + Returns + int + """ + return len(repo.git.rev_list(ref).strip().splitlines()) + + @classmethod + def find_all(cls, repo, ref, **kwargs): + """ + Find all commits matching the given criteria. + ``repo`` + is the Repo + + ``ref`` + is the ref from which to begin (SHA1 or name) + + ``options`` + is a Hash of optional arguments to git where + ``max_count`` is the maximum number of commits to fetch + ``skip`` is the number of commits to skip + + Returns + GitPython.Commit[] + """ + options = {'pretty': 'raw'} + options.update(kwargs) + + output = repo.git.rev_list(ref, **options) + return cls.list_from_string(repo, output) + + @classmethod + def list_from_string(cls, repo, text): + """ + Parse out commit information into a list of Commit objects + + ``repo`` + is the Repo + + ``text`` + is the text output from the git command (raw format) + + Returns + GitPython.Commit[] + """ + lines = [l for l in text.splitlines() if l.strip()] + + commits = [] + + while lines: + id = lines.pop(0).split()[-1] + tree = lines.pop(0).split()[-1] + + parents = [] + while lines and re.search(r'^parent', lines[0]): + parents.append(lines.pop(0).split()[-1]) + author, authored_date = cls.actor(lines.pop(0)) + committer, committed_date = cls.actor(lines.pop(0)) + + messages = [] + while lines and re.search(r'^ {4}', lines[0]): + messages.append(lines.pop(0).strip()) + + message = messages and messages[0] or '' + + commits.append(Commit(repo, id=id, parents=parents, tree=tree, author=author, authored_date=authored_date, + committer=committer, committed_date=committed_date, message=message)) + + return commits + + @classmethod + def diff(cls, repo, a, b = None, paths = []): + """ + Show diffs between two trees: + + ``repo`` + is the Repo + + ``a`` + is a named commit + + ``b`` + is an optional named commit. Passing a list assumes you + wish to omit the second named commit and limit the diff to the + given paths. + + ``paths`` + is a list of paths to limit the diff. + + Returns + GitPython.Diff[] + """ + if isinstance(b, list): + paths = b + b = None + + if paths: + paths.insert(0, "--") + + if b: + paths.insert(0, b) + paths.insert(0, a) + text = repo.git.diff(*paths, **{'full_index': True}) + return diff.Diff.list_from_string(repo, text) + + @property + def diffs(self): + if not self.parents: + d = self.repo.git.show(self.id, **{'full_index': True, 'pretty': 'raw'}) + if re.search(r'diff --git a', d): + if not re.search(r'^diff --git a', d): + p = re.compile(r'.+?(diff --git a)', re.MULTILINE | re.DOTALL) + d = p.sub(r'diff --git a', d, 1) + else: + d = '' + return diff.Diff.list_from_string(self.repo, d) + else: + return self.diff(self.repo, self.parents[0].id, self.id) + + @property + def stats(self): + if not self.parents: + text = self.repo.git.diff(self.id, **{'numstat': True}) + text2 = "" + for line in text.splitlines(): + (insertions, deletions, filename) = line.split("\t") + text2 += "%s\t%s\t%s\n" % (deletions, insertions, filename) + text = text2 + else: + text = self.repo.git.diff(self.parents[0].id, self.id, **{'numstat': True}) + return stats.Stats.list_from_string(self.repo, text) + + def __str__(self): + """ Convert commit to string which is SHA1 """ + return self.id + + def __repr__(self): + return '' % self.id + + @classmethod + def actor(cls, line): + """ + Parse out the actor (author or committer) info + + Returns + [str (actor name and email), time (acted at time)] + """ + m = re.search(r'^.+? (.*) (\d+) .*$', line) + actor, epoch = m.groups() + return [Actor.from_string(actor), time.gmtime(int(epoch))] diff --git a/lib/git_python/diff.py b/lib/git_python/diff.py new file mode 100644 index 00000000..9d2690be --- /dev/null +++ b/lib/git_python/diff.py @@ -0,0 +1,75 @@ +import re +import commit + +class Diff(object): + def __init__(self, repo, a_path, b_path, a_commit, b_commit, a_mode, b_mode, new_file, deleted_file, diff): + self.repo = repo + self.a_path = a_path + self.b_path = b_path + + if not a_commit or re.search(r'^0{40}$', a_commit): + self.a_commit = None + else: + self.a_commit = commit.Commit(repo, **{'id': a_commit}) + if not b_commit or re.search(r'^0{40}$', b_commit): + self.b_commit = None + else: + self.b_commit = commit.Commit(repo, **{'id': b_commit}) + + self.a_mode = a_mode + self.b_mode = b_mode + self.new_file = new_file + self.deleted_file = deleted_file + self.diff = diff + + @classmethod + def list_from_string(cls, repo, text): + lines = text.splitlines() + a_mode = None + b_mode = None + diffs = [] + while lines: + m = re.search(r'^diff --git a/(\S+) b/(\S+)$', lines.pop(0)) + if m: + a_path, b_path = m.groups() + if re.search(r'^old mode', lines[0]): + m = re.search(r'^old mode (\d+)', lines.pop(0)) + if m: + a_mode, = m.groups() + m = re.search(r'^new mode (\d+)', lines.pop(0)) + if m: + b_mode, = m.groups() + if re.search(r'^diff --git', lines[0]): + diffs.append(Diff(repo, a_path, b_path, None, None, a_mode, b_mode, False, False, None)) + continue + + new_file = False + deleted_file = False + + if re.search(r'^new file', lines[0]): + m = re.search(r'^new file mode (.+)', lines.pop(0)) + if m: + b_mode, = m.groups() + a_mode = None + new_file = True + elif re.search(r'^deleted file', lines[0]): + m = re.search(r'^deleted file mode (.+)$', lines.pop(0)) + if m: + a_mode, = m.groups() + b_mode = None + deleted_file = True + + m = re.search(r'^index ([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+) ?(.+)?$', lines.pop(0)) + if m: + a_commit, b_commit, b_mode = m.groups() + if b_mode: + b_mode = b_mode.strip() + + diff_lines = [] + while lines and not re.search(r'^diff', lines[0]): + diff_lines.append(lines.pop(0)) + + diff = "\n".join(diff_lines) + diffs.append(Diff(repo, a_path, b_path, a_commit, b_commit, a_mode, b_mode, new_file, deleted_file, diff)) + + return diffs \ No newline at end of file diff --git a/lib/git_python/errors.py b/lib/git_python/errors.py new file mode 100644 index 00000000..4083841e --- /dev/null +++ b/lib/git_python/errors.py @@ -0,0 +1,5 @@ +class InvalidGitRepositoryError(Exception): + pass + +class NoSuchPathError(Exception): + pass diff --git a/lib/git_python/git.py b/lib/git_python/git.py new file mode 100644 index 00000000..4bc8760a --- /dev/null +++ b/lib/git_python/git.py @@ -0,0 +1,72 @@ +import os +import subprocess +import re +from utils import * +from method_missing import MethodMissingMixin + +class Git(MethodMissingMixin): + def __init__(self, git_dir): + super(Git, self).__init__() + self.git_dir = git_dir + + git_binary = "/usr/bin/env git" + + @property + def get_dir(self): + return self.git_dir + + def execute(self, command): + print command + proc = subprocess.Popen(command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + stdout_value = proc.communicate()[0] + return stdout_value + + def transform_kwargs(self, **kwargs): + """ + Transforms Python style kwargs into git command line options. + """ + args = [] + for k, v in kwargs.items(): + if len(k) == 1: + if v is True: + args.append("-%s" % k) + else: + args.append("-%s %r" % (k, v)) + else: + if v is True: + args.append("--%s" % dashify(k)) + else: + args.append("--%s=%r" % (dashify(k), v)) + return args + + def method_missing(self, method, *args, **kwargs): + """ + Run the given git command with the specified arguments and return + the result as a String + + ``method`` + is the command + + ``args`` + is the list of arguments + + ``kwargs`` + is a dict of keyword arguments + + Examples + git.rev_list('master', max_count=10, header=True) + + Returns + str + """ + opt_args = self.transform_kwargs(**kwargs) + ext_args = map(lambda a: (a == '--') and a or "%s" % shell_escape(a), args) + args = opt_args + ext_args + + call = "%s --git-dir=%s %s %s" % (self.git_binary, self.git_dir, dashify(method), ' '.join(args)) + stdout_value = self.execute(call) + return stdout_value diff --git a/lib/git_python/head.py b/lib/git_python/head.py new file mode 100644 index 00000000..cb432bd9 --- /dev/null +++ b/lib/git_python/head.py @@ -0,0 +1,107 @@ +import commit + +class Head(object): + """ + A Head is a named reference to a Commit. Every Head instance contains a name + and a Commit object. + + Examples:: + + >>> repo = Repo("/path/to/repo") + >>> head = repo.heads[0] + + >>> head.name + 'master' + + >>> head.commit + + + >>> head.commit.id + '1c09f116cbc2cb4100fb6935bb162daa4723f455' + """ + + def __init__(self, name, commit): + """ + Instantiate a new Head + + `name` + is the name of the head + + `commit` + is the Commit that the head points to + + Returns + GitPython.Head + """ + self.name = name + self.commit = commit + + @classmethod + def find_all(cls, repo, **kwargs): + """ + Find all Heads + + `repo` + is the Repo + + `kwargs` + is a dict of options + + Returns + GitPython.Head[] + """ + + options = {'sort': "committerdate", + 'format': "%(refname)%00%(objectname)"} + options.update(kwargs) + + output = repo.git.for_each_ref("refs/heads", **options) + return cls.list_from_string(repo, output) + + @classmethod + def list_from_string(cls, repo, text): + """ + Parse out head information into an array of baked head objects + + ``repo`` + is the Repo + ``text`` + is the text output from the git command + + Returns + GitPython.Head[] + """ + heads = [] + + for line in text.splitlines(): + heads.append(cls.from_string(repo, line)) + + return heads + + @classmethod + def from_string(cls, repo, line): + """ + Create a new Head instance from the given string. + + ``repo`` + is the Repo + + ``line`` + is the formatted head information + + Format + name: [a-zA-Z_/]+ + + id: [0-9A-Fa-f]{40} + + Returns + GitPython.Head + """ + print line + full_name, ids = line.split("\x00") + name = full_name.split("/")[-1] + c = commit.Commit(repo, **{'id': ids}) + return Head(name, c) + + def __repr__(self): + return '' % self.name diff --git a/lib/git_python/lazy.py b/lib/git_python/lazy.py new file mode 100644 index 00000000..f6d31e8d --- /dev/null +++ b/lib/git_python/lazy.py @@ -0,0 +1,26 @@ +class LazyMixin(object): + lazy_properties = [] + + def __init__(self): + self.__baked__ = False + + def __getattribute__(self, attr): + val = object.__getattribute__(self, attr) + if val is not None: + return val + else: + self.__prebake__() + return object.__getattribute__(self, attr) + + def __bake__(self): + """ This method should be overridden in the derived class. """ + raise NotImplementedError(" '__bake__' method has not been implemented.") + + def __prebake__(self): + if self.__baked__: + return + self.__bake__() + self.__baked__ = True + + def __bake_it__(self): + self.__baked__ = True diff --git a/lib/git_python/method_missing.py b/lib/git_python/method_missing.py new file mode 100644 index 00000000..63bc36a9 --- /dev/null +++ b/lib/git_python/method_missing.py @@ -0,0 +1,21 @@ +class MethodMissingMixin(object): + """ + A Mixin' to implement the 'method_missing' Ruby-like protocol. + + This was `taken from a blog post `_ + """ + def __getattribute__(self, attr): + try: + return object.__getattribute__(self, attr) + except: + class MethodMissing(object): + def __init__(self, wrapped, method): + self.__wrapped__ = wrapped + self.__method__ = method + def __call__(self, *args, **kwargs): + return self.__wrapped__.method_missing(self.__method__, *args, **kwargs) + return MethodMissing(self, attr) + + def method_missing(self, *args, **kwargs): + """ This method should be overridden in the derived class. """ + raise NotImplementedError(str(self.__wrapped__) + " 'method_missing' method has not been implemented.") diff --git a/lib/git_python/repo.py b/lib/git_python/repo.py new file mode 100644 index 00000000..a9bf30dd --- /dev/null +++ b/lib/git_python/repo.py @@ -0,0 +1,417 @@ +import os +import re +from errors import InvalidGitRepositoryError, NoSuchPathError +from utils import touch +from git import Git +from head import Head +from blob import Blob +from tag import Tag +from commit import Commit +from tree import Tree + +class Repo(object): + DAEMON_EXPORT_FILE = 'git-daemon-export-ok' + + def __init__(self, path): + """ + Create a new Repo instance + + ``path`` + is the path to either the root git directory or the bare git repo + + Examples:: + + repo = Repo("/Users/mtrier/Development/git-python") + repo = Repo("/Users/mtrier/Development/git-python.git") + + Returns + ``GitPython.Repo`` + """ + epath = os.path.abspath(path) + + if os.path.exists(os.path.join(epath, '.git')): + self.path = os.path.join(epath, '.git') + self.bare = False + elif os.path.exists(epath) and re.search('\.git$', epath): + self.path = epath + self.bare = True + elif os.path.exists(epath): + raise InvalidGitRepositoryError(epath) + else: + raise NoSuchPathError(epath) + self.git = Git(self.path) + + @property + def description(self): + """ + The project's description. Taken verbatim from GIT_REPO/description + + Returns + str + """ + try: + f = open(os.path.join(self.path, 'description')) + result = f.read() + return result.rstrip() + finally: + f.close() + + @property + def heads(self): + """ + A list of ``Head`` objects representing the branch heads in + this repo + + Returns + ``GitPython.Head[]`` + """ + return Head.find_all(self) + + # alias heads + branches = heads + + @property + def tags(self): + """ + A list of ``Tag`` objects that are available in this repo + + Returns + ``GitPython.Tag[]`` + """ + return Tag.find_all(self) + + def commits(self, start = 'master', max_count = 10, skip = 0): + """ + A list of Commit objects representing the history of a given ref/commit + + ``start`` + is the branch/commit name (default 'master') + + ``max_count`` + is the maximum number of commits to return (default 10) + + ``skip`` + is the number of commits to skip (default 0) + + Returns + ``GitPython.Commit[]`` + """ + options = {'max_count': max_count, + 'skip': skip} + + return Commit.find_all(self, start, **options) + + def commits_between(self, frm, to): + """ + The Commits objects that are reachable via ``to`` but not via ``frm`` + Commits are returned in chronological order. + + ``from`` + is the branch/commit name of the younger item + + ``to`` + is the branch/commit name of the older item + + Returns + ``GitPython.Commit[]`` + """ + return Commit.find_all(self, "%s..%s" % (frm, to)).reverse() + + def commits_since(self, start = 'master', since = '1970-01-01'): + """ + The Commits objects that are newer than the specified date. + Commits are returned in chronological order. + + ``start`` + is the branch/commit name (default 'master') + + ``since`` + is a string represeting a date/time + + Returns + ``GitPython.Commit[]`` + """ + options = {'since': since} + + return Commit.find_all(self, start, **options) + + def commit_count(self, start = 'master'): + """ + The number of commits reachable by the given branch/commit + + ``start`` + is the branch/commit name (default 'master') + + Returns + int + """ + return Commit.count(self, start) + + def commit(self, id): + """ + The Commit object for the specified id + + ``id`` + is the SHA1 identifier of the commit + + Returns + GitPython.Commit + """ + options = {'max_count': 1} + + return Commit.find_all(self, id, **options)[0] + + def commit_deltas_from(self, other_repo, ref = 'master', other_ref = 'master'): + """ + Returns a list of commits that is in ``other_repo`` but not in self + + Returns + ``GitPython.Commit[]`` + """ + repo_refs = self.git.rev_list(ref).strip().splitlines() + other_repo_refs = other_repo.git.rev_list(other_ref).strip().splitlines() + + diff_refs = list(set(other_repo_refs) - set(repo_refs)) + return map(lambda ref: Commit.find_all(other_repo, ref, **{'max_count': 1}[0]), diff_refs) + + def tree(self, treeish = 'master', paths = []): + """ + The Tree object for the given treeish reference + + ``treeish`` + is the reference (default 'master') + ``paths`` + is an optional Array of directory paths to restrict the tree (deafult []) + + Examples:: + + repo.tree('master', ['lib/']) + + + Returns + ``GitPython.Tree`` + """ + return Tree.construct(self, treeish, paths) + + def blob(self, id): + """ + The Blob object for the given id + + ``id`` + is the SHA1 id of the blob + + Returns + ``GitPython.Blob`` + """ + return Blob(self, **{'id': id}) + + def log(self, commit = 'master', path = None, **kwargs): + """ + The commit log for a treeish + + Returns + ``GitPython.Commit[]`` + """ + options = {'pretty': 'raw'} + options.update(kwargs) + if path: + arg = [commit, '--', path] + else: + arg = [commit] + commits = self.git.log(*arg, **options) + return Commit.list_from_string(self, commits) + + def diff(self, a, b, *paths): + """ + The diff from commit ``a`` to commit ``b``, optionally restricted to the given file(s) + + ``a`` + is the base commit + ``b`` + is the other commit + + ``paths`` + is an optional list of file paths on which to restrict the diff + """ + self.git.diff(a, b, '--', *paths) + + def commit_diff(self, commit): + """ + The commit diff for the given commit + ``commit`` is the commit name/id + + Returns + ``GitPython.Diff[]`` + """ + return Commit.diff(self, commit) + + @classmethod + def init_bare(self, path, **kwargs): + """ + Initialize a bare git repository at the given path + + ``path`` + is the full path to the repo (traditionally ends with /.git) + + ``kwargs`` + is any additional options to the git init command + + Examples:: + + GitPython.Repo.init_bare('/var/git/myrepo.git') + + Returns + ``GitPython.Repo`` (the newly created repo) + """ + git = Git(path) + git.init(**kwargs) + return Repo(path) + + def fork_bare(self, path, **kwargs): + """ + Fork a bare git repository from this repo + + ``path`` + is the full path of the new repo (traditionally ends with /.git) + + ``options`` + is any additional options to the git clone command + + Returns + ``GitPython.Repo`` (the newly forked repo) + """ + options = {'bare': True, 'shared': False} + options.update(kwargs) + self.git.clone(self.path, path, **options) + return Repo(path) + + def archive_tar(self, treeish = 'master', prefix = None): + """ + Archive the given treeish + + ``treeish`` + is the treeish name/id (default 'master') + + ``prefix`` + is the optional prefix + + Examples:: + + >>> repo.archive_tar + + + >>> repo.archive_tar('a87ff14') + + + >>> repo.archive_tar('master', 'myproject/') + + + Returns + str (containing tar archive) + """ + options = {} + if prefix: + options['prefix'] = prefix + return self.git.archive(treeish, **options) + + def archive_tar_gz(self, treeish = 'master', prefix = None): + """ + Archive and gzip the given treeish + + ``treeish`` + is the treeish name/id (default 'master') + + ``prefix`` + is the optional prefix + + Examples:: + + >>> repo.archive_tar_gz + + + >>> repo.archive_tar_gz('a87ff14') + + + >>> repo.archive_tar_gz('master', 'myproject/') + + + Returns + str (containing tar.gz archive) + """ + kwargs = {} + if prefix: + kwargs['prefix'] = prefix + self.git.archive(treeish, "| gzip", **kwargs) + + def enable_daemon_serve(self): + """ + Enable git-daemon serving of this repository by writing the + git-daemon-export-ok file to its git directory + + Returns + None + """ + if self.bare: + touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) + else: + touch(os.path.join(self.path, '.git', DAEMON_EXPORT_FILE)) + + def disable_daemon_serve(self): + """ + Disable git-daemon serving of this repository by ensuring there is no + git-daemon-export-ok file in its git directory + + Returns + None + """ + if self.bare: + return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) + else: + return os.remove(os.path.join(self.path, '.git', DAEMON_EXPORT_FILE)) + + def get_alternates(self): + """ + The list of alternates for this repo + + Returns + list[str] (pathnames of alternates) + """ + alternates_path = os.path.join(self.path, *['objects', 'info', 'alternates']) + + if os.path.exists(alternates_path): + try: + f = open(alternates_path) + alts = f.read() + finally: + f.close() + return alts.strip().splitlines() + else: + return [] + + def set_alternates(self, alts): + """ + Sets the alternates + + ``alts`` + is the Array of String paths representing the alternates + + Returns + None + """ + for alt in alts: + if not os.path.exists(alt): + raise NoSuchPathError("Could not set alternates. Alternate path %s must exist" % alt) + + if not alts: + os.remove(os.path.join(self.path, *['objects', 'info', 'alternates'])) + else: + try: + f = open(os.path.join(self.path, *['objects', 'info', 'alternates']), 'w') + f.write("\n".join(alts)) + finally: + f.close() + + alternates = property(get_alternates, set_alternates) + + def __repr__(self): + return '' % self.path diff --git a/lib/git_python/stats.py b/lib/git_python/stats.py new file mode 100644 index 00000000..f1e10348 --- /dev/null +++ b/lib/git_python/stats.py @@ -0,0 +1,17 @@ +class Stats(object): + def __init__(self, repo, total, files): + self.repo = repo + self.total = total + self.files = files + + @classmethod + def list_from_string(cls, repo, text): + hsh = {'total': {'insertions': 0, 'deletions': 0, 'lines': 0, 'files': 0}, 'files': {}} + for line in text.splitlines(): + (insertions, deletions, filename) = line.split("\t") + hsh['total']['insertions'] += int(insertions) + hsh['total']['deletions'] += int(deletions) + hsh['total']['lines'] = (hsh['total']['deletions'] + hsh['total']['insertions']) + hsh['total']['files'] += 1 + hsh['files'][filename.strip()] = {'insertions': int(insertions), 'deletions': int(deletions)} + return Stats(repo, hsh['total'], hsh['files']) \ No newline at end of file diff --git a/lib/git_python/tag.py b/lib/git_python/tag.py new file mode 100644 index 00000000..7da48d85 --- /dev/null +++ b/lib/git_python/tag.py @@ -0,0 +1,85 @@ +from commit import Commit + +class Tag(object): + def __init__(self, name, commit): + """ + Instantiate a new Tag + + ``name`` + is the name of the head + + ``commit`` + is the Commit that the head points to + + Returns + ``GitPython.Tag`` + """ + self.name = name + self.commit = commit + + @classmethod + def find_all(cls, repo, **kwargs): + """ + Find all Tags + + ``repo`` + is the Repo + + ``kwargs`` + is a dict of options + + Returns + ``GitPython.Tag[]`` + """ + options = {'sort': "committerdate", + 'format': "%(refname)%00%(objectname)"} + options.update(**kwargs) + + output = repo.git.for_each_ref("refs/tags", **options) + return cls.list_from_string(repo, output) + + @classmethod + def list_from_string(cls, repo, text): + """ + Parse out tag information into an array of baked Tag objects + + ``repo`` + is the Repo + + ``text`` + is the text output from the git command + + Returns + ``GitPython.Tag[]`` + """ + tags = [] + for line in text.splitlines(): + tags.append(cls.from_string(repo, line)) + return tags + + @classmethod + def from_string(cls, repo, line): + """ + Create a new Tag instance from the given string. + + ``repo`` + is the Repo + + ``line`` + is the formatted tag information + + Format + name: [a-zA-Z_/]+ + + id: [0-9A-Fa-f]{40} + + Returns + ``GitPython.Tag`` + """ + full_name, ids = line.split("\x00") + name = full_name.split("/")[-1] + commit = Commit(repo, **{'id': ids}) + return Tag(name, commit) + + def __repr__(self): + return '' % self.name diff --git a/lib/git_python/tree.py b/lib/git_python/tree.py new file mode 100644 index 00000000..e1afc7ff --- /dev/null +++ b/lib/git_python/tree.py @@ -0,0 +1,86 @@ +from lazy import LazyMixin +import blob + +class Tree(LazyMixin): + def __init__(self, repo, **kwargs): + LazyMixin.__init__(self) + self.repo = repo + self.id = None + self.contents = None + + for k, v in kwargs.items(): + setattr(self, k, v) + + def __bake__(self): + temp = Tree.construct(self.repo, self.id) + self.contents = temp.contents + + @classmethod + def construct(cls, repo, treeish, paths = []): + output = repo.git.ls_tree(treeish, *paths) + return Tree(repo, **{'id': treeish}).construct_initialize(repo, treeish, output) + + def construct_initialize(self, repo, id, text): + self.repo = repo + self.id = id + self.contents = [] + self.__baked__ = False + + for line in text.splitlines(): + self.contents.append(self.content_from_string(self.repo, line)) + + self.contents = [c for c in self.contents if c is not None] + + self.__bake_it__() + return self + + def content_from_string(self, repo, text): + """ + Parse a content item and create the appropriate object + + ``repo`` + is the Repo + + ``text`` + is the single line containing the items data in `git ls-tree` format + + Returns + ``GitPython.Blob`` or ``GitPython.Tree`` + """ + try: + mode, typ, id, name = text.expandtabs(1).split(" ", 4) + except: + return None + + if typ == "tree": + return Tree(repo, **{'id': id, 'mode': mode, 'name': name}) + elif typ == "blob": + return blob.Blob(repo, **{'id': id, 'mode': mode, 'name': name}) + elif typ == "commit": + return None + else: + raise(TypeError, "Invalid type: %s" % typ) + + def __div__(self, file): + """ + Find the named object in this tree's contents + + Examples:: + + >>> Repo('/path/to/python-git').tree/'lib' + + >>> Repo('/path/to/python-git').tree/'README.txt' + + + Returns + ``GitPython.Blob`` or ``GitPython.Tree`` or ``None`` if not found + """ + contents = [c for c in self.contents if c.name == file] + return contents and contents[0] or None + + @property + def basename(self): + os.path.basename(self.name) + + def __repr__(self): + return '' % self.id diff --git a/lib/git_python/utils.py b/lib/git_python/utils.py new file mode 100644 index 00000000..2bd6f2cd --- /dev/null +++ b/lib/git_python/utils.py @@ -0,0 +1,8 @@ +def shell_escape(string): + return str(string).replace("'", "\\\\'") + +def dashify(string): + return string.replace('_', '-') + +def touch(filename): + open(filename, "a").close() -- cgit v1.2.1