def __init__(self, git_path: str = ""): if git_path: path = git_path else: path = find_path() super().__init__(path) self.index = Index(path)
def add(paths: List[str]) -> None: """ 添加 paths 的内容到 git 管理 就是将这些路径中的文件添加到 ./git/objects , 然后再使用这些文件生成的 sha1 生成 .git/index 索引 """ git_path = find_path() index = Index(git_path) blob = Blob(git_path) # 若索引存在,则将没有数据变动的索引记录下来,放到 entries 中 all_entries = index.read_index() entries = [e for e in all_entries if e.path not in paths] # 将有变动的文件重新生成加密的对象,放入索引中 for path in paths: sha1 = blob.compress(path) st, flags = os.stat(path), len(path.encode()) assert flags < (1 << 12) entry = IndexEntry(int(st.st_ctime), 0, int(st.st_mtime), 0, st.st_dev, st.st_ino, st.st_mode, st.st_uid, st.st_gid, st.st_size, bytes.fromhex(sha1), flags, path) entries.append(entry) # 根据 path 排序 entries.sort(key=operator.attrgetter('path')) index.write_index(entries)
def __init__(self, git_path: str = ""): if git_path: self.git_path = git_path else: self.git_path = find_path() self.work_path = os.path.realpath(os.path.join(self.git_path, "..")) self.index = Index(self.git_path) self.blob = Blob(self.git_path) ignore_file_name = ".gitignore" ignore_path = os.path.join(self.work_path, ignore_file_name) self.ignore_pattern = set() if os.path.exists(ignore_path): self.ignore = set(read_file(ignore_path).decode().splitlines()) for ignore_item in self.ignore: if ignore_item.startswith(" patten "): self.ignore_pattern.add(ignore_item[8:]) self.ignore.add(ignore_file_name) else: self.ignore = [] self.ignore -= self.ignore_pattern
def commit(message, author): path = find_path() tree_obj = Tree(path) commit_obj = Commit(path) tree = tree_obj.write_tree() parent = commit_obj.get_local_master_hash() auth_time = time.strftime("%Y-%m-%d %H:%M:%S") lines = ['tree ' + tree] if parent: lines.append('parent ' + parent) lines.append('author {} {}'.format(author, auth_time)) lines.append('committer {} {}'.format(author, auth_time)) lines.append('') lines.append(message) lines.append('') data = '\n'.join(lines).encode() sha1 = commit_obj.compress(data) master_path = os.path.join(commit_obj.get_master_path()) write_file(master_path, (sha1 + '\n').encode()) print('committed to master: {:7}'.format(sha1)) return sha1
def __init__(self, git_path: str = ""): if git_path: self.git_path = git_path else: self.git_path = find_path() self.objs_path = os.path.join(self.git_path, "objects")
def __init__(self, git_path: str = ""): if git_path: self.git_path = git_path else: self.git_path = find_path()