aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-03-25 12:57:18 (EDT)
committerChris Aniszczyk2011-03-31 17:37:46 (EDT)
commitaa2db1875f9de53fd9f5a1c377cc8e6c5b3e75f6 (patch)
tree92167891cc044ae737c1dccf2faa3bbc38113312
parent0930c26da63dc739b10cc2105e3839f947fc09e6 (diff)
downloadegit-aa2db1875f9de53fd9f5a1c377cc8e6c5b3e75f6.zip
egit-aa2db1875f9de53fd9f5a1c377cc8e6c5b3e75f6.tar.gz
egit-aa2db1875f9de53fd9f5a1c377cc8e6c5b3e75f6.tar.bz2
Check for trailing newline before writing to .gitignore file.refs/changes/03/2903/6
IgnoreOperation wrote entries to the .gitignore file without checking if the file currently had a trailing newline which would cause entries to get written on the same line as other entries for files that were missing a trailing newline. Added unit test to IgnoreOperationTest for this case. Bug: 339419 Change-Id: I422ec5e92e3fcfea0d8bf6d3bfaf38245e505733 Signed-off-by: Kevin Sawicki <kevin@github.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/IgnoreOperationTest.java20
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java38
2 files changed, 55 insertions, 3 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/IgnoreOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/IgnoreOperationTest.java
index 96d9804..7b27fb2 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/IgnoreOperationTest.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/IgnoreOperationTest.java
@@ -13,8 +13,10 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayInputStream;
import java.io.File;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
@@ -125,4 +127,22 @@ public class IgnoreOperationTest extends GitTestCase {
assertTrue(operation.isGitignoreOutsideWSChanged());
}
+ @Test
+ public void testIgnoreNoTrailingNewline() throws Exception {
+ String existing = "/nonewline";
+ IFile ignore = project.getProject().getFile(
+ Constants.GITIGNORE_FILENAME);
+ assertFalse(ignore.exists());
+ ignore.create(new ByteArrayInputStream(existing.getBytes()),
+ IResource.FORCE, new NullProgressMonitor());
+
+ IFolder binFolder = project.getProject().getFolder("bin");
+ IgnoreOperation operation = new IgnoreOperation(
+ new IResource[] { binFolder });
+ operation.execute(new NullProgressMonitor());
+
+ String content = project.getFileContent(Constants.GITIGNORE_FILENAME);
+ assertEquals(existing + "\n/bin\n", content);
+ assertFalse(operation.isGitignoreOutsideWSChanged());
+ }
}
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java
index ff903a8..8ec9638 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java
@@ -13,7 +13,10 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
@@ -125,10 +128,9 @@ public class IgnoreOperation implements IEGitOperation {
}
private void addIgnore(IProgressMonitor monitor, IResource resource)
- throws UnsupportedEncodingException, CoreException {
+ throws UnsupportedEncodingException, CoreException, IOException {
IContainer container = resource.getParent();
String entry = "/" + resource.getName() + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
- ByteArrayInputStream entryBytes = asStream(entry);
if (container instanceof IWorkspaceRoot) {
Repository repository = RepositoryMapping.getMapping(
@@ -154,7 +156,9 @@ public class IgnoreOperation implements IEGitOperation {
} else {
IFile gitignore = container.getFile(new Path(
Constants.GITIGNORE_FILENAME));
+ entry = getEntry(gitignore.getLocation().toFile(), entry);
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
+ ByteArrayInputStream entryBytes = asStream(entry);
if (gitignore.exists())
gitignore.appendContents(entryBytes, true, true, subMonitor);
else
@@ -162,9 +166,35 @@ public class IgnoreOperation implements IEGitOperation {
}
}
+ private boolean prependNewline(File file) throws IOException {
+ boolean prepend = false;
+ long length = file.length();
+ if (length > 0) {
+ RandomAccessFile raf = new RandomAccessFile(file, "r"); //$NON-NLS-1$
+ try {
+ // Read the last byte and see if it is a newline
+ ByteBuffer buffer = ByteBuffer.allocate(1);
+ FileChannel channel = raf.getChannel();
+ channel.position(length - 1);
+ if (channel.read(buffer) > 0) {
+ buffer.rewind();
+ prepend = buffer.get() != '\n';
+ }
+ } finally {
+ raf.close();
+ }
+ }
+ return prepend;
+ }
+
+ private String getEntry(File file, String entry) throws IOException {
+ return prependNewline(file) ? "\n" + entry : entry; //$NON-NLS-1$
+ }
+
private void updateGitIgnore(File gitIgnore, String entry)
throws CoreException {
try {
+ String ignoreLine = entry;
if (!gitIgnore.exists())
if (!gitIgnore.createNewFile()) {
String error = NLS.bind(
@@ -172,10 +202,12 @@ public class IgnoreOperation implements IEGitOperation {
gitIgnore.getAbsolutePath());
throw new CoreException(Activator.error(error, null));
}
+ else
+ ignoreLine = getEntry(gitIgnore, ignoreLine);
FileOutputStream os = new FileOutputStream(gitIgnore, true);
try {
- os.write(entry.getBytes());
+ os.write(ignoreLine.getBytes());
} finally {
os.close();
}