| | |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | |
| | | import com.google.inject.Inject; |
| | | |
| | | import org.eclipse.jgit.lib.Repository; |
| | | import org.eclipse.jgit.lib.RepositoryCache; |
| | | import org.eclipse.jgit.lib.RepositoryCache.FileKey; |
| | |
| | | |
| | | private MirrorService mirrorExecutor; |
| | | |
| | | @Inject |
| | | public RepositoryManager( |
| | | IRuntimeManager runtimeManager, |
| | | IPluginManager pluginManager, |
| | |
| | | @Override |
| | | public void addToCachedRepositoryList(RepositoryModel model) { |
| | | if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) { |
| | | repositoryListCache.put(model.name.toLowerCase(), model); |
| | | String key = getRepositoryKey(model.name); |
| | | repositoryListCache.put(key, model); |
| | | |
| | | // update the fork origin repository with this repository clone |
| | | if (!StringUtils.isEmpty(model.originRepository)) { |
| | | String originKey = model.originRepository.toLowerCase(); |
| | | String originKey = getRepositoryKey(model.originRepository); |
| | | if (repositoryListCache.containsKey(originKey)) { |
| | | RepositoryModel origin = repositoryListCache.get(originKey); |
| | | origin.addFork(model.name); |
| | |
| | | if (StringUtils.isEmpty(name)) { |
| | | return null; |
| | | } |
| | | return repositoryListCache.remove(name.toLowerCase()); |
| | | String key = getRepositoryKey(name); |
| | | return repositoryListCache.remove(key); |
| | | } |
| | | |
| | | /** |
| | |
| | | // rebuild fork networks |
| | | for (RepositoryModel model : repositoryListCache.values()) { |
| | | if (!StringUtils.isEmpty(model.originRepository)) { |
| | | String originKey = model.originRepository.toLowerCase(); |
| | | String originKey = getRepositoryKey(model.originRepository); |
| | | if (repositoryListCache.containsKey(originKey)) { |
| | | RepositoryModel origin = repositoryListCache.get(originKey); |
| | | origin.addFork(model.name); |
| | |
| | | /** |
| | | * Returns the JGit repository for the specified name. |
| | | * |
| | | * @param repositoryName |
| | | * @param name |
| | | * @param logError |
| | | * @return repository or null |
| | | */ |
| | | @Override |
| | | public Repository getRepository(String repositoryName, boolean logError) { |
| | | // Decode url-encoded repository name (issue-278) |
| | | // http://stackoverflow.com/questions/17183110 |
| | | repositoryName = repositoryName.replace("%7E", "~").replace("%7e", "~"); |
| | | public Repository getRepository(String name, boolean logError) { |
| | | String repositoryName = fixRepositoryName(name); |
| | | |
| | | if (isCollectingGarbage(repositoryName)) { |
| | | logger.warn(MessageFormat.format("Rejecting request for {0}, busy collecting garbage!", repositoryName)); |
| | |
| | | * Returns the repository model for the specified repository. This method |
| | | * does not consider user access permissions. |
| | | * |
| | | * @param repositoryName |
| | | * @param name |
| | | * @return repository model or null |
| | | */ |
| | | @Override |
| | | public RepositoryModel getRepositoryModel(String repositoryName) { |
| | | // Decode url-encoded repository name (issue-278) |
| | | // http://stackoverflow.com/questions/17183110 |
| | | repositoryName = repositoryName.replace("%7E", "~").replace("%7e", "~"); |
| | | public RepositoryModel getRepositoryModel(String name) { |
| | | String repositoryName = fixRepositoryName(name); |
| | | |
| | | String repositoryKey = repositoryName.toLowerCase(); |
| | | String repositoryKey = getRepositoryKey(repositoryName); |
| | | if (!repositoryListCache.containsKey(repositoryKey)) { |
| | | RepositoryModel model = loadRepositoryModel(repositoryName); |
| | | if (model == null) { |
| | |
| | | // cached model |
| | | RepositoryModel model = repositoryListCache.get(repositoryKey); |
| | | |
| | | if (gcExecutor.isCollectingGarbage(model.name)) { |
| | | if (isCollectingGarbage(model.name)) { |
| | | // Gitblit is busy collecting garbage, use our cached model |
| | | RepositoryModel rm = DeepCopier.copy(model); |
| | | rm.isCollectingGarbage = true; |
| | |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | | * Replaces illegal character patterns in a repository name. |
| | | * |
| | | * @param repositoryName |
| | | * @return a corrected name |
| | | */ |
| | | private String fixRepositoryName(String repositoryName) { |
| | | if (StringUtils.isEmpty(repositoryName)) { |
| | | return repositoryName; |
| | | } |
| | | |
| | | // Decode url-encoded repository name (issue-278) |
| | | // http://stackoverflow.com/questions/17183110 |
| | | String name = repositoryName.replace("%7E", "~").replace("%7e", "~"); |
| | | name = name.replace("%2F", "/").replace("%2f", "/"); |
| | | |
| | | if (name.charAt(name.length() - 1) == '/') { |
| | | name = name.substring(0, name.length() - 1); |
| | | } |
| | | |
| | | // strip duplicate-slashes from requests for repositoryName (ticket-117, issue-454) |
| | | // specify first char as slash so we strip leading slashes |
| | | char lastChar = '/'; |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (char c : name.toCharArray()) { |
| | | if (c == '/' && lastChar == c) { |
| | | continue; |
| | | } |
| | | sb.append(c); |
| | | lastChar = c; |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * Returns the cache key for the repository name. |
| | | * |
| | | * @param repositoryName |
| | | * @return the cache key for the repository |
| | | */ |
| | | private String getRepositoryKey(String repositoryName) { |
| | | String name = fixRepositoryName(repositoryName); |
| | | return StringUtils.stripDotGit(name).toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (!caseSensitiveCheck && settings.getBoolean(Keys.git.cacheRepositoryList, true)) { |
| | | // if we are caching use the cache to determine availability |
| | | // otherwise we end up adding a phantom repository to the cache |
| | | return repositoryListCache.containsKey(repositoryName.toLowerCase()); |
| | | String key = getRepositoryKey(repositoryName); |
| | | return repositoryListCache.containsKey(key); |
| | | } |
| | | Repository r = getRepository(repositoryName, false); |
| | | if (r == null) { |
| | |
| | | } |
| | | String userProject = ModelUtils.getPersonalPath(username); |
| | | if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) { |
| | | String originKey = origin.toLowerCase(); |
| | | String originKey = getRepositoryKey(origin); |
| | | String userPath = userProject + "/"; |
| | | |
| | | // collect all origin nodes in fork network |
| | |
| | | } |
| | | |
| | | if (originModel.originRepository != null) { |
| | | String ooKey = originModel.originRepository.toLowerCase(); |
| | | String ooKey = getRepositoryKey(originModel.originRepository); |
| | | roots.add(ooKey); |
| | | originModel = repositoryListCache.get(ooKey); |
| | | } else { |
| | |
| | | if (repository.startsWith(userPath)) { |
| | | RepositoryModel model = repositoryListCache.get(repository); |
| | | if (!StringUtils.isEmpty(model.originRepository)) { |
| | | if (roots.contains(model.originRepository.toLowerCase())) { |
| | | String ooKey = getRepositoryKey(model.originRepository); |
| | | if (roots.contains(ooKey)) { |
| | | // user has a fork in this graph |
| | | return model.name; |
| | | } |
| | |
| | | public ForkModel getForkNetwork(String repository) { |
| | | if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) { |
| | | // find the root, cached |
| | | RepositoryModel model = repositoryListCache.get(repository.toLowerCase()); |
| | | String key = getRepositoryKey(repository); |
| | | RepositoryModel model = repositoryListCache.get(key); |
| | | while (model.originRepository != null) { |
| | | model = repositoryListCache.get(model.originRepository.toLowerCase()); |
| | | String originKey = getRepositoryKey(model.originRepository); |
| | | model = repositoryListCache.get(originKey); |
| | | } |
| | | ForkModel root = getForkModelFromCache(model.name); |
| | | return root; |
| | |
| | | } |
| | | |
| | | private ForkModel getForkModelFromCache(String repository) { |
| | | RepositoryModel model = repositoryListCache.get(repository.toLowerCase()); |
| | | String key = getRepositoryKey(repository); |
| | | RepositoryModel model = repositoryListCache.get(key); |
| | | if (model == null) { |
| | | return null; |
| | | } |
| | |
| | | @Override |
| | | public void updateRepositoryModel(String repositoryName, RepositoryModel repository, |
| | | boolean isCreate) throws GitBlitException { |
| | | if (gcExecutor.isCollectingGarbage(repositoryName)) { |
| | | if (isCollectingGarbage(repositoryName)) { |
| | | throw new GitBlitException(MessageFormat.format("sorry, Gitblit is busy collecting garbage in {0}", |
| | | repositoryName)); |
| | | } |
| | |
| | | |
| | | // update this repository's origin's fork list |
| | | if (!StringUtils.isEmpty(repository.originRepository)) { |
| | | RepositoryModel origin = repositoryListCache.get(repository.originRepository.toLowerCase()); |
| | | String originKey = getRepositoryKey(repository.originRepository); |
| | | RepositoryModel origin = repositoryListCache.get(originKey); |
| | | if (origin != null && !ArrayUtils.isEmpty(origin.forks)) { |
| | | origin.forks.remove(repositoryName); |
| | | origin.forks.add(repository.name); |
| | |
| | | } |
| | | |
| | | protected void confirmWriteAccess() { |
| | | if (runtimeManager.isServingRepositories()) { |
| | | try { |
| | | if (!getRepositoriesFolder().exists()) { |
| | | getRepositoriesFolder().mkdirs(); |
| | | } |
| | | File file = File.createTempFile(".test-", ".txt", getRepositoriesFolder()); |
| | | file.delete(); |
| | | } catch (Exception e) { |
| | | logger.error(""); |
| | | logger.error(Constants.BORDER2); |
| | | logger.error("Please check filesystem permissions!"); |
| | | logger.error("FAILED TO WRITE TO REPOSITORIES FOLDER!!", e); |
| | | logger.error(Constants.BORDER2); |
| | | logger.error(""); |
| | | try { |
| | | if (!getRepositoriesFolder().exists()) { |
| | | getRepositoriesFolder().mkdirs(); |
| | | } |
| | | File file = File.createTempFile(".test-", ".txt", getRepositoriesFolder()); |
| | | file.delete(); |
| | | } catch (Exception e) { |
| | | logger.error(""); |
| | | logger.error(Constants.BORDER2); |
| | | logger.error("Please check filesystem permissions!"); |
| | | logger.error("FAILED TO WRITE TO REPOSITORIES FOLDER!!", e); |
| | | logger.error(Constants.BORDER2); |
| | | logger.error(""); |
| | | } |
| | | } |
| | | } |