@NotNull
 private Set<VcsRef> readBranches(@NotNull GitRepository repository) {
   StopWatch sw = StopWatch.start("readBranches in " + repository.getRoot().getName());
   VirtualFile root = repository.getRoot();
   repository.update();
   Collection<GitLocalBranch> localBranches = repository.getBranches().getLocalBranches();
   Collection<GitRemoteBranch> remoteBranches = repository.getBranches().getRemoteBranches();
   Set<VcsRef> refs = new THashSet<VcsRef>(localBranches.size() + remoteBranches.size());
   for (GitLocalBranch localBranch : localBranches) {
     refs.add(
         myVcsObjectsFactory.createRef(
             localBranch.getHash(), localBranch.getName(), GitRefManager.LOCAL_BRANCH, root));
   }
   for (GitRemoteBranch remoteBranch : remoteBranches) {
     refs.add(
         myVcsObjectsFactory.createRef(
             remoteBranch.getHash(),
             remoteBranch.getNameForLocalOperations(),
             GitRefManager.REMOTE_BRANCH,
             root));
   }
   String currentRevision = repository.getCurrentRevision();
   if (currentRevision != null) { // null => fresh repository
     refs.add(
         myVcsObjectsFactory.createRef(
             HashImpl.build(currentRevision), "HEAD", GitRefManager.HEAD, root));
   }
   sw.report();
   return refs;
 }
 private static <T> void addNewElements(
     @NotNull Collection<T> original, @NotNull Collection<T> toAdd) {
   for (T item : toAdd) {
     if (!original.contains(item)) {
       original.add(item);
     }
   }
 }
 public static <T> void assertSameElements(
     String message, Collection<? extends T> collection, Collection<T> expected) {
   assertNotNull(collection);
   assertNotNull(expected);
   if (collection.size() != expected.size()
       || !new HashSet<T>(expected).equals(new HashSet<T>(collection))) {
     Assert.assertEquals(message, toString(expected, "\n"), toString(collection, "\n"));
     Assert.assertEquals(message, new HashSet<T>(expected), new HashSet<T>(collection));
   }
 }
 public static <T> void assertUnorderedCollection(
     Collection<? extends T> collection, Consumer<T>... checkers) {
   Assert.assertNotNull(collection);
   if (collection.size() != checkers.length) {
     Assert.fail(toString(collection));
   }
   Set<Consumer<T>> checkerSet = new HashSet<Consumer<T>>(Arrays.asList(checkers));
   int i = 0;
   Throwable lastError = null;
   for (final T actual : collection) {
     boolean flag = true;
     for (final Consumer<T> condition : checkerSet) {
       Throwable error = accepts(condition, actual);
       if (error == null) {
         checkerSet.remove(condition);
         flag = false;
         break;
       } else {
         lastError = error;
       }
     }
     if (flag) {
       lastError.printStackTrace();
       Assert.fail("Incorrect element(" + i + "): " + actual);
     }
     i++;
   }
 }
 static void importInjections(
     final Collection<BaseInjection> existingInjections,
     final Collection<BaseInjection> importingInjections,
     final Collection<BaseInjection> originalInjections,
     final Collection<BaseInjection> newInjections) {
   final MultiValuesMap<InjectionPlace, BaseInjection> placeMap =
       new MultiValuesMap<InjectionPlace, BaseInjection>();
   for (BaseInjection exising : existingInjections) {
     for (InjectionPlace place : exising.getInjectionPlaces()) {
       placeMap.put(place, exising);
     }
   }
   main:
   for (BaseInjection other : importingInjections) {
     final List<BaseInjection> matchingInjections =
         ContainerUtil.concat(
             other.getInjectionPlaces(),
             new Function<InjectionPlace, Collection<? extends BaseInjection>>() {
               public Collection<? extends BaseInjection> fun(final InjectionPlace o) {
                 final Collection<BaseInjection> collection = placeMap.get(o);
                 return collection == null ? Collections.<BaseInjection>emptyList() : collection;
               }
             });
     if (matchingInjections.isEmpty()) {
       newInjections.add(other);
     } else {
       BaseInjection existing = null;
       for (BaseInjection injection : matchingInjections) {
         if (injection.equals(other)) continue main;
         if (existing == null && injection.sameLanguageParameters(other)) {
           existing = injection;
         }
       }
       if (existing == null) continue main; // skip!! language changed
       final BaseInjection newInjection = existing.copy();
       newInjection.mergeOriginalPlacesFrom(other, true);
       if (!newInjection.equals(existing)) {
         originalInjections.add(existing);
         newInjections.add(newInjection);
       }
     }
   }
 }
 public static <T> void assertOrderedCollection(
     Collection<? extends T> collection, Consumer<T>... checkers) {
   Assert.assertNotNull(collection);
   if (collection.size() != checkers.length) {
     Assert.fail(toString(collection));
   }
   int i = 0;
   for (final T actual : collection) {
     try {
       checkers[i].consume(actual);
     } catch (AssertionFailedError e) {
       System.out.println(i + ": " + actual);
       throw e;
     }
     i++;
   }
 }
 public boolean setHostInjectionEnabled(
     final PsiLanguageInjectionHost host,
     final Collection<String> languages,
     final boolean enabled) {
   final ArrayList<BaseInjection> originalInjections = new ArrayList<BaseInjection>();
   final ArrayList<BaseInjection> newInjections = new ArrayList<BaseInjection>();
   for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) {
     for (BaseInjection injection : getInjections(support.getId())) {
       if (!languages.contains(injection.getInjectedLanguageId())) continue;
       boolean replace = false;
       final ArrayList<InjectionPlace> newPlaces = new ArrayList<InjectionPlace>();
       for (InjectionPlace place : injection.getInjectionPlaces()) {
         if (place.isEnabled() != enabled
             && place.getElementPattern() != null
             && (place.getElementPattern().accepts(host)
                 || place.getElementPattern().accepts(host.getParent()))) {
           newPlaces.add(place.enabled(enabled));
           replace = true;
         } else newPlaces.add(place);
       }
       if (replace) {
         originalInjections.add(injection);
         final BaseInjection newInjection = injection.copy();
         newInjection.setInjectionPlaces(newPlaces.toArray(new InjectionPlace[newPlaces.size()]));
         newInjections.add(newInjection);
       }
     }
   }
   if (!originalInjections.isEmpty()) {
     replaceInjectionsWithUndo(
         host.getProject(),
         newInjections,
         originalInjections,
         Collections.<PsiElement>emptyList());
     return true;
   }
   return false;
 }
 /** @param removeProcessor parent, child */
 public static <T> Collection<T> removeAncestors(
     final Collection<T> files,
     final Convertor<T, String> convertor,
     final PairProcessor<T, T> removeProcessor) {
   if (files.isEmpty()) return files;
   final TreeMap<String, T> paths = new TreeMap<String, T>();
   for (T file : files) {
     final String path = convertor.convert(file);
     assert path != null;
     final String canonicalPath = toCanonicalPath(path);
     paths.put(canonicalPath, file);
   }
   final List<Map.Entry<String, T>> ordered =
       new ArrayList<Map.Entry<String, T>>(paths.entrySet());
   final List<T> result = new ArrayList<T>(ordered.size());
   result.add(ordered.get(0).getValue());
   for (int i = 1; i < ordered.size(); i++) {
     final Map.Entry<String, T> entry = ordered.get(i);
     final String child = entry.getKey();
     boolean parentNotFound = true;
     for (int j = i - 1; j >= 0; j--) {
       // possible parents
       final String parent = ordered.get(j).getKey();
       if (parent == null) continue;
       if (startsWith(child, parent)
           && removeProcessor.process(ordered.get(j).getValue(), entry.getValue())) {
         parentNotFound = false;
         break;
       }
     }
     if (parentNotFound) {
       result.add(entry.getValue());
     }
   }
   return result;
 }
 @TestOnly
 public static void disallowRootAccess(@NotNull String... roots) {
   for (String root : roots) {
     ourAdditionalRoots.remove(FileUtil.toSystemIndependentName(root));
   }
 }
Esempio n. 10
0
 public static void assertSize(int expectedSize, final Collection<?> c) {
   assertEquals(toString(c), expectedSize, c.size());
 }
Esempio n. 11
0
 public static void assertEmpty(final Collection<?> collection) {
   assertEmpty(collection.toString(), collection);
 }
Esempio n. 12
0
 public static void assertNotEmpty(final Collection<?> collection) {
   if (collection == null) return;
   assertTrue(!collection.isEmpty());
 }
Esempio n. 13
0
 public static <T> T assertOneElement(Collection<T> collection) {
   Assert.assertNotNull(collection);
   Assert.assertEquals(toString(collection), 1, collection.size());
   return collection.iterator().next();
 }
  @NotNull
  @Override
  public List<TimedVcsCommit> getCommitsMatchingFilter(
      @NotNull final VirtualFile root,
      @NotNull VcsLogFilterCollection filterCollection,
      int maxCount)
      throws VcsException {
    if (!isRepositoryReady(root)) {
      return Collections.emptyList();
    }

    List<String> filterParameters = ContainerUtil.newArrayList();

    if (filterCollection.getBranchFilter() != null
        && !filterCollection.getBranchFilter().getBranchNames().isEmpty()) {
      GitRepository repository = getRepository(root);
      assert repository != null
          : "repository is null for root " + root + " but was previously reported as 'ready'";

      boolean atLeastOneBranchExists = false;
      for (String branchName : filterCollection.getBranchFilter().getBranchNames()) {
        if (branchName.equals("HEAD")
            || repository.getBranches().findBranchByName(branchName) != null) {
          filterParameters.add(branchName);
          atLeastOneBranchExists = true;
        }
      }
      if (!atLeastOneBranchExists) { // no such branches in this repository => filter matches
        // nothing
        return Collections.emptyList();
      }
    } else {
      filterParameters.addAll(GitHistoryUtils.LOG_ALL);
    }

    if (filterCollection.getUserFilter() != null) {
      String authorFilter =
          StringUtil.join(
              ContainerUtil.map(
                  filterCollection.getUserFilter().getUserNames(root), UserNameRegex.INSTANCE),
              "|");
      filterParameters.add(prepareParameter("author", StringUtil.escapeBackSlashes(authorFilter)));
      filterParameters.add(
          "--extended-regexp"); // extended regexp required for correctly filtering user names
    }

    if (filterCollection.getDateFilter() != null) {
      // assuming there is only one date filter, until filter expressions are defined
      VcsLogDateFilter filter = filterCollection.getDateFilter();
      if (filter.getAfter() != null) {
        filterParameters.add(prepareParameter("after", filter.getAfter().toString()));
      }
      if (filter.getBefore() != null) {
        filterParameters.add(prepareParameter("before", filter.getBefore().toString()));
      }
    }

    if (filterCollection.getTextFilter() != null) {
      String textFilter = filterCollection.getTextFilter().getText();
      filterParameters.add(prepareParameter("grep", StringUtil.escapeChars(textFilter, '[', ']')));
    }

    filterParameters.add(
        "--regexp-ignore-case"); // affects case sensitivity of any filter (except file filter)
    if (maxCount > 0) {
      filterParameters.add(prepareParameter("max-count", String.valueOf(maxCount)));
    }

    // note: structure filter must be the last parameter, because it uses "--" which separates
    // parameters from paths
    if (filterCollection.getStructureFilter() != null) {
      Collection<VirtualFile> files = filterCollection.getStructureFilter().getFiles();
      if (!files.isEmpty()) {
        filterParameters.add("--full-history");
        filterParameters.add("--simplify-merges");
        filterParameters.add("--");
        for (VirtualFile file : files) {
          filterParameters.add(file.getPath());
        }
      }
    }

    List<TimedVcsCommit> commits = ContainerUtil.newArrayList();
    GitHistoryUtils.readCommits(
        myProject,
        root,
        filterParameters,
        EmptyConsumer.<VcsUser>getInstance(),
        EmptyConsumer.<VcsRef>getInstance(),
        new CollectConsumer<TimedVcsCommit>(commits));
    return commits;
  }