def _tree_from_structure(self, structure): # TODO : Support directories tree = Tree() for file_info in structure: # str only try: data = file_info["data"].encode("ascii") name = file_info["name"].encode("ascii") mode = file_info["mode"] except: # Skip file on encoding errors continue blob = Blob() blob.data = data # Store file's contents self.repo.object_store.add_object(blob) # Add blob entry tree.add(name, mode, blob.id) # Store tree self.repo.object_store.add_object(tree) return tree.id
def _update_file(self, name, subdir, filename, data, commit_msg): # first, create a new blob for the data blob = Blob.from_string(data.encode('utf-8')) # fetch the old tree object, add new page try: subdir_tree = _walk_git_repo_tree(self.repo, self.current_tree, subdir) except KeyError: # we need to create the subdir_tree as well, since it does not exist # yet subdir_tree = Tree() subdir_tree.add(_git_default_file_mode, filename, blob.id) # create new root tree tree = self.current_tree tree.add(stat.S_IFDIR, subdir, subdir_tree.id) # create commit commit = Commit() commit.parents = [self.current_commit.id] commit.tree = tree.id commit.author = commit.committer = self.wiki_user commit.commit_time = commit.author_time = int(time.time()) commit.commit_timezone = commit.author_timezone = parse_timezone(time.timezone)[0] commit.encoding = 'UTF-8' commit.message = commit_msg.encode('utf-8') # store all objects self.repo.object_store.add_object(blob) self.repo.object_store.add_object(subdir_tree) self.repo.object_store.add_object(tree) self.repo.object_store.add_object(commit) # update the branch self.repo.refs[self.head] = commit.id
def __init_code__(): # initialize the repo if it doesn't exists, or load it if it does if not path.exists(LOGS_PATH): print "creating folder "+ LOGS_PATH mkdir(LOGS_PATH) repo = Repo.init(LOGS_PATH) blob = Blob.from_string("data") tree =Tree() tree.add(0100644, "initfile", blob.id) c = Commit() c.tree = tree.id author = "Writer [email protected]" c.author=c.committer=author c.commit_time=c.author_time=int(time()) tz = parse_timezone('+0200') c.commit_timezone=c.author_timezone=tz c.encoding="UTF-8" c.message="initial commit" store = repo.object_store store.add_object(blob) store.add_object(tree) store.add_object(c) repo.refs['refs/heads/master'] = c.id repo.refs['HEAD'] = 'ref: refs/heads/master' print "success!" else: #this is how to create a Repo object from an existing repository from dulwich.errors import NotGitRepository try: repo = Repo(LOGS_PATH) except NotGitRepository as e: raise GitFileError("Error: the path %s exists but is not a git repository."%LOGS_PATH) return repo
def init_the_git(config): path = config.get('Local', 'path') repo = Repo.init(path) blob = Blob.from_string(open(os.path.join(path, '.git-dropbox.cnf')).read()) tree = Tree() tree.add(".git-dropbox.cnf", 0100644, blob.id) commit = Commit() commit.tree = tree.id commit.author = config.get('Local', 'user') commit.committer = 'Git-dropbox' commit.commit_time = int(time()) commit.author_time = os.path.getctime(os.path.join(path, '.git-dropbox.cnf')) commit.commit_timezone = commit.author_timezone = parse_timezone('-0200')[0] commit.encoding = 'UTF-8' commit.message = 'Initial commit' object_store = repo.object_store object_store.add_object(blob) object_store.add_object(tree) object_store.add_object(commit) repo.refs['refs/heads/master'] = commit.id
def create_commit(data, marker='Default', blob=None): if not blob: blob = Blob.from_string('The blob content %s' % marker) tree = Tree() tree.add("thefile_%s" % marker, 0o100644, blob.id) cmt = Commit() if data: assert isinstance(data[-1], Commit) cmt.parents = [data[-1].id] cmt.tree = tree.id author = "John Doe %s <*****@*****.**>" % marker cmt.author = cmt.committer = author tz = parse_timezone('-0200')[0] cmt.commit_time = cmt.author_time = int(time()) cmt.commit_timezone = cmt.author_timezone = tz cmt.encoding = "UTF-8" cmt.message = "The commit message %s" % marker tag = Tag() tag.tagger = "*****@*****.**" tag.message = "Annotated tag" tag.tag_timezone = parse_timezone('-0200')[0] tag.tag_time = cmt.author_time tag.object = (Commit, cmt.id) tag.name = "v_%s_0.1" % marker return blob, tree, tag, cmt
def test_add(self): myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86" x = Tree() x.add("myname", 0100755, myhexsha) self.assertEquals(x["myname"], (0100755, myhexsha)) self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha), x.as_raw_string())
def test_emit_commit(self): b = Blob() b.data = "FOO" t = Tree() t.add(stat.S_IFREG | 0644, "foo", b.id) c = Commit() c.committer = c.author = "Jelmer <jelmer@host>" c.author_time = c.commit_time = 1271345553 c.author_timezone = c.commit_timezone = 0 c.message = "msg" c.tree = t.id self.store.add_objects([(b, None), (t, None), (c, None)]) self.fastexporter.emit_commit(c, "refs/heads/master") self.assertEquals("""blob mark :1 data 3 FOO commit refs/heads/master mark :2 author Jelmer <jelmer@host> 1271345553 +0000 committer Jelmer <jelmer@host> 1271345553 +0000 data 3 msg M 644 1 foo """, self.stream.getvalue())
def test_add(self): myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86' x = Tree() x.add(b'myname', 0o100755, myhexsha) self.assertEqual(x[b'myname'], (0o100755, myhexsha)) self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha), x.as_raw_string())
def git_repo_init(gitdir): os.mkdir(gitdir) repo = Repo.init_bare(gitdir) blob = Blob.from_string("""Why, Hello there! This is your friendly Legislation tracker, Billy here. This is a git repo full of everything I write to the DB. This isn't super useful unless you're debugging production issues. Fondly, Bill, your local Billy instance.""") tree = Tree() tree.add("README", 0100644, blob.id) commit = Commit() commit.tree = tree.id author = "Billy <billy@localhost>" commit.author = commit.committer = author commit.commit_time = commit.author_time = int(time()) tz = parse_timezone('-0400')[0] commit.commit_timezone = commit.author_timezone = tz commit.encoding = "UTF-8" commit.message = "Initial commit" repo.object_store.add_object(blob) repo.object_store.add_object(tree) repo.object_store.add_object(commit) repo.refs['refs/heads/master'] = commit.id
def test_commit_no_parent(self): a = Blob.from_string(b"The Foo\n") ta = Tree() ta.add(b"somename", 0o100644, a.id) ca = make_commit(tree=ta.id) self.repo.object_store.add_objects([(a, None), (ta, None), (ca, None)]) outstream = StringIO() porcelain.show(self.repo.path, objects=[ca.id], outstream=outstream) self.assertMultiLineEqual(outstream.getvalue(), """\ -------------------------------------------------- commit: 344da06c1bb85901270b3e8875c988a027ec087d Author: Test Author <*****@*****.**> Committer: Test Committer <*****@*****.**> Date: Fri Jan 01 2010 00:00:00 +0000 Test message. diff --git /dev/null b/somename new mode 100644 index 0000000..ea5c7bf 100644 --- /dev/null +++ b/somename @@ -0,0 +1 @@ +The Foo """)
def test_simple(self): myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86' x = Tree() x[b'myname'] = (0o100755, myhexsha) self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha), x.as_raw_string()) self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha), bytes(x))
def create_repo(design, initialize): full_path = os.path.join(settings.GIT_ROOT, design.repo_path) os.makedirs(full_path) repo = Repo.init_bare(full_path) if initialize: blob = Blob.from_string(str("%s Git-a-thing design repository\n" % design.name)) tree = Tree() tree.add("README", 0100644, blob.id) do_commit(repo, tree, [blob], "Initialize repository", settings.GITATHING_COMMITER)
def test_full_tree(self): c = self.make_commit(commit_time=30) t = Tree() t.add(b'data-x', 0o644, Blob().id) c.tree = t c1 = Commit() c1.set_raw_string(c.as_raw_string()) self.assertEqual(t.id, c1.tree) self.assertEqual(c.as_raw_string(), c1.as_raw_string())
def _get_example_tar_stream(self, *tar_stream_args, **tar_stream_kwargs): store = MemoryObjectStore() b1 = Blob.from_string(b"somedata") store.add_object(b1) t1 = Tree() t1.add(b"somename", 0o100644, b1.id) store.add_object(t1) stream = b''.join( tar_stream(store, t1, *tar_stream_args, **tar_stream_kwargs)) return BytesIO(stream)
def write_git_tree(self): git_tree = GitTree() for key, entry in self._entries: git_tree.add( name=key, mode=entry.mode, hexsha=entry.sha, ) self._git_store.add_object(git_tree) return git_tree.id
def test_add_old_order(self): myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86" x = Tree() warnings.simplefilter("ignore", DeprecationWarning) try: x.add(0100755, "myname", myhexsha) finally: warnings.resetwarnings() self.assertEqual(x["myname"], (0100755, myhexsha)) self.assertEqual("100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())
def test_git_dir(self): obj = Tree() a = Blob() a.data = b"foo" obj.add(b".git", 0o100644, a.id) self.repo.object_store.add_objects( [(a, None), (obj, None)]) self.assertEqual( [(obj.id, 'invalid name .git')], [(sha, str(e)) for (sha, e) in porcelain.fsck(self.repo)])
def build_tree(path): tree = Tree() for basename, entry in trees[path].items(): if isinstance(entry, dict): mode = stat.S_IFDIR sha = build_tree(pathjoin(path, basename)) else: (mode, sha) = entry tree.add(basename, mode, sha) object_store.add_object(tree) return tree.id
def test_add_old_order(self): myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86' x = Tree() warnings.simplefilter("ignore", DeprecationWarning) try: x.add(0o100755, b'myname', myhexsha) finally: warnings.resetwarnings() self.assertEqual(x[b'myname'], (0o100755, myhexsha)) self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha), x.as_raw_string())
def build_tree(path): tree = Tree() for basename, entry in trees[path].iteritems(): if type(entry) == dict: mode = stat.S_IFDIR sha = build_tree(os.path.join(path, basename)) else: (mode, sha) = entry tree.add(mode, basename, sha) object_store.add_object(tree) return tree.id
def store_tree(node): tree = Tree() for name in node: if isinstance(node[name], dict): tree.add(name.encode(args.encoding), MODE_TREE, store_tree(node[name]).id) else: tree.add(name.encode(args.encoding), *node[name]) repo.object_store.add_object(tree) return tree
def test_simple(self): store = MemoryObjectStore() b1 = Blob.from_string(b"somedata") store.add_object(b1) t1 = Tree() t1.add(b"somename", 0o100644, b1.id) store.add_object(t1) stream = b''.join(tar_stream(store, t1, 10)) out = BytesIO(stream) tf = tarfile.TarFile(fileobj=out) self.addCleanup(tf.close) self.assertEqual(["somename"], tf.getnames())
def test_submodule_not_checked_out(self): a = Blob.from_string(b'irrelevant\n') with self.wt.lock_tree_write(): (index, index_path) = self.wt._lookup_index(b'a') index[b'a'] = IndexEntry(0, 0, 0, 0, S_IFGITLINK, 0, 0, 0, a.id, 0, 0) self.wt._index_dirty = True os.mkdir(self.wt.abspath('a')) t = Tree() t.add(b"a", S_IFGITLINK, a.id) self.store.add_object(t) self.expectDelta([], tree_id=t.id)
def test_import_tree_with_unusual_mode_file(self): blob = Blob.from_string(b"bar1") tree = Tree() tree.add(b"foo", stat.S_IFREG | 0o664, blob.id) objects = {blob.id: blob, tree.id: tree} ret, child_modes = import_git_tree(self._texts, self._mapping, b"bla", b"bla", (None, tree.id), None, None, b"somerevid", [ ], objects.__getitem__, (None, stat.S_IFDIR), DummyStoreUpdater( ), self._mapping.generate_file_id) self.assertEqual(child_modes, {b"bla/foo": stat.S_IFREG | 0o664})
def create_commit(marker=None): blob = Blob.from_string(b'The blob content ' + marker) tree = Tree() tree.add(b"thefile " + marker, 0o100644, blob.id) cmt = Commit() cmt.tree = tree.id cmt.author = cmt.committer = b"John Doe <*****@*****.**>" cmt.message = marker tz = parse_timezone(b'-0200')[0] cmt.commit_time = cmt.author_time = int(time.time()) cmt.commit_timezone = cmt.author_timezone = tz return cmt, tree, blob
def test_submodule(self): self.subtree = self.make_branch_and_tree('a', format="git") a = Blob.from_string(b'irrelevant\n') self.build_tree_contents([('a/.git/HEAD', a.id)]) with self.wt.lock_tree_write(): (index, index_path) = self.wt._lookup_index(b'a') index[b'a'] = IndexEntry(0, 0, 0, 0, S_IFGITLINK, 0, 0, 0, a.id, 0) self.wt._index_dirty = True t = Tree() t.add(b"a", S_IFGITLINK, a.id) self.store.add_object(t) self.expectDelta([], tree_id=t.id)
def create_commit(marker=None): blob = Blob.from_string('The blob content %s' % marker) tree = Tree() tree.add("thefile %s" % marker, 0o100644, blob.id) cmt = Commit() cmt.tree = tree.id cmt.author = cmt.committer = "John Doe <*****@*****.**>" cmt.message = "%s" % marker tz = parse_timezone('-0200')[0] cmt.commit_time = cmt.author_time = int(time.time()) cmt.commit_timezone = cmt.author_timezone = tz return cmt, tree, blob
def test_tree_copy_after_update(self): """Check Tree.id is correctly updated when the tree is copied after updated. """ shas = [] tree = Tree() shas.append(tree.id) tree.add(b'data', 0o644, Blob().id) copied = tree.copy() shas.append(tree.id) shas.append(copied.id) self.assertNotIn(shas[0], shas[1:]) self.assertEqual(shas[1], shas[2])
def compute_tree_hash(dirname): tree = Tree() for entry in sorted(os.listdir(dirname)): fname = os.path.join(dirname, entry) if os.path.isdir(fname): thash = GitRepo.compute_tree_hash(fname) mode = stat.S_IFDIR # os.stat(fname)[stat.ST_MODE] tree.add(entry, mode, thash) elif os.path.isfile(fname): bhash = GitRepo.compute_blob_hash(fname) mode = os.stat(fname)[stat.ST_MODE] tree.add(entry, mode, bhash) return tree.id
def add_blob(self, gc, name, contents): b = Blob.from_string(contents) t = Tree() t.add(name.encode('utf-8'), 0o644 | stat.S_IFREG, b.id) c = Commit() c.tree = t.id c.committer = c.author = b'Somebody <*****@*****.**>' c.commit_time = c.author_time = 800000 c.commit_timezone = c.author_timezone = 0 c.message = b'do something' gc.repo.object_store.add_objects([(b, None), (t, None), (c, None)]) gc.repo[gc.ref] = c.id return b.id.decode('ascii')
def _on_tree(repo, tree, components, obj): """Mounts an object on a tree, using the given path components. :param tree: Tree object to mount on. :param components: A list of strings of subpaths (i.e. ['foo', 'bar'] is equivalent to '/foo/bar') :param obj: Object to mount. If None, removes the object found at path and prunes the tree downwards. :return: A list of new entities that need to be added to the object store, where the last one is the new tree. """ # pattern-matching: if len(components) == 1: if isinstance(obj, Blob): mode = 0o100644 elif isinstance(obj, Tree): mode = 0o040000 elif obj is None: mode = None else: raise TypeError('Can only mount Blobs or Trees') name = components[0] if mode is not None: tree[name] = mode, obj.id return [tree] if name in tree: del tree[name] return [tree] elif len(components) > 1: a, bc = components[0], components[1:] if a in tree: a_tree = repo[tree[a][1]] if not isinstance(a_tree, Tree): a_tree = Tree() else: a_tree = Tree() res = _on_tree(repo, a_tree, bc, obj) a_tree_new = res[-1] if a_tree_new.items(): tree[a] = 0o040000, a_tree_new.id return res + [tree] # tree is empty if a in tree: del tree[a] return [tree] else: raise ValueError('Components can\'t be empty.')
def test_tree_diff(self): f = StringIO() store = MemoryObjectStore() added = Blob.from_string("add\n") removed = Blob.from_string("removed\n") changed1 = Blob.from_string("unchanged\nremoved\n") changed2 = Blob.from_string("unchanged\nadded\n") unchanged = Blob.from_string("unchanged\n") tree1 = Tree() tree1.add(0644, "removed.txt", removed.id) tree1.add(0644, "changed.txt", changed1.id) tree1.add(0644, "unchanged.txt", changed1.id) tree2 = Tree() tree2.add(0644, "added.txt", added.id) tree2.add(0644, "changed.txt", changed2.id) tree1.add(0644, "unchanged.txt", changed1.id) store.add_objects([(o, None) for o in [ tree1, tree2, added, removed, changed1, changed2, unchanged]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEquals([ 'diff --git a/changed.txt b/changed.txt', 'index bf84e48..1be2436 644', '--- a/changed.txt', '+++ b/changed.txt', '@@ -1,2 +1,2 @@', ' unchanged', '-removed', '+added', 'diff --git a/removed.txt /dev/null', 'deleted mode 644', 'index 2c3f0b3..e69de29', '--- a/removed.txt', '+++ /dev/null', '@@ -1,1 +1,0 @@', '-removed', 'diff --git a/unchanged.txt /dev/null', 'deleted mode 644', 'index bf84e48..e69de29', '--- a/unchanged.txt', '+++ /dev/null', '@@ -1,2 +1,0 @@', '-unchanged', '-removed', 'diff --git /dev/null b/added.txt', 'new mode 644', 'index e69de29..76d4bb8 644', '--- /dev/null', '+++ b/added.txt', '@@ -1,0 +1,1 @@', '+add' ], f.getvalue().splitlines())
def test_tree_diff_submodule(self): f = BytesIO() store = MemoryObjectStore() tree1 = Tree() tree1.add( b"asubmodule", S_IFGITLINK, b"06d0bdd9e2e20377b3180e4986b14c8549b393e4", ) tree2 = Tree() tree2.add( b"asubmodule", S_IFGITLINK, b"cc975646af69f279396d4d5e1379ac6af80ee637", ) store.add_objects([(o, None) for o in [tree1, tree2]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual( [ b"diff --git a/asubmodule b/asubmodule", b"index 06d0bdd..cc97564 160000", b"--- a/asubmodule", b"+++ b/asubmodule", b"@@ -1 +1 @@", b"-Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4", b"+Subproject commit cc975646af69f279396d4d5e1379ac6af80ee637", ], f.getvalue().splitlines(), )
def test_renamed_file(self): self.build_tree(['a']) self.wt.add(['a']) self.wt.rename_one('a', 'b') a = Blob.from_string(b'contents of a\n') self.store.add_object(a) oldt = Tree() oldt.add(b"a", stat.S_IFREG | 0o644, a.id) self.store.add_object(oldt) newt = Tree() newt.add(b"b", stat.S_IFREG | 0o644, a.id) self.store.add_object(newt) self.expectDelta([ ('modify', (b'', stat.S_IFDIR, oldt.id), (b'', stat.S_IFDIR, newt.id)), ('delete', (b'a', stat.S_IFREG | 0o644, a.id), (None, None, None)), ('add', (None, None, None), (b'b', stat.S_IFREG | 0o644, a.id)), ], tree_id=oldt.id) if dulwich_version >= (0, 19, 15): self.expectDelta([('modify', (b'', stat.S_IFDIR, oldt.id), (b'', stat.S_IFDIR, newt.id)), ('rename', (b'a', stat.S_IFREG | 0o644, a.id), (b'b', stat.S_IFREG | 0o644, a.id))], tree_id=oldt.id, rename_detector=RenameDetector(self.store))
def __init__(self, id=None): """Create a new Directory object to mutate the specified tree. Args: id: ID of the tree to mutate. """ if id is not None: self.tree = gitrepo.get_object(id) self.dirty = False else: self.tree = Tree() self.dirty = True self.subdirs = {}
def test_commit_with_change(self): a = Blob.from_string(b"The Foo\n") ta = Tree() ta.add(b"somename", 0o100644, a.id) ca = make_commit(tree=ta.id) b = Blob.from_string(b"The Bar\n") tb = Tree() tb.add(b"somename", 0o100644, b.id) cb = make_commit(tree=tb.id, parents=[ca.id]) self.repo.object_store.add_objects([(a, None), (b, None), (ta, None), (tb, None), (ca, None), (cb, None)]) outstream = StringIO() porcelain.show(self.repo.path, objects=[cb.id], outstream=outstream) self.assertMultiLineEqual( outstream.getvalue(), """\ -------------------------------------------------- commit: 2c6b6c9cb72c130956657e1fdae58e5b103744fa Author: Test Author <*****@*****.**> Committer: Test Committer <*****@*****.**> Date: Fri Jan 01 2010 00:00:00 +0000 Test message. diff --git a/somename b/somename index ea5c7bf..fd38bcb 100644 --- a/somename +++ b/somename @@ -1 +1 @@ -The Foo +The Bar """)
def test_simple(self): c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]]) b = Blob() b.data = b"foo the bar" t = Tree() t.add(b"somename", 0o100644, b.id) self.repo.object_store.add_object(t) self.repo.object_store.add_object(b) sha = porcelain.commit_tree( self.repo.path, t.id, message=b"Withcommit.", author=b"Joe <*****@*****.**>", committer=b"Jane <*****@*****.**>") self.assertTrue(isinstance(sha, bytes)) self.assertEqual(len(sha), 40)
def test_simple_bytesio(self): f = BytesIO() c = Commit() c.committer = c.author = b"Jelmer <*****@*****.**>" c.commit_time = c.author_time = 1271350201 c.commit_timezone = c.author_timezone = 0 c.message = b"This is the first line\nAnd this is the second line.\n" c.tree = Tree().id write_commit_patch(f, c, b"CONTENTS", (1, 1), version="custom") f.seek(0) lines = f.readlines() self.assertTrue(lines[0].startswith( b"From 0b0d34d1b5b596c928adc9a727a4b9e03d025298")) self.assertEqual(lines[1], b"From: Jelmer <*****@*****.**>\n") self.assertTrue(lines[2].startswith(b"Date: ")) self.assertEqual([ b"Subject: [PATCH 1/1] This is the first line\n", b"And this is the second line.\n", b"\n", b"\n", b"---\n"], lines[3:8]) self.assertEqual([ b"CONTENTS-- \n", b"custom\n"], lines[-2:]) if len(lines) >= 12: # diffstat may not be present self.assertEqual(lines[8], b" 0 files changed\n")
def test_symlink(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo filed = Blob.from_string(b'file d') filee = Blob.from_string(b'd') tree = Tree() tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id) tree[b'c/e'] = (stat.S_IFLNK, filee.id) # symlink repo.object_store.add_objects([(o, None) for o in [filed, filee, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() # symlink to d epath = os.path.join(repo.path, 'c', 'e') self.assertTrue(os.path.exists(epath)) self.assertReasonableIndexEntry( index[b'c/e'], stat.S_IFLNK, 0 if sys.platform == 'win32' else 1, filee.id) self.assertFileContents(epath, 'd', symlink=True)
def oldcreate(self, name): """ create a new repo :param name: the name to give the new repo :type name: string """ repo = Repo.init_bare(name) from dulwich.objects import Tree tree = Tree() from dulwich.objects import Commit, parse_timezone from time import time commit = Commit() commit.tree = tree.id #is the tree.id the sha1 of the files contained in tree author = "New Project Wizard <wizard@host>" commit.author = commit.committer = author commit.commit_time = commit.author_time = int(time()) tz = parse_timezone('+1000')[0] commit.commit_timezone = commit.author_timezone = tz commit.encoding = "UTF-8" commit.message = "New Project." object_store = repo.object_store #then add the tree object_store.add_object(tree) #then add the commit object_store.add_object(commit) repo.refs['refs/heads/master'] = commit.id return {"success": "%s.git created" % name}
def test_send_pack_no_sideband64k_with_update_ref_error(self): # No side-bank-64k reported by server shouldn't try to parse # side band data pkts = [b'55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}' b'\x00 report-status delete-refs ofs-delta\n', b'', b"unpack ok", b"ng refs/foo/bar pre-receive hook declined", b''] for pkt in pkts: if pkt == b'': self.rin.write(b"0000") else: self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt) self.rin.seek(0) tree = Tree() commit = Commit() commit.tree = tree commit.parents = [] commit.author = commit.committer = b'test user' commit.commit_time = commit.author_time = 1174773719 commit.commit_timezone = commit.author_timezone = 0 commit.encoding = b'UTF-8' commit.message = b'test message' def determine_wants(refs): return {b'refs/foo/bar': commit.id, } def generate_pack_contents(have, want): return [(commit, None), (tree, ''), ] self.assertRaises(UpdateRefsError, self.client.send_pack, "blah", determine_wants, generate_pack_contents)
def test_git_dir(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo filea = Blob.from_string(b'file a') filee = Blob.from_string(b'd') tree = Tree() tree[b'.git/a'] = (stat.S_IFREG | 0o644, filea.id) tree[b'c/e'] = (stat.S_IFREG | 0o644, filee.id) repo.object_store.add_objects([(o, None) for o in [filea, filee, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 1) # filea apath = os.path.join(repo.path, '.git', 'a') self.assertFalse(os.path.exists(apath)) # filee epath = os.path.join(repo.path, 'c', 'e') self.assertTrue(os.path.exists(epath)) self.assertReasonableIndexEntry(index[b'c/e'], stat.S_IFREG | 0o644, 1, filee.id) self.assertFileContents(epath, b'd')
def _create_file(repository, filename, contents, request=None, reason=''): """ Creates a new blob containing 'contents' within 'repository'. """ branch = _get_branch(request) blob = Blob.from_string(contents) try: last_commit = repository.get_object(repository.refs[branch]) tree = repository.get_object(last_commit.tree) commit_parents = [repository.refs[branch]] except KeyError: tree = Tree() commit_parents = None # TODO: The dulwich documentation puts these arguments in the wrong order. # I should probably tell someone about this. Example here by 'tree.add': # http://samba.org/~jelmer/dulwich/docs/tutorial/object-store.html tree.add(0100644, filename, blob.id) commit = _create_commit(tree, reason, True, request) repository.refs[branch] = commit.id # Add our blob, followed by our tree, and finally our commit. for object in [blob, tree, commit]: repository.object_store.add_object(object)
def test_get_ctag(self): gc = self.create_store() self.assertEqual(Tree().id.decode('ascii'), gc.get_ctag()) self.add_blob(gc, 'foo.ics', EXAMPLE_VCALENDAR1) self.assertEqual( gc._get_current_tree().id.decode('ascii'), gc.get_ctag())
def test_emit_commit(self): b = Blob() b.data = "FOO" t = Tree() t.add("foo", stat.S_IFREG | 0644, b.id) c = Commit() c.committer = c.author = "Jelmer <jelmer@host>" c.author_time = c.commit_time = 1271345553 c.author_timezone = c.commit_timezone = 0 c.message = "msg" c.tree = t.id self.store.add_objects([(b, None), (t, None), (c, None)]) self.fastexporter.emit_commit(c, "refs/heads/master") self.assertEquals("""blob mark :1 data 3 FOO commit refs/heads/master mark :2 author Jelmer <jelmer@host> 1271345553 +0000 committer Jelmer <jelmer@host> 1271345553 +0000 data 3 msg M 644 1 foo """, self.stream.getvalue())
def test_tree_serialize(self): blob = make_object(Blob, data=b'i am a blob') tree = Tree() tree[b'blob'] = (stat.S_IFREG, blob.id) with self.assert_serialization_on_change(tree): tree[b'blob2'] = (stat.S_IFREG, blob.id)
def test_no_decode_encode(self): repo_dir = tempfile.mkdtemp() repo_dir_bytes = repo_dir.encode(sys.getfilesystemencoding()) self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo file = Blob.from_string(b'foo') tree = Tree() latin1_name = u'À'.encode('latin1') utf8_name = u'À'.encode('utf8') tree[latin1_name] = (stat.S_IFREG | 0o644, file.id) tree[utf8_name] = (stat.S_IFREG | 0o644, file.id) repo.object_store.add_objects( [(o, None) for o in [file, tree]]) build_index_from_tree( repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() latin1_path = os.path.join(repo_dir_bytes, latin1_name) self.assertTrue(os.path.exists(latin1_path)) utf8_path = os.path.join(repo_dir_bytes, utf8_name) self.assertTrue(os.path.exists(utf8_path))
def tree_from_dir(repo, dirname): abspath = os.path.abspath(dirname) tree = Tree() for name in os.listdir(dirname): path = os.path.join(abspath, name) s = os.lstat(path) mode = s.st_mode if S_ISDIR(mode): obj = tree_from_dir(repo, path) else: obj = Blob.from_string(file(path).read()) repo.object_store.add_object(obj) tree.add(name, mode, obj.id) repo.object_store.add_object(tree) return tree
def test_empty_root(self): child_ie = InventoryDirectory(b'bar', 'bar', b'bar') t = directory_to_tree('', [child_ie], lambda p, x: None, {}, None, allow_empty=True) self.assertEqual(Tree(), t)
def iter_changes(self, old_ctag, new_ctag): """Get changes between two versions of this store. :param old_ctag: Old ctag (None for empty Store) :param new_ctag: New ctag :return: Iterator over (name, content_type, old_etag, new_etag) """ if old_ctag is None: t = Tree() self.repo.object_store.add_object(t) old_ctag = t.id.decode('ascii') previous = { name: (content_type, etag) for (name, content_type, etag) in self.iter_with_etag(old_ctag) } for (name, new_content_type, new_etag) in (self.iter_with_etag(new_ctag)): try: (old_content_type, old_etag) = previous[name] except KeyError: old_etag = None else: assert old_content_type == new_content_type if old_etag != new_etag: yield (name, new_content_type, old_etag, new_etag) if old_etag is not None: del previous[name] for (name, (old_content_type, old_etag)) in previous.items(): yield (name, old_content_type, old_etag, None)
def step_impl_given(context): context.test_git_repo_dir = tempfile.mkdtemp( 'paasta_tools_deployments_json_itest') context.test_git_repo = Repo.init(context.test_git_repo_dir) print 'Temp repo in %s' % context.test_git_repo_dir blob = Blob.from_string("My file content\n") tree = Tree() tree.add("spam", 0100644, blob.id) commit = Commit() commit.author = commit.committer = "itest author" commit.commit_time = commit.author_time = int(time()) commit.commit_timezone = commit.author_timezone = parse_timezone( '-0200')[0] commit.message = "Initial commit" commit.tree = tree.id object_store = context.test_git_repo.object_store object_store.add_object(blob) object_store.add_object(tree) object_store.add_object(commit) context.test_git_repo.refs['refs/heads/master'] = commit.id context.expected_commit = commit.id
def _put(self, key, data): commit = self._create_top_commit() commit.message = ('Updated key {}'.format(self.subdir + '/' + key)).encode('utf8') blob = Blob.from_string(data) try: parent_commit = self.repo[self._refname] except KeyError: # branch does not exist, start with an empty tree tree = Tree() else: commit.parents = [parent_commit.id] tree = self.repo[parent_commit.tree] objects_to_add = [blob] components = self._key_components(key) if self.subdir: components = self._subdir_components + components res = _on_tree(self.repo, tree, components, blob) objects_to_add.extend(res) commit.tree = res[-1].id objects_to_add.append(commit) # add objects for obj in objects_to_add: self.repo.object_store.add_object(obj) # update refs self.repo.refs[self._refname] = commit.id return key
def savedir(dirname): tree = Tree() names = dirs[dirname] for name in names: basename = os.path.basename(name) sha = blobs.get(name, None) if sha is not None: tree.add(basename.encode('utf-8'), 0100644, sha) continue subtree = savedir(name) tree.add(basename.encode('utf-8'), 040000, subtree.id) repo.object_store.add_object(tree) return tree
def initial_commit(repo): # Add file content blob = Blob.from_string("My file content\n") # Add file tree = Tree() tree.add(0100644, "spam", blob.id) # Set commit commit = make_commit(repo, tree.id, "Initial commit") # Initial commit object_store = repo.object_store object_store.add_object(blob) object_store.add_object(tree) object_store.add_object(commit) # Update master update_master(repo, commit.id) # Set the master branch as the default repo.refs['HEAD'] = 'ref: refs/heads/master'
def setup_app(command, conf, vars): """Place any commands to setup kekswiki here""" # Don't reload the app if it was loaded under the testing environment if not pylons.test.pylonsapp: load_environment(conf.global_conf, conf.local_conf) # create repository directory try: mkdir("wiki") except OSError: pass # create repository try: repo = Repo.init("wiki") except OSError: repo = Repo("wiki") # create blob blob = Blob.from_string("Kekswiki is a collaborative hypertext editing system.\n") # add blob to tree tree = Tree() tree.add(0100644, "Kekswiki", blob.id) # commit commit = Commit() commit.tree = tree.id author = "Anonymous <*****@*****.**>" commit.author = commit.committer = author commit.commit_time = commit.author_time = int(time()) tz = parse_timezone('+0100')[0] # FIXME: get proper timezone commit.commit_timezone = commit.author_timezone = tz commit.encoding = "UTF-8" commit.message = "Initial commit" # save everything object_store = repo.object_store object_store.add_object(blob) object_store.add_object(tree) object_store.add_object(commit) # create master branch, set it as current repo.refs.add_if_new('refs/heads/master', commit.id) repo.refs.set_symbolic_ref('HEAD', 'refs/heads/master')