Merge pull request #2061 from dmochalov/fix-gitignore-new-line

[NETBEANS-4108] fixed git -> ignore should add a new line at the end …
diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java
index a5c19d6..255ef1b 100644
--- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java
+++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java
@@ -137,9 +137,7 @@
             for (ListIterator<IgnoreRule> it = ignoreRules.listIterator(); it.hasNext(); ) {
                 String s = it.next().getPattern(false);
                 bw.write(s, 0, s.length());
-                if (it.hasNext()) {
-                    bw.newLine();
-                }
+                bw.newLine();
             }
         } finally {
             if (bw != null) {
diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java
index 5b72680..2fff96b 100644
--- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java
+++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java
@@ -98,23 +98,15 @@
     }
 
     protected void write(File file, String str) throws IOException {
-        FileWriter w = null;
-        try {
-            w = new FileWriter(file);
+        try (FileWriter w = new FileWriter(file)) {
             w.write(str);
             w.flush();
-        } finally {
-            if (w != null) {
-                w.close();
-            }
         }
     }
 
     protected String read(File file) throws IOException {
         StringBuilder sb = new StringBuilder();
-        BufferedReader r = null;
-        try {
-            r = new BufferedReader(new FileReader(file));
+        try (BufferedReader r = new BufferedReader(new FileReader(file))) {
             String s = r.readLine();
             if (s != null) {
                 while( true ) {
@@ -124,14 +116,22 @@
                     sb.append('\n');
                 }
             }
-        } finally {
-            if (r != null) {
-                r.close();
-            }
         }
         return sb.toString();
     }
 
+    protected boolean containsCRorLF(File file) throws IOException {
+        try (BufferedReader r = new BufferedReader(new FileReader(file))) {
+            int i;
+            while ((i = r.read()) > -1) {
+                if (i == '\n' || i == '\r') {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     protected static void assertStatus (Map<File, GitStatus> statuses, File workDir, File file, boolean tracked, Status headVsIndex, Status indexVsWorking, Status headVsWorking, boolean conflict) {
         GitStatus status = statuses.get(file);
         assertNotNull(file.getAbsolutePath() + " not in " + statuses.keySet(), status);
diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java
index 98f7c2c..28234bd 100644
--- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java
+++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java
@@ -589,4 +589,13 @@
         statuses = client.getStatus(new File[0], NULL_PROGRESS_MONITOR);
         assertEquals(Status.STATUS_IGNORED, statuses.get(f).getStatusIndexWC());
     }
+
+    public void testGitIgnoreEndsWithNewLine () throws Exception {
+        File f = new File(workDir, "file");
+        f.createNewFile();
+        File gitIgnore = new File(workDir, Constants.DOT_GIT_IGNORE);
+        File[] ignores = getClient(workDir).ignore(new File[] { f }, NULL_PROGRESS_MONITOR);
+        assertTrue(gitIgnore.exists());
+        assertTrue("The .gitignore file should ends with an empty new line.",containsCRorLF(gitIgnore));
+    }
 }