Ejemplo n.º 1
0
    def test_is_detached(self):
        gitdir = repo_create(".")

        detached = is_detached(gitdir)
        self.assertFalse(detached)

        head = gitdir / "HEAD"
        with head.open(mode="w") as f:
            f.write("d6ae59694dfec74d7f5ca87608f31c884dc9b0f9")
        detached = is_detached(gitdir)
        self.assertTrue(detached)
Ejemplo n.º 2
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 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())
Ejemplo n.º 3
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())
Ejemplo n.º 4
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