/**
  * Sort changes by roots
  *
  * @param changes a change list
  * @param exceptions exceptions to collect
  * @return sorted changes
  */
 private static Map<VirtualFile, Collection<Change>> sortChangesByGitRoot(
     @NotNull List<Change> changes, List<VcsException> exceptions) {
   Map<VirtualFile, Collection<Change>> result = new HashMap<VirtualFile, Collection<Change>>();
   for (Change change : changes) {
     final ContentRevision afterRevision = change.getAfterRevision();
     final ContentRevision beforeRevision = change.getBeforeRevision();
     // nothing-to-nothing change cannot happen.
     assert beforeRevision != null || afterRevision != null;
     // note that any path will work, because changes could happen within single vcs root
     final FilePath filePath =
         afterRevision != null ? afterRevision.getFile() : beforeRevision.getFile();
     final VirtualFile vcsRoot;
     try {
       // the parent paths for calculating roots in order to account for submodules that contribute
       // to the parent change. The path "." is never is valid change, so there should be no
       // problem
       // with it.
       vcsRoot = GitUtil.getGitRoot(filePath.getParentPath());
     } catch (VcsException e) {
       exceptions.add(e);
       continue;
     }
     Collection<Change> changeList = result.get(vcsRoot);
     if (changeList == null) {
       changeList = new ArrayList<Change>();
       result.put(vcsRoot, changeList);
     }
     changeList.add(change);
   }
   return result;
 }
  @NotNull
  private GitFileRevision createParentRevision(
      @NotNull GitRepository repository,
      @NotNull GitFileRevision currentRevision,
      @NotNull String parentHash)
      throws VcsException {
    FilePath currentRevisionPath = currentRevision.getPath();
    if (currentRevisionPath.isDirectory()) {
      // for directories the history doesn't follow renames
      return makeRevisionFromHash(currentRevisionPath, parentHash);
    }

    // can't limit by the path: in that case rename information will be missed
    Collection<Change> changes =
        GitChangeUtils.getDiff(
            myProject, repository.getRoot(), parentHash, currentRevision.getHash(), null);
    for (Change change : changes) {
      ContentRevision afterRevision = change.getAfterRevision();
      ContentRevision beforeRevision = change.getBeforeRevision();
      if (afterRevision != null && afterRevision.getFile().equals(currentRevisionPath)) {
        // if the file was renamed, taking the path how it was in the parent; otherwise the path
        // didn't change
        FilePath path =
            (beforeRevision != null ? beforeRevision.getFile() : afterRevision.getFile());
        return new GitFileRevision(myProject, path, new GitRevisionNumber(parentHash), true);
      }
    }
    LOG.error(
        String.format(
            "Could not find parent revision. Will use the path from parent revision. Current revision: %s, parent hash: %s",
            currentRevision, parentHash));
    return makeRevisionFromHash(currentRevisionPath, parentHash);
  }
  @NotNull
  public AnalysisScope getScope(
      @NotNull AnalysisUIOptions uiOptions,
      @NotNull AnalysisScope defaultScope,
      @NotNull Project project,
      Module module) {
    AnalysisScope scope;
    if (isProjectScopeSelected()) {
      scope = new AnalysisScope(project);
      uiOptions.SCOPE_TYPE = AnalysisScope.PROJECT;
    } else {
      final SearchScope customScope = getCustomScope();
      if (customScope != null) {
        scope = new AnalysisScope(customScope, project);
        uiOptions.SCOPE_TYPE = AnalysisScope.CUSTOM;
        uiOptions.CUSTOM_SCOPE_NAME = customScope.getDisplayName();
      } else if (isModuleScopeSelected()) {
        scope = new AnalysisScope(module);
        uiOptions.SCOPE_TYPE = AnalysisScope.MODULE;
      } else if (isUncommitedFilesSelected()) {
        final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
        List<VirtualFile> files;
        if (myChangeLists.getSelectedItem() == ALL) {
          files = changeListManager.getAffectedFiles();
        } else {
          files = new ArrayList<VirtualFile>();
          for (ChangeList list : changeListManager.getChangeListsCopy()) {
            if (!Comparing.strEqual(list.getName(), (String) myChangeLists.getSelectedItem()))
              continue;
            final Collection<Change> changes = list.getChanges();
            for (Change change : changes) {
              final ContentRevision afterRevision = change.getAfterRevision();
              if (afterRevision != null) {
                final VirtualFile vFile = afterRevision.getFile().getVirtualFile();
                if (vFile != null) {
                  files.add(vFile);
                }
              }
            }
          }
        }
        scope = new AnalysisScope(project, new HashSet<VirtualFile>(files));
        uiOptions.SCOPE_TYPE = AnalysisScope.UNCOMMITTED_FILES;
      } else {
        scope = defaultScope;
        uiOptions.SCOPE_TYPE = defaultScope.getScopeType(); // just not project scope
      }
    }
    uiOptions.ANALYZE_TEST_SOURCES = isInspectTestSources();
    scope.setIncludeTestSource(isInspectTestSources());
    scope.setScope(getCustomScope());

    FindSettings.getInstance().setDefaultScopeName(scope.getDisplayName());
    return scope;
  }
Example #4
0
 /**
  * @param change "Change" description.
  * @return Return true if the "Change" object is created for "Rename" operation: in this case name
  *     of files for "before" and "after" revisions must not coniside.
  */
 public static boolean isRenameChange(Change change) {
   boolean isRenamed = false;
   ContentRevision before = change.getBeforeRevision();
   ContentRevision after = change.getAfterRevision();
   if (before != null && after != null) {
     String prevFile = getCanonicalLocalPath(before.getFile().getPath());
     String newFile = getCanonicalLocalPath(after.getFile().getPath());
     isRenamed = !prevFile.equals(newFile);
   }
   return isRenamed;
 }
 public boolean isConflictingChange(final Project project) {
   ContentRevision afterRevision = getChange(project).getAfterRevision();
   if (afterRevision == null) return false;
   try {
     afterRevision.getContent();
   } catch (VcsException e) {
     if (e.getCause() instanceof ApplyPatchException) {
       return true;
     }
   }
   return false;
 }
 private File[] getSelectedIoFiles() {
   final List<Change> changes = getSelectedChanges();
   final List<File> files = new ArrayList<>();
   for (Change change : changes) {
     final ContentRevision afterRevision = change.getAfterRevision();
     if (afterRevision != null) {
       final FilePath file = afterRevision.getFile();
       final File ioFile = file.getIOFile();
       files.add(ioFile);
     }
   }
   return files.toArray(new File[files.size()]);
 }
 private void preloadRevisionContents(ContentRevision cr) throws VcsException {
   if (cr instanceof BinaryContentRevision) {
     ((BinaryContentRevision) cr).getBinaryContent();
   } else {
     cr.getContent();
   }
 }
 private boolean checkIfThereAreFakeRevisions(final Project project, final Change[] changes) {
   boolean needsConvertion = false;
   for (Change change : changes) {
     final ContentRevision beforeRevision = change.getBeforeRevision();
     final ContentRevision afterRevision = change.getAfterRevision();
     if (beforeRevision instanceof FakeRevision) {
       VcsDirtyScopeManager.getInstance(project).fileDirty(beforeRevision.getFile());
       needsConvertion = true;
     }
     if (afterRevision instanceof FakeRevision) {
       VcsDirtyScopeManager.getInstance(project).fileDirty(afterRevision.getFile());
       needsConvertion = true;
     }
   }
   return needsConvertion;
 }
  @Override
  public void actionPerformed(@NotNull AnActionEvent event) {
    final Project project = event.getProject();
    if (project == null) {
      return;
    }

    final Set<VirtualFile> conflictedFiles =
        new TreeSet<VirtualFile>(
            new Comparator<VirtualFile>() {
              @Override
              public int compare(@NotNull VirtualFile f1, @NotNull VirtualFile f2) {
                return f1.getPresentableUrl().compareTo(f2.getPresentableUrl());
              }
            });
    for (Change change : ChangeListManager.getInstance(project).getAllChanges()) {
      if (change.getFileStatus() != FileStatus.MERGED_WITH_CONFLICTS) {
        continue;
      }
      final ContentRevision before = change.getBeforeRevision();
      final ContentRevision after = change.getAfterRevision();
      if (before != null) {
        final VirtualFile file = before.getFile().getVirtualFile();
        if (file != null) {
          conflictedFiles.add(file);
        }
      }
      if (after != null) {
        final VirtualFile file = after.getFile().getVirtualFile();
        if (file != null) {
          conflictedFiles.add(file);
        }
      }
    }

    AbstractVcsHelper.getInstance(project)
        .showMergeDialog(
            new ArrayList<VirtualFile>(conflictedFiles),
            GitVcs.getInstance(project).getMergeProvider());
    for (VirtualFile conflictedFile : conflictedFiles) {
      final GitRepository repo =
          GitRepositoryManager.getInstance(project).getRepositoryForFile(conflictedFile);
      if (repo != null) {
        repo.update(GitRepository.TrackedTopic.ALL_CURRENT);
      }
    }
  }
  protected VirtualFile[] getSelectedFiles() {
    final Change[] changes = getSelectedChanges();
    Collection<VirtualFile> files = new HashSet<VirtualFile>();
    for (Change change : changes) {
      final ContentRevision afterRevision = change.getAfterRevision();
      if (afterRevision != null) {
        final VirtualFile file = afterRevision.getFile().getVirtualFile();
        if (file != null && file.isValid()) {
          files.add(file);
        }
      }
    }

    files.addAll(getSelectedVirtualFiles(null));

    return VfsUtilCore.toVirtualFileArray(files);
  }
  private boolean enabled(final Project project, final Change[] changes) {
    final boolean noChange = (project == null) || (changes == null) || (changes.length != 1);
    if (noChange) {
      return false;
    } else {
      final Change change = changes[0];

      final ContentRevision revision =
          (change.getBeforeRevision() != null)
              ? change.getBeforeRevision()
              : change.getAfterRevision();
      if ((revision == null) || (!(revision.getRevisionNumber() instanceof SvnRevisionNumber))) {
        return false;
      }

      return checkVcs(project, change);
    }
  }
  private ShelvedBinaryFile shelveBinaryFile(final Change change) throws IOException {
    final ContentRevision beforeRevision = change.getBeforeRevision();
    final ContentRevision afterRevision = change.getAfterRevision();
    File beforeFile = beforeRevision == null ? null : beforeRevision.getFile().getIOFile();
    File afterFile = afterRevision == null ? null : afterRevision.getFile().getIOFile();
    String shelvedPath = null;
    if (afterFile != null) {
      String shelvedName = FileUtil.getNameWithoutExtension(afterFile.getName());
      String shelvedExt = FileUtil.getExtension(afterFile.getName());
      File shelvedFile =
          FileUtil.findSequentNonexistentFile(
              myFileProcessor.getBaseIODir(), shelvedName, shelvedExt);

      myFileProcessor.saveFile(afterRevision.getFile().getIOFile(), shelvedFile);

      shelvedPath = shelvedFile.getPath();
    }
    String beforePath = ChangesUtil.getProjectRelativePath(myProject, beforeFile);
    String afterPath = ChangesUtil.getProjectRelativePath(myProject, afterFile);
    return new ShelvedBinaryFile(beforePath, afterPath, shelvedPath);
  }
 private void deleteAddedFilesLocally(final List<Change> changes) {
   if (myIndicator != null) {
     myIndicator.setText("Deleting added files locally...");
     myIndicator.setFraction(0);
   }
   final int changesSize = changes.size();
   for (int i = 0; i < changesSize; i++) {
     final Change c = changes.get(i);
     if (c.getType() == Change.Type.NEW) {
       ContentRevision rev = c.getAfterRevision();
       assert rev != null;
       final File ioFile = rev.getFile().getIOFile();
       if (myIndicator != null) {
         myIndicator.setText2(ioFile.getAbsolutePath());
         myIndicator.setFraction(((double) i) / changesSize);
       }
       FileUtil.delete(ioFile);
     }
   }
   if (myIndicator != null) {
     myIndicator.setText2("");
   }
 }
Example #14
0
 private static void correctListForRevision(
     final ProjectLevelVcsManager plVcsManager,
     final ContentRevision revision,
     final SVNChangelistClient client,
     final String name) {
   if (revision != null) {
     final FilePath path = revision.getFile();
     final AbstractVcs vcs = plVcsManager.getVcsFor(path);
     if (vcs != null && VCS_NAME.equals(vcs.getName())) {
       try {
         client.doAddToChangelist(new File[] {path.getIOFile()}, SVNDepth.EMPTY, name, null);
       } catch (SVNException e) {
         // left in default list
       }
     }
   }
 }
 private static FileContent createBinaryFileContent(
     final Project project, final ContentRevision contentRevision, final String fileName)
     throws VcsException, IOException {
   final FileContent fileContent;
   if (contentRevision == null) {
     fileContent =
         FileContent.createFromTempFile(project, fileName, fileName, ArrayUtil.EMPTY_BYTE_ARRAY);
   } else {
     byte[] content = ((BinaryContentRevision) contentRevision).getBinaryContent();
     fileContent =
         FileContent.createFromTempFile(
             project,
             contentRevision.getFile().getName(),
             contentRevision.getFile().getName(),
             content == null ? ArrayUtil.EMPTY_BYTE_ARRAY : content);
   }
   return fileContent;
 }
  private static String getPropertyList(
      final ContentRevision contentRevision, final SVNRevision revision, final SVNWCClient client)
      throws SVNException {
    if (contentRevision == null) {
      return "";
    }

    final StringBuilder sb = new StringBuilder();
    final List<SVNPropertyData> lines = new ArrayList<SVNPropertyData>();

    final File ioFile = contentRevision.getFile().getIOFile();
    final ISVNPropertyHandler propertyHandler = createHandler(revision, lines);

    if (contentRevision instanceof SvnRepositoryContentRevision) {
      final SvnRepositoryContentRevision svnRevision =
          (SvnRepositoryContentRevision) contentRevision;
      client.doGetProperty(
          SVNURL.parseURIEncoded(svnRevision.getFullPath()),
          null,
          revision,
          revision,
          SVNDepth.EMPTY,
          propertyHandler);
    } else {
      client.doGetProperty(ioFile, null, revision, revision, SVNDepth.EMPTY, propertyHandler, null);
    }

    Collections.sort(
        lines,
        new Comparator<SVNPropertyData>() {
          public int compare(final SVNPropertyData o1, final SVNPropertyData o2) {
            return o1.getName().compareTo(o2.getName());
          }
        });
    for (SVNPropertyData line : lines) {
      addPropertyPresentation(line, sb);
    }
    return sb.toString();
  }
Example #17
0
 public static boolean isChangeForFolder(Change change) {
   ContentRevision revB = change.getBeforeRevision();
   ContentRevision revA = change.getAfterRevision();
   return (revA != null && revA.getFile().isDirectory())
       || (revB != null && revB.getFile().isDirectory());
 }
Example #18
0
  public void reportAppendableHistory(
      FilePath path,
      final VcsAppendableHistorySessionPartner partner,
      @Nullable final SVNRevision from,
      @Nullable final SVNRevision to,
      final int limit,
      SVNRevision peg,
      final boolean forceBackwards)
      throws VcsException {
    FilePath committedPath = path;
    Change change = ChangeListManager.getInstance(myVcs.getProject()).getChange(path);
    if (change != null) {
      final ContentRevision beforeRevision = change.getBeforeRevision();
      final ContentRevision afterRevision = change.getAfterRevision();
      if (beforeRevision != null
          && afterRevision != null
          && !beforeRevision.getFile().equals(afterRevision.getFile())
          && afterRevision.getFile().equals(path)) {
        committedPath = beforeRevision.getFile();
      }
      // revision can be VcsRevisionNumber.NULL
      if (peg == null
          && change.getBeforeRevision() != null
          && change.getBeforeRevision().getRevisionNumber() instanceof SvnRevisionNumber) {
        peg = ((SvnRevisionNumber) change.getBeforeRevision().getRevisionNumber()).getRevision();
      }
    }

    final boolean showMergeSources =
        SvnConfiguration.getInstance(myVcs.getProject()).isShowMergeSourcesInAnnotate();
    final LogLoader logLoader;
    if (path.isNonLocal()) {
      logLoader =
          new RepositoryLoader(
              myVcs, committedPath, from, to, limit, peg, forceBackwards, showMergeSources);
    } else {
      logLoader = new LocalLoader(myVcs, committedPath, from, to, limit, peg, showMergeSources);
    }

    try {
      logLoader.preliminary();
    } catch (SVNCancelException e) {
      throw new VcsException(e);
    } catch (SVNException e) {
      throw new VcsException(e);
    }
    logLoader.check();
    if (showMergeSources) {
      logLoader.initSupports15();
    }

    final SvnHistorySession historySession =
        new SvnHistorySession(
            myVcs,
            Collections.<VcsFileRevision>emptyList(),
            committedPath,
            showMergeSources && Boolean.TRUE.equals(logLoader.mySupport15),
            null,
            false,
            !path.isNonLocal());

    final Ref<Boolean> sessionReported = new Ref<Boolean>();
    final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    if (indicator != null) {
      indicator.setText(SvnBundle.message("progress.text2.collecting.history", path.getName()));
    }
    final Consumer<VcsFileRevision> consumer =
        new Consumer<VcsFileRevision>() {
          @Override
          public void consume(VcsFileRevision vcsFileRevision) {
            if (!Boolean.TRUE.equals(sessionReported.get())) {
              partner.reportCreatedEmptySession(historySession);
              sessionReported.set(true);
            }
            partner.acceptRevision(vcsFileRevision);
          }
        };

    logLoader.setConsumer(consumer);
    logLoader.load();
    logLoader.check();
  }
  @Override
  public Pair<SvnChangeList, FilePath> getOneList(final VirtualFile file, VcsRevisionNumber number)
      throws VcsException {
    final RootUrlInfo rootUrlInfo =
        myVcs.getSvnFileUrlMapping().getWcRootForFilePath(new File(file.getPath()));
    if (rootUrlInfo == null) return null;
    final VirtualFile root = rootUrlInfo.getVirtualFile();
    if (root == null) return null;
    final SvnRepositoryLocation svnRootLocation =
        (SvnRepositoryLocation) getLocationFor(new FilePathImpl(root));
    if (svnRootLocation == null) return null;
    final String url = svnRootLocation.getURL();
    final long revision;
    try {
      revision = Long.parseLong(number.asString());
    } catch (NumberFormatException e) {
      throw new VcsException(e);
    }

    final SvnChangeList[] result = new SvnChangeList[1];
    final SVNLogClient logger;
    final SVNRevision revisionBefore;
    final SVNURL repositoryUrl;
    final SVNURL svnurl;
    final SVNInfo targetInfo;
    try {
      logger = myVcs.createLogClient();
      revisionBefore = SVNRevision.create(revision);

      svnurl = SVNURL.parseURIEncoded(url);
      final SVNWCClient client = myVcs.createWCClient();
      final SVNInfo info = client.doInfo(svnurl, SVNRevision.UNDEFINED, SVNRevision.HEAD);
      targetInfo = client.doInfo(new File(file.getPath()), SVNRevision.UNDEFINED);
      if (info == null) {
        throw new VcsException("Can not get repository URL");
      }
      repositoryUrl = info.getRepositoryRootURL();
    } catch (SVNException e) {
      LOG.info(e);
      throw new VcsException(e);
    }

    tryExactHit(svnRootLocation, result, logger, revisionBefore, repositoryUrl, svnurl);
    if (result[0] == null) {
      tryByRoot(result, logger, revisionBefore, repositoryUrl);
      if (result[0] == null) {
        FilePath path =
            tryStepByStep(svnRootLocation, result, logger, revisionBefore, targetInfo, svnurl);
        path = path == null ? new FilePathImpl(file) : path;
        // and pass & take rename context there
        return new Pair<SvnChangeList, FilePath>(result[0], path);
      }
    }
    if (result[0].getChanges().size() == 1) {
      final Collection<Change> changes = result[0].getChanges();
      final Change change = changes.iterator().next();
      final ContentRevision afterRevision = change.getAfterRevision();
      if (afterRevision != null) {
        return new Pair<SvnChangeList, FilePath>(result[0], afterRevision.getFile());
      } else {
        return new Pair<SvnChangeList, FilePath>(result[0], new FilePathImpl(file));
      }
    }
    String relativePath =
        SVNPathUtil.getRelativePath(
            targetInfo.getRepositoryRootURL().toString(), targetInfo.getURL().toString());
    relativePath = relativePath.startsWith("/") ? relativePath : "/" + relativePath;
    final Change targetChange = result[0].getByPath(relativePath);
    if (targetChange == null) {
      FilePath path =
          tryStepByStep(svnRootLocation, result, logger, revisionBefore, targetInfo, svnurl);
      path = path == null ? new FilePathImpl(file) : path;
      // and pass & take rename context there
      return new Pair<SvnChangeList, FilePath>(result[0], path);
    }
    return new Pair<SvnChangeList, FilePath>(result[0], new FilePathImpl(file));
  }