diff options
-rw-r--r-- | org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/IgnoreOperationTest.java | 20 | ||||
-rw-r--r-- | org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java | 38 |
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 96d9804eb6..7b27fb224b 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 ff903a8892..8ec963880c 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(); } |