def get_config(self): """Retrieve the config object. :return: `ConfigFile` object for the ``.git/config`` file. """ from dulwich.config import ConfigFile path = os.path.join(self._controldir, 'config') try: return ConfigFile.from_path(path) except (IOError, OSError) as e: if e.errno != errno.ENOENT: raise ret = ConfigFile() ret.path = path return ret
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 fetch_refs(remote_name = 'origin', local='.'): """ Fetch references from a Git remote repository :param remote_name: <str> git name of remote repository, _default='origin'_ :param local: <str> full path to local repository, _default='.'_ :return entries: <TreeEntry> named tuples """ #import rpdb; rpdb.set_trace() # **Fetch refs from remote** # create a dulwich Repo object from path to local repo r = Repo(local) # local repository objsto = r.object_store # create a ObjectStore object from the local repo determine_wants = objsto.determine_wants_all # built in dulwich function gitdir = os.path.join(local, r.controldir()) # the git folder cnf_file = os.path.join(gitdir, 'config') # path to config cnf = ConfigFile.from_path(cnf_file) # config remote = cnf.get(('remote', remote_name), 'url') # url of remote # correctly parse host path and create dulwich Client object from it client, host_path = get_transport_and_path(remote) remote_refs = client.fetch(host_path, r, determine_wants, sys.stdout.write) # **Store refs fetched by dulwich** dulwich_refs = os.path.join(gitdir, DULWICH_REFS) with open(dulwich_refs, 'wb') as file: writer = csv.writer(file, delimiter=' ') for key, value in remote_refs.items(): writer.writerow([key, value]) # **save remote refs shas for future checkout** remote_dir = os.path.join(gitdir, 'refs', 'remotes', remote_name) # .git/refs/remotes ensure_dir_exists(remote_dir) # built in dulwich function headref = 0 # head branch ref if remote_refs.has_key('HEAD'): headref = remote_refs.pop('HEAD') # sha of HEAD i_head = remote_refs.values().index(headref) # index of head ref head_branch = remote_refs.keys()[i_head] # name of head branch branch_key = head_branch.rsplit('/',1)[-1] # branch head_file = os.path.join(remote_dir, 'HEAD') # path to branch shas file head_ref = '/'.join(['refs','remotes',remote_name,branch_key]) with open(head_file, 'wb') as GitFile: GitFile.write('ref: ' + head_ref + '\n') # remote branch refs for key, value in remote_refs.items(): key = key.rsplit('/',1)[-1] # get just the remote's branch reffile = os.path.join(remote_dir, key) # path to branch shas file with open(reffile, 'wb') as GitFile: GitFile.write(value + '\n') if headref: remote_refs['HEAD'] = headref # restore HEAD sha return remote_refs
def _init_files(self, bare): """Initialize a default set of named files.""" from dulwich.config import ConfigFile self._put_named_file('description', b"Unnamed repository") f = BytesIO() cf = ConfigFile() cf.set(b"core", b"repositoryformatversion", b"0") if self._determine_file_mode(): cf.set(b"core", b"filemode", True) else: cf.set(b"core", b"filemode", False) cf.set(b"core", b"bare", bare) cf.set(b"core", b"logallrefupdates", True) cf.write_to_file(f) self._put_named_file('config', f.getvalue()) self._put_named_file(os.path.join('info', 'exclude'), b'')
def test_write_to_file_section(self): c = ConfigFile() c.set((b"core", ), b"foo", b"bar") f = BytesIO() c.write_to_file(f) self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
def test_set_hash_gets_quoted(self): c = ConfigFile() c.set(b"xandikos", b"color", b"#665544") f = BytesIO() c.write_to_file(f) self.assertEqual(b"[xandikos]\n\tcolor = \"#665544\"\n", f.getvalue())
def _init_files(self, bare): """Initialize a default set of named files.""" from dulwich.config import ConfigFile self._put_named_file('description', "Unnamed repository") f = StringIO() cf = ConfigFile() cf.set("core", "repositoryformatversion", "0") cf.set("core", "filemode", "true") cf.set("core", "bare", str(bare).lower()) cf.set("core", "logallrefupdates", "true") cf.write_to_file(f) self._put_named_file('config', f.getvalue()) self._put_named_file(os.path.join('info', 'exclude'), '')
def test_comment_before_section(self): cf = self.from_file(b"# foo\n[section]\n") self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
def from_file(self, text): return ConfigFile.from_file(StringIO(text))
def test_comment_after_variable(self): cf = self.from_file(b"[section]\nbar= foo # a comment\n") self.assertEqual(ConfigFile({(b"section", ): {b"bar": b"foo"}}), cf)
def test_write_to_file_subsection(self): c = ConfigFile() c.set(("branch", "blie"), "foo", "bar") f = BytesIO() c.write_to_file(f) self.assertEqual("[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
def gen_configs(): for path in config_file + self._config_files: try: yield ConfigFile.from_path(path) except (IOError, OSError, ValueError): continue
def test_comment_character_within_section_string(self): cf = self.from_file(b"[branch \"foo#bar\"] # a comment\nbar= foo\n") self.assertEqual( ConfigFile({(b"branch", b"foo#bar"): { b"bar": b"foo" }}), cf)
def test_comment_character_within_value_string(self): cf = self.from_file(b"[section]\nbar= \"foo#bar\"\n") self.assertEqual(ConfigFile({(b"section", ): { b"bar": b"foo#bar" }}), cf)
def test_write_to_file_empty(self): c = ConfigFile() f = StringIO() c.write_to_file(f) self.assertEquals("", f.getvalue())
def test_write_to_file_subsection(self): c = ConfigFile() c.set(("branch", "blie"), "foo", "bar") f = StringIO() c.write_to_file(f) self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
def _init_files(self, bare): """Initialize a default set of named files.""" from dulwich.config import ConfigFile self._put_named_file('description', b"Unnamed repository") f = BytesIO() cf = ConfigFile() cf.set(b"core", b"repositoryformatversion", b"0") cf.set(b"core", b"filemode", b"true") cf.set(b"core", b"bare", bare) cf.set(b"core", b"logallrefupdates", True) cf.write_to_file(f) self._put_named_file('config', f.getvalue()) self._put_named_file(os.path.join('info', 'exclude'), b'')
def test_empty_line_before_section(self): cf = self.from_file(b"\n[section]\n") self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
def __init__(self): from dulwich.config import ConfigFile BaseRepo.__init__(self, MemoryObjectStore(), DictRefsContainer({})) self._named_files = {} self.bare = True self._config = ConfigFile()
def test_comment_after_section(self): cf = self.from_file(b"[section] # foo\n") self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
def test_write_to_file_empty(self): c = ConfigFile() f = BytesIO() c.write_to_file(f) self.assertEqual(b"", f.getvalue())
def test_write_to_file_subsection(self): c = ConfigFile() c.set((b"branch", b"blie"), b"foo", b"bar") f = BytesIO() c.write_to_file(f) self.assertEqual(b'[branch "blie"]\n\tfoo = bar\n', f.getvalue())
def test_write_to_file_subsection(self): c = ConfigFile() c.set((b"branch", b"blie"), b"foo", b"bar") f = BytesIO() c.write_to_file(f) self.assertEqual(b"[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
def test_set_hash_gets_quoted(self): c = ConfigFile() c.set(b"xandikos", b"color", b"#665544") f = BytesIO() c.write_to_file(f) self.assertEqual(b'[xandikos]\n\tcolor = "#665544"\n', f.getvalue())
def from_file(self, text): return ConfigFile.from_file(BytesIO(text))
def test_empty(self): ConfigFile()
def test_eq(self): self.assertEqual(ConfigFile(), ConfigFile())
def test_write_to_file_section(self): c = ConfigFile() c.set(("core", ), "foo", "bar") f = StringIO() c.write_to_file(f) self.assertEquals("[core]\nfoo = bar\n", f.getvalue())
def test_from_file_empty(self): cf = self.from_file(b"") self.assertEqual(ConfigFile(), cf)
def test_write_to_file_section(self): c = ConfigFile() c.set(("core", ), "foo", "bar") f = StringIO() c.write_to_file(f) self.assertEqual("[core]\n\tfoo = bar\n", f.getvalue())