def __init__(self, source_repo_path, source_repo_branch, target_repo_path, target_repo_relative_directory, target_repo_commit_branch): self.source_repo_path = source_repo_path self.source_repo_branch = source_repo_branch self.target_repo_path = target_repo_path self.target_repo_commit_branch = target_repo_commit_branch # Instantiate repo objects self.source_repo = Repo(self.source_repo_path) self.target_repo = Repo(self.target_repo_path) # saving the head of the source repo self.source_head = self.source_repo.head.name # extract repo names self.source_repo_name = get_repo_name(self.source_repo) self.target_repo_name = get_repo_name(self.target_repo) # export paths self.target_repo_archive_path_root = "{}/{}".format(COMMIT_EXPORT_PATH, self.source_repo_name) self.target_repo_archive_path_tar = "{}/{}".format(self.target_repo_archive_path_root, "commit_archive.tar") self.target_repo_directory = "{}/{}".format(self.target_repo_path, target_repo_relative_directory) # ingores to add at each commit self.ignores_to_add = extract_gitinore(self.source_repo_path) # initialize commit infos self._exact_commits()
def _prepare(cls, create, **kwargs): extract = super(ExtractFactory, cls)._prepare(create, **kwargs) chapter = kwargs.pop('chapter', None) with open(extract.get_path(relative=False), "w") as f: f.write("This dumb content is just here to prove you zep12 is far better than old module") if chapter: if chapter.tutorial: repo = Repo(os.path.join(settings.ZDS_APP['tutorial']['repo_path'], chapter.tutorial.get_phy_slug())) index = repo.index index.add([extract.get_path(relative=True)]) man_path = os.path.join(chapter.tutorial.get_path(), "manifest.json") chapter.tutorial.dump_json(path=man_path) index.add(["manifest.json"]) chapter.tutorial.sha_draft = index.commit( "bla", author=Actor("bla", "*****@*****.**"), committer=Actor("bla", "*****@*****.**")) chapter.tutorial.save() elif chapter.part: repo = Repo(os.path.join(settings.ZDS_APP['tutorial']['repo_path'], chapter.part.tutorial.get_phy_slug())) index = repo.index index.add([extract.get_path(relative=True)]) man_path = os.path.join(chapter.part.tutorial.get_path(), "manifest.json") chapter.part.tutorial.dump_json(path=man_path) index.add(["manifest.json"]) chapter.part.tutorial.sha_draft = index.commit( "bla", author=Actor("bla", "*****@*****.**"), committer=Actor("bla", "*****@*****.**")) chapter.part.tutorial.save() return extract
def pull_request(pathList): addList, deleteList = [], [] for path in pathList: repo = Repo(path) # 检查版本库是否为空 if repo.bare: return None # 获取远程默认版本库 origin remote = repo.remote() try: # 从远程版本库拉取分支 remote.pull() except Exception as e: print(path) print(str(e)) git = repo.git # 读取本地版本库的信息 strList = git.log('--numstat').split() # 需要计算的文件 file = 'Units.cpp' # 运行前需要设置时间 add, delete = count_add_delete(strList, file, 'Jun',10, 'Jun', 16) addList.append(add) deleteList.append(delete) print('add:', addList) print('delete:', deleteList) write_excel(addList, deleteList)
def detect_all(self, args): historage = Repo(args.historage_dir) pull_up_method_candidates = detect_pull_up_method(historage) print(('"old_commit","new_commit","old_org_commit","new_org_commit",' + '"src_method","dst_method","similarity","isSamePrameters"')) for info in pull_up_method_candidates: print((','.join(['"' + str(s) + '"' for s in info])))
def get_git_repo() -> Repo: """ Find the path containing the git repo. Start by checking the current working directory, and proceed up the directory tree if a git repo can't be found. returns: Paath to closest git repo raises FileNotFoundError: if no git repo is found in the current path """ def _traverse_dirs(path: str) -> Generator[str, None, None]: # first yield the current directory yield path # then start yielding parents until we reach the root while True: parent = os.path.dirname(path) if path != parent: yield parent path = parent else: break cwd = os.getcwd() for dir_ in _traverse_dirs(cwd): try: repo = Repo(dir_) return repo except InvalidGitRepositoryError: pass raise FileNotFoundError("No git repo found in path: {}".format(cwd))
def _prepare(cls, create, **kwargs): light = kwargs.pop('light', False) tuto = super(BigTutorialFactory, cls)._prepare(create, **kwargs) path = tuto.get_path() real_content = content if light: real_content = content_light if not os.path.isdir(path): os.makedirs(path, mode=0o777) man = export_tutorial(tuto) repo = Repo.init(path, bare=False) repo = Repo(path) f = open(os.path.join(path, 'manifest.json'), "w") f.write(json_writer.dumps(man, indent=4, ensure_ascii=False).encode('utf-8')) f.close() f = open(os.path.join(path, tuto.introduction), "w") f.write(real_content.encode('utf-8')) f.close() f = open(os.path.join(path, tuto.conclusion), "w") f.write(real_content.encode('utf-8')) f.close() repo.index.add(['manifest.json', tuto.introduction, tuto.conclusion]) cm = repo.index.commit("Init Tuto") tuto.sha_draft = cm.hexsha tuto.sha_beta = None tuto.gallery = GalleryFactory() for author in tuto.authors.all(): UserGalleryFactory(user=author, gallery=tuto.gallery) return tuto
def github(): """ Entry point for github webhook testkey""" logger.info("newnewnewcodenewcode") sha,signature = request.headers.get('X-Hub-Signature').split('=') logger.info(f'signature:{signature}'+ str(request.data)) secret = str.encode(current_app.config.get('GITHUB_SECRET')) hashhex = hmac.new(secret, request.data, digestmod='sha1').hexdigest() logger.info(f'hashhex:{hashhex}') if hmac.compare_digest(hashhex, signature): try: if 'GIT_DIR' in os.environ: del os.environ['GIT_DIR'] logger.info('hash对比正确' + current_app.config.get('REPO_PATH')) logger.info("开始拉仓库") # repo = Repo(current_app.config.get('REPO_PATH')) repo = Repo("/usr/local/python3/graduate998/backStageNew") # repo.git.pull() #--- origin = repo.remotes.origin origin.pull() #--- # 获取默认版本库 origin # remote = repo.remote() # 从远程版本库拉取分支 # remote.pull() logger.info("pull操作完成了啊!") # commit = request.json['after'][0:6] # logger.info('Repository updated with commit {}'.format(commit)) except: logger.info(traceback.format_exc()) return jsonify({"error": str(traceback.format_exc())}),500 return jsonify({}),200
def _prepare(cls, create, **kwargs): tuto = super(MiniTutorialFactory, cls)._prepare(create, **kwargs) path = tuto.get_path() if not os.path.isdir(path): os.makedirs(path, mode=0o777) man = export_tutorial(tuto) repo = Repo.init(path, bare=False) repo = Repo(path) file = open(os.path.join(path, 'manifest.json'), "w") file.write( json_writer.dumps( man, indent=4, ensure_ascii=False).encode('utf-8')) file.close() file = open(os.path.join(path, tuto.introduction), "w") file.write(u'Test') file.close() file = open(os.path.join(path, tuto.conclusion), "w") file.write(u'Test') file.close() repo.index.add(['manifest.json', tuto.introduction, tuto.conclusion]) cm = repo.index.commit("Init Tuto") tuto.sha_draft = cm.hexsha return tuto
def get_text(self, sha=None): if self.chapter.tutorial: tutorial = self.chapter.tutorial else: tutorial = self.chapter.part.tutorial repo = Repo(tutorial.get_path()) # find hash code if sha is None: sha = tutorial.sha_draft manifest = get_blob(repo.commit(sha).tree, "manifest.json") tutorial_version = json_reader.loads(manifest) if "parts" in tutorial_version: for part in tutorial_version["parts"]: if "chapters" in part: for chapter in part["chapters"]: if "extracts" in chapter: for extract in chapter["extracts"]: if extract["pk"] == self.pk: path_ext = extract["text"] break if "chapter" in tutorial_version: chapter = tutorial_version["chapter"] if "extracts" in chapter: for extract in chapter["extracts"]: if extract["pk"] == self.pk: path_ext = extract["text"] break if path_ext: return get_blob(repo.commit(sha).tree, path_ext) else: return None
def __init__(self): try: self.repo = Repo('.') except InvalidGitRepositoryError: msg = 'Not in a git repository' raise ConfigurationError(msg, 1) self._load_config() self._missing_config = [] config = { 'deploy.repo-name': { 'required': True, }, 'deploy.required-umask': { 'required': False, 'default': None, }, 'user.name': { 'required': True, }, 'user.email': { 'required': True, }, } self._register_config(config) self.register_drivers()
def git_pull(remote_url, local, idc_dir): """ @remote: git remote address @local: local git repo dir """ commit_msg = None if not os.path.exists(local): os.mkdir(local) logging.info('git start pull from remote repo.') if not os.path.exists( "{0}/.git".format(local)) or not git.repo.fun.is_git_dir( "{0}/.git".format(local)): repo_clone = Repo.clone_from(remote_url, local) if not repo_clone: logging.debug("clone repo from {0} to {1}failed.\n".format( remote_url, local)) return repo = Repo(local) remote = None git_commit = git.Git(idc_dir) last_commit_log = "/var/log/last_commit.log" last_commit = None if os.path.exists(last_commit_log): with file(last_commit_log) as fhand: last_log_commit = fhand.read().strip() # Warning: for performance only check 500 commit, use this write mysql for commit in repo.iter_commits('master', max_count=500): if last_commit == commit.hexsha: last_commit = last_log_commit break if not last_commit: last_commit = repo.head.commit.hexsha with FileLock(os.path.split(local)[1]) as _flock: for remote_name in repo.remotes: if remote_name.url == remote_url: remote = remote_name break if remote is None: remote_name = "%x" % (random.randint(0, 1000000) ^ int(time.time())) remote = repo.create_remote(remote_name, remote_url) ret = 0 try: info_list = remote.pull() for info in info_list: ret = info.flags & (git.FetchInfo.REJECTED | git.FetchInfo.ERROR) if ret > 0: logging.warning("[conf-pull] pull from remote error!\n") commit_msg = git_commit.log("{0}..HEAD".format(last_commit), "--pretty=%H,%ai,%s", idc_dir) except (git.GitCommandError, git.RepositoryDirtyError), e: logging.warning("git pull error, error={0}\n".format(str(e))) except Exception: logging.warning("git pull error, other exception.\n")
def _init_repo(self, **kw): try: init_folder = False if not os.path.exists(self.dev_root): if self._force: if not self.repo_server.has_repo(self.name): self.repo_server.create_repo(self.name) init_folder = True self.repo_server.clone_repo(self.name, self.dev_root) if self.type == 'module' and init_folder: local_repo_path = os.path.join(self.dev_root, self.name) os.makedirs(local_repo_path) shutil.copy( os.path.join(self.repo_server._resrc, ".gitignore"), self.dev_root) else: raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), self.dev_root) self._repo = Repo(self.dev_root) if init_folder: self.stage_changes() self.commit_changes("first_commit") self.push_changes() except Exception as err: raise RepoPkgInitError(err)
def cli(config_path: str) -> None: """Prints information about envadmin configuration.""" click.echo("📝 Envadmin Status 📝") click.echo("Version: %s" % constants.VERSION) if os.path.isfile(os.path.join(config_path, ".envadmin")): click.echo("Configuration: Found") parser = configparser.ConfigParser() parser.read(os.path.join(config_path, ".envadmin")) repo_path = parser.get("main", "repo_path") else: click.echo("Configuration: Not found!") click.echo("Git Repo: N/A") return if os.path.isdir(repo_path): try: Repo(repo_path) click.echo("Git Repo: Found") except GitError: click.echo("Git Repo: Error loading git information.") return else: click.echo("Git Repo: Path not valid.") return
def _check_repo_and_getcommit(self): #TODO improve this logic. I would like to retrain not only for new commits but also fo new data. # Also add tests repo = Repo(search_parent_directories=True) if not repo.is_dirty() or True: return repo.head.object.hexsha else: raise ValueError('working directory is not clean! please commit before running the server')
def load_json_for_public(self, sha=None): if sha is None: sha = self.sha_public repo = Repo(self.get_path()) mantuto = get_blob(repo.commit(sha).tree, 'manifest.json') data = json_reader.loads(mantuto) return data
def get_latest_status(self): """ Property for viewing repository latest commit status message. """ repo = Repo( Repo.get_repository_location(self.owner.username, self.name)) return repo.get_latest_status()
def check_branch(package_path, branch_name): """ Given a package path and a branch name, check if the branch exists in the package """ repo = Repo(package_path) if branch_name in repo.branches: return True return False
def git_pull(remote_url, git_local): """ @remote_url: git remote address @git_local: local git repo dir """ logging.info('git start pull from remote repo.') # check if we need to clean the local git clean_local_git(False, git_local, remote_url) # git clone if not exist if not os.path.exists("{0}/.git".format(git_local)) or \ not git.repo.fun.is_git_dir("{0}/.git".format(git_local)): logging.info('git clone from [%s] to [%s], and depth=1.', remote_url, git_local) repo_clone = Repo.clone_from(remote_url, git_local,depth=1) if not repo_clone: logging.error( "clone repo from {0} to {1}failed.".format(remote_url, git_local)) logging.info('========run stop========\n\n') sys.exit(1) # get repo repo = Repo(git_local) type(repo.git).GIT_PYTHON_TRACE = 'full' remote = None # get remote url for item in repo.remotes: if item.url == remote_url: remote = item break if remote is None: clean_local_git(True, git_local, remote_url) # start pull ssh_executable = os.path.join(g_script_path, 'debug_ssh.sh') with repo.git.custom_environment(GIT_SSH_COMMAND=ssh_executable): try: logging.info('git pull from remote=%s, url=%s', remote.name, remote.url) info_list = remote.pull() for info in info_list: ret = info.flags & (git.FetchInfo.REJECTED | git.FetchInfo.ERROR) if ret > 0: logging.warning("git pull from remote ERROR: %s", info_list) logging.info('========run stop========\n\n') sys.exit(1) else: logging.info("git pull return mesg: %s", info_list) except BadName: logging.warn("git pull warn: ", exc_info=True) except Exception: clean_local_git(True, git_local, remote_url) # get last commit msg last_commit_id = str(repo.head.commit) return last_commit_id
def on_get(self, req, res): repoPath = './IntallPlist' #本地仓库路径 result = 'success' #默认返回success msg = [] repo = None # 判断本地是否有仓库 if os.path.exists(repoPath): repo = Repo(repoPath) msg.append('已有仓库') else: repo = Repo.clone_from( url='[email protected]:hillyoung/TestPlist.git', to_path=repoPath) msg.append('不存在仓库') git = repo.git git.checkout('*') remote = repo.remote() remote.pull() msg.append('完成拉取远程仓库代码') # 判断是否传入了文件名 name = req.params.get('name', None) if name is None: msg.append('传入无效参数') result = 'fail' else: msg.append('传入应用名:' + name) # 构造plist文件内容及相关控制逻辑 directoryPath = repoPath + '/' + name if os.path.exists(directoryPath) != True: os.mkdir(directoryPath) filePath = directoryPath + '/install.plist' #plist文件相对路径 rContent = None #用于缓存仓库中plist文件的内容 flag = 'w' #文件的打开访问,默认以写入方式打开 if os.path.exists(filePath): #判断仓库中是否存在plist文件 flag = 'r+' #如果plist文件存在,则以读写的方式打开 msg.append('存在plist文件') content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>items</key><array><dict><key>assets</key><array><dict><key>kind</key><string>software-package</string><key>url</key><string>https://raw.githubusercontent.com/hillyoung/AppInstaller/master/app/' + name + '/LiemsMobileEnterprise.ipa</string></dict></array><key>metadata</key><dict><key>bundle-identifier</key> <string>net.luculent.liems.hhgs</string><key>bundle-version</key><string>1.0.1</string><key>kind</key><string>software</string><key>title</key><string>测试安装</string></dict></dict></array></dict></plist>' with open(filePath, flag) as flie_object: #写入plist if flag == 'r+': rContent = flie_object.read() flie_object.seek(0) msg.append(rContent) if rContent != content: #写入plist内容 flie_object.write(content) flie_object.close() #提交plist文件 git.add('*') git.commit('-m', '添加' + name) remote.push() res.body = json.dumps({'result': result, 'msg': msg}) res.status = falcon.HTTP_200 # This is the default status res.append_header('Access-Control-Allow-Origin', '*')
def run(self, pack): """ :param pack: Installed Pack Name to get info about :type pack: ``str`` """ packs_base_paths = get_packs_base_paths() pack_path = None metadata_file = None for packs_base_path in packs_base_paths: pack_path = os.path.join(packs_base_path, pack) pack_yaml_path = os.path.join(pack_path, MANIFEST_FILE_NAME) if os.path.isfile(pack_yaml_path): metadata_file = pack_yaml_path break # Pack doesn't exist, finish execution normally with empty metadata if not os.path.isdir(pack_path): return {"pack": None, "git_status": None} if not metadata_file: error = 'Pack "%s" doesn\'t contain pack.yaml file.' % (pack) raise Exception(error) try: details = self._parse_yaml_file(metadata_file) except Exception as e: error = 'Pack "%s" doesn\'t contain a valid pack.yaml file: %s' % ( pack, six.text_type(e), ) raise Exception(error) try: repo = Repo(pack_path) git_status = "Status:\n%s\n\nRemotes:\n%s" % ( repo.git.status().split("\n")[0], "\n".join([remote.url for remote in repo.remotes]), ) ahead_behind = repo.git.rev_list("--left-right", "--count", "HEAD...origin/master").split() # Dear god. if ahead_behind != ["0", "0"]: git_status += "\n\n" git_status += "%s commits ahead " if ahead_behind[ 0] != "0" else "" git_status += "and " if "0" not in ahead_behind else "" git_status += "%s commits behind " if ahead_behind[ 1] != "0" else "" git_status += "origin/master." except InvalidGitRepositoryError: git_status = None return {"pack": details, "git_status": git_status}
def initial(self, branch): if not os.path.exists(self.local_path): os.makedirs(self.local_path) git_local_path = os.path.join(self.local_path, '.git') if not is_git_dir(git_local_path): self.repo = Repo.clone_from(self.repo_url, to_path=self.local_path, branch=branch) else: self.repo = Repo(self.local_path)
def _save_run() -> str: assert SkipBlock.logger.path is not None repo = Repo() _write_replay_file() repo.git.add("-A") commit = repo.index.commit( f"{repo.active_branch.name}@{flags.NAME}::{SkipBlock.logger.path.name}" ) commit_sha = commit.hexsha return commit_sha
def load_json_for_public(self, sha=None): if sha is None: sha = self.sha_public repo = Repo(self.get_path()) mantuto = get_blob(repo.commit(sha).tree, 'manifest.json') data = json_reader.loads(mantuto) if 'licence' in data: data['licence'] = Licence.objects.filter( code=data['licence']).first() return data
def _decorator(request, *args, **kwargs): try: repo = Repo(Repo.get_repository_location(kwargs['username'], kwargs['repository'])) branches = repo.get_branches() if 'rev' not in kwargs or kwargs['rev'] in branches + ['HEAD']: return func(request, *args, **kwargs) else: raise Http404() except: raise Http404()
def getCommits(repo_name): shas = set() commits_list = [] repo = Repo(os.path.join(work_dir, repo_name)) for b in repo.remote().fetch(): if '/' not in b.name: continue print("start to check branch {} of {}".format(b.name, repo_name)) branch_name = b.name.split('/')[1] repo.git.checkout('-B', branch_name, b.name) commits = list(repo.iter_commits()) for idx, commit in enumerate(commits): if str(commit) in shas: continue else: shas.add(str(commit)) commit_collection = {} xml_cnt = 0 kot_jav_cnt = 0 file_list = list(commit.stats.files) for file in file_list: if len(file) > 4 and file[-4:] == '.xml': xml_cnt += 1 elif len(file) > 3 and file[-3:] == '.kt' or len( file) > 5 and file[-5:] == '.java': kot_jav_cnt += 1 if xml_cnt >= 1 and kot_jav_cnt >= 1: commit_collection["commit_id"] = str(commit) commit_collection["commit_msg"] = str.strip(commit.message) commit_collection["commit_time"] = str( commit.committed_datetime) commit_collection["committer_name"] = commit.author.name commit_collection["committer_email"] = commit.author.email diff_files = [] if not commit.parents: continue else: for diff in commit.parents[0].diff(commit): diff_file = {} diff_file["file_path"] = diff.a_path diff_file["change_type"] = diff.change_type diff_file["lang"] = os.path.splitext( diff.a_path)[1][1:] diff_files.append(diff_file) commit_collection["diff_files"] = diff_files commit_collection["parent_commit_num"] = len(commit.parents) commits_list.append(commit_collection) repo_dump = json.dumps(commits_list, indent=2, ensure_ascii=False) with open(os.path.join(stats_dir, repo_name + ".json"), "w") as commit_fd: commit_fd.write(repo_dump) print(repo_name + " done")
def init_bare_repo_signal(sender, instance, created, **kwargs): """ Initialize a bare git repository on server's deposit after it's creation. """ if created: # Create repository folder under GIT_DEPOSIT_ROOT/username/repository.git and initialize it as bare. repository_location = Repo.get_repository_location( instance.owner.username, instance.name) repo = Repo(repository_location) repo.init_bare_repo()
def readme(request, username, repository, rev): """ Returns README content of a folder inside the given repository. """ repo = Repo(Repo.get_repository_location(username, repository)) request_sections = partition_url(request.path_info) rdm = GitBlob(repo=repo, path='/'.join(request_sections[5:]), rev=rev).show() return HttpResponse(rdm)
def __init_repo(self): from git.repo import Repo try: repo = Repo(self.cwd, search_parent_directories=True) self.repo = repo self.top_level = repo.git.rev_parse('--show-toplevel') except Exception: raise RuntimeError( 'fn: directory is not a git repository, or git is not installed.' )
def __init_repo(self): from git.repo import Repo try: repo = Repo(self.cwd, search_parent_directories=True) self.top_level = repo.git.rev_parse('--show-toplevel') self.repo = repo # self.top_level = self.repo.git.rev_parse('--show-toplevel') except Exception: # git sha will be '' if we are not in a git repo pass
def __init__(self, database): logging.info("Initializing GitSync.") self.database = database self.charts = dict() self.url = DEFAULT_GITREPO try: self.repo = Repo(REPO_DIRECTORY) except InvalidGitRepositoryError: logging.info("Cloning repository in %s", REPO_DIRECTORY) self.repo = Repo.clone_from(self.url, REPO_DIRECTORY)