Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Stocker2011-04-06 19:10:16 +0000
committerRobin Stocker2011-04-06 20:40:28 +0000
commit3151657404859454bdf60345c09b536d1269c0c8 (patch)
treea1dba4dab35fc08b06574c79838749254225803c
parent6e10aa42e90a25b82f00f0c27574f57ffa9e4a25 (diff)
downloadjgit-3151657404859454bdf60345c09b536d1269c0c8.tar.gz
jgit-3151657404859454bdf60345c09b536d1269c0c8.tar.xz
jgit-3151657404859454bdf60345c09b536d1269c0c8.zip
Refactor reading and writing heads in Repository
Add private methods which are used for reading and writing MERGE_HEAD and CHERRY_PICK_HEAD files, as suggested in the comments on change I947967fdc2f1d55016c95106b104c2afcc9797a1. Change-Id: If4617a05ee57054b8b1fcba36a06a641340ecc0e Signed-off-by: Robin Stocker <robin@nibor.org>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java87
1 files changed, 47 insertions, 40 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
index 4847a5dbf5..aab8a8e619 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
@@ -1158,15 +1158,8 @@ public abstract class Repository {
if (isBare() || getDirectory() == null)
throw new NoWorkTreeException();
- File mergeHeadFile = new File(getDirectory(), Constants.MERGE_HEAD);
- byte[] raw;
- try {
- raw = IO.readFully(mergeHeadFile);
- } catch (FileNotFoundException notFound) {
- return null;
- }
-
- if (raw.length == 0)
+ byte[] raw = readGitDirectoryFile(Constants.MERGE_HEAD);
+ if (raw == null)
return null;
LinkedList<ObjectId> heads = new LinkedList<ObjectId>();
@@ -1190,21 +1183,7 @@ public abstract class Repository {
* @throws IOException
*/
public void writeMergeHeads(List<ObjectId> heads) throws IOException {
- File mergeHeadFile = new File(gitDir, Constants.MERGE_HEAD);
- if (heads != null) {
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(mergeHeadFile));
- try {
- for (ObjectId id : heads) {
- id.copyTo(bos);
- bos.write('\n');
- }
- } finally {
- bos.close();
- }
- } else {
- FileUtils.delete(mergeHeadFile);
- }
+ writeHeadsFile(heads, Constants.MERGE_HEAD);
}
/**
@@ -1223,16 +1202,8 @@ public abstract class Repository {
if (isBare() || getDirectory() == null)
throw new NoWorkTreeException();
- File mergeHeadFile = new File(getDirectory(),
- Constants.CHERRY_PICK_HEAD);
- byte[] raw;
- try {
- raw = IO.readFully(mergeHeadFile);
- } catch (FileNotFoundException notFound) {
- return null;
- }
-
- if (raw.length == 0)
+ byte[] raw = readGitDirectoryFile(Constants.CHERRY_PICK_HEAD);
+ if (raw == null)
return null;
return ObjectId.fromString(raw, 0);
@@ -1248,18 +1219,54 @@ public abstract class Repository {
* @throws IOException
*/
public void writeCherryPickHead(ObjectId head) throws IOException {
- File cherryPickHeadFile = new File(gitDir, Constants.CHERRY_PICK_HEAD);
- if (head != null) {
+ List<ObjectId> heads = (head != null) ? Collections.singletonList(head)
+ : null;
+ writeHeadsFile(heads, Constants.CHERRY_PICK_HEAD);
+ }
+
+ /**
+ * Read a file from the git directory.
+ *
+ * @param filename
+ * @return the raw contents or null if the file doesn't exist or is empty
+ * @throws IOException
+ */
+ private byte[] readGitDirectoryFile(String filename) throws IOException {
+ File file = new File(getDirectory(), filename);
+ try {
+ byte[] raw = IO.readFully(file);
+ return raw.length > 0 ? raw : null;
+ } catch (FileNotFoundException notFound) {
+ return null;
+ }
+ }
+
+ /**
+ * Write the given heads to a file in the git directory.
+ *
+ * @param heads
+ * a list of object ids to write or null if the file should be
+ * deleted.
+ * @param filename
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ private void writeHeadsFile(List<ObjectId> heads, String filename)
+ throws FileNotFoundException, IOException {
+ File headsFile = new File(getDirectory(), filename);
+ if (heads != null) {
BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(cherryPickHeadFile));
+ new FileOutputStream(headsFile));
try {
- head.copyTo(bos);
- bos.write('\n');
+ for (ObjectId id : heads) {
+ id.copyTo(bos);
+ bos.write('\n');
+ }
} finally {
bos.close();
}
} else {
- FileUtils.delete(cherryPickHeadFile, FileUtils.SKIP_MISSING);
+ FileUtils.delete(headsFile, FileUtils.SKIP_MISSING);
}
}
}

Back to the top