{"thread":{"id":"21105","subject":"[JGIT PATCH] Add support for logAllRefUpdates configuration parameter","startedAt":"2009-10-01T14:13:09Z","lastAt":"2009-10-01T14:13:09Z","messageCount":1,"participants":["Christian Halstrick"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"124083","messageId":"1254406389-7472-1-git-send-email-christian.halstrick@sap.com","threadId":"21105","inReplyTo":null,"subject":"[JGIT PATCH] Add support for logAllRefUpdates configuration parameter","fromName":"Christian Halstrick","fromEmail":"christian.halstrick@sap.com","sentAt":"2009-10-01T14:13:09Z","receivedAt":"2009-10-01T14:13:09Z","isPatch":true,"sender":{"key":"christian.halstrick@sap.com","avatar":"https://gravatar.com/avatar/032940727041870bc7374e44e8f659ac0a8f8d97d679b4c2cbd05bcb39b088bb?d=mp&s=160"},"body":"From: Christian Halstrick <christian.halstrick@sap.com>\n\nHonor the configuration parameter core.logAllRefUpdates when writing reflogs.\nInstead of writing reflog entries always only write reflogs if this parameter\nis set to true or if the corresponding file in the <git-dir>/logs directory\nalready exists. In other words: if you are updating a ref and this parameter is\nset to false and there is no file corresponding to your ref in the\n<git-dir>/logs folder then no reflog will be written.\n\nThis is a fix for the issue http://code.google.com/p/egit/issues/detail?id=4\n\nSigned-off-by: Matthias Sohn <matthias.sohn@sap.com>\nSigned-off-by: Christian Halstrick <christian.halstrick@sap.com>\n---\n .../tst/org/eclipse/jgit/lib/ReflogConfigTest.java |  116 ++++++++++++++++++++\n .../src/org/eclipse/jgit/lib/CoreConfig.java       |   10 ++\n .../src/org/eclipse/jgit/lib/RefLogWriter.java     |   18 ++--\n .../src/org/eclipse/jgit/lib/Repository.java       |    1 +\n 4 files changed, 137 insertions(+), 8 deletions(-)\n create mode 100644 org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java\n\ndiff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java\nnew file mode 100644\nindex 0000000..6021050\n--- /dev/null\n+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java\n@@ -0,0 +1,116 @@\n+/*\n+ * Copyright (C) 2009, Christian Halstrick, Matthias Sohn, SAP AG\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Eclipse Foundation, Inc. nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+package org.eclipse.jgit.lib;\n+\n+import java.io.IOException;\n+\n+public class ReflogConfigTest extends RepositoryTestCase {\n+\tpublic void testlogAllRefUpdates() throws Exception {\n+\t\tlong commitTime = 1154236443000L;\n+\t\tint tz = -4 * 60;\n+\t\tint nrOfReflogs;\n+\n+\t\t// check how many entries exist in the reflog and turn off writing\n+\t\t// reflogs\n+\t\tassertTrue(db.getConfig().getCore().isLogAllRefUpdates());\n+\t\tnrOfReflogs = db.getReflogReader(Constants.HEAD).getReverseEntries()\n+\t\t\t\t.size();\n+\t\tdb.getConfig().setBoolean(\"core\", null, \"logAllRefUpdates\", false);\n+\n+\t\t// do one commit and check that reflog size is 0: no reflogs should be\n+\t\t// written\n+\t\tfinal Tree t = new Tree(db);\n+\t\taddFileToTree(t, \"i-am-a-file\", \"and this is the data in me\\n\");\n+\t\tcommit(t, \"A Commit\\n\", new PersonIdent(jauthor, commitTime, tz),\n+\t\t\t\tnew PersonIdent(jcommitter, commitTime, tz));\n+\t\tcommitTime += 100;\n+\t\tassertTrue(\n+\t\t\t\t\"Reflog for HEAD should contain one entry\",\n+\t\t\t\tdb.getReflogReader(Constants.HEAD).getReverseEntries().size() == nrOfReflogs);\n+\n+\t\t// set the logAllRefUpdates parameter to true and check it\n+\t\tdb.getConfig().setBoolean(\"core\", null, \"logAllRefUpdates\", true);\n+\t\tassertTrue(db.getConfig().getCore().isLogAllRefUpdates());\n+\n+\t\t// do one commit and check that reflog size is increased\n+\t\taddFileToTree(t, \"i-am-another-file\", \"and this is other data in me\\n\");\n+\t\tcommit(t, \"A Commit\\n\", new PersonIdent(jauthor, commitTime, tz),\n+\t\t\t\tnew PersonIdent(jcommitter, commitTime, tz));\n+\t\tcommitTime += 100;\n+\t\tassertTrue(\n+\t\t\t\t\"Reflog for HEAD should contain one additional entry\",\n+\t\t\t\tdb.getReflogReader(Constants.HEAD).getReverseEntries().size() == nrOfReflogs + 1);\n+\n+\t\t// set the logAllRefUpdates parameter to false and check it\n+\t\tdb.getConfig().setBoolean(\"core\", null, \"logAllRefUpdates\", false);\n+\t\tassertFalse(db.getConfig().getCore().isLogAllRefUpdates());\n+\n+\t\t// do one commit and check that reflog size is 2\n+\t\taddFileToTree(t, \"i-am-anotheranother-file\",\n+\t\t\t\t\"and this is other other data in me\\n\");\n+\t\tcommit(t, \"A Commit\\n\", new PersonIdent(jauthor, commitTime, tz),\n+\t\t\t\tnew PersonIdent(jcommitter, commitTime, tz));\n+\t\tassertTrue(\n+\t\t\t\t\"Reflog for HEAD should contain two additional entries\",\n+\t\t\t\tdb.getReflogReader(Constants.HEAD).getReverseEntries().size() == nrOfReflogs + 2);\n+\t}\n+\n+\tprivate void addFileToTree(final Tree t, String filename, String content)\n+\t\t\tthrows IOException {\n+\t\tFileTreeEntry f = t.addFile(filename);\n+\t\twriteTrashFile(f.getName(), content);\n+\t\tt.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);\n+\t}\n+\n+\tprivate void commit(final Tree t, String commitMsg, PersonIdent author,\n+\t\t\tPersonIdent committer) throws IOException {\n+\t\tfinal Commit commit = new Commit(db);\n+\t\tcommit.setAuthor(author);\n+\t\tcommit.setCommitter(committer);\n+\t\tcommit.setMessage(commitMsg);\n+\t\tcommit.setTree(t);\n+\t\tObjectWriter writer = new ObjectWriter(db);\n+\t\tcommit.setCommitId(writer.writeCommit(commit));\n+\n+\t\tint nl = commitMsg.indexOf('\\n');\n+\t\tfinal RefUpdate ru = db.updateRef(Constants.HEAD);\n+\t\tru.setNewObjectId(commit.getCommitId());\n+\t\tru.setRefLogMessage(\"commit : \"\n+\t\t\t\t+ ((nl == -1) ? commitMsg : commitMsg.substring(0, nl)), false);\n+\t\tru.forceUpdate();\n+\t}\n+}\ndiff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CoreConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CoreConfig.java\nindex d44120a..a945894 100644\n--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CoreConfig.java\n+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CoreConfig.java\n@@ -57,9 +57,12 @@ public CoreConfig parse(final Config cfg) {\n \n \tprivate final int packIndexVersion;\n \n+\tprivate final boolean logAllRefUpdates;\n+\n \tprivate CoreConfig(final Config rc) {\n \t\tcompression = rc.getInt(\"core\", \"compression\", DEFAULT_COMPRESSION);\n \t\tpackIndexVersion = rc.getInt(\"pack\", \"indexversion\", 2);\n+\t\tlogAllRefUpdates = rc.getBoolean(\"core\", \"logAllRefUpdates\", true);\n \t}\n \n \t/**\n@@ -77,4 +80,11 @@ public int getCompression() {\n \tpublic int getPackIndexVersion() {\n \t\treturn packIndexVersion;\n \t}\n+\n+\t/**\n+\t * @return whether to log all refUpdates\n+\t */\n+\tpublic boolean isLogAllRefUpdates() {\n+\t\treturn logAllRefUpdates;\n+\t}\n }\ndiff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefLogWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefLogWriter.java\nindex 4141a13..a473c42 100644\n--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefLogWriter.java\n+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefLogWriter.java\n@@ -112,16 +112,18 @@ private static void appendOneRecord(final ObjectId oldId,\n \t\tfinal byte[] rec = Constants.encode(r.toString());\n \t\tfinal File logdir = new File(db.getDirectory(), Constants.LOGS);\n \t\tfinal File reflog = new File(logdir, refName);\n-\t\tfinal File refdir = reflog.getParentFile();\n+\t\tif (reflog.exists() || db.getConfig().getCore().isLogAllRefUpdates()) {\n+\t\t\tfinal File refdir = reflog.getParentFile();\n \n-\t\tif (!refdir.exists() && !refdir.mkdirs())\n-\t\t\tthrow new IOException(\"Cannot create directory \" + refdir);\n+\t\t\tif (!refdir.exists() && !refdir.mkdirs())\n+\t\t\t\tthrow new IOException(\"Cannot create directory \" + refdir);\n \n-\t\tfinal FileOutputStream out = new FileOutputStream(reflog, true);\n-\t\ttry {\n-\t\t\tout.write(rec);\n-\t\t} finally {\n-\t\t\tout.close();\n+\t\t\tfinal FileOutputStream out = new FileOutputStream(reflog, true);\n+\t\t\ttry {\n+\t\t\t\tout.write(rec);\n+\t\t\t} finally {\n+\t\t\t\tout.close();\n+\t\t\t}\n \t\t}\n \t}\n \ndiff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java\nindex f2451d4..1eb5895 100644\n--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java\n+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java\n@@ -187,6 +187,7 @@ public void create(boolean bare) throws IOException {\n \t\tcfg.setBoolean(\"core\", null, \"filemode\", true);\n \t\tif (bare)\n \t\t\tcfg.setBoolean(\"core\", null, \"bare\", true);\n+\t\tcfg.setBoolean(\"core\", null, \"logAllRefupdates\", !bare);\n \t\tcfg.save();\n \t}\n \n-- \n1.6.4.msysgit.0\n"}]}