def is_valid_repo_group(repo_group_name, base_path, skip_path_check=False): """ Returns True if given path is a repository group False otherwise :param repo_name: :param base_path: """ full_path = os.path.join(base_path, repo_group_name) # check if it's not a repo if is_valid_repo(repo_group_name, base_path): return False try: # we need to check bare git repos at higher level # since we might match branches/hooks/info/objects or possible # other things inside bare git repo get_scm(os.path.dirname(full_path)) return False except VCSError: pass # check if it's a valid path if skip_path_check or os.path.isdir(full_path): return True return False
def get_repo(path=None, alias=None, create=False): """ Returns ``Repository`` object of type linked with given ``alias`` at the specified ``path``. If ``alias`` is not given it will try to guess it using get_scm method """ if create: if not (path or alias): raise TypeError( "If create is specified, we need path and scm type") return get_backend(alias)(path, create=True) if path is None: path = abspath(os.path.curdir) try: scm, path = get_scm(path, search_up=True) path = abspath(path) alias = scm except VCSError: raise VCSError("No scm found at %s" % path) if alias is None: alias = get_scm(path)[0] backend = get_backend(alias) repo = backend(path, create=create) return repo
def is_valid_repo_group(repo_group_name, base_path, skip_path_check=False): """ Returns True if given path is a repository group False otherwise :param repo_name: :param base_path: """ full_path = os.path.join(safe_str(base_path), safe_str(repo_group_name)) # check if it's not a repo if is_valid_repo(repo_group_name, base_path): return False try: # we need to check bare git repos at higher level # since we might match branches/hooks/info/objects or possible # other things inside bare git repo get_scm(os.path.dirname(full_path)) return False except VCSError: pass # check if it's a valid path if skip_path_check or os.path.isdir(full_path): return True return False
def test_get_two_scms_for_path(self): multialias_repo_path = os.path.join(TESTS_TMP_PATH, 'hg-git-repo-2') if os.path.isdir(multialias_repo_path): shutil.rmtree(multialias_repo_path) os.mkdir(multialias_repo_path) with pytest.raises(VCSError): get_scm(multialias_repo_path)
def _get_repos(p): if not os.access(p, os.R_OK) or not os.access(p, os.X_OK): log.warning('ignoring repo path without access: %s' % (p, )) return if not os.access(p, os.W_OK): log.warning('repo path without write access: %s' % (p, )) for dirpath in os.listdir(p): if os.path.isfile(os.path.join(p, dirpath)): continue cur_path = os.path.join(p, dirpath) # skip removed repos if skip_removed_repos and REMOVED_REPO_PAT.match(dirpath): continue #skip .<somethin> dirs if dirpath.startswith('.'): continue try: scm_info = get_scm(cur_path) yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info except VCSError: if not recursive: continue #check if this dir containts other repos for recursive scan rec_path = os.path.join(p, dirpath) if os.path.isdir(rec_path): for inner_scm in _get_repos(rec_path): yield inner_scm
def _get_repos(p): if not os.access(p, os.R_OK) or not os.access(p, os.X_OK): log.warning('ignoring repo path without access: %s', p) return if not os.access(p, os.W_OK): log.warning('repo path without write access: %s', p) for dirpath in os.listdir(p): if os.path.isfile(os.path.join(p, dirpath)): continue cur_path = os.path.join(p, dirpath) # skip removed repos if skip_removed_repos and REMOVED_REPO_PAT.match(dirpath): continue #skip .<somethin> dirs if dirpath.startswith('.'): continue try: scm_info = get_scm(cur_path) yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info except VCSError: if not recursive: continue #check if this dir containts other repos for recursive scan rec_path = os.path.join(p, dirpath) if not os.path.islink(rec_path) and os.path.isdir(rec_path): for inner_scm in _get_repos(rec_path): yield inner_scm
def get_filesystem_repos(path): """ Scans given path for repos and return (name,(type,path)) tuple :param path: path to scan for repositories :param recursive: recursive search and return names with subdirs in front """ # remove ending slash for better results path = path.rstrip(os.sep) log.debug('now scanning in %s', path) def isdir(*n): return os.path.isdir(os.path.join(*n)) for root, dirs, _files in os.walk(path): recurse_dirs = [] for subdir in dirs: # skip removed repos if REMOVED_REPO_PAT.match(subdir): continue # skip .<something> dirs TODO: rly? then we should prevent creating them ... if subdir.startswith('.'): continue cur_path = os.path.join(root, subdir) if isdir(cur_path, '.git'): log.warning('ignoring non-bare Git repo: %s', cur_path) continue if (isdir(cur_path, '.hg') or isdir(cur_path, '.svn') or isdir(cur_path, 'objects') and (isdir(cur_path, 'refs') or os.path.isfile(os.path.join(cur_path, 'packed-refs')))): if not os.access(cur_path, os.R_OK) or not os.access( cur_path, os.X_OK): log.warning('ignoring repo path without access: %s', cur_path) continue if not os.access(cur_path, os.W_OK): log.warning('repo path without write access: %s', cur_path) try: scm_info = get_scm(cur_path) assert cur_path.startswith(path) repo_path = cur_path[len(path) + 1:] yield repo_path, scm_info continue # no recursion except VCSError: # We should perhaps ignore such broken repos, but especially # the bare git detection is unreliable so we dive into it pass recurse_dirs.append(subdir) dirs[:] = recurse_dirs
def get_filesystem_repos(path): """ Scans given path for repos and return (name,(type,path)) tuple :param path: path to scan for repositories :param recursive: recursive search and return names with subdirs in front """ # remove ending slash for better results path = safe_str(path.rstrip(os.sep)) log.debug('now scanning in %s', path) def isdir(*n): return os.path.isdir(os.path.join(*n)) for root, dirs, _files in os.walk(path): recurse_dirs = [] for subdir in dirs: # skip removed repos if REMOVED_REPO_PAT.match(subdir): continue #skip .<something> dirs TODO: rly? then we should prevent creating them ... if subdir.startswith('.'): continue cur_path = os.path.join(root, subdir) if isdir(cur_path, '.git'): log.warning('ignoring non-bare Git repo: %s', cur_path) continue if (isdir(cur_path, '.hg') or isdir(cur_path, '.svn') or isdir(cur_path, 'objects') and (isdir(cur_path, 'refs') or os.path.isfile(os.path.join(cur_path, 'packed-refs')))): if not os.access(cur_path, os.R_OK) or not os.access(cur_path, os.X_OK): log.warning('ignoring repo path without access: %s', cur_path) continue if not os.access(cur_path, os.W_OK): log.warning('repo path without write access: %s', cur_path) try: scm_info = get_scm(cur_path) assert cur_path.startswith(path) repo_path = cur_path[len(path) + 1:] yield repo_path, scm_info continue # no recursion except VCSError: # We should perhaps ignore such broken repos, but especially # the bare git detection is unreliable so we dive into it pass recurse_dirs.append(subdir) dirs[:] = recurse_dirs
def get_repo(path=None, alias=None, create=False): """ Returns ``Repository`` object of type linked with given ``alias`` at the specified ``path``. If ``alias`` is not given it will try to guess it using get_scm method """ if create: if not (path or alias): raise TypeError("If create is specified, we need path and scm type") return get_backend(alias)(path, create=True) if path is None: path = abspath(os.path.curdir) try: scm, path = get_scm(path, search_up=True) path = abspath(path) alias = scm except VCSError: raise VCSError("No scm found at %s" % path) if alias is None: alias = get_scm(path)[0] backend = get_backend(alias) repo = backend(path, create=create) return repo
def is_valid_repo(repo_name, base_path, scm=None): """ Returns True if given path is a valid repository False otherwise. If scm param is given also compare if given scm is the same as expected from scm parameter :param repo_name: :param base_path: :param scm: :return True: if given path is a valid repository """ full_path = os.path.join(safe_str(base_path), safe_str(repo_name)) try: scm_ = get_scm(full_path) if scm: return scm_[0] == scm return True except VCSError: return False
def __get_instance(self): repo_full_path = self.repo_full_path try: alias = get_scm(repo_full_path)[0] log.debug('Creating instance of %s repository' % alias) backend = get_backend(alias) except VCSError: log.error(traceback.format_exc()) log.error('Perhaps this repository is in db and not in ' 'filesystem run rescan repositories with ' '"destroy old data " option from admin panel') return if alias == 'hg': repo = backend(safe_str(repo_full_path), create=False, baseui=self._ui) else: repo = backend(repo_full_path, create=False) return repo
def get_current_revision(quiet=False): """ Returns tuple of (number, id) from repository containing this package or None if repository could not be found. :param quiet: prints error for fetching revision if True """ try: from kallithea.lib.vcs import get_repo from kallithea.lib.vcs.utils.helpers import get_scm repopath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) scm = get_scm(repopath)[0] repo = get_repo(path=repopath, alias=scm) wk_dir = repo.workdir cur_rev = wk_dir.get_changeset() return (cur_rev.revision, cur_rev.short_id) except Exception as err: if not quiet: print ("WARNING: Cannot retrieve kallithea's revision. " "disregard this if you don't know what that means. " "Original error was: %s" % err) return None
def test_get_scm(self): assert ('hg', TEST_HG_REPO) == get_scm(TEST_HG_REPO) assert ('git', TEST_GIT_REPO) == get_scm(TEST_GIT_REPO)
def test_get_scm_error_path(self): with pytest.raises(VCSError): get_scm('err')
def test_get_scm(self): self.assertEqual(('hg', TEST_HG_REPO), get_scm(TEST_HG_REPO)) self.assertEqual(('git', TEST_GIT_REPO), get_scm(TEST_GIT_REPO))