diff options
Diffstat (limited to 'org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java')
-rw-r--r-- | org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java | 38 |
1 files changed, 35 insertions, 3 deletions
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(); } |