From 7fc4a7e4ab4a3b05d9dd6704ecaefb2a8953551f Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Thu, 31 May 2012 14:55:54 +0200 Subject: gbp.git.repository: add GitRepository.make_tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Guido Günther --- gbp/git/repository.py | 23 +++++++++++++++++++++-- tests/test_GitRepository.py | 28 ++++++++++++---------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/gbp/git/repository.py b/gbp/git/repository.py index 1c6fc34d..8a99ff66 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -636,7 +636,6 @@ class GitRepository(object): @return: C{True} if the repository has that tree, C{False} otherwise @rtype: C{bool} """ - out, ret = self._git_getoutput('ls-tree', [ treeish ]) return [ True, False ][ret != 0] @@ -644,7 +643,7 @@ class GitRepository(object): """ Create a tree object from the current index - @param index_file: alternate index file to write the current index to + @param index_file: alternate index file to read changes from @type index_file: C{str} @return: the new tree object's sha1 @rtype: C{str} @@ -659,6 +658,26 @@ class GitRepository(object): raise GitRepositoryError("Can't write out current index") return tree[0].strip() + def make_tree(self, contents): + """ + Create a tree based on contents. I{contents} has the same format than + the I{GitRepository.list_tree} output. + """ + out='' + args = GitArgs('-z') + + for obj in contents: + mode, type, sha1, name = obj + out += '%s %s %s\t%s\0' % (mode, type, sha1, name) + + sha1, err, ret = self._git_inout('mktree', + args.args, + out, + capture_stderr=True) + if ret: + raise GitRepositoryError("Failed to mktree: '%s'" % err) + return sha1.strip() + def get_obj_type(self, obj): """ Get type of a git repository object diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py index 5393071e..121be901 100644 --- a/tests/test_GitRepository.py +++ b/tests/test_GitRepository.py @@ -617,33 +617,29 @@ def test_update_ref(): """ -def test_write_file(): +def test_make_tree(): """ - Test git-write-file + Test git-mk-tree Methods tested: - L{gbp.git.GitRepository.write_file} - - >>> import gbp.git - >>> repo = gbp.git.GitRepository(repo_dir) - >>> repo.write_file('testfile') - '19af7398c894bc5e86e17259317e4db519e9241f' - """ - - -def test_list_tree(): - """ - Test git-ls-tree - - Methods tested: - L{gbp.git.GitRepository.list_tree} + - L{gbp.git.GitRepository.make_tree} >>> import gbp.git >>> repo = gbp.git.GitRepository(repo_dir) - >>> repo.list_tree('HEAD') + >>> sha1 = repo.write_file('testfile') + >>> sha1 + '19af7398c894bc5e86e17259317e4db519e9241f' + >>> head = repo.list_tree('HEAD') + >>> head [['100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', 'testfile']] + >>> head.append(['100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', 'testfile2']) + >>> repo.make_tree(head) + '745951810c9e22fcc6de9b23f05efd6ab5512123' """ + def test_update_submodules(): """ Updating submodules if we don't have any is a noop -- cgit v1.2.3