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

[PATCH 4/7] Extract lockAndWriteFile method in RefDatabase for reuse

From
Charles O'Farrell <charleso@charleso.org>
Date
Aug 14, 2008, 10:13 UTC
Message-ID
<1218708829-8175-5-git-send-email-charleso@charleso.org>
In-Reply-To
<1218708829-8175-4-git-send-email-charleso@charleso.org>
Signed-off-by: Charles O'Farrell <charleso@charleso.org>
---
 .../src/org/spearce/jgit/lib/RefDatabase.java      |   25 ++++++++++++--------
 1 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
index 3e68a8d..3d8280d 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
@@ -158,16 +158,7 @@ class RefDatabase {
 	 */
 	void link(final String name, final String target) throws IOException {
 		final byte[] content = ("ref: " + target + "\n").getBytes(CHAR_ENC);
-		final LockFile lck = new LockFile(fileForRef(name));
-		if (!lck.lock())
-			throw new ObjectWritingException("Unable to lock " + name);
-		try {
-			lck.write(content);
-		} catch (IOException ioe) {
-			throw new ObjectWritingException("Unable to write " + name, ioe);
-		}
-		if (!lck.commit())
-			throw new ObjectWritingException("Unable to write " + name);
+		lockAndWriteFile(fileForRef(name), content);
 		setModified();
 		db.fireRefsMaybeChanged();
 	}
@@ -427,6 +418,20 @@ class RefDatabase {
 		}
 	}
 
+	private void lockAndWriteFile(File file, byte[] content) throws IOException {
+		String name = file.getName();
+		final LockFile lck = new LockFile(file);
+		if (!lck.lock())
+			throw new ObjectWritingException("Unable to lock " + name);
+		try {
+			lck.write(content);
+		} catch (IOException ioe) {
+			throw new ObjectWritingException("Unable to write " + name, ioe);
+		}
+		if (!lck.commit())
+			throw new ObjectWritingException("Unable to write " + name);
+	}
+
 	private static String readLine(final File file)
 			throws FileNotFoundException, IOException {
 		final BufferedReader br = openReader(file);
-- 
1.6.0.rc2.35.g04c6e
Previous: Charles O'FarrellNext: Charles O'Farrell
Message 5 of 11 in “jgit: Branch command now supports deletion”
  1. 0/7 jgit: Branch command now supports deletionCharles O'Farrell, Aug 14, 2008
  2. 1/7 Refactor of WalkRemoteObjectDatabase ref writing into common classCharles O'Farrell, Aug 14, 2008
  3. 2/7 Refactor of RefUpdate force to call common updateImpl instead of duplicationCharles O'Farrell, Aug 14, 2008
  4. 3/7 Minor refactor of constants, including log and ROOT_DIRCharles O'Farrell, Aug 14, 2008
  5. 4/7 Extract lockAndWriteFile method in RefDatabase for reuseCharles O'Farrell, Aug 14, 2008
  6. 5/7 Added removePackedRef method to RefDatabase for packed branch deletionCharles O'Farrell, Aug 14, 2008
  7. 6/7 Added ref deletion to RefUpdateCharles O'Farrell, Aug 14, 2008
  8. 7/7 jgit: Added branch deletion to jgit commandCharles O'Farrell, Aug 14, 2008
  9. Shawn O. PearceAug 14, 2008
  10. 2/7 Refactor of RefUpdate force to call common updateImpl instead of duplicationCharles O'Farrell, Aug 15, 2008
  11. 6/7 Added ref deletion to RefUpdateCharles O'Farrell, Aug 15, 2008

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.