Exemplo n.º 1
0
  protected SubmoduleModel getSubmodule(
      Map<String, SubmoduleModel> submodules, String repositoryName, String path) {
    SubmoduleModel model = submodules.get(path);
    if (model == null) {
      // undefined submodule?!
      model = new SubmoduleModel(path.substring(path.lastIndexOf('/') + 1), path, path);
      model.hasSubmodule = false;
      model.gitblitPath = model.name;
      return model;
    } else {
      // extract the repository name from the clone url
      List<String> patterns = GitBlit.getStrings(Keys.git.submoduleUrlPatterns);
      String submoduleName =
          StringUtils.extractRepositoryPath(model.url, patterns.toArray(new String[0]));

      // determine the current path for constructing paths relative
      // to the current repository
      String currentPath = "";
      if (repositoryName.indexOf('/') > -1) {
        currentPath = repositoryName.substring(0, repositoryName.lastIndexOf('/') + 1);
      }

      // try to locate the submodule repository
      // prefer bare to non-bare names
      List<String> candidates = new ArrayList<String>();

      // relative
      candidates.add(currentPath + StringUtils.stripDotGit(submoduleName));
      candidates.add(candidates.get(candidates.size() - 1) + ".git");

      // relative, no subfolder
      if (submoduleName.lastIndexOf('/') > -1) {
        String name = submoduleName.substring(submoduleName.lastIndexOf('/') + 1);
        candidates.add(currentPath + StringUtils.stripDotGit(name));
        candidates.add(currentPath + candidates.get(candidates.size() - 1) + ".git");
      }

      // absolute
      candidates.add(StringUtils.stripDotGit(submoduleName));
      candidates.add(candidates.get(candidates.size() - 1) + ".git");

      // absolute, no subfolder
      if (submoduleName.lastIndexOf('/') > -1) {
        String name = submoduleName.substring(submoduleName.lastIndexOf('/') + 1);
        candidates.add(StringUtils.stripDotGit(name));
        candidates.add(candidates.get(candidates.size() - 1) + ".git");
      }

      // create a unique, ordered set of candidate paths
      Set<String> paths = new LinkedHashSet<String>(candidates);
      for (String candidate : paths) {
        if (GitBlit.self().hasRepository(candidate)) {
          model.hasSubmodule = true;
          model.gitblitPath = candidate;
          return model;
        }
      }

      // we do not have a copy of the submodule, but we need a path
      model.gitblitPath = candidates.get(0);
      return model;
    }
  }