From 2da2b90e6930023ec5739ae0b714bbdb30874583 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 12:09:16 +0200 Subject: repo: removed a few methods because of redundancy or because it will be obsolete once the interface overhaul is finished. This commit is just intermediate --- lib/git/repo.py | 76 +++++++++++++++++---------------------------------------- 1 file changed, 22 insertions(+), 54 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index 1640fd32..40c71fd8 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -116,9 +116,12 @@ class Repo(object): """ return Tag.list_items(self) - def blame(self, commit, file): + def blame(self, ref, file): """ - The blame information for the given file at the given commit + The blame information for the given file at the given ref. + + ``ref`` + Ref object or Commit Returns list: [git.Commit, list: []] @@ -126,7 +129,7 @@ class Repo(object): changed within the given commit. The Commit objects will be given in order of appearance. """ - data = self.git.blame(commit, '--', file, p=True) + data = self.git.blame(ref, '--', file, p=True) commits = {} blames = [] info = None @@ -196,17 +199,17 @@ class Repo(object): # END distinguish hexsha vs other information return blames - def commits(self, start=None, path='', max_count=None, skip=0): + def commits(self, start=None, paths='', max_count=None, skip=0): """ A list of Commit objects representing the history of a given ref/commit ``start`` - is a ref to start the commits from. If start is None, + is a Ref or Commit to start the commits from. If start is None, the active branch will be used - ``path`` - is an optional path to limit the returned commits to - Commits that do not contain that path will not be returned. + ``paths`` + is an optional path or a list of paths to limit the returned commits to + Commits that do not contain that path or the paths will not be returned. ``max_count`` is the maximum number of commits to return (default None) @@ -225,63 +228,27 @@ class Repo(object): options.pop('max_count') if start is None: start = self.active_branch - return Commit.list_items(self, start, path, **options) + + return Commit.list_items(self, start, paths, **options) - def commits_between(self, frm, to): + def commits_between(self, frm, to, *args, **kwargs): """ 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 + is the Ref/Commit name of the younger item ``to`` - is the branch/commit name of the older item + is the Ref/Commit name of the older item Returns ``git.Commit[]`` """ return reversed(Commit.list_items(self, "%s..%s" % (frm, to))) - def commits_since(self, start='master', path='', 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') - - ``path`` - is an optinal path to limit the returned commits to. - - - ``since`` - is a string represeting a date/time - - Returns - ``git.Commit[]`` - """ - options = {'since': since} - - return Commit.list_items(self, start, path, **options) - def commit_count(self, start='master', path=''): - """ - The number of commits reachable by the given branch/commit - - ``start`` - is the branch/commit name (default 'master') - - ``path`` - is an optional path - Commits that do not contain the path will not contribute to the count. - - Returns - ``int`` - """ - return Commit.count(self, start, path) - - def commit(self, id=None, path = ''): + def commit(self, id=None, paths = ''): """ The Commit object for the specified id @@ -290,8 +257,9 @@ class Repo(object): if None, it defaults to the active branch - ``path`` - is an optional path, if set the returned commit must contain the path. + ``paths`` + is an optional path or a list of paths, + if set the returned commit must contain the path or paths Returns ``git.Commit`` @@ -300,7 +268,7 @@ class Repo(object): id = self.active_branch options = {'max_count': 1} - commits = Commit.list_items(self, id, path, **options) + commits = Commit.list_items(self, id, paths, **options) if not commits: raise ValueError, "Invalid identifier %s, or given path '%s' too restrictive" % ( id, path ) @@ -317,7 +285,7 @@ class Repo(object): 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.list_items(other_repo, ref, max_count=1)[0], diff_refs) + return map(lambda ref: Commit(other_repo, ref ), diff_refs) def tree(self, treeish=None): """ -- cgit v1.2.1 From 9ce1193c851e98293a237ad3d2d87725c501e89f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 14:11:34 +0200 Subject: Added Commit.iter_parents to iterate all parents Renamed Commit.commits to iter_commits repo: assured proper use of the terms revision ( rev ) and reference ( ref ) --- lib/git/repo.py | 120 +++++++++++++++++++++++++------------------------------- 1 file changed, 54 insertions(+), 66 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index 40c71fd8..9b947d60 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -116,12 +116,12 @@ class Repo(object): """ return Tag.list_items(self) - def blame(self, ref, file): + def blame(self, rev, file): """ - The blame information for the given file at the given ref. + The blame information for the given file at the given revision. - ``ref`` - Ref object or Commit + ``rev`` + revision specifier, see git-rev-parse for viable options. Returns list: [git.Commit, list: []] @@ -199,37 +199,29 @@ class Repo(object): # END distinguish hexsha vs other information return blames - def commits(self, start=None, paths='', max_count=None, skip=0): + def iter_commits(self, rev=None, paths='', **kwargs): """ A list of Commit objects representing the history of a given ref/commit - ``start`` - is a Ref or Commit to start the commits from. If start is None, - the active branch will be used + ``rev`` + revision specifier, see git-rev-parse for viable options. + If None, the active branch will be used. ``paths`` is an optional path or a list of paths to limit the returned commits to Commits that do not contain that path or the paths will not be returned. - - ``max_count`` - is the maximum number of commits to return (default None) - - ``skip`` - is the number of commits to skip (default 0) which will effectively - move your commit-window by the given number. + + ``kwargs`` + Arguments to be passed to git-rev-parse - common ones are + max_count and skip Returns ``git.Commit[]`` """ - options = {'max_count': max_count, - 'skip': skip} - - if max_count is None: - options.pop('max_count') - if start is None: - start = self.active_branch + if rev is None: + rev = self.active_branch - return Commit.list_items(self, start, paths, **options) + return Commit.list_items(self, rev, paths, **kwargs) def commits_between(self, frm, to, *args, **kwargs): """ @@ -248,50 +240,31 @@ class Repo(object): return reversed(Commit.list_items(self, "%s..%s" % (frm, to))) - def commit(self, id=None, paths = ''): + def commit(self, rev=None): """ - The Commit object for the specified id + The Commit object for the specified revision - ``id`` - is the SHA1 identifier of the commit or a ref or a ref name - if None, it defaults to the active branch + ``rev`` + revision specifier, see git-rev-parse for viable options. - - ``paths`` - is an optional path or a list of paths, - if set the returned commit must contain the path or paths - Returns ``git.Commit`` """ - if id is None: - id = self.active_branch - options = {'max_count': 1} - - commits = Commit.list_items(self, id, paths, **options) - - if not commits: - raise ValueError, "Invalid identifier %s, or given path '%s' too restrictive" % ( id, path ) - return commits[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 - git.Commit[] - """ - repo_refs = self.git.rev_list(ref, '--').strip().splitlines() - other_repo_refs = other_repo.git.rev_list(other_ref, '--').strip().splitlines() + if rev is None: + rev = self.active_branch + + # NOTE: currently we are not checking wheter rev really points to a commit + # If not, the system will barf on access of the object, but we don't do that + # here to safe cycles + c = Commit(self, rev) + return c - diff_refs = list(set(other_repo_refs) - set(repo_refs)) - return map(lambda ref: Commit(other_repo, ref ), diff_refs) - def tree(self, treeish=None): + def tree(self, ref=None): """ The Tree object for the given treeish reference - ``treeish`` + ``ref`` is a Ref instance defaulting to the active_branch if None. Examples:: @@ -305,27 +278,42 @@ class Repo(object): A ref is requried here to assure you point to a commit or tag. Otherwise it is not garantueed that you point to the root-level tree. - If you need a non-root level tree, find it by iterating the root tree. - """ - if treeish is None: - treeish = self.active_branch - if not isinstance(treeish, Ref): - raise ValueError( "Treeish reference required, got %r" % treeish ) + If you need a non-root level tree, find it by iterating the root tree. Otherwise + it cannot know about its path relative to the repository root and subsequent + operations might have unexpected results. + """ + if ref is None: + ref = self.active_branch + if not isinstance(ref, Reference): + raise ValueError( "Reference required, got %r" % ref ) # As we are directly reading object information, we must make sure # we truly point to a tree object. We resolve the ref to a sha in all cases # to assure the returned tree can be compared properly. Except for # heads, ids should always be hexshas - hexsha, typename, size = self.git.get_object_header( treeish ) + hexsha, typename, size = self.git.get_object_header( ref ) if typename != "tree": - hexsha, typename, size = self.git.get_object_header( str(treeish)+'^{tree}' ) + # will raise if this is not a valid tree + hexsha, typename, size = self.git.get_object_header( str(ref)+'^{tree}' ) # END tree handling - treeish = hexsha + ref = hexsha # the root has an empty relative path and the default mode - return Tree(self, treeish, 0, '') + return Tree(self, ref, 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 + git.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(other_repo, ref ), diff_refs) def diff(self, a, b, *paths): """ -- cgit v1.2.1 From 9f51eeb2f9a1efe22e45f7a1f7b963100f2f8e6b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 14:56:20 +0200 Subject: repo: removed commits_between but added a note about how this can be achieved using the iter_commits method; reorganized methods within the type as a start for more interface changes --- lib/git/repo.py | 424 +++++++++++++++++++++++++++----------------------------- 1 file changed, 206 insertions(+), 218 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index 9b947d60..9ac6ce0c 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -116,130 +116,6 @@ class Repo(object): """ return Tag.list_items(self) - def blame(self, rev, file): - """ - The blame information for the given file at the given revision. - - ``rev`` - revision specifier, see git-rev-parse for viable options. - - Returns - list: [git.Commit, list: []] - A list of tuples associating a Commit object with a list of lines that - changed within the given commit. The Commit objects will be given in order - of appearance. - """ - data = self.git.blame(ref, '--', file, p=True) - commits = {} - blames = [] - info = None - - for line in data.splitlines(False): - parts = self.re_whitespace.split(line, 1) - firstpart = parts[0] - if self.re_hexsha_only.search(firstpart): - # handles - # 634396b2f541a9f2d58b00be1a07f0c358b999b3 1 1 7 - indicates blame-data start - # 634396b2f541a9f2d58b00be1a07f0c358b999b3 2 2 - digits = parts[-1].split(" ") - if len(digits) == 3: - info = {'id': firstpart} - blames.append([None, []]) - # END blame data initialization - else: - m = self.re_author_committer_start.search(firstpart) - if m: - # handles: - # author Tom Preston-Werner - # author-mail - # author-time 1192271832 - # author-tz -0700 - # committer Tom Preston-Werner - # committer-mail - # committer-time 1192271832 - # committer-tz -0700 - IGNORED BY US - role = m.group(0) - if firstpart.endswith('-mail'): - info["%s_email" % role] = parts[-1] - elif firstpart.endswith('-time'): - info["%s_date" % role] = int(parts[-1]) - elif role == firstpart: - info[role] = parts[-1] - # END distinguish mail,time,name - else: - # handle - # filename lib/grit.rb - # summary add Blob - # - if firstpart.startswith('filename'): - info['filename'] = parts[-1] - elif firstpart.startswith('summary'): - info['summary'] = parts[-1] - elif firstpart == '': - if info: - sha = info['id'] - c = commits.get(sha) - if c is None: - c = Commit( self, id=sha, - 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[sha] = c - # END if commit objects needs initial creation - m = self.re_tab_full_line.search(line) - text, = m.groups() - blames[-1][0] = c - blames[-1][1].append( text ) - info = None - # END if we collected commit info - # END distinguish filename,summary,rest - # END distinguish author|committer vs filename,summary,rest - # END distinguish hexsha vs other information - return blames - - def iter_commits(self, rev=None, paths='', **kwargs): - """ - A list of Commit objects representing the history of a given ref/commit - - ``rev`` - revision specifier, see git-rev-parse for viable options. - If None, the active branch will be used. - - ``paths`` - is an optional path or a list of paths to limit the returned commits to - Commits that do not contain that path or the paths will not be returned. - - ``kwargs`` - Arguments to be passed to git-rev-parse - common ones are - max_count and skip - - Returns - ``git.Commit[]`` - """ - if rev is None: - rev = self.active_branch - - return Commit.list_items(self, rev, paths, **kwargs) - - def commits_between(self, frm, to, *args, **kwargs): - """ - The Commits objects that are reachable via ``to`` but not via ``frm`` - Commits are returned in chronological order. - - ``from`` - is the Ref/Commit name of the younger item - - ``to`` - is the Ref/Commit name of the older item - - Returns - ``git.Commit[]`` - """ - return reversed(Commit.list_items(self, "%s..%s" % (frm, to))) - - def commit(self, rev=None): """ The Commit object for the specified revision @@ -259,7 +135,6 @@ class Repo(object): c = Commit(self, rev) return c - def tree(self, ref=None): """ The Tree object for the given treeish reference @@ -302,6 +177,33 @@ class Repo(object): # the root has an empty relative path and the default mode return Tree(self, ref, 0, '') + def iter_commits(self, rev=None, paths='', **kwargs): + """ + A list of Commit objects representing the history of a given ref/commit + + ``rev`` + revision specifier, see git-rev-parse for viable options. + If None, the active branch will be used. + + ``paths`` + is an optional path or a list of paths to limit the returned commits to + Commits that do not contain that path or the paths will not be returned. + + ``kwargs`` + Arguments to be passed to git-rev-parse - common ones are + max_count and skip + + Note: to receive only commits between two named revisions, use the + "revA..revB" revision specifier + + Returns + ``git.Commit[]`` + """ + if rev is None: + rev = self.active_branch + + return Commit.iter_items(self, rev, paths, **kwargs) + 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 @@ -315,6 +217,102 @@ class Repo(object): diff_refs = list(set(other_repo_refs) - set(repo_refs)) return map(lambda ref: Commit(other_repo, ref ), diff_refs) + + def _get_daemon_export(self): + filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) + return os.path.exists(filename) + + def _set_daemon_export(self, value): + filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) + fileexists = os.path.exists(filename) + if value and not fileexists: + touch(filename) + elif not value and fileexists: + os.unlink(filename) + + daemon_export = property(_get_daemon_export, _set_daemon_export, + doc="If True, git-daemon may export this repository") + del _get_daemon_export + del _set_daemon_export + + def _get_alternates(self): + """ + The list of alternates for this repo from which objects can be retrieved + + Returns + list of strings being 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 at which + git should look for objects, i.e. /home/user/repo/.git/objects + + Raises + NoSuchPathError + + 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, doc="Retrieve a list of alternates paths or set a list paths to be used as alternates") + + @property + def is_dirty(self): + """ + Return the status of the index. + + Returns + ``True``, if the index has any uncommitted changes, + otherwise ``False`` + + NOTE + Working tree changes that have not been staged will not be detected ! + """ + if self.bare: + # Bare repositories with no associated working directory are + # always consired to be clean. + return False + + return len(self.git.diff('HEAD', '--').strip()) > 0 + + @property + def active_branch(self): + """ + The name of the currently active branch. + + Returns + Head to the active branch + """ + return Head( self, self.git.symbolic_ref('HEAD').strip() ) + + def diff(self, a, b, *paths): """ The diff from commit ``a`` to commit ``b``, optionally restricted to the given file(s) @@ -341,6 +339,89 @@ class Repo(object): ``git.Diff[]`` """ return Commit.diff(self, commit) + + def blame(self, rev, file): + """ + The blame information for the given file at the given revision. + + ``rev`` + revision specifier, see git-rev-parse for viable options. + + Returns + list: [git.Commit, list: []] + A list of tuples associating a Commit object with a list of lines that + changed within the given commit. The Commit objects will be given in order + of appearance. + """ + data = self.git.blame(rev, '--', file, p=True) + commits = {} + blames = [] + info = None + + for line in data.splitlines(False): + parts = self.re_whitespace.split(line, 1) + firstpart = parts[0] + if self.re_hexsha_only.search(firstpart): + # handles + # 634396b2f541a9f2d58b00be1a07f0c358b999b3 1 1 7 - indicates blame-data start + # 634396b2f541a9f2d58b00be1a07f0c358b999b3 2 2 + digits = parts[-1].split(" ") + if len(digits) == 3: + info = {'id': firstpart} + blames.append([None, []]) + # END blame data initialization + else: + m = self.re_author_committer_start.search(firstpart) + if m: + # handles: + # author Tom Preston-Werner + # author-mail + # author-time 1192271832 + # author-tz -0700 + # committer Tom Preston-Werner + # committer-mail + # committer-time 1192271832 + # committer-tz -0700 - IGNORED BY US + role = m.group(0) + if firstpart.endswith('-mail'): + info["%s_email" % role] = parts[-1] + elif firstpart.endswith('-time'): + info["%s_date" % role] = int(parts[-1]) + elif role == firstpart: + info[role] = parts[-1] + # END distinguish mail,time,name + else: + # handle + # filename lib/grit.rb + # summary add Blob + # + if firstpart.startswith('filename'): + info['filename'] = parts[-1] + elif firstpart.startswith('summary'): + info['summary'] = parts[-1] + elif firstpart == '': + if info: + sha = info['id'] + c = commits.get(sha) + if c is None: + c = Commit( self, id=sha, + 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[sha] = c + # END if commit objects needs initial creation + m = self.re_tab_full_line.search(line) + text, = m.groups() + blames[-1][0] = c + blames[-1][1].append( text ) + info = None + # END if we collected commit info + # END distinguish filename,summary,rest + # END distinguish author|committer vs filename,summary,rest + # END distinguish hexsha vs other information + return blames @classmethod def init_bare(self, path, mkdir=True, **kwargs): @@ -454,99 +535,6 @@ class Repo(object): gf.close() return sio.getvalue() - def _get_daemon_export(self): - filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) - return os.path.exists(filename) - - def _set_daemon_export(self, value): - filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) - fileexists = os.path.exists(filename) - if value and not fileexists: - touch(filename) - elif not value and fileexists: - os.unlink(filename) - - daemon_export = property(_get_daemon_export, _set_daemon_export, - doc="If True, git-daemon may export this repository") - del _get_daemon_export - del _set_daemon_export - - def _get_alternates(self): - """ - The list of alternates for this repo from which objects can be retrieved - - Returns - list of strings being 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 at which - git should look for objects, i.e. /home/user/repo/.git/objects - - Raises - NoSuchPathError - - 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, doc="Retrieve a list of alternates paths or set a list paths to be used as alternates") - - @property - def is_dirty(self): - """ - Return the status of the index. - - Returns - ``True``, if the index has any uncommitted changes, - otherwise ``False`` - - NOTE - Working tree changes that have not been staged will not be detected ! - """ - if self.bare: - # Bare repositories with no associated working directory are - # always consired to be clean. - return False - - return len(self.git.diff('HEAD', '--').strip()) > 0 - - @property - def active_branch(self): - """ - The name of the currently active branch. - - Returns - Head to the active branch - """ - return Head( self, self.git.symbolic_ref('HEAD').strip() ) def __repr__(self): return '' % self.path -- cgit v1.2.1 From 39229db70e71a6be275dafb3dd9468930e40ae48 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 15:42:00 +0200 Subject: Object can now create objects of the proper type in case one attempts to create an object directly - this feature is used in several places now, allowing for additional type-checking --- lib/git/repo.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index 9ac6ce0c..1d24edb7 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -129,10 +129,8 @@ class Repo(object): if rev is None: rev = self.active_branch - # NOTE: currently we are not checking wheter rev really points to a commit - # If not, the system will barf on access of the object, but we don't do that - # here to safe cycles - c = Commit(self, rev) + c = Object(self, rev) + assert c.type == "commit", "Revision %s did not point to a commit, but to %s" % (rev, c) return c def tree(self, ref=None): -- cgit v1.2.1 From 806450db9b2c4a829558557ac90bd7596a0654e0 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 16:13:51 +0200 Subject: repo.commit_delta_base: removed --- lib/git/repo.py | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index 1d24edb7..cfa73c43 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -202,20 +202,6 @@ class Repo(object): return Commit.iter_items(self, rev, paths, **kwargs) - 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 - git.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(other_repo, ref ), diff_refs) - - def _get_daemon_export(self): filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) return os.path.exists(filename) -- cgit v1.2.1 From 00c5497f190172765cc7a53ff9d8852a26b91676 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 16:52:20 +0200 Subject: repo: made init and clone methods less specific, previously they wanted to do it 'barely' only. New method names closely follow the default git command names --- lib/git/repo.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index cfa73c43..39b1cb50 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -408,52 +408,52 @@ class Repo(object): return blames @classmethod - def init_bare(self, path, mkdir=True, **kwargs): + def init(cls, path=None, mkdir=True, **kwargs): """ - Initialize a bare git repository at the given path + Initialize a git repository at the given path if specified ``path`` is the full path to the repo (traditionally ends with /.git) + or None in which case the repository will be created in the current + working directory ``mkdir`` if specified will create the repository directory if it doesn't - already exists. Creates the directory with a mode=0755. + already exists. Creates the directory with a mode=0755. + Only effective if a path is explicitly given ``kwargs`` - keyword arguments serving as additional options to the git init command + keyword arguments serving as additional options to the git-init command Examples:: - git.Repo.init_bare('/var/git/myrepo.git') + git.Repo.init('/var/git/myrepo.git',bare=True) Returns ``git.Repo`` (the newly created repo) """ - if mkdir and not os.path.exists(path): + if mkdir and path and not os.path.exists(path): os.makedirs(path, 0755) git = Git(path) - output = git.init('--bare', **kwargs) + output = git.init(path, **kwargs) return Repo(path) - create = init_bare - def fork_bare(self, path, **kwargs): + def clone(self, path, **kwargs): """ - Fork a bare git repository from this repo + Create a clone from this repository. ``path`` is the full path of the new repo (traditionally ends with /.git) ``kwargs`` - keyword arguments to be given to the git clone command + keyword arguments to be given to the git-clone command Returns - ``git.Repo`` (the newly forked repo) + ``git.Repo`` (the newly cloned repo) """ - options = {'bare': True} - options.update(kwargs) - self.git.clone(self.path, path, **options) + self.git.clone(self.path, path, **kwargs) return Repo(path) def archive_tar(self, treeish='master', prefix=None): -- cgit v1.2.1 From b67bd4c730273a9b6cce49a8444fb54e654de540 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 15 Oct 2009 18:05:28 +0200 Subject: Improved archive function by allowing it to directly write to an output stream - previously it would cache everything to memory and try to provide zipping functionality itself gitcmd: allows the output stream to be set explicitly which is mainly useful for archiving operations --- lib/git/repo.py | 67 +++++++++++++++++++++------------------------------------ 1 file changed, 25 insertions(+), 42 deletions(-) (limited to 'lib/git/repo.py') diff --git a/lib/git/repo.py b/lib/git/repo.py index 39b1cb50..554c10cb 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -456,48 +456,27 @@ class Repo(object): self.git.clone(self.path, path, **kwargs) 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 to prepend to each filename in the archive - - Examples:: - - >>> repo.archive_tar - - - >>> repo.archive_tar('a87ff14') - - >>> repo.archive_tar('master', 'myproject/') - - - Returns - str (containing bytes of tar archive) + def archive(self, ostream, treeish=None, prefix=None, **kwargs): """ - 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 + Archive the tree at the given revision. + ``ostream`` + file compatible stream object to which the archive will be written ``treeish`` - is the treeish name/id (default 'master') + is the treeish name/id, defaults to active branch ``prefix`` is the optional prefix to prepend to each filename in the archive + + ``kwargs`` + Additional arguments passed to git-archive + NOTE: Use the 'format' argument to define the kind of format. Use + specialized ostreams to write any format supported by python Examples:: - >>> repo.archive_tar_gz + >>> repo.archive(open("archive" >>> repo.archive_tar_gz('a87ff14') @@ -506,18 +485,22 @@ class Repo(object): >>> repo.archive_tar_gz('master', 'myproject/') - Returns - str (containing the bytes of tar.gz archive) + Raise + GitCommandError in case something went wrong + """ - kwargs = {} - if prefix: + if treeish is None: + treeish = self.active_branch + if prefix and 'prefix' not in kwargs: kwargs['prefix'] = prefix - resultstr = self.git.archive(treeish, **kwargs) - sio = StringIO.StringIO() - gf = gzip.GzipFile(fileobj=sio, mode ='wb') - gf.write(resultstr) - gf.close() - return sio.getvalue() + kwargs['as_process'] = True + kwargs['output_stream'] = ostream + + proc = self.git.archive(treeish, **kwargs) + status = proc.wait() + if status != 0: + raise GitCommandError( "git-archive", status, proc.stderr.read() ) + def __repr__(self): -- cgit v1.2.1