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 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 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_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 _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 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 _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 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 __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 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_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 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 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_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 _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 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 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 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 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 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 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 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 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 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 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')
def _dulwich_tag(self, tag, author, message=None): """ Creates a tag in git via dulwich calls: **tag** - string :: "<project>-[start|sync]-<timestamp>" **author** - string :: "Your Name <*****@*****.**>" """ if not message: message = tag # Open the repo _repo = Repo(self.config['top_dir']) master_branch = 'master' # Build the commit object blob = Blob.from_string("empty") tree = Tree() tree.add(tag, 0100644, blob.id) commit = Commit() commit.tree = tree.id commit.author = commit.committer = author commit.commit_time = commit.author_time = int(time()) tz = parse_timezone('-0200')[0] commit.commit_timezone = commit.author_timezone = tz commit.encoding = "UTF-8" commit.message = 'Tagging repo for deploy: ' + message # Add objects to the repo store instance 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_branch] = commit.id # Build the tag object and tag tag = Tag() tag.tagger = author tag.message = message tag.name = tag tag.object = (Commit, commit.id) tag.tag_time = commit.author_time tag.tag_timezone = tz object_store.add_object(tag) _repo['refs/tags/' + tag] = tag.id
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'-Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4', b'+Submodule commit cc975646af69f279396d4d5e1379ac6af80ee637', ], f.getvalue().splitlines())
def test_import_tree_with_file_exe(self): blob = Blob.from_string(b"bar") tree = Tree() tree.add(b"foo", 0o100755, blob.id) objects = {blob.id: blob, tree.id: tree} ret, child_modes = import_git_tree( self._texts, self._mapping, b"", b"", (None, tree.id), None, None, b"somerevid", [], objects.__getitem__, (None, stat.S_IFDIR), DummyStoreUpdater(), self._mapping.generate_file_id) self.assertEqual(child_modes, {}) self.assertEqual(2, len(ret)) self.assertEqual(None, ret[0][0]) self.assertEqual("", ret[0][1]) self.assertEqual(None, ret[1][0]) self.assertEqual("foo", ret[1][1]) ie = ret[0][3] self.assertEqual("directory", ie.kind) ie = ret[1][3] self.assertEqual("file", ie.kind) self.assertEqual(True, ie.executable)
def build_tree(path): if path: tree = self._get_object(path, branch=branch, commit_sha=commit_sha) if not tree: tree = Tree() if not isinstance(tree, Tree): self.delete(path, branch=branch) tree = Tree() else: tree = root_tree for basename, entry in trees[path].iteritems(): if type(entry) == dict: mode = stat.S_IFDIR sha = build_tree(pathjoin(path, basename)) else: (mode, sha) = entry tree.add(basename, mode, sha) self.repo.object_store.add_object(tree) return tree.id
def test_versioned_replace_by_dir(self): self.build_tree(['a']) self.wt.add(['a']) self.wt.commit('') os.unlink('a') os.mkdir('a') olda = Blob.from_string(b'contents of a\n') oldt = Tree() oldt.add(b"a", stat.S_IFREG | 0o644, olda.id) newt = Tree() newa = Tree() newt.add(b"a", stat.S_IFDIR, newa.id) self.expectDelta([((b'', b''), (stat.S_IFDIR, stat.S_IFDIR), (oldt.id, newt.id)), ((b'a', b'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))], want_unversioned=False) self.expectDelta([((b'', b''), (stat.S_IFDIR, stat.S_IFDIR), (oldt.id, newt.id)), ((b'a', b'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))], want_unversioned=True)
def test_import_tree_with_file(self): blob = Blob.from_string(b"bar1") tree = Tree() tree.add(b"foo", stat.S_IFREG | 0o644, 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, {}) self.assertEqual(2, len(ret)) self.assertEqual(None, ret[0][0]) self.assertEqual("bla", ret[0][1]) self.assertEqual(None, ret[1][0]) self.assertEqual("bla/foo", ret[1][1]) ie = ret[0][3] self.assertEqual("directory", ie.kind) ie = ret[1][3] self.assertEqual("file", ie.kind) self.assertEqual(b"git:bla/foo", ie.file_id) self.assertEqual(b"somerevid", ie.revision) self.assertEqual(osutils.sha_strings([b"bar1"]), ie.text_sha1) self.assertEqual(False, ie.executable)
def test_directory_converted_to_submodule(self): base_inv = Inventory() base_inv.add_path("foo", "directory") base_inv.add_path("foo/bar", "file") othertree = Blob.from_string(b"someotherthing") blob = Blob.from_string(b"bar") tree = Tree() tree.add(b"bar", 0o160000, blob.id) objects = {tree.id: tree} ret, child_modes = import_git_submodule( self._texts, self._mapping, b"foo", b"foo", (tree.id, othertree.id), base_inv, base_inv.root.file_id, b"somerevid", [], objects.__getitem__, (stat.S_IFDIR | 0o755, S_IFGITLINK), DummyStoreUpdater(), self._mapping.generate_file_id) self.assertEqual(child_modes, {}) self.assertEqual(2, len(ret)) self.assertEqual(ret[0], ("foo/bar", None, base_inv.path2id("foo/bar"), None)) self.assertEqual(ret[1][:3], ("foo", "foo", self._mapping.generate_file_id("foo"))) ie = ret[1][3] self.assertEqual(ie.kind, "tree-reference")
def write(self, data): commit = Commit() # commit metadata author = b"tinydb" commit.author = commit.committer = author commit.commit_time = commit.author_time = int(time.time()) tz = time.timezone if (time.localtime().tm_isdst) else time.altzone commit.commit_timezone = commit.author_timezone = tz commit.encoding = b'UTF-8' commit.message = ( 'Updated by tinydb-git {}'.format(__version__).encode('utf8')) # prepare blob blob = Blob.from_string(self._serialize(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] # no subdirs in filename, add directly to tree tree.add(self.filename, 0o100644, blob.id) commit.tree = tree.id # add objects self.repo.object_store.add_object(blob) self.repo.object_store.add_object(tree) self.repo.object_store.add_object(commit) # update refs self.repo.refs[self._refname] = commit.id
def test_copied_file(self): self.build_tree(['a']) self.wt.add(['a']) self.wt.copy_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"a", stat.S_IFREG | 0o644, a.id) 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)), ('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)), ('copy', (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, find_copies_harder=True)) self.expectDelta([ ('modify', (b'', stat.S_IFDIR, oldt.id), (b'', stat.S_IFDIR, newt.id)), ('add', (None, None, None), (b'b', stat.S_IFREG | 0o644, a.id)), ], tree_id=oldt.id, rename_detector=RenameDetector( self.store, find_copies_harder=False))
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 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) paasta_print("Temp repo in %s" % context.test_git_repo_dir) blob = Blob.from_string(b"My file content\n") tree = Tree() tree.add(b"spam", 0o0100644, blob.id) commit = Commit() commit.author = commit.committer = b"itest author" commit.commit_time = commit.author_time = int(time()) commit.commit_timezone = commit.author_timezone = parse_timezone(b"-0200")[0] commit.message = b"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[b"refs/heads/master"] = commit.id context.expected_commit_as_bytes = commit.id context.expected_commit = context.expected_commit_as_bytes.decode()
def inventory_to_tree_and_blobs(repo, mapping, revision_id): stack = [] cur = "" tree = Tree() inv = repo.get_inventory(revision_id) for path, entry in inv.iter_entries(): while stack and not path.startswith(cur): tree.serialize() sha = tree.sha().hexdigest() yield sha, tree t = (stat.S_IFDIR, splitpath(cur)[-1:][0].encode('UTF-8'), sha) cur, tree = stack.pop() tree.add(*t) if type(entry) == InventoryDirectory: stack.append((cur, tree)) cur = path tree = Tree() if type(entry) == InventoryFile: #FIXME: We can make potentially make this Lazy to avoid shaing lots of stuff # and having all these objects in memory at once blob = Blob() _, blob._text = repo.iter_files_bytes([(entry.file_id, revision_id, path)]).next() sha = blob.sha().hexdigest() yield sha, blob name = splitpath(path)[-1:][0].encode('UTF-8') mode = stat.S_IFREG | 0644 if entry.executable: mode |= 0111 tree.add(mode, name, sha) while len(stack) > 1: tree.serialize() sha = tree.sha().hexdigest() yield sha, tree t = (stat.S_IFDIR, splitpath(cur)[-1:][0].encode('UTF-8'), sha) cur, tree = stack.pop() tree.add(*t) tree.serialize() yield tree.sha().hexdigest(), tree
def render_tree(tree, path): new_tree = Tree() for entry in tree.items(): item = repo[entry.sha] if isinstance(item, Tree): new_tree.add(entry.path, entry.mode, render_tree(item, path+(entry.path,))) elif isinstance(item, Blob): if entry.path.startswith(".") or ("." not in entry.path) or entry.path.endswith(".py") or entry.path.endswith(".html") or entry.path.startswith("mnist.") or entry.path.endswith(".model"): continue if not entry.path.endswith(".rst"): new_tree.add(entry.path, entry.mode, entry.sha) else: name = "index.html" if entry.path == 'README.rst' else (entry.path[:-4]+".html") new_tree.add(name, entry.mode, render_rst(item, os.path.join(*(path +(name,))))) store.add_object(new_tree) return new_tree.id
def _merge_branches(self, base, mine, other, take_mine=False): def load_json(path, branch): try: blob = self.blob(path, branch) except (KeyError, TypeError): return {} else: return loads(blob.as_raw_string()) merge_tree = Tree() base_tree, my_tree, other_tree = (self._get_tree(x) for x in (base, mine, other)) ren_detector = RenameDetector(self._repo.object_store) conflicts = {} my_changes, other_changes = (tree_changes(self._repo.object_store, base_tree.id, x.id, want_unchanged=True, rename_detector=ren_detector) for x in (my_tree, other_tree)) changes_by_path = defaultdict(list) for change in chain(my_changes, other_changes): if change.type == CHANGE_DELETE or change.type == CHANGE_RENAME: path = change.old.path else: path = change.new.path changes_by_path[path].append(change) had_conflict = False for path, changes in changes_by_path.items(): if len(changes) == 2: my_changes, other_changes = changes if my_changes.type == CHANGE_DELETE: if other_changes.type in (CHANGE_RENAME, CHANGE_MODIFY): merge_tree.add(other_changes.new.path, FILE_MODE, other_changes.new.sha) else: continue elif other_changes.type == CHANGE_DELETE: if my_changes.type in (CHANGE_RENAME, CHANGE_MODIFY): merge_tree.add(my_changes.new.path, FILE_MODE, my_changes.new.sha) else: continue else: jsons = [load_json(path, x) for x in (base, mine, other)] base_json, my_json, other_json = jsons # When dealing with renames, file contents are under the # 'new' path. Note that the file will be finally stored # under the name given by the last rename. if other_changes.type == CHANGE_RENAME: other_json = load_json(other_changes.new.path, other) path = other_changes.new.path if my_changes.type == CHANGE_RENAME: my_json = load_json(my_changes.new.path, mine) path = my_changes.new.path if take_mine: merged_json = my_json or other_json or base_json else: merged_json, merge_conflict = merge_jsons(*jsons) if merge_conflict: conflicts[path] = merged_json had_conflict = had_conflict or merge_conflict merged_blob = Blob.from_string( dumps(merged_json, sort_keys=True, indent=4)) self._update_store(merged_blob) merge_tree.add(path, FILE_MODE, merged_blob.id) else: data = (load_json(path, mine) or load_json(path, other) or load_json(path, base)) blob = Blob.from_string(dumps(data, sort_keys=True, indent=4)) self._update_store(blob) merge_tree.add(path, FILE_MODE, blob.id) self._update_store(merge_tree) return merge_tree, conflicts
def test_tree_diff(self): f = BytesIO() store = MemoryObjectStore() added = Blob.from_string(b"add\n") removed = Blob.from_string(b"removed\n") changed1 = Blob.from_string(b"unchanged\nremoved\n") changed2 = Blob.from_string(b"unchanged\nadded\n") unchanged = Blob.from_string(b"unchanged\n") tree1 = Tree() tree1.add(b"removed.txt", 0o644, removed.id) tree1.add(b"changed.txt", 0o644, changed1.id) tree1.add(b"unchanged.txt", 0o644, changed1.id) tree2 = Tree() tree2.add(b"added.txt", 0o644, added.id) tree2.add(b"changed.txt", 0o644, changed2.id) tree2.add(b"unchanged.txt", 0o644, 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.assertEqual([ b'diff --git a/added.txt b/added.txt', b'new file mode 644', b'index 0000000..76d4bb8', b'--- /dev/null', b'+++ b/added.txt', b'@@ -0,0 +1 @@', b'+add', b'diff --git a/changed.txt b/changed.txt', b'index bf84e48..1be2436 644', b'--- a/changed.txt', b'+++ b/changed.txt', b'@@ -1,2 +1,2 @@', b' unchanged', b'-removed', b'+added', b'diff --git a/removed.txt b/removed.txt', b'deleted file mode 644', b'index 2c3f0b3..0000000', b'--- a/removed.txt', b'+++ /dev/null', b'@@ -1 +0,0 @@', b'-removed', ], f.getvalue().splitlines())
# abstraction of this store = repo.object_store print("Object store: {}".format(store)) store.add_object(blob) print("Added one object") # notes: # - single object file in .git/objects now # - "git cat-file -p <hash>": see our file! # - file is compressed (hexdump -C .git/objects/*) # now lets create a real "tree" object so we can give our Blob a # filename tree = Tree() tree.add(b"a_file.txt", 0o100644, blob.id) print("Tree: {}".format(tree.sha().hexdigest())) # note: still not "in" the object repository store.add_object(tree) # From a user perpective, Trees live in Commit objects commit = Commit() commit.tree = tree commit.author = commit.committer = b"meejah <*****@*****.**>" commit.commit_time = commit.author_time = int(time()) # seconds since epoch commit.commit_timezone = commit.author_timezone = -7 * ( 60 * 60) # seconds offset; MST commit.encoding = b"utf8" commit.message = b"The beginning" # no commit.parents because this is the first Commit