def rev_parse(args): if args.type: fmt = args.type.encode() repo = GitRepository.find() print(repo.object_find(args.name, args.type, follow=True))
def tag(args): repo = GitRepository.find() if args.name: repo.tag_create(args.name, args.object, args.create_tag_object) else: refs = repo.ref_list() repo.show_ref(refs["tags"], with_hash=False)
def hash_object(args): if args.write: repo = GitRepository(".") else: repo = None with open(args.path, "rb") as fd: sha = object_hash(fd, args.type.encode(), repo) print(sha)
def getRepository(path): """ A factory method using Chain-Of-Command pattern to construct a repository object. It's annoying that this method is so closely coupled to each implementation of IRepository, but this developer feels like its unavoidable here. """ try: return GitRepository(path) except NotAValidRepositoryException: raise # pass (added with support for SVN)
def checkout(args): repo = GitRepository.find() obj = repo.object_read(repo.object_find(args.commit)) # If the object is a commit, we grab its tree if obj.fmt == b"commit": obj = repo.object_read(obj.kvlm[b"tree"][0].decode("ascii")) # Verify that path is an empty directory if os.path.exists(args.path): if not os.path.isdir(args.path): raise Exception(f"Not a directory {args.path}!") if os.listdir(args.path): raise Exception(f"Not empty {args.path}!") else: os.makedirs(args.path) repo.tree_checkout(obj, os.path.realpath(args.path).encode())
def cat_file(args): GitRepository.find().cat_file(args.object, fmt=args.type.encode())
def init(args): GitRepository.create(args.path)
def show_ref(args): repo = GitRepository.find() repo.show_ref(repo.ref_list(), prefix="refs")
def log(args): repo = GitRepository.find() print("digraph wyaglog{") repo.log_graphviz(repo.object_find(args.commit)) print("}")
def add_commit_reference(logger): # mysql操作对象初始化 db_url = {'host': '192.168.33.7', 'db_name': 'tms', 'username': '******', 'password': '******'} db_option = DBOption(db_url, 'mysql') db_option.db_connect() record_sql = "SELECT history.id, history.repository_path, history.push_type, history.commits, " \ "history.reference_name, repo_server.root_path, repo_server.backup_server_path, repo.id " \ "FROM repo_push_history AS history JOIN repo_server ON history.server_ip=repo_server.ip_address " \ "JOIN repo_repo AS repo ON repo.path=history.repository_path WHERE history.status=1 and " \ "history.server_ip IN('192.168.33.9', '192.168.33.13') LIMIT 100 " new_references = [] new_commits = [] for record in db_option.fetch_all(record_sql): logger.info(record[7]) row_id, repo_path, push_type, commits, reference_name, root_path, backup_server_path, repo_id = \ record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7] if push_type == 'create_reference': if reference_name.strip().startswith('refs/heads/'): new_references.append((reference_name, 1, repo_id)) else: new_references.append((reference_name, 0, repo_id)) elif push_type == 'update_reference': repo_path = repo_path.replace(root_path, backup_server_path) repo_obj = GitRepository(repo_path) if commits.find('/') > 0: for commit in repo_obj.diff_commit(commits.split('/')[0], commits.split('/')[1]): if len(commit) == 9: new_commits.append((commit[0], '', commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], repo_id)) else: new_commits.append((commit[0], commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], commit[9], repo_id)) else: commit = repo_obj.log_commit(commits) if len(commit) == 9: new_commits.append((commit[0], '', commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], repo_id)) else: new_commits.append((commit[0], commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], commit[9], repo_id)) elif push_type == 'delete_reference': update_reference_sql = "UPDATE repo_refs set status=1, push_id={0} WHERE repository_id={1} " \ "and name='{2}'".format(row_id, repo_id, reference_name) db_option.insert_one(update_reference_sql) update_sql = "UPDATE repo_push_history set status=3, import_time='{0}' " \ "WHERE id={1}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), row_id) db_option.insert_one(update_sql) # 新增的分支和tag添加入数据库 if new_references: insert_refs_sql = "INSERT INTO repo_refs (name, category, repository_id) VALUES (%s, %s, %s)" db_option.insert_all(insert_refs_sql, new_references) # 新增的commit添加入数据库 if new_commits: insert_commit_sql = "INSERT INTO repo_commit (commit_hash, parent_hash, tree_hash, author_name, author_email, " \ "author_time, committer_name, committer_email, commit_time, message, repository_id) " \ "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" print new_commits db_option.insert_all(insert_commit_sql, new_commits) db_option.close()