[JGIT PATCH] Improve error handling for writing FETCH_HEAD

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

 



PrintWriter hides error handling from us and we want it. We
also want \n as line terminator so Writer is just as simple. Try-
finally blocks added for cleanup on failure.

Signed-off-by: Robin Rosenberg <robin.rosenberg@xxxxxxxxxx>
---
 .../spearce/jgit/transport/FetchHeadRecord.java    |   25 +++++++++---------
 .../org/spearce/jgit/transport/FetchProcess.java   |   27 ++++++++++---------
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/FetchHeadRecord.java b/org.spearce.jgit/src/org/spearce/jgit/transport/FetchHeadRecord.java
index d957028..62ec38a 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/FetchHeadRecord.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/FetchHeadRecord.java
@@ -41,7 +41,8 @@
 import static org.spearce.jgit.lib.Constants.R_REMOTES;
 import static org.spearce.jgit.lib.Constants.R_TAGS;
 
-import java.io.PrintWriter;
+import java.io.IOException;
+import java.io.Writer;
 
 import org.spearce.jgit.lib.ObjectId;
 
@@ -54,7 +55,7 @@
 
 	URIish sourceURI;
 
-	void write(final PrintWriter pw) {
+	void write(final Writer pw) throws IOException {
 		final String type;
 		final String name;
 		if (sourceName.startsWith(R_HEADS)) {
@@ -71,16 +72,16 @@ void write(final PrintWriter pw) {
 			name = sourceName;
 		}
 
-		pw.print(newValue.name());
-		pw.print('\t');
+		pw.write(newValue.name());
+		pw.write('\t');
 		if (notForMerge)
-			pw.print("not-for-merge");
-		pw.print('\t');
-		pw.print(type);
-		pw.print(" '");
-		pw.print(name);
-		pw.print("' of ");
-		pw.print(sourceURI);
-		pw.println();
+			pw.write("not-for-merge");
+		pw.write('\t');
+		pw.write(type);
+		pw.write(" '");
+		pw.write(name);
+		pw.write("' of ");
+		pw.write(sourceURI.toString());
+		pw.write("\n");
 	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/FetchProcess.java b/org.spearce.jgit/src/org/spearce/jgit/transport/FetchProcess.java
index 08d7d65..c899c8c 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/FetchProcess.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/FetchProcess.java
@@ -41,7 +41,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -264,20 +264,21 @@ private void removeFetchHeadRecord(final ObjectId want) {
 	private void updateFETCH_HEAD(final FetchResult result) throws IOException {
 		final LockFile lock = new LockFile(new File(transport.local
 				.getDirectory(), "FETCH_HEAD"));
-		if (lock.lock()) {
-			final PrintWriter pw = new PrintWriter(new OutputStreamWriter(lock
-					.getOutputStream())) {
-				@Override
-				public void println() {
-					print('\n');
+		try {
+			if (lock.lock()) {
+				final Writer w = new OutputStreamWriter(lock.getOutputStream());
+				try {
+					for (final FetchHeadRecord h : fetchHeadUpdates) {
+						h.write(w);
+						result.add(h);
+					}
+				} finally {
+					w.close();
 				}
-			};
-			for (final FetchHeadRecord h : fetchHeadUpdates) {
-				h.write(pw);
-				result.add(h);
+				lock.commit();
 			}
-			pw.close();
-			lock.commit();
+		} finally {
+			lock.unlock();
 		}
 	}
 
-- 
1.6.3.2.199.g7340d

--
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]