def checkout(gitdir: pathlib.Path, obj_name: str) -> None: # PUT YOUR CODE HERE update_ref(gitdir, "HEAD", obj_name) hash = commit_parse(read_object(obj_name, gitdir)[1]) files = find_tree_files(hash, gitdir) index = read_index(gitdir) names = [] for i in index: names.append(i.name) update_index(gitdir, [pathlib.Path(i[1]) for i in files], write=True) for i in names: first = i.split("/")[0] if pathlib.Path(first).is_dir(): shutil.rmtree(first) else: if pathlib.Path(first).exists(): os.remove(first) for i in files: isFound = i[1].find("/") if isFound != -1: elem1 = os.path.split(i[1])[0] if pathlib.Path(elem1).exists() == False: os.makedirs(elem1) with open(i[1], "wb") as f: f.write(read_object(i[0], gitdir)[1])
def test_update_ref(self): gitdir = repo_create(".") master_sha = "d6ae59694dfec74d7f5ca87608f31c884dc9b0f9" update_ref(gitdir, "refs/heads/master", master_sha) master = gitdir / "refs" / "heads" / "master" with master.open() as f: sha = f.read().strip() self.assertEqual(master_sha, sha)
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
def checkout(gitdir: pathlib.Path, obj_name: str) -> None: update_ref(gitdir, "HEAD", obj_name) index_names = [entry.name for entry in read_index(gitdir)] _, commit_data = read_object(obj_name, gitdir) tree_hash = commit_parse(commit_data) files = find_tree_files(tree_hash, gitdir) to_be_updated = [pathlib.Path(i[1]) for i in files] update_index(gitdir, to_be_updated, write=True) for name in index_names: nodes = name.split("\\") if pathlib.Path(nodes[0]).is_dir(): shutil.rmtree(nodes[0]) else: if pathlib.Path(nodes[0]).exists(): os.remove(nodes[0]) for sha, name in files: if name.find("\\") != -1: prefix, _ = os.path.split(name) if not pathlib.Path(prefix).exists(): os.makedirs(prefix) _, content = read_object(sha, gitdir) with open(name, "wb") as file_obj: file_obj.write(content)
def cmd_update_ref(args: argparse.Namespace) -> None: gitdir = repo_find() update_ref(gitdir, args.ref, args.newvalue)