def load_package_by_local_repository(self): """ 패키지를 해당 위치의 리파지토리로 로드 :return: """ logging.debug('load_package_by_local_repository :' + self.__package_path) if is_git_dir(osp.join(self.__package_path, '.git')): logging.debug("load local path is git directory") self.__package_repository = Repo(self.__package_path) # check is repository self.__package_repository.git.reset('--hard') # check branch if conf.DEFAULT_SCORE_BRANCH != conf.DEFAULT_SCORE_BRANCH_MASTER: self.__package_repository.git.checkout( conf.DEFAULT_SCORE_BRANCH) # git pull ? self.update_repository() return True else: logging.debug( "load local path is not git directory, so all file remove") # git 이 아닐경우 삭제 shutil.rmtree(self.__package_path, True) return False
def init(cls, path: str): git_path = '%s/.git' % path if os.path.exists(git_path) and is_git_dir(git_path): cls.native_repo = Repo(path) else: # auto generate .git when bare is False # 只有当bare为False时, 才自动生成.git文件夹 cls.native_repo = Repo.init(path, bare=False)
def updateGitWorkingDirs(workingDirs, root, dirs, gitDir=".git"): if gitDir in dirs and is_git_dir(os.path.join(root, gitDir)): assert os.path.isdir(root), root repo = GitRepo(git.Repo(root)) key = os.path.abspath(root) workingDirs[key] = repo.asRepoView() return key return None
def git_exists(self, local_dir): """``true``, the local_dir is a git working tree directory. Arguments: local_dir {str} -- local repo dir """ git_path = self.abspath(os.path.join(local_dir, '.git')) return is_git_dir(git_path)
def git_up_all(self, sourcetree=False): if sourcetree: print((colored("Loading projects from ", attrs=["bold"]) + colored("SourceTree" + "\n", color="green"))) projects = read_projects_from_sourcetree() else: print((colored("Loading projects from ", attrs=["bold"]) + colored(PROJECTS_FILE + "\n", color="green"))) projects = read_projects_from_json(PROJECTS_FILE) if not projects: print((colored("Could not read projects\n", color="red"))) return False results = {} for project_name, project_settings in projects.items(): """ This needs to be rewritten and tested! """ # Check if we are able to initialize settings to an object project = Project(project_settings) if not project: print((colored("Could not read project " + project_name, color="red"))) continue print((colored("- Working on: " + project.name + " @" + project.absolute_path, attrs=["underline"]))) # Check if a dir exists or clone if (not os.path.isdir(project.absolute_path) or os.listdir( project.absolute_path) == []) and project.git_url: print( "Project is not directory or not initialized repo. Cloning from url" ) project.repo = Repo.clone_from(url=project.git_url, to_path=project.absolute_path) print((colored("Repository cloned", color="green"))) # Check if the is not git and if not skip if not is_git_dir(os.path.join(project.absolute_path, ".git")) and not project.git_url: print((colored( "Project path is not a git dir and has no info. Skipping", color="red"))) continue # @todo probably should be moved if self.sync_repository(project): results.update({project.name: True}) else: results.update({project.name: False}) print((colored("- Finished on: " + project.name + " @" + project.absolute_path, attrs=["underline"]) + "\n")) self.print_results(results)
def git(self): if self._git_repo is None: from git.repo.fun import is_git_dir if not is_git_dir(self.root_dir): raise InvalidProjectError( f"Project directory {self.root_dir} is not a git repository" ) self._git_repo = Repo(self.root_dir) return self._git_repo
def findContainingRepo(rootDir, gitDir=".git"): """ Walk parents looking for a git repository. """ current = os.path.abspath(rootDir) while current and current != os.sep: if is_git_dir(os.path.join(current, gitDir)): return GitRepo(git.Repo(current)) current = os.path.dirname(current) return None
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 __initial(self): """ initialize git repository :return: """ assert os.path.exists(self.__local_path), 'Git path ie not exist.' git_local_path = os.path.join(self.__local_path, '.git') assert is_git_dir(git_local_path), '%s is not git repository'.format( git_local_path) self.__repo = Repo(self.__local_path) pass
def initial(self, repo_url, branch): """ 初始化git仓库 :param repo_url: :param branch: :return: """ 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(repo_url, to_path=self.local_path, branch=branch) else: self.repo = Repo(self.local_path)
def get_results(repos_dir, settings): paths = [] collect_only_dirty = settings.get('dirty', False) show_full_path = settings.get('full_path', False) for path, dirs, files in os.walk(repos_dir): if is_git_dir(path): repo = Repo(path) path = os.path.dirname(path) if show_full_path: path = PATH.format(heading=os.path.join(repos_dir, path)) else: path = PATH.format(heading=os.path.relpath(path, repos_dir)) bare = repo.bare dirty = repo.is_dirty() if (collect_only_dirty and dirty) or (not collect_only_dirty): paths.append({'path': path, 'bare': bare, 'dirty': dirty}) return paths
def git_up_all(self, sourcetree=False): if sourcetree: print(colored("Loading projects from ", attrs=["bold"]) + colored("SourceTree" + "\n", color="green")) projects = read_projects_from_sourcetree() else: print(colored("Loading projects from ", attrs=["bold"]) + colored(PROJECTS_FILE + "\n", color="green")) projects = read_projects_from_json(PROJECTS_FILE) if not projects: print(colored("Could not read projects\n", color="red")) return False results = {} for project_name, project_settings in projects.iteritems(): """ This needs to be rewritten and tested! """ # Check if we are able to initialize settings to an object project = Project(project_settings) if not project: print (colored("Could not read project " + project_name, color="red")) continue print(colored("- Working on: " + project.name + " @" + project.absolute_path, attrs=["underline"])) # Check if a dir exists or clone if (not os.path.isdir(project.absolute_path) or os.listdir( project.absolute_path) == []) and project.git_url: print ("Project is not directory or not initialized repo. Cloning from url") project.repo = Repo.clone_from(url=project.git_url, to_path=project.absolute_path) print (colored("Repository cloned", color="green")) # Check if the is not git and if not skip if not is_git_dir(os.path.join(project.absolute_path, ".git")) and not project.git_url: print (colored("Project path is not a git dir and has no info. Skipping", color="red")) continue # @todo probably should be moved if self.sync_repository(project): results.update({project.name: True}) else: results.update({project.name: False}) print(colored("- Finished on: " + project.name + " @" + project.absolute_path, attrs=["underline"]) + "\n") self.print_results(results)
def read_repo(url, data_path): """Read articles from a git repo, save repo in data_path""" p = giturlparse.parse(url) assert p.valid, "git url %s invalid" % url repo_path = os.path.abspath(os.path.join(data_path, "%s.%s" % (p.owner, p.host))) if is_git_dir(os.path.join(repo_path, ".git")): repo = git.Repo.init(repo_path) assert not repo.bare try: repo.git(git_dir=repo_path).pull() except git.exc.GitCommandError as ex: logger.warning(str(ex)) if not ex.status == 128: raise else: repo = git.Repo.clone_from(url, repo_path) for x in read_articles(repo_path): yield x
def read_repo(url, data_path): """Read articles from a git repo, save repo in data_path""" p = giturlparse.parse(url) assert p.valid, "git url %s invalid" % url repo_path = os.path.abspath( os.path.join(data_path, "%s.%s" % (p.owner, p.host))) if is_git_dir(os.path.join(repo_path, ".git")): repo = git.Repo.init(repo_path) assert not repo.bare try: repo.git(git_dir=repo_path).pull() except git.exc.GitCommandError as ex: logger.warning(str(ex)) if not ex.status == 128: raise else: repo = git.Repo.clone_from(url, repo_path) for x in read_articles(repo_path): yield x
def load_version(self, version): """ 버젼을 로드 합니다. :param version: :return: """ logging.debug('load score version: ' + str(version)) score_version_path = osp.join(self.__package_path, 'deploy', str(version)) logging.debug('package path :' + self.__package_path) logging.debug('load_version score path score_version_path:' + str(score_version_path)) # 이미 받은 버젼일 수도 있으므로 if is_git_dir(osp.join(score_version_path, '.git')): version_repo = Repo(str(score_version_path)) version_repo.git.reset('--hard') version_repo.git.checkout(version) elif version in self.__score_versions: # 리파지토리에 Score 버젼이 있다면 repo = Repo(self.__package_path) version_repo = repo.clone(str(score_version_path)) version_repo.git.checkout(version) # manager 가 해당 디렉토리의 package.json 파일을 읽어서 실행시킴 with open(os.path.join(str(score_version_path), 'package.json'), "r") as package: score_info = json.loads(package.read()) package.close() score_info["version"] = version # score package.json 파일에서 main 함수를 가져와서 실행 _score_main = osp.join(score_version_path, score_info["main"] + ".py") # 스코어 정보를 저장하고 출력해 줍니다. # score에 package.json 파일의 내용을 입력 해 줍니다. self.__scores[version] = util.load_user_score(_score_main)(score_info) return self.score_version(version, True)
def refresh_exploitdb_repository(self): self.console_logger.info("Refreshing ExploitDB Repository") repo = None origin = None if fun.is_git_dir(self.edb_path): repo = Repo(self.edb_path) else: repo = Repo.init(self.edb_path) try: origin = repo.remote('origin') except ValueError: origin = repo.create_remote('origin', self.exploitdb_repo) origin.fetch() if 'master' not in repo.heads: repo.create_head('master', origin.refs.master) repo.heads.master.set_tracking_branch(origin.refs.master) repo.heads.master.checkout() origin.pull() self.console_logger.info("Finished Refreshing ExploitDB Repository")
def git_repos_in(roots: List[Path]) -> List[Path]: from subprocess import check_output outputs = check_output([ 'fdfind', # '--follow', # right, not so sure about follow... make configurable? '--hidden', '--full-path', '--type', 'f', '/HEAD', # judging by is_git_dir, it should always be here.. *roots, ]).decode('utf8').splitlines() candidates = set(Path(o).resolve().absolute().parent for o in outputs) # exclude stuff within .git dirs (can happen for submodules?) candidates = {c for c in candidates if '.git' not in c.parts[:-1]} candidates = {c for c in candidates if is_git_dir(c)} repos = list(sorted(map(_git_root, candidates))) return repos
def execute_find_secrets_request(request: FindSecretsRequest) -> List[Secret]: """ Executes the search for secrets with the given request :param FindSecretsRequest request: request object containing the path to the git repository and other configurations for the search :return: list of secret objects that represent the secrets found by the search """ path = request.path repo_config = request.repo_config search_config = request.search_config if not repo_config: repo_config = RepoConfig() if not search_config: search_config = SearchConfig() token_key = repo_config.access_token_env_key token_exists = token_key and token_key in os.environ repo = None repo_path = path if is_git_dir(path + os.path.sep + ".git"): # If repo is local and env key for access token is present, display warning if token_exists: warnings.warn( "Warning: local repository path provided with an access token - " "Token will be ignored") else: # If repo is remote, append access token to path from its env key git_url = path if token_exists: git_url = _append_env_access_token_to_path(path, token_key) # We pre-clone the repo to fix a bug that causes truffleHog to crash # on Windows machines when run on remote repositories. try: repo_path = tempfile.mkdtemp() repo = Repo.clone_from(git_url, repo_path) except Exception as e: _delete_tempdir(repo_path) raise TrufflehogApiError(e) do_regex = search_config.regexes secrets = None try: output = truffleHog.find_strings( git_url=None, since_commit=repo_config.since_commit, max_depth=search_config.max_depth, do_regex=do_regex, do_entropy=search_config.entropy_checks_enabled, custom_regexes=search_config.regexes, branch=repo_config.branch, repo_path=repo_path, path_inclusions=search_config.include_search_paths, path_exclusions=search_config.exclude_search_paths) secrets = _convert_default_output_to_secrets(output) _clean_up(output) except Exception as e: raise TrufflehogApiError(e) # Delete our clone of the remote repo (if it exists) if repo is not None: repo.close( ) # truffleHog doesn't do this, which causes a bug on Windows _delete_tempdir(repo_path) return secrets
def dquery_is_git_dir(dir_path): git_dir_path = os.path.join(dir_path, '.git') return os.path.isdir(git_dir_path) and is_git_dir(git_dir_path)
def execCommand(repoPath, command, remote): if(command == "create"): if(is_git_dir(os.path.join(repoPath, ".git"))): print("Git-Repo existiert bereits. Abbruch...") return 0; repoObj = git.Repo.init(repoPath, True); docFile = open(os.path.join(repoPath, "docid"), 'w'); docFile.write("0"); docFile.close(); repoObj.index.add(["docid"]); repoObj.index.commit("Erstelle neues Buchhaltungsrepo."); print("Git-Repo wurde angelegt."); return 0; repoObj = git.Repo(repoPath); remoteExists = False; try: remoteObj = repoObj.remote(remote); remoteExists = True; except ValueError: pass; if len(repoObj.untracked_files) != 0 or repoObj.is_dirty(): print("Im Git-Repo sind Aenderungen ohne Commit gemacht wurden. Achtung, Inkosistenzen sind möglich!") return 1; if(command == "transaction"): if remoteExists: remoteObj.pull(); genDirList(repoPath); printAccountStructure(repoPath); fromAccount = getAccountInt("Bitte das Startkonto eingeben: "); toAccount = getAccountInt("Bitte das Zielkonto eingeben: ", fromAccount); transferAmount = readTransferAmount(); # Open start and end account file fromAccountFile = open(os.path.join(repoPath, dirList[fromAccount], "account")); toAccountFile = open(os.path.join(repoPath, dirList[toAccount], "account")); # Calculate new amounts fromAccountAmount = int(fromAccountFile.readline()); fromAccountAmount -= transferAmount; toAccountAmount = int(toAccountFile.readline()); toAccountAmount += transferAmount; # Close files and reopen them fromAccountFile.close(); toAccountFile.close(); fromAccountFile = open(os.path.join(repoPath, dirList[fromAccount], "account"), 'w'); toAccountFile = open(os.path.join(repoPath, dirList[toAccount], "account"), 'w'); # Write new amounts and close files fromAccountFile.write(str(fromAccountAmount)); toAccountFile.write(str(toAccountAmount)); fromAccountFile.close(); toAccountFile.close(); repoObj.index.add([os.path.join(dirList[fromAccount], "account"), os.path.join(dirList[toAccount], "account")]); importDocumentQuest = raw_input("Möchtest du ein Dokument importieren ([y]es/[n]o): "); if importDocumentQuest == 'y' or importDocumentQuest == 'yes': importDocument(repoObj, repoPath); transferMsg = raw_input("Bitte eine Meldung für die Transaktion angeben: "); repoObj.index.commit(transferMsg); if remoteExists: remoteObj.push(); print("Transaktion erfolgreich ausgeführt"); elif(command == "accounts"): printAccountStructure(repoPath); elif(command == "createaccount"): if remoteExists: remoteObj.pull(); acctName = raw_input("Bitte den Namen des Kontos angeben (einzelne Ebene durch \"/\" abtrennen): "); acctNameList = acctName.split(sep="/"); if(os.path.isfile(os.path.join(repoPath, os.path.sep.join(acctNameList), "account"))): print("Konto existiert bereits.") return 1; path = repoPath; for dirName in acctNameList: path = os.path.join(path, dirName); try: os.makedirs(path) except OSError as e: if e.errno == errno.EEXIST and os.path.isdir(path): pass; else: raise; pathFile = os.path.join(path, "account") if(not os.path.isfile(pathFile)): accountFile = open(pathFile, 'w'); accountFile.write("0"); accountFile.close(); repoObj.index.add([pathFile]); repoObj.index.commit("Lege neues Konto \"{}\" an.".format(acctName)); if remoteExists: remoteObj.push(); print("Konto wurde erfolgreich angelegt."); elif(command == "list"): for commit in repoObj.iter_commits(): dateTuple = time.localtime(commit.authored_date); commitMsg = commit.message.strip(); print("{:04d}-{:02d}-{:02d} {:02d}:{:02d} -- {}".format(dateTuple[0], dateTuple[1], dateTuple[2], dateTuple[3], dateTuple[4], commitMsg)); if len(commit.parents) > 0: diffList = commit.parents[0].diff(commit, create_patch=True) if len(diffList) >= 2: fromAccount = None; toAccount = None; differenceAmount = 0; for diffEntry in diffList: if(diffEntry.deleted_file or diffEntry.new_file or diffEntry.renamed or diffEntry.a_path == "docid"): break; diffStr = diffEntry.diff.decode("utf-8") removedValue = re.search(r"^-(-?\d+)", diffStr, re.MULTILINE); removedValue = int(removedValue.group(1)); addedValue = re.search(r"^\+(-?\d+)", diffStr, re.MULTILINE); addedValue = int(addedValue.group(1)); difference = addedValue - removedValue; if difference < 0: fromAccount = os.path.dirname(diffEntry.a_path); elif difference > 0: toAccount = os.path.dirname(diffEntry.a_path); differenceAmount = difference; else: print("Eine Differenz von 0. Da ist wohl was schief gegangen."); exit(1); continue; differenceAmount = str(differenceAmount); differenceAmount = differenceAmount[:len(differenceAmount)-2] + "." + differenceAmount[len(differenceAmount)-2:] try: print("{} --> {}: {}".format(fromAccount, toAccount, differenceAmount)); except Exception: pass; print("") return 0;