Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndre Weinand2002-02-15 06:56:11 -0500
committerAndre Weinand2002-02-15 06:56:11 -0500
commit5a364bdc962138bbecf838976a88f8464e9aad9e (patch)
tree52f25cfe338d6cd2f833125ee4c4b51b0fc679d0
parentb9928bc259c0e27c460d9af42089c6d610b9e61b (diff)
downloadeclipse.platform.team-5a364bdc962138bbecf838976a88f8464e9aad9e.tar.gz
eclipse.platform.team-5a364bdc962138bbecf838976a88f8464e9aad9e.tar.xz
eclipse.platform.team-5a364bdc962138bbecf838976a88f8464e9aad9e.zip
#9540: Compare with patch: it should not be possible to check items that could not be applied
-rw-r--r--bundles/org.eclipse.compare/buildnotes_compare.html27
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java4
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java4
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java11
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java11
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java2
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java122
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java25
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/buildnotes_compare.html27
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java4
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java4
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java11
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java11
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java2
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java122
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java25
16 files changed, 282 insertions, 130 deletions
diff --git a/bundles/org.eclipse.compare/buildnotes_compare.html b/bundles/org.eclipse.compare/buildnotes_compare.html
index 735a0ab2b..810ad50f0 100644
--- a/bundles/org.eclipse.compare/buildnotes_compare.html
+++ b/bundles/org.eclipse.compare/buildnotes_compare.html
@@ -11,6 +11,33 @@
<h1>
Eclipse Platform Build Notes<br>
Compare</h1>
+Eclipse Build Input February ??th 2002
+
+<h2>
+What's new in this drop</h2>
+Patch: for every rejected file a task marker is added
+
+<h3>
+API changes</h3>
+
+<h3>
+API Additions</h3>
+
+<h3>
+Other highlights</h3>
+
+<h2>
+Known deficiencies</h2>
+
+<h2>
+Problem reports fixed</h2>
+<a href="http://dev.eclipse.org/bugs/show_bug.cgi?id=9540">#9540</a>: Compare with patch: it should not be possible to check items that could not be applied<br>
+
+<h2>
+Problem reports closed</h2>
+
+<h1>
+<hr WIDTH="100%"></h1>
Eclipse Build Input February 12th 2002
<h2>
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java
index 1ed9d6397..1edccfb27 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java
@@ -273,7 +273,7 @@ public final class CompareUIPlugin extends AbstractUIPlugin {
* compare editor on the result.
*
* @param input the input on which to open the compare editor
- * @see ICompareEditorInput
+ * @see CompareEditorInput
*/
public void openCompareEditor(CompareEditorInput input) {
@@ -298,7 +298,7 @@ public final class CompareUIPlugin extends AbstractUIPlugin {
* compare dialog on the result.
*
* @param input the input on which to open the compare editor
- * @see ICompareEditorInput
+ * @see CompareEditorInput
*/
public void openCompareDialog(final CompareEditorInput input) {
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java
index 28dff2473..cc4a73539 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java
@@ -27,12 +27,14 @@ import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.internal.*;
import org.eclipse.ui.IActionDelegate;
+import org.eclipse.ui.actions.GlobalBuildAction;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.compare.internal.*;
+
public class CompareWithPatchAction implements IActionDelegate {
static class PatchWizardDialog extends WizardDialog {
@@ -160,7 +162,7 @@ public class CompareWithPatchAction implements IActionDelegate {
private void triggerBuild() {
if (fSavedFiles && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) {
- new GlobalBuildAction(CompareUIPlugin.getActiveWorkbench(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
+ new GlobalBuildAction(CompareUIPlugin.getActiveWorkbench(), CompareUIPlugin.getShell(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
}
}
}
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java
index 9682d5cfe..5239340c5 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java
@@ -16,7 +16,8 @@ import org.eclipse.compare.structuremergeviewer.Differencer;
IPath fOldPath, fNewPath;
long fOldDate, fNewDate; // if 0: no file
List fHunks= new ArrayList();
- boolean fIsEnabled= true;
+ boolean fMatches= false;
+ private boolean fIsEnabled2= true;
String fRejected;
@@ -27,6 +28,14 @@ import org.eclipse.compare.structuremergeviewer.Differencer;
fNewDate= newPath == null ? 0 : newDate;
}
+ boolean isEnabled() {
+ return fIsEnabled2;
+ }
+
+ void setEnabled(boolean b) {
+ fIsEnabled2= b;
+ }
+
void reverse() {
IPath tp= fOldPath;
fOldPath= fNewPath;
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java
index 28dc90b55..e49d5ad6a 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java
@@ -18,7 +18,8 @@ import org.eclipse.jface.util.Assert;
int fOldStart, fOldLength;
int fNewStart, fNewLength;
String[] fLines;
- boolean fIsEnabled= true;
+ boolean fMatches= false;
+ private boolean fIsEnabled2= true;
/* package */ Hunk(Diff parent, int[] oldRange, int[] newRange, List lines) {
@@ -41,6 +42,14 @@ import org.eclipse.jface.util.Assert;
fLines= (String[]) lines.toArray(new String[lines.size()]);
}
+ boolean isEnabled() {
+ return fIsEnabled2;
+ }
+
+ void setEnabled(boolean b) {
+ fIsEnabled2= b;
+ }
+
void reverse() {
int t= fOldStart;
fOldStart= fNewStart;
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java
index 1f26392b3..d99a4f819 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java
@@ -84,7 +84,7 @@ import org.eclipse.compare.internal.*;
for (int i= 0; i < diffs.length; i++) {
Diff diff= diffs[i];
- if (diff.fIsEnabled) {
+ if (diff.isEnabled()) {
IPath path= fPatcher.getPath(diff);
createPath(fRoot, rootFolder, path, diff, false);
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java
index 1053e186f..d3e6a25e1 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java
@@ -25,7 +25,9 @@ public class Patcher {
private static final String DEV_NULL= "/dev/null"; //$NON-NLS-1$
- private static final String MARKER_TYPE= "org.eclipse.compare.rejectedPatchMarker";
+ private static final String REJECT_FILE_EXTENSION= ".rej"; //$NON-NLS-1$
+
+ private static final String MARKER_TYPE= "org.eclipse.compare.rejectedPatchMarker"; //$NON-NLS-1$
// diff formats
private static final int CONTEXT= 0;
@@ -569,7 +571,7 @@ public class Patcher {
}
}
- //---- applying a patch file
+ //---- applying a patch file
/**
* Tries to patch the given lines with the specified Diff.
@@ -581,6 +583,7 @@ public class Patcher {
Iterator iter= diff.fHunks.iterator();
while (iter.hasNext()) {
Hunk hunk= (Hunk) iter.next();
+ hunk.fMatches= false;
shift= patch(hunk, lines, shift, failedHunks);
}
}
@@ -644,7 +647,12 @@ public class Patcher {
}
return shift;
}
-
+
+ /**
+ * Tries to apply the given hunk on the specified lines.
+ * The parameter shift is added to the line numbers given
+ * in the hunk.
+ */
private boolean tryPatch(Hunk hunk, List lines, int shift) {
int pos= hunk.fOldStart + shift;
int contextMatches= 0;
@@ -721,17 +729,16 @@ public class Patcher {
lines.add(pos, line);
pos++;
} else
- Assert.isTrue(false, "doPatch: unknown control charcter: " + controlChar); //$NON-NLS-1$
+ Assert.isTrue(false, "doPatch: unknown control character: " + controlChar); //$NON-NLS-1$
}
+ hunk.fMatches= true;
return hunk.fNewLength - hunk.fOldLength;
}
public void applyAll(IResource target, IProgressMonitor pm) {
final int WORK_UNIT= 10;
-
- if (DEBUG) System.out.println("applyAll: start"); //$NON-NLS-1$
-
+
IFile file= null; // file to be patched
IContainer container= null;
if (target instanceof IContainer)
@@ -740,8 +747,7 @@ public class Patcher {
file= (IFile) target;
container= file.getParent();
} else {
- System.out.println("applyAll: not yet implemented"); //$NON-NLS-1$
- return;
+ Assert.isTrue(false);
}
if (pm != null)
@@ -752,7 +758,7 @@ public class Patcher {
int workTicks= WORK_UNIT;
Diff diff= fDiffs[i];
- if (diff.fIsEnabled) {
+ if (diff.isEnabled()) {
IPath path= getPath(diff);
if (pm != null)
@@ -768,7 +774,7 @@ public class Patcher {
case Differencer.ADDITION:
// patch it and collect rejected hunks
result= apply(diff, file, true, failed);
- updateFile(createString(result), file, new SubProgressMonitor(pm, workTicks));
+ store(createString(result), file, new SubProgressMonitor(pm, workTicks));
workTicks-= WORK_UNIT;
break;
case Differencer.DELETION:
@@ -778,7 +784,7 @@ public class Patcher {
case Differencer.CHANGE:
// patch it and collect rejected hunks
result= apply(diff, file, false, failed);
- updateFile(createString(result), file, new SubProgressMonitor(pm, workTicks));
+ store(createString(result), file, new SubProgressMonitor(pm, workTicks));
workTicks-= WORK_UNIT;
break;
}
@@ -787,12 +793,12 @@ public class Patcher {
IPath pp= null;
if (path.segmentCount() > 1) {
pp= path.removeLastSegments(1);
- pp= pp.append(path.lastSegment() + ".rej"); //$NON-NLS-1$
+ pp= pp.append(path.lastSegment() + REJECT_FILE_EXTENSION);
} else
- pp= new Path(path.lastSegment() + ".rej"); //$NON-NLS-1$
+ pp= new Path(path.lastSegment() + REJECT_FILE_EXTENSION);
file= createPath(container, pp);
if (file != null) {
- updateFile(getRejected(failed), file, pm);
+ store(getRejected(failed), file, pm);
try {
IMarker marker= file.createMarker(MARKER_TYPE);
marker.setAttribute(marker.MESSAGE, PatchMessages.getString("Patcher.Marker.message")); //$NON-NLS-1$
@@ -826,8 +832,11 @@ public class Patcher {
// IWorkspace.validateEdit(IFile[], Object context);
}
- List apply(Diff diff, IFile file, boolean create, List failedHunks) {
-
+ /**
+ * Reads the contents from the given file and returns them as
+ * a List of lines.
+ */
+ private List load(IFile file, boolean create) {
List lines= null;
if (!create && file != null) {
// read current contents
@@ -836,9 +845,7 @@ public class Patcher {
is= file.getContents();
BufferedReader reader= new BufferedReader(new InputStreamReader(is));
lines= new LineReader(reader).readLines();
- if (DEBUG) System.out.println(" creating reader successful"); //$NON-NLS-1$
} catch(CoreException ex) {
- if (DEBUG) System.out.println(" reading contents: " + ex); //$NON-NLS-1$
} finally {
if (is != null)
try {
@@ -850,27 +857,19 @@ public class Patcher {
if (lines == null)
lines= new ArrayList();
-
- patch(diff, lines, failedHunks);
-
return lines;
}
- String getRejected(List failedHunks) {
- if (failedHunks.size() <= 0)
- return null;
+ List apply(Diff diff, IFile file, boolean create, List failedHunks) {
- StringBuffer sb= new StringBuffer();
- Iterator iter= failedHunks.iterator();
- while (iter.hasNext()) {
- Hunk hunk= (Hunk) iter.next();
- sb.append(hunk.getRejectedDescription());
- sb.append('\n');
- sb.append(hunk.getContent());
- }
- return sb.toString();
+ List lines= load(file, create);
+ patch(diff, lines, failedHunks);
+ return lines;
}
+ /**
+ * Deletes the given file.
+ */
private void deleteFile(IFile file, IProgressMonitor pm) {
try {
file.delete(true, true, pm);
@@ -881,16 +880,10 @@ public class Patcher {
}
}
- private String createString(List lines) {
- // convert the result into a String
- StringBuffer sb= new StringBuffer();
- Iterator iter= lines.iterator();
- while (iter.hasNext())
- sb.append((String)iter.next());
- return sb.toString();
- }
-
- private void updateFile(String contents, IFile file, IProgressMonitor pm) {
+ /**
+ * Converts the string into bytes and stores them in the given file.
+ */
+ private void store(String contents, IFile file, IProgressMonitor pm) {
// and save it
InputStream is= new ByteArrayInputStream(contents.getBytes());
@@ -913,16 +906,49 @@ public class Patcher {
}
}
- private IFile createPath(IContainer folder, IPath path) {
+ /**
+ * Concatenates all strings found in the gievn List.
+ */
+ private String createString(List lines) {
+ StringBuffer sb= new StringBuffer();
+ Iterator iter= lines.iterator();
+ while (iter.hasNext())
+ sb.append((String)iter.next());
+ return sb.toString();
+ }
+
+ String getRejected(List failedHunks) {
+ if (failedHunks.size() <= 0)
+ return null;
+
+ StringBuffer sb= new StringBuffer();
+ Iterator iter= failedHunks.iterator();
+ while (iter.hasNext()) {
+ Hunk hunk= (Hunk) iter.next();
+ sb.append(hunk.getRejectedDescription());
+ sb.append('\n');
+ sb.append(hunk.getContent());
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Ensures that a file with the given path exists in
+ * the given container. Folder are created as necessary.
+ */
+ private IFile createPath(IContainer container, IPath path) {
if (path.segmentCount() > 1) {
- IFolder f= folder.getFolder(path.uptoSegment(1));
- //System.out.println("createPath: " + f + " " + f.exists());
+ IFolder f= container.getFolder(path.uptoSegment(1));
return createPath(f, path.removeFirstSegments(1));
}
// a leaf
- return folder.getFile(path);
+ return container.getFile(path);
}
+ /**
+ * Returns the given string with all whitespace characters removed.
+ * Whitespace is defined by <code>Character.isWhitespace(...)</code>.
+ */
private static String stripWhiteSpace(String s) {
StringBuffer sb= new StringBuffer();
int l= s.length();
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java
index 99e82b5f6..3ba7b9174 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java
@@ -152,12 +152,16 @@ import org.eclipse.compare.structuremergeviewer.*;
TreeItem ti= (TreeItem) e.item;
Object data= e.item.getData();
if (e.detail == SWT.CHECK) {
- boolean enabled= ti.getChecked();
+ boolean checked= ti.getChecked();
if (data instanceof Hunk) {
- ((Hunk)data).fIsEnabled= enabled;
+ Hunk hunk= (Hunk) data;
+ checked= checked && hunk.fMatches;
+ hunk.setEnabled(checked);
+ ti.setChecked(checked);
updateGrayedState(ti);
} else if (data instanceof Diff) {
- ((Diff)data).fIsEnabled= enabled;
+ Diff diff= (Diff) data;
+ diff.setEnabled(checked && diff.fMatches);
updateCheckedState(ti);
}
} else {
@@ -367,6 +371,7 @@ import org.eclipse.compare.structuremergeviewer.*;
for (int i= 0; i < children.length; i++) {
TreeItem item= children[i];
Diff diff= (Diff) item.getData();
+ diff.fMatches= false;
String error= null;
boolean create= false;
@@ -377,10 +382,9 @@ import org.eclipse.compare.structuremergeviewer.*;
p= p.removeFirstSegments(strip);
file= existsInSelection(p);
if (file == null) {
- diff.fIsEnabled= true;
+ diff.fMatches= true;
} else {
// file already exists
- diff.fIsEnabled= false;
error= PatchMessages.getString("PreviewPatchPage.FileExists.error"); //$NON-NLS-1$
}
create= true;
@@ -389,12 +393,11 @@ import org.eclipse.compare.structuremergeviewer.*;
if (strip > 0 && strip < p.segmentCount())
p= p.removeFirstSegments(strip);
file= existsInSelection(p);
- diff.fIsEnabled= file != null;
+ diff.fMatches= file != null;
if (file != null) {
- diff.fIsEnabled= true;
+ diff.fMatches= true;
} else {
// file doesn't exist
- diff.fIsEnabled= false;
error= PatchMessages.getString("PreviewPatchPage.FileDoesNotExist.error"); //$NON-NLS-1$
}
}
@@ -415,9 +418,9 @@ import org.eclipse.compare.structuremergeviewer.*;
String hunkError= null;
if (failed)
hunkError= PatchMessages.getString("PreviewPatchPage.NoMatch.error"); //$NON-NLS-1$
- hunk.fIsEnabled= diff.fIsEnabled && !failed;
- hunkItems[h].setChecked(hunk.fIsEnabled);
- if (hunk.fIsEnabled) {
+ hunk.setEnabled(diff.isEnabled() && !failed);
+ hunkItems[h].setChecked(hunk.isEnabled());
+ if (hunk.isEnabled()) {
checkedSubs++;
checked= true;
}
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/buildnotes_compare.html b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/buildnotes_compare.html
index 735a0ab2b..810ad50f0 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/buildnotes_compare.html
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/buildnotes_compare.html
@@ -11,6 +11,33 @@
<h1>
Eclipse Platform Build Notes<br>
Compare</h1>
+Eclipse Build Input February ??th 2002
+
+<h2>
+What's new in this drop</h2>
+Patch: for every rejected file a task marker is added
+
+<h3>
+API changes</h3>
+
+<h3>
+API Additions</h3>
+
+<h3>
+Other highlights</h3>
+
+<h2>
+Known deficiencies</h2>
+
+<h2>
+Problem reports fixed</h2>
+<a href="http://dev.eclipse.org/bugs/show_bug.cgi?id=9540">#9540</a>: Compare with patch: it should not be possible to check items that could not be applied<br>
+
+<h2>
+Problem reports closed</h2>
+
+<h1>
+<hr WIDTH="100%"></h1>
Eclipse Build Input February 12th 2002
<h2>
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java
index 1ed9d6397..1edccfb27 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java
@@ -273,7 +273,7 @@ public final class CompareUIPlugin extends AbstractUIPlugin {
* compare editor on the result.
*
* @param input the input on which to open the compare editor
- * @see ICompareEditorInput
+ * @see CompareEditorInput
*/
public void openCompareEditor(CompareEditorInput input) {
@@ -298,7 +298,7 @@ public final class CompareUIPlugin extends AbstractUIPlugin {
* compare dialog on the result.
*
* @param input the input on which to open the compare editor
- * @see ICompareEditorInput
+ * @see CompareEditorInput
*/
public void openCompareDialog(final CompareEditorInput input) {
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java
index 28dff2473..cc4a73539 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java
@@ -27,12 +27,14 @@ import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.internal.*;
import org.eclipse.ui.IActionDelegate;
+import org.eclipse.ui.actions.GlobalBuildAction;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.compare.internal.*;
+
public class CompareWithPatchAction implements IActionDelegate {
static class PatchWizardDialog extends WizardDialog {
@@ -160,7 +162,7 @@ public class CompareWithPatchAction implements IActionDelegate {
private void triggerBuild() {
if (fSavedFiles && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) {
- new GlobalBuildAction(CompareUIPlugin.getActiveWorkbench(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
+ new GlobalBuildAction(CompareUIPlugin.getActiveWorkbench(), CompareUIPlugin.getShell(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
}
}
}
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java
index 9682d5cfe..5239340c5 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Diff.java
@@ -16,7 +16,8 @@ import org.eclipse.compare.structuremergeviewer.Differencer;
IPath fOldPath, fNewPath;
long fOldDate, fNewDate; // if 0: no file
List fHunks= new ArrayList();
- boolean fIsEnabled= true;
+ boolean fMatches= false;
+ private boolean fIsEnabled2= true;
String fRejected;
@@ -27,6 +28,14 @@ import org.eclipse.compare.structuremergeviewer.Differencer;
fNewDate= newPath == null ? 0 : newDate;
}
+ boolean isEnabled() {
+ return fIsEnabled2;
+ }
+
+ void setEnabled(boolean b) {
+ fIsEnabled2= b;
+ }
+
void reverse() {
IPath tp= fOldPath;
fOldPath= fNewPath;
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java
index 28dc90b55..e49d5ad6a 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Hunk.java
@@ -18,7 +18,8 @@ import org.eclipse.jface.util.Assert;
int fOldStart, fOldLength;
int fNewStart, fNewLength;
String[] fLines;
- boolean fIsEnabled= true;
+ boolean fMatches= false;
+ private boolean fIsEnabled2= true;
/* package */ Hunk(Diff parent, int[] oldRange, int[] newRange, List lines) {
@@ -41,6 +42,14 @@ import org.eclipse.jface.util.Assert;
fLines= (String[]) lines.toArray(new String[lines.size()]);
}
+ boolean isEnabled() {
+ return fIsEnabled2;
+ }
+
+ void setEnabled(boolean b) {
+ fIsEnabled2= b;
+ }
+
void reverse() {
int t= fOldStart;
fOldStart= fNewStart;
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java
index 1f26392b3..d99a4f819 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PatchCompareInput.java
@@ -84,7 +84,7 @@ import org.eclipse.compare.internal.*;
for (int i= 0; i < diffs.length; i++) {
Diff diff= diffs[i];
- if (diff.fIsEnabled) {
+ if (diff.isEnabled()) {
IPath path= fPatcher.getPath(diff);
createPath(fRoot, rootFolder, path, diff, false);
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java
index 1053e186f..d3e6a25e1 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Patcher.java
@@ -25,7 +25,9 @@ public class Patcher {
private static final String DEV_NULL= "/dev/null"; //$NON-NLS-1$
- private static final String MARKER_TYPE= "org.eclipse.compare.rejectedPatchMarker";
+ private static final String REJECT_FILE_EXTENSION= ".rej"; //$NON-NLS-1$
+
+ private static final String MARKER_TYPE= "org.eclipse.compare.rejectedPatchMarker"; //$NON-NLS-1$
// diff formats
private static final int CONTEXT= 0;
@@ -569,7 +571,7 @@ public class Patcher {
}
}
- //---- applying a patch file
+ //---- applying a patch file
/**
* Tries to patch the given lines with the specified Diff.
@@ -581,6 +583,7 @@ public class Patcher {
Iterator iter= diff.fHunks.iterator();
while (iter.hasNext()) {
Hunk hunk= (Hunk) iter.next();
+ hunk.fMatches= false;
shift= patch(hunk, lines, shift, failedHunks);
}
}
@@ -644,7 +647,12 @@ public class Patcher {
}
return shift;
}
-
+
+ /**
+ * Tries to apply the given hunk on the specified lines.
+ * The parameter shift is added to the line numbers given
+ * in the hunk.
+ */
private boolean tryPatch(Hunk hunk, List lines, int shift) {
int pos= hunk.fOldStart + shift;
int contextMatches= 0;
@@ -721,17 +729,16 @@ public class Patcher {
lines.add(pos, line);
pos++;
} else
- Assert.isTrue(false, "doPatch: unknown control charcter: " + controlChar); //$NON-NLS-1$
+ Assert.isTrue(false, "doPatch: unknown control character: " + controlChar); //$NON-NLS-1$
}
+ hunk.fMatches= true;
return hunk.fNewLength - hunk.fOldLength;
}
public void applyAll(IResource target, IProgressMonitor pm) {
final int WORK_UNIT= 10;
-
- if (DEBUG) System.out.println("applyAll: start"); //$NON-NLS-1$
-
+
IFile file= null; // file to be patched
IContainer container= null;
if (target instanceof IContainer)
@@ -740,8 +747,7 @@ public class Patcher {
file= (IFile) target;
container= file.getParent();
} else {
- System.out.println("applyAll: not yet implemented"); //$NON-NLS-1$
- return;
+ Assert.isTrue(false);
}
if (pm != null)
@@ -752,7 +758,7 @@ public class Patcher {
int workTicks= WORK_UNIT;
Diff diff= fDiffs[i];
- if (diff.fIsEnabled) {
+ if (diff.isEnabled()) {
IPath path= getPath(diff);
if (pm != null)
@@ -768,7 +774,7 @@ public class Patcher {
case Differencer.ADDITION:
// patch it and collect rejected hunks
result= apply(diff, file, true, failed);
- updateFile(createString(result), file, new SubProgressMonitor(pm, workTicks));
+ store(createString(result), file, new SubProgressMonitor(pm, workTicks));
workTicks-= WORK_UNIT;
break;
case Differencer.DELETION:
@@ -778,7 +784,7 @@ public class Patcher {
case Differencer.CHANGE:
// patch it and collect rejected hunks
result= apply(diff, file, false, failed);
- updateFile(createString(result), file, new SubProgressMonitor(pm, workTicks));
+ store(createString(result), file, new SubProgressMonitor(pm, workTicks));
workTicks-= WORK_UNIT;
break;
}
@@ -787,12 +793,12 @@ public class Patcher {
IPath pp= null;
if (path.segmentCount() > 1) {
pp= path.removeLastSegments(1);
- pp= pp.append(path.lastSegment() + ".rej"); //$NON-NLS-1$
+ pp= pp.append(path.lastSegment() + REJECT_FILE_EXTENSION);
} else
- pp= new Path(path.lastSegment() + ".rej"); //$NON-NLS-1$
+ pp= new Path(path.lastSegment() + REJECT_FILE_EXTENSION);
file= createPath(container, pp);
if (file != null) {
- updateFile(getRejected(failed), file, pm);
+ store(getRejected(failed), file, pm);
try {
IMarker marker= file.createMarker(MARKER_TYPE);
marker.setAttribute(marker.MESSAGE, PatchMessages.getString("Patcher.Marker.message")); //$NON-NLS-1$
@@ -826,8 +832,11 @@ public class Patcher {
// IWorkspace.validateEdit(IFile[], Object context);
}
- List apply(Diff diff, IFile file, boolean create, List failedHunks) {
-
+ /**
+ * Reads the contents from the given file and returns them as
+ * a List of lines.
+ */
+ private List load(IFile file, boolean create) {
List lines= null;
if (!create && file != null) {
// read current contents
@@ -836,9 +845,7 @@ public class Patcher {
is= file.getContents();
BufferedReader reader= new BufferedReader(new InputStreamReader(is));
lines= new LineReader(reader).readLines();
- if (DEBUG) System.out.println(" creating reader successful"); //$NON-NLS-1$
} catch(CoreException ex) {
- if (DEBUG) System.out.println(" reading contents: " + ex); //$NON-NLS-1$
} finally {
if (is != null)
try {
@@ -850,27 +857,19 @@ public class Patcher {
if (lines == null)
lines= new ArrayList();
-
- patch(diff, lines, failedHunks);
-
return lines;
}
- String getRejected(List failedHunks) {
- if (failedHunks.size() <= 0)
- return null;
+ List apply(Diff diff, IFile file, boolean create, List failedHunks) {
- StringBuffer sb= new StringBuffer();
- Iterator iter= failedHunks.iterator();
- while (iter.hasNext()) {
- Hunk hunk= (Hunk) iter.next();
- sb.append(hunk.getRejectedDescription());
- sb.append('\n');
- sb.append(hunk.getContent());
- }
- return sb.toString();
+ List lines= load(file, create);
+ patch(diff, lines, failedHunks);
+ return lines;
}
+ /**
+ * Deletes the given file.
+ */
private void deleteFile(IFile file, IProgressMonitor pm) {
try {
file.delete(true, true, pm);
@@ -881,16 +880,10 @@ public class Patcher {
}
}
- private String createString(List lines) {
- // convert the result into a String
- StringBuffer sb= new StringBuffer();
- Iterator iter= lines.iterator();
- while (iter.hasNext())
- sb.append((String)iter.next());
- return sb.toString();
- }
-
- private void updateFile(String contents, IFile file, IProgressMonitor pm) {
+ /**
+ * Converts the string into bytes and stores them in the given file.
+ */
+ private void store(String contents, IFile file, IProgressMonitor pm) {
// and save it
InputStream is= new ByteArrayInputStream(contents.getBytes());
@@ -913,16 +906,49 @@ public class Patcher {
}
}
- private IFile createPath(IContainer folder, IPath path) {
+ /**
+ * Concatenates all strings found in the gievn List.
+ */
+ private String createString(List lines) {
+ StringBuffer sb= new StringBuffer();
+ Iterator iter= lines.iterator();
+ while (iter.hasNext())
+ sb.append((String)iter.next());
+ return sb.toString();
+ }
+
+ String getRejected(List failedHunks) {
+ if (failedHunks.size() <= 0)
+ return null;
+
+ StringBuffer sb= new StringBuffer();
+ Iterator iter= failedHunks.iterator();
+ while (iter.hasNext()) {
+ Hunk hunk= (Hunk) iter.next();
+ sb.append(hunk.getRejectedDescription());
+ sb.append('\n');
+ sb.append(hunk.getContent());
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Ensures that a file with the given path exists in
+ * the given container. Folder are created as necessary.
+ */
+ private IFile createPath(IContainer container, IPath path) {
if (path.segmentCount() > 1) {
- IFolder f= folder.getFolder(path.uptoSegment(1));
- //System.out.println("createPath: " + f + " " + f.exists());
+ IFolder f= container.getFolder(path.uptoSegment(1));
return createPath(f, path.removeFirstSegments(1));
}
// a leaf
- return folder.getFile(path);
+ return container.getFile(path);
}
+ /**
+ * Returns the given string with all whitespace characters removed.
+ * Whitespace is defined by <code>Character.isWhitespace(...)</code>.
+ */
private static String stripWhiteSpace(String s) {
StringBuffer sb= new StringBuffer();
int l= s.length();
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java
index 99e82b5f6..3ba7b9174 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/PreviewPatchPage.java
@@ -152,12 +152,16 @@ import org.eclipse.compare.structuremergeviewer.*;
TreeItem ti= (TreeItem) e.item;
Object data= e.item.getData();
if (e.detail == SWT.CHECK) {
- boolean enabled= ti.getChecked();
+ boolean checked= ti.getChecked();
if (data instanceof Hunk) {
- ((Hunk)data).fIsEnabled= enabled;
+ Hunk hunk= (Hunk) data;
+ checked= checked && hunk.fMatches;
+ hunk.setEnabled(checked);
+ ti.setChecked(checked);
updateGrayedState(ti);
} else if (data instanceof Diff) {
- ((Diff)data).fIsEnabled= enabled;
+ Diff diff= (Diff) data;
+ diff.setEnabled(checked && diff.fMatches);
updateCheckedState(ti);
}
} else {
@@ -367,6 +371,7 @@ import org.eclipse.compare.structuremergeviewer.*;
for (int i= 0; i < children.length; i++) {
TreeItem item= children[i];
Diff diff= (Diff) item.getData();
+ diff.fMatches= false;
String error= null;
boolean create= false;
@@ -377,10 +382,9 @@ import org.eclipse.compare.structuremergeviewer.*;
p= p.removeFirstSegments(strip);
file= existsInSelection(p);
if (file == null) {
- diff.fIsEnabled= true;
+ diff.fMatches= true;
} else {
// file already exists
- diff.fIsEnabled= false;
error= PatchMessages.getString("PreviewPatchPage.FileExists.error"); //$NON-NLS-1$
}
create= true;
@@ -389,12 +393,11 @@ import org.eclipse.compare.structuremergeviewer.*;
if (strip > 0 && strip < p.segmentCount())
p= p.removeFirstSegments(strip);
file= existsInSelection(p);
- diff.fIsEnabled= file != null;
+ diff.fMatches= file != null;
if (file != null) {
- diff.fIsEnabled= true;
+ diff.fMatches= true;
} else {
// file doesn't exist
- diff.fIsEnabled= false;
error= PatchMessages.getString("PreviewPatchPage.FileDoesNotExist.error"); //$NON-NLS-1$
}
}
@@ -415,9 +418,9 @@ import org.eclipse.compare.structuremergeviewer.*;
String hunkError= null;
if (failed)
hunkError= PatchMessages.getString("PreviewPatchPage.NoMatch.error"); //$NON-NLS-1$
- hunk.fIsEnabled= diff.fIsEnabled && !failed;
- hunkItems[h].setChecked(hunk.fIsEnabled);
- if (hunk.fIsEnabled) {
+ hunk.setEnabled(diff.isEnabled() && !failed);
+ hunkItems[h].setChecked(hunk.isEnabled());
+ if (hunk.isEnabled()) {
checkedSubs++;
checked= true;
}

Back to the top