1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import os import git
class GitRepository: """Git 仓库管理
Returns: [type]: [description] """
def __init__(self, local_path, repo_url=None, branch='master'): self.local_path = local_path self.repo_url = repo_url self.repo = None self.initial(repo_url, branch)
def initial(self, repo_url, branch): os.makedirs(self.local_path, exist_ok=True) git_local_path = os.path.join(self.local_path, '.git') if os.path.exists(git_local_path): print('已存在git') self.repo = git.Repo(self.local_path) else: self.repo = git.Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
def add(self, file_list): self.repo.index.add(file_list)
def pull(self): self.repo.git.pull()
def commit(self): commit_log = self.repo.git.log( '--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50, date='format:%Y-%m-%d %H:%M', ) log_list = commit_log.split("\n") return [eval(item) for item in log_list]
def get_now_branch(self): return self.repo.active_branch
def get_branch_list(self): branches = self.repo.remote().refs return [ item.remote_head for item in branches if item.remote_head not in [ 'HEAD', ] ]
def get_tag_list(self): return [tag.name for tag in self.repo.tags]
def change_to_branch(self, branch): self.repo.git.checkout(branch)
def change_to_commit(self, branch, commit): self.change_to_branch(branch=branch) self.repo.git.reset('--hard', commit)
def change_to_tag(self, tag): self.repo.git.checkout(tag)
if __name__ == '__main__': git_repo = GitRepository() git_repo.repo.create_tag("v0.1")
git_repo.repo.create_head("dev")
git_repo.repo.index.reset(commit="哈希地址", head=True)
|