git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Alex Blewitt <alex.blewitt@gmail.com>
Date
Apr 19, 2009, 13:09 UTC
Message-ID
<AFFAB806-28F7-4D48-B603-B7B96052B0F3@gmail.com>

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@gmail.com>
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
Next: Robin Rosenberg
Message 1 of 6 in “Add support for writing/appending .gitignore file”
  1. Add support for writing/appending .gitignore fileAlex Blewitt, Apr 19, 2009
  2. Robin RosenbergApr 19, 2009
  3. Alex BlewittApr 20, 2009
  4. Robin RosenbergApr 20, 2009
  5. Alex BlewittApr 20, 2009
  6. Robin RosenbergApr 20, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.