Beispiel #1
0
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 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())
Beispiel #3
0
def commit(gitdir: pathlib.Path, message: str, author: tp.Optional[str] = None) -> str:
    commit_sha = commit_tree(gitdir, write_tree(gitdir, read_index(gitdir)), message, author=author)
    if is_detached(gitdir):
        ref = gitdir / "HEAD"
    else:
        ref = pathlib.Path(get_ref(gitdir))
    f = open(gitdir / ref, "w")
    f.write(commit_sha)
    f.close()
    return commit_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
Beispiel #5
0
def checkout(gitdir: pathlib.Path, obj_name: str) -> None:
    ref = get_ref(gitdir)
    if os.path.isfile(gitdir / ref):
        f = pathlib.Path(gitdir / ref).open("r")
        ref = f.read()
        f.close()
    fmt, old_content = read_object(ref, gitdir)
    old_content = old_content.decode()
    tree_sha = old_content[5:45]
    old_objects = find_tree_files(tree_sha, gitdir)
    dirs = gitdir.absolute().parent
    for i in old_objects:
        os.remove(dirs / i[0])
        next_path = pathlib.Path(i[0]).parent
        while len(next_path.parents) > 0:
            os.rmdir(next_path)
            next_path = pathlib.Path(next_path).parent
    f = pathlib.Path(gitdir / "HEAD").open("w")
    f.write(obj_name)
    f.close()
    fmt, new_content = read_object(obj_name, gitdir)
    new_content = new_content.decode()
    new_tree_sha = new_content[5:45]
    new_objects = find_tree_files(new_tree_sha, gitdir)
    for i in new_objects:
        z = len(pathlib.Path(i[0]).parents)
        sub_path = dirs
        for sub in range(z - 2, -1, -1):
            sub_path /= pathlib.Path(i[0]).parents[sub]
            if not os.path.isdir(sub_path):
                os.mkdir(sub_path)
        fmt, obj_content = read_object(i[1], gitdir)
        if fmt == "blob":
            pathlib.Path(dirs / i[0]).touch()
            f = pathlib.Path(dirs / i[0]).open("w")
            f.write(obj_content.decode())
            f.close()
        else:
            os.mkdir(dirs / i[0])
Beispiel #6
0
def checkout(gitdir: pathlib.Path, obj_name: str) -> None:
    ref = get_ref(gitdir)
    if os.path.isfile(gitdir / ref):
        branch_head = open(gitdir / ref, "r")
        ref = branch_head.read()
        branch_head.close()
    fmt, old_content = read_object(ref, gitdir)
    old_content_s = old_content.decode()
    objects = find_tree_files(old_content_s[5:25], gitdir)
    project_dir = gitdir.absolute().parent
    for obj in objects:
        os.remove(project_dir / obj[0])
        par_path = pathlib.Path(obj[0]).parent
        while len(par_path.parents) > 0:
            os.rmdir(par_path)
            par_path = pathlib.Path(par_path).parent
    f_ref = open(gitdir / "HEAD", "w")
    f_ref.write(obj_name)
    f_ref.close()
    fmt, new_content = read_object(obj_name, gitdir)
    new_content_s = new_content.decode()
    objects = find_tree_files(new_content_s[5:25], gitdir)
    for obj in objects:
        par_cnt = len(pathlib.Path(obj[0]).parents)
        par_path = project_dir
        for par in range(par_cnt - 2, -1, -1):
            par_path /= pathlib.Path(obj[0]).parents[par]
            if not os.path.isdir(par_path):
                os.mkdir(par_path)
        fmt, obj_content = read_object(obj[1], gitdir)
        if fmt == "blob":
            pathlib.Path(project_dir / obj[0]).touch()
            f_blob = open(project_dir / obj[0], "w")
            f_blob.write(obj_content.decode())
            f_blob.close()
        else:
            os.mkdir(project_dir / obj[0])
Beispiel #7
0
def checkout(gitdir: pathlib.Path, obj_name: str) -> None:
    # PUT YOUR CODE HERE
    ref = get_ref(gitdir)
    if os.path.isfile(gitdir / ref):
        branch_head = open(gitdir / ref, "r")
        ref = branch_head.read()
        branch_head.close()
    _, old_store_bytes = read_object(ref, gitdir)
    old_store_string = old_store_bytes.decode()
    objects = find_tree_files(old_store_string[5:25], gitdir)
    project_dir = gitdir.absolute().parent
    for obj in objects:
        os.remove(project_dir / obj[0])
        par_path = pathlib.Path(obj[0]).parent
        while len(par_path.parents) > 0:
            os.rmdir(par_path)
            par_path = pathlib.Path(par_path).parent
    f_ref = open(gitdir / "HEAD", "w")
    f_ref.write(obj_name)
    f_ref.close()
    fmt, new_store_bytes = read_object(obj_name, gitdir)
    new_store_string = new_store_bytes.decode()
    objects = find_tree_files(new_store_string[5:25], gitdir)
    for obj in objects:
        parents_number = len(pathlib.Path(obj[0]).parents)
        par_path = project_dir
        for par in range(parents_number - 2, -1, -1):
            par_path /= pathlib.Path(obj[0]).parents[par]
            if not os.path.isdir(par_path):
                os.mkdir(par_path)
        fmt, obj_store = read_object(obj[1], gitdir)
        if fmt.__eq__("blob"):
            f = open(pathlib.Path(project_dir / obj[0]), "x")
            f.write(obj_store.decode())
            f.close()
        else:
            os.mkdir(project_dir / obj[0])
Beispiel #8
0
 def test_get_ref(self):
     gitdir = repo_create(".")
     ref = get_ref(gitdir)
     self.assertEqual("refs/heads/master", ref)