Skip to content

Commit

Permalink
Relative path resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernard Labno committed Jun 25, 2012
1 parent ec76f0d commit 06a1f8a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 37 deletions.
14 changes: 1 addition & 13 deletions src/pl/com/it_crowd/cra/model/AssistantHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ContentRevision;
import org.jetbrains.idea.svn.RootUrlInfo;
import org.jetbrains.idea.svn.SvnVcs;

import java.io.File;

Expand All @@ -19,17 +17,7 @@ public static boolean affectsFile(Change change, File ioFile)
|| afterRevision != null && ioFileAbsolutePath.endsWith(afterRevision.getFile().getPath());
}

public static String getRelativePath(File file, SvnVcs vcs)
{
final RootUrlInfo wcRootForFilePath = vcs.getSvnFileUrlMapping().getWcRootForFilePath(file);
if (wcRootForFilePath == null) {
throw new IllegalArgumentException("Cannot get working copy root for file: " + file.getAbsolutePath());
}
final int length = wcRootForFilePath.getIoFile().getAbsolutePath().length();
return file.getAbsolutePath().substring(length);
}

// --------------------------- CONSTRUCTORS ---------------------------
// --------------------------- CONSTRUCTORS ---------------------------

private AssistantHelper()
{
Expand Down
100 changes: 85 additions & 15 deletions src/pl/com/it_crowd/cra/model/CodeReviewAssistant.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package pl.com.it_crowd.cra.model;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.RootsToWorkingCopies;
import org.jetbrains.idea.svn.SvnVcs;
import org.jetbrains.idea.svn.WorkingCopy;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.ISVNDiffStatusHandler;
import org.tmatesoft.svn.core.wc.SVNDiffStatus;
Expand All @@ -32,6 +39,8 @@ public class CodeReviewAssistant implements ProjectComponent {

public static final String REVISION_RANGE_PROPERTY = "revisionRange";

public static final String STATE_PROPERTY = "state";

private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

private final List<File> changedFiles = new ArrayList<File>();
Expand All @@ -48,15 +57,25 @@ public class CodeReviewAssistant implements ProjectComponent {

private final List<File> unmodifiableChangedFilesView;

private State state = State.CLEAR;

public static enum State {
CLEAR,
LOADING,
LOADED
}

// -------------------------- STATIC METHODS --------------------------

private static Change createChange(SvnVcs vcs, File file, int startRevision, int endRevision)
{
Change change;
final String relativePath = AssistantHelper.getRelativePath(file, vcs).substring(1);
final SVNURL repositoryRootURL = vcs.getInfo(file).getRepositoryRootURL();
final SVNURL urlForFile = vcs.getSvnFileUrlMapping().getUrlForFile(file);
final String relativePath = urlForFile.getPath().substring(repositoryRootURL.getPath().length());
final SVNRepository repository;
try {
repository = vcs.createRepository(vcs.getInfo(file).getRepositoryRootURL());
repository = vcs.createRepository(repositoryRootURL);
} catch (SVNException ex) {
throw new RuntimeException(ex);
}
Expand Down Expand Up @@ -135,31 +154,80 @@ public List<File> getChangedFiles()
return unmodifiableChangedFilesView;
}

public void removePropertyChangeListener(PropertyChangeListener listener)
{
changeSupport.removePropertyChangeListener(listener);
}

public void selectFile(@Nullable Integer index)
{
File oldValue = this.currentFile;
this.currentFile = index == null ? null : changedFiles.get(index);
changeSupport.firePropertyChange(CURRENT_FILE_PROPERTY, oldValue, this.currentFile);
}

public void setRevisionRange(int startRevision, int endRevision) throws SVNException
public void setRevisionRange(final int startRevision, final int endRevision)
{
final RevisionRange oldRange = this.revisionRange;
this.revisionRange = new RevisionRange(startRevision, endRevision);
changeSupport.firePropertyChange(REVISION_RANGE_PROPERTY, oldRange, this.revisionRange);
changedFiles.clear();
changes.clear();
changeSupport.firePropertyChange(CHANGED_FILES_PROPERTY, null, changedFiles);
clear();
final State oldState = state;
state = State.LOADING;
changeSupport.firePropertyChange(STATE_PROPERTY, oldState, state);

final SvnVcs svnVcs = SvnVcs.getInstance(project);
final SVNRevision svnRevision = SVNRevision.create(startRevision);
svnVcs.createDiffClient()
.doDiffStatus(new File(project.getBasePath()), svnRevision, SVNRevision.create(endRevision), svnRevision, SVNDepth.INFINITY, true,
diffStatusHandler);
changeSupport.firePropertyChange(CHANGED_FILES_PROPERTY, null, changedFiles);
selectFile(null);

ProgressManager.getInstance().run(new Task.Backgroundable(project, "Getting changed files") {
public void run(@NotNull ProgressIndicator progressIndicator)
{
final SvnVcs svnVcs = SvnVcs.getInstance(project);
final SVNRevision svnRevision = SVNRevision.create(startRevision);

final RootsToWorkingCopies rootsToWorkingCopies = svnVcs.getRootsToWorkingCopies();
if (rootsToWorkingCopies == null) {
throw new RuntimeException("Cannot get VCS roots to working copies");
}
final VirtualFile baseDir = project.getBaseDir();
final WorkingCopy wcRoot = rootsToWorkingCopies.getWcRoot(baseDir);
if (wcRoot == null) {
throw new RuntimeException("Cannot get working copy root for path " + baseDir);
}
final SVNURL wcRootUrl = wcRoot.getUrl();
try {
svnVcs.createDiffClient()
.doDiffStatus(wcRootUrl, svnRevision, SVNRevision.create(endRevision), svnRevision, SVNDepth.INFINITY, true, diffStatusHandler);
} catch (SVNException e) {
throw new RuntimeException("Cannot get list of changed files from " + wcRootUrl, e);
}
changeSupport.firePropertyChange(CHANGED_FILES_PROPERTY, null, changedFiles);
}

@Override
public void onSuccess()
{
State oldState = state;
state = State.LOADED;
changeSupport.firePropertyChange(STATE_PROPERTY, oldState, state);
}

@Override
public void onCancel()
{
clear();
State oldState = state;
state = State.CLEAR;
changeSupport.firePropertyChange(STATE_PROPERTY, oldState, state);
}
});
}

private void clear()
{
changedFiles.clear();
changes.clear();
changeSupport.firePropertyChange(CHANGED_FILES_PROPERTY, null, changedFiles);
}

// -------------------------- INNER CLASSES --------------------------

Expand All @@ -175,10 +243,12 @@ public void handleDiffStatus(SVNDiffStatus svnDiffStatus) throws SVNException
final int startRevision = getRevisionRange().getStartRevision();
final int endRevision = getRevisionRange().getEndRevision();
final SVNStatusType modificationType = svnDiffStatus.getModificationType();
if (!SVNStatusType.UNCHANGED.equals(modificationType) && !SVNStatusType.STATUS_NONE.equals(modificationType)) {
changedFiles.add(svnDiffStatus.getFile());
final File svnDiffStatusFile = new File(vcs.getSvnFileUrlMapping().getLocalPath(svnDiffStatus.getURL().toString()));
if (!SVNStatusType.UNCHANGED.equals(modificationType) && !SVNStatusType.STATUS_NONE.equals(modificationType) && !SVNStatusType.STATUS_DELETED
.equals(modificationType) && svnDiffStatusFile.exists()) {
changedFiles.add(svnDiffStatusFile);
if (SVNNodeKind.FILE.equals(svnDiffStatus.getKind())) {
changes.add(createChange(vcs, svnDiffStatus.getFile(), startRevision, endRevision));
changes.add(createChange(vcs, svnDiffStatusFile, startRevision, endRevision));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pl/com/it_crowd/cra/ui/CodeReviewAssistantPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public ChangeDiffRequestPresentable convert(Change o)
final ChangeDiffRequest request = new ChangeDiffRequest(CodeReviewAssistantPanel.this.project, changeList, context.getActionsFactory(),
context.isShowFrame());

DiffRequest simpleRequest = null;
DiffRequest simpleRequest;
try {
request.quickCheckHaveStuff();
simpleRequest = request.init(0);
Expand Down
20 changes: 12 additions & 8 deletions src/pl/com/it_crowd/cra/ui/RevisionRangeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.intellij.openapi.ui.Messages;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.tmatesoft.svn.core.SVNException;
import pl.com.it_crowd.cra.model.CodeReviewAssistant;

import javax.swing.JButton;
Expand All @@ -21,6 +20,8 @@
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class RevisionRangeDialog extends JDialog {
// ------------------------------ FIELDS ------------------------------
Expand Down Expand Up @@ -116,14 +117,17 @@ private void onOK()
Messages.showWarningDialog(e.getMessage(), "Invalid End Revision");
return;
}
try {
assistant.setRevisionRange(startRevision, endRevision);
} catch (SVNException e) {
Messages.showWarningDialog(e.getMessage(), "Invalid Revision Range");
return;
}
assistant.setRevisionRange(startRevision, endRevision);
assistant.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt)
{
if (CodeReviewAssistant.STATE_PROPERTY.equals(evt.getPropertyName()) && !CodeReviewAssistant.State.LOADING.equals(evt.getNewValue())) {
CodeReviewAssistantPanel.show(project);
assistant.removePropertyChangeListener(this);
}
}
});
dispose();
CodeReviewAssistantPanel.show(project);
}

/**
Expand Down

0 comments on commit 06a1f8a

Please sign in to comment.