[EGIT PATCH] Add support for writing/appending .gitignore file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is in addition to the other patches mailed earlier and attached with issue 64

From 34fd7fc8cd721c4f44b5b31d3d5960f89b59bf8b Mon Sep 17 00:00:00 2001
From: Alex Blewitt <alex.blewitt@xxxxxxxxx>
Date: Sun, 19 Apr 2009 14:03:46 +0100
Subject: [PATCH] Added support for writing/appending .gitignore file

---
.../egit/ui/internal/actions/IgnoreAction.java | 48 +++++++++++ ++++++---
 1 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/ actions/IgnoreAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ ui/internal/actions/IgnoreAction.java
index 1215823..832b098 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/ IgnoreAction.java +++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/ IgnoreAction.java
@@ -7,7 +7,17 @@
*******************************************************************************/
 package org.spearce.egit.ui.internal.actions;

+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.team.core.Team;

@@ -17,17 +27,43 @@
  */
 public class IgnoreAction extends RepositoryAction {
 	
+	private static final String GITIGNORE_ENCODING = "UTF-8";
+	private static final String GITIGNORE = ".gitignore";
+
 	@SuppressWarnings("restriction")
 	@Override
 	public void run(IAction action) {
-
+		NullProgressMonitor m = new NullProgressMonitor();
 		IResource[] resources = getSelectedResources();
-		for (IResource resource : resources) {
- // NB This does the same thing in DecoratableResourceAdapter, but neither currently consult .gitignore
-			if (!Team.isIgnoredHint(resource))
-			{
-				// TODO Actually add to .gitignore here
+		try {
+			for (IResource resource : resources) {
+ // TODO This is pretty inefficient; multiple ignores in the same directory cause multiple writes. + // NB This does the same thing in DecoratableResourceAdapter, but neither currently consult .gitignore
+				if (!Team.isIgnoredHint(resource)) {
+					IContainer container = resource.getParent();
+					IFile gitignore = container.getFile(new Path(GITIGNORE));
+ String entry = "/" + resource.getName() + "\n"; //$NON-NLS-1$ // $NON-NLS-2$
+					// TODO What is the character set and new-line convention?
+					if(gitignore.exists()) {
+ // This is ugly. CVS uses an internal representation of the .gitignore to re-write/overwrite each time.
+						ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
+ out.write(entry.getBytes(GITIGNORE_ENCODING)); // TODO Default encoding? + gitignore.appendContents(new ByteArrayInputStream(out.toByteArray()),true,true,m);
+					} else {
+ ByteArrayInputStream bais = new ByteArrayInputStream( entry.getBytes(GITIGNORE_ENCODING) ); //$NON- NLS-1$
+						gitignore.create( bais,true,m);					
+					}
+				}
 			}
+		} catch (UnsupportedEncodingException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (CoreException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
 		}
 		return;
 	}
--
1.6.2.2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]