def test_resolve_head(self): gitdir = repo_create(".") data = resolve_head(gitdir) self.assertIsNone(data) master_sha = "d6ae59694dfec74d7f5ca87608f31c884dc9b0f9" master = gitdir / "refs" / "heads" / "master" self.fs.create_file(master, contents=master_sha) data = resolve_head(gitdir) self.assertEqual(master_sha, data)
def commit(gitdir: pathlib.Path, message: str, author: tp.Optional[str] = None) -> str: tree = write_tree(gitdir, read_index(gitdir), str(gitdir.parent)) parent_commit = resolve_head(gitdir) commit_hash = commit_tree(gitdir, tree, message, parent_commit, author) return commit_hash
def commit(gitdir: pathlib.Path, message: str, author: tp.Optional[str] = None) -> str: hash_commit = commit_tree( gitdir, write_tree(gitdir, read_index(gitdir), str(gitdir.parent)), message, resolve_head(gitdir), author) return hash_commit
def checkout(gitdir: pathlib.Path, obj_name: str) -> None: if is_detached(gitdir) and get_ref(gitdir) == obj_name: return elif get_ref(gitdir).split("/")[2] == obj_name: return elif resolve_head(gitdir) == obj_name: return elif (gitdir / 'refs' / 'heads' / obj_name).exists(): with open(gitdir / 'refs' / 'heads' / obj_name, 'r') as file: obj_name = file.read() index = read_index(gitdir) for entry in index: if pathlib.Path(entry.name).exists(): if '/' in entry.name: shutil.rmtree(entry.name[:entry.name.find('/')]) else: os.remove(entry.name) path_to_commit = gitdir / "objects" / obj_name[:2] / obj_name[2:] if path_to_commit: with open(path_to_commit, 'rb') as file: raw = file.read() data = commit_parse(raw) tree_sha = data[data.find(b'tree ') + 5:data.find(b'\n')].decode() for file in find_tree_files(tree_sha, gitdir): if '/' in file[0]: dir_name = file[0][:file[0].find('/')] os.mkdir(dir_name) with open(file[0], 'w') as new_file: header, content = read_object(file[1], gitdir) new_file.write(content.decode())
def checkout(gitdir: pathlib.Path, obj_name: str) -> None: if is_detached(gitdir) and get_ref(gitdir) == obj_name: return elif get_ref(gitdir).split("/")[2] == obj_name: return elif resolve_head(gitdir) == obj_name: return elif (gitdir / "refs" / "heads" / obj_name).exists(): with open(gitdir / "refs" / "heads" / obj_name, "r") as f1: obj_name = f1.read() index = read_index(gitdir) for entry in index: if os.path.exists(entry.name): if "/" in entry.name: shutil.rmtree(entry.name[:entry.name.find("/")]) else: os.remove(entry.name) with open(gitdir / "objects" / obj_name[:2] / obj_name[2:], "rb") as f2: commit_content = f2.read() tree_sha = commit_parse(commit_content).decode() for file in find_tree_files(tree_sha, gitdir): if "/" in file[0]: dir_name = file[0][:file[0].find("/")] os.mkdir(dir_name) with open(file[0], "w") as f3: header, content = read_object(file[1], gitdir) f3.write(content.decode())
def commit(gitdir: pathlib.Path, message: str, author: tp.Optional[str] = None) -> str: # PUT YOUR CODE HERE parent = resolve_head(gitdir) tree = write_tree(gitdir, read_index(gitdir), str(gitdir.parent)) com = commit_tree(gitdir, tree, message, parent, author) return com
def commit(gitdir: pathlib.Path, message: str, author: tp.Optional[str] = None) -> str: # делаем сам коммит files_in_index = read_index(gitdir) tree_hash = write_tree(gitdir, files_in_index) parent = resolve_head(gitdir) commit_sha = commit_tree(gitdir, tree_hash, message, parent=parent, author=author) update_ref(gitdir, get_ref(gitdir), commit_sha) return commit_sha