diff options
author | Guido Günther <agx@sigxcpu.org> | 2011-03-18 11:12:22 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2011-03-18 19:59:52 +0100 |
commit | fadcfcb35b264f9e9d8762b590512f9049ecff5e (patch) | |
tree | 372b54e56be69b94d5c648f0bc41642c97c415ed | |
parent | ddf5ea3883723f9a64845511583b6dad8ff3600d (diff) |
gbp: Add git.archive() and git.{has,get,update,add}_submodules()
and testcases.
Heavily based on work by Sean Finney and Chow Loong Jin
-rw-r--r-- | gbp/git.py | 61 | ||||
-rw-r--r-- | tests/04_test_gbp_submodules.py | 84 |
2 files changed, 145 insertions, 0 deletions
@@ -482,6 +482,67 @@ class GitRepository(object): args.append(patch) GitCommand("apply", args)() + def archive(self, format, prefix, output, treeish, **kwargs): + args = [ '--format=%s' % format, '--prefix=%s' % prefix, + '--output=%s' % output, treeish ] + out, ret = self.__git_getoutput('archive', args, **kwargs) + if ret: + raise GitRepositoryError, "unable to archive %s"%(treeish) + + + def has_submodules(self): + """Does the repo have submodules""" + if os.path.exists('.gitmodules'): + return True + else: + return False + + + def add_submodule(self, repo_path): + """Add a submodule""" + GitCommand("submodule", [ "add", repo_path ])() + + + def update_submodules(self, init=True, recursive=True): + """Update all submodules""" + if not self.has_submodules(): + return + args = [ "update" ] + if recursive: + args.append("--recursive") + if init: + args.append("--init") + GitCommand("submodule", args)() + + + def get_submodules(self, treeish, path=None, recursive=True): + """ list the submodules of treeish + + returns a list of submodule/commit-id tuples + """ + # Note that we is lstree instead of submodule commands because + # there's no way to list the submodules of another branch with + # the latter. + submodules = [] + if path is None: + path = "." + + args = [ treeish ] + if recursive: + args += ['-r'] + + out, ret = self.__git_getoutput('ls-tree', args, cwd=path) + for line in out: + mode, objtype, commit, name = line.split() + # A submodules is shown as "commit" object in ls-tree: + if objtype == "commit": + nextpath = os.path.sep.join([path, name]) + submodules.append( (nextpath, commit) ) + if recursive: + submodules += self.get_submodules(commit, path=nextpath, + recursive=recursive) + return submodules + class FastImport(object): """Invoke git-fast-import""" diff --git a/tests/04_test_gbp_submodules.py b/tests/04_test_gbp_submodules.py new file mode 100644 index 00000000..c48cac45 --- /dev/null +++ b/tests/04_test_gbp_submodules.py @@ -0,0 +1,84 @@ +# vim: set fileencoding=utf-8 : + +import os +import shutil +import tempfile + +import gbp.git +import gbp.command_wrappers + +top = None + +repo = None +repo_dir = None + +submodule = None +submodule_dir = None + +def setup(): + global repo, repo_dir, submodule, submodule_dir, top + + top = os.path.abspath(os.curdir) + + repo_dir = os.path.join(top, 'gbp_%s_repo' % __name__) + repo = gbp.git.create_repo(repo_dir) + + submodule_dir = os.path.join(top, 'gbp_%s_submodule' % __name__) + submodule = gbp.git.create_repo(submodule_dir) + + os.chdir(repo_dir) + + +def teardown(): + os.chdir(top) + if not os.getenv("GBP_TESTS_NOCLEAN"): + if repo_dir: + shutil.rmtree(repo_dir) + if submodule_dir: + shutil.rmtree(submodule_dir) + + +def test_empty_has_submodules(): + """Test empty repo for submodules""" + assert not repo.has_submodules() + + +def _add_dummy_data(): + shutil.copy(".git/HEAD", "testfile") + gbp.command_wrappers.GitAdd()(['-f', '.']) + gbp.command_wrappers.GitCommand("commit", ["-mfoo", "-a"])() + + +def test_add_files(): + """Add some dummy data""" + _add_dummy_data() + assert True + + +def test_add_submodule_files(): + """Add some dummy data""" + os.chdir(submodule_dir) + _add_dummy_data() + os.chdir(repo_dir) + assert True + + +def test_add_submodule(): + """Add a submodule""" + repo.add_submodule(submodule_dir) + gbp.command_wrappers.GitCommand("commit", + ["-m 'Added submodule %s'" % submodule_dir, + "-a"])() + +def test_has_submodules(): + """Check for submodules""" + assert repo.has_submodules() + + +def test_get_submodules(): + """Check for submodules list of (name, hash)""" + submodule = repo.get_submodules("master")[0] + assert submodule[0] == './gbp_04_test_gbp_submodules_submodule' + assert len(submodule[1]) == 40 + +# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: |