def get_config(self): from dulwich.config import ConfigFile try: with self._controltransport.get('config') as f: return ConfigFile.from_file(f) except NoSuchFile: return ConfigFile()
def testSubmodules(self): cf = ConfigFile.from_file(BytesIO(b"""\ [submodule "core/lib"] path = core/lib url = https://github.com/phhusson/QuasselC.git """)) got = list(parse_submodules(cf)) self.assertEqual([ (b'core/lib', b'https://github.com/phhusson/QuasselC.git', b'core/lib')], got)
def testSubmodules(self): cf = ConfigFile.from_file(BytesIO(b"""\ [submodule "core/lib"] path = core/lib url = https://github.com/phhusson/QuasselC.git """)) got = list(parse_submodules(cf)) self.assertEqual([ (b'core/lib', b'https://github.com/phhusson/QuasselC.git', b'core/lib')], got)
def get_nodes(self, path): """ Returns combined ``DirNode`` and ``FileNode`` objects list representing state of changeset at the given ``path``. If node at the given ``path`` is not instance of ``DirNode``, ChangesetError would be raised. """ if self._get_kind(path) != NodeKind.DIR: raise ChangesetError("Directory does not exist for revision %s at " " '%s'" % (self.revision, path)) path = path.rstrip('/') id = self._get_id_for_path(path) tree = self.repository._repo[id] dirnodes = [] filenodes = [] als = self.repository.alias for name, stat, id in tree.items(): obj_path = safe_str(name) if path != '': obj_path = '/'.join((path, obj_path)) if objects.S_ISGITLINK(stat): root_tree = self.repository._repo[self._tree_id] cf = ConfigFile.from_file( BytesIO( self.repository._repo.get_object( root_tree[b'.gitmodules'][1]).data)) url = ascii_str(cf.get(('submodule', obj_path), 'url')) dirnodes.append( SubModuleNode(obj_path, url=url, changeset=ascii_str(id), alias=als)) continue obj = self.repository._repo.get_object(id) if obj_path not in self._stat_modes: self._stat_modes[obj_path] = stat if isinstance(obj, objects.Tree): dirnodes.append(DirNode(obj_path, changeset=self)) elif isinstance(obj, objects.Blob): filenodes.append(FileNode(obj_path, changeset=self, mode=stat)) else: raise ChangesetError("Requested object should be Tree " "or Blob, is %r" % type(obj)) nodes = dirnodes + filenodes for node in nodes: if node.path not in self.nodes: self.nodes[node.path] = node nodes.sort() return nodes
def get_node(self, path): """ Returns ``Node`` object from the given ``path``. If there is no node at the given ``path``, ``ChangesetError`` would be raised. """ path = path.rstrip('/') if path not in self.nodes: try: id_ = self._get_id_for_path(path) except ChangesetError: raise NodeDoesNotExistError( "Cannot find one of parents' " "directories for a given path: %s" % path) stat = self._stat_modes.get(path) if stat and objects.S_ISGITLINK(stat): tree = self.repository._repo[self._tree_id] cf = ConfigFile.from_file( BytesIO( self.repository._repo.get_object( tree[b'.gitmodules'][1]).data)) url = ascii_str(cf.get(('submodule', path), 'url')) node = SubModuleNode(path, url=url, changeset=ascii_str(id_), alias=self.repository.alias) else: obj = self.repository._repo.get_object(id_) if isinstance(obj, objects.Tree): if path == '': node = RootNode(changeset=self) else: node = DirNode(path, changeset=self) node._tree = obj elif isinstance(obj, objects.Blob): node = FileNode(path, changeset=self) node._blob = obj else: raise NodeDoesNotExistError( "There is no file nor directory " "at the given path: '%s' at revision %s" % (path, self.short_id)) # cache node self.nodes[path] = node return self.nodes[path]
def upload(self, cmd: str, meta: dict): """Push the current state of the registry to Git.""" index = os.path.join(self.cached_repo, self.INDEX_FILE) if os.path.exists(index): os.remove(index) self._log.info("Writing the new index.json ...") with open(index, "w") as _out: json.dump(self.contents, _out) git.add(self.cached_repo, [index]) message = self.COMMIT_MESSAGES[cmd].format(**meta) if self.signoff: global_conf_path = os.path.expanduser("~/.gitconfig") if os.path.exists(global_conf_path): with open(global_conf_path, "br") as _in: conf = ConfigFile.from_file(_in) try: name = conf.get(b"user", b"name").decode() email = conf.get(b"user", b"email").decode() message += self.DCO_MESSAGE.format(name=name, email=email) except KeyError: self._log.warning( "Did not find name or email in %s, committing without DCO.", global_conf_path) else: self._log.warning( "Global git configuration file %s does not exist, " "committing without DCO.", global_conf_path) else: self._log.info("Committing the index without DCO.") git.commit(self.cached_repo, message=message) self._log.info("Pushing the updated index ...") # TODO: change when https://github.com/dulwich/dulwich/issues/631 gets addressed git.push(self.cached_repo, self.remote_url, b"master") if self._are_local_and_remote_heads_different(): self._log.error("Push has failed") raise ValueError("Push has failed")
def from_file(self, text): return ConfigFile.from_file(StringIO(text))
def from_file(self, text): return ConfigFile.from_file(BytesIO(text))
def from_file(self, text): return ConfigFile.from_file(StringIO(text))
def from_file(self, text): return ConfigFile.from_file(BytesIO(text))