{"thread":{"id":"17571","subject":"[JGIT PATCH 3/3] Record pushes done over anonymous git:// using peer IP","startedAt":"2009-02-04T23:15:40Z","lastAt":"2009-02-04T23:15:42Z","messageCount":3,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"103230","messageId":"1233789342-28079-1-git-send-email-spearce@spearce.org","threadId":"17571","inReplyTo":null,"subject":"[JGIT PATCH 1/3] Allow RefUpdate callers to control the PersonIdent recorded","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-02-04T23:15:40Z","receivedAt":"2009-02-04T23:15:40Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"In server applications we may want to ensure the remote user\nidentity is used within the reflog, rather than whatever the\nlocal repository might come up with.\n\nThis change introduces an optional RefLogIdent property of\nthe RefUpdate, settable by the caller to override any default\nguessing performed within the ref log writer.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/lib/RefLogWriter.java     |   71 ++++++++++++-------\n .../src/org/spearce/jgit/lib/RefUpdate.java        |   32 ++++++++-\n 2 files changed, 75 insertions(+), 28 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefLogWriter.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefLogWriter.java\nindex 6de8c0f..a077051 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefLogWriter.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefLogWriter.java\n@@ -42,7 +42,6 @@\n import java.io.File;\n import java.io.FileOutputStream;\n import java.io.IOException;\n-import java.io.PrintWriter;\n \n /**\n  * Utility class to add reflog entries\n@@ -50,6 +49,49 @@\n  * @author Dave Watson\n  */\n public class RefLogWriter {\n+\tstatic void append(final RefUpdate u, final String msg) throws IOException {\n+\t\tfinal ObjectId oldId = u.getOldObjectId();\n+\t\tfinal ObjectId newId = u.getNewObjectId();\n+\t\tfinal Repository db = u.getRepository();\n+\t\tfinal PersonIdent ident = u.getRefLogIdent();\n+\n+\t\tappendOneRecord(oldId, newId, ident, msg, db, u.getName());\n+\t}\n+\n+\tprivate static void appendOneRecord(final ObjectId oldId,\n+\t\t\tfinal ObjectId newId, PersonIdent ident, final String msg,\n+\t\t\tfinal Repository db, final String refName) throws IOException {\n+\t\tif (ident == null)\n+\t\t\tident = new PersonIdent(db);\n+\t\telse\n+\t\t\tident = new PersonIdent(ident);\n+\n+\t\tfinal StringBuilder r = new StringBuilder();\n+\t\tr.append(ObjectId.toString(oldId));\n+\t\tr.append(' ');\n+\t\tr.append(ObjectId.toString(newId));\n+\t\tr.append(' ');\n+\t\tr.append(ident.toExternalString());\n+\t\tr.append('\\t');\n+\t\tr.append(msg);\n+\t\tr.append('\\n');\n+\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+\n+\t\tif (!refdir.exists() && !refdir.mkdirs())\n+\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}\n+\t}\n+\n \t/**\n \t * Writes reflog entry for ref specified by refName\n \t * \n@@ -64,33 +106,10 @@\n \t * @param refName\n \t *            full ref name\n \t * @throws IOException\n+\t * @deprecated rely upon {@link RefUpdate}'s automatic logging instead.\n \t */\n \tpublic static void writeReflog(Repository repo, ObjectId oldCommit,\n \t\t\tObjectId commit, String message, String refName) throws IOException {\n-\t\tString entry = buildReflogString(repo, oldCommit, commit, message);\n-\n-\t\tFile directory = repo.getDirectory();\n-\t\tFile reflogfile = new File(directory, Constants.LOGS + \"/\" + refName);\n-\t\tFile reflogdir = reflogfile.getParentFile();\n-\t\tif (!reflogdir.exists())\n-\t\t\tif (!reflogdir.mkdirs())\n-\t\t\t\tthrow new IOException(\"Cannot create directory \"+reflogdir);\n-\t\tPrintWriter writer = new PrintWriter(new FileOutputStream(reflogfile, true));\n-\t\twriter.println(entry);\n-\t\twriter.close();\n+\t\tappendOneRecord(oldCommit, commit, null, message, repo, refName);\n \t}\n-\n-\tprivate static String buildReflogString(Repository repo,\n-\t\t\tObjectId oldCommit, ObjectId commit, String message) {\n-\t\tPersonIdent me = new PersonIdent(repo);\n-\t\tString initial = \"\";\n-\t\tif (oldCommit == null) {\n-\t\t\toldCommit = ObjectId.zeroId();\n-\t\t\tinitial = \" (initial)\";\n-\t\t}\n-\t\tString s = oldCommit.name() + \" \" + commit.name() + \" \"\n-\t\t\t\t+ me.toExternalString() + \"\\t\" + message + initial;\n-\t\treturn s;\n-\t}\n-\n }\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java\nindex 1417f2c..1ce1e71 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java\n@@ -140,6 +140,9 @@\n \t/** Does this specification ask for forced updated (rewind/reset)? */\n \tprivate boolean force;\n \n+\t/** Identity to record action as within the reflog. */\n+\tprivate PersonIdent refLogIdent;\n+\n \t/** Message the caller wants included in the reflog. */\n \tprivate String refLogMessage;\n \n@@ -164,6 +167,11 @@ RefUpdate(final RefDatabase r, final Ref ref, final File f) {\n \t\tlooseFile = f;\n \t}\n \n+\t/** @return the repository the updated ref resides in */\n+\tpublic Repository getRepository() {\n+\t\treturn db.getRepository();\n+\t}\n+\n \t/**\n \t * Get the name of the ref this update will operate on.\n \t * \n@@ -232,6 +240,27 @@ public void setForceUpdate(final boolean b) {\n \t\tforce = b;\n \t}\n \n+\t/** @return identity of the user making the change in the reflog. */\n+\tpublic PersonIdent getRefLogIdent() {\n+\t\treturn refLogIdent;\n+\t}\n+\n+\t/**\n+\t * Set the identity of the user appearing in the reflog.\n+\t * <p>\n+\t * The timestamp portion of the identity is ignored. A new identity with the\n+\t * current timestamp will be created automatically when the update occurs\n+\t * and the log record is written.\n+\t * \n+\t * @param pi\n+\t *            identity of the user. If null the identity will be\n+\t *            automatically determined based on the repository\n+\t *            configuration.\n+\t */\n+\tpublic void setRefLogIdent(final PersonIdent pi) {\n+\t\trefLogIdent = pi;\n+\t}\n+\n \t/**\n \t * Get the message to include in the reflog.\n \t * \n@@ -450,8 +479,7 @@ else if (status == Result.FAST_FORWARD)\n \t\t\telse if (status == Result.NEW)\n \t\t\t\tmsg += \": created\";\n \t\t}\n-\t\tRefLogWriter.writeReflog(db.getRepository(), oldValue, newValue, msg,\n-\t\t\t\tgetName());\n+\t\tRefLogWriter.append(this, msg);\n \t\tif (!lock.commit())\n \t\t\treturn Result.LOCK_FAILURE;\n \t\tdb.stored(this.ref.getOrigName(),  ref.getName(), newValue, lock.getCommitLastModified());\n-- \n1.6.1.2.492.g8554a\n"},{"id":"103229","messageId":"1233789342-28079-2-git-send-email-spearce@spearce.org","threadId":"17571","inReplyTo":"1233789342-28079-1-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 2/3] Allow the reflog identity to be set in ReceivePack","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-02-04T23:15:41Z","receivedAt":"2009-02-04T23:15:41Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Daemons which run ReceivePack can now set the identity used to\nrecord updates in the reflog of any affected refs.  If not set\nthen it defaults to the repository owner's identity, or that\nof the running JVM.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../org/spearce/jgit/transport/ReceivePack.java    |   26 ++++++++++++++++++++\n 1 files changed, 26 insertions(+), 0 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java\nindex bb6bf26..ba09366 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java\n@@ -57,6 +57,7 @@\n import org.spearce.jgit.lib.Constants;\n import org.spearce.jgit.lib.NullProgressMonitor;\n import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.PersonIdent;\n import org.spearce.jgit.lib.Ref;\n import org.spearce.jgit.lib.RefComparator;\n import org.spearce.jgit.lib.RefUpdate;\n@@ -94,6 +95,9 @@\n \t/** Should an incoming transfer permit non-fast-forward requests? */\n \tprivate boolean allowNonFastForwards;\n \n+\t/** Identity to record action as within the reflog. */\n+\tprivate PersonIdent refLogIdent;\n+\n \t/** Hook to validate the update commands before execution. */\n \tprivate PreReceiveHook preReceive;\n \n@@ -221,6 +225,27 @@ public void setAllowNonFastForwards(final boolean canRewind) {\n \t\tallowNonFastForwards = canRewind;\n \t}\n \n+\t/** @return identity of the user making the changes in the reflog. */\n+\tpublic PersonIdent getRefLogIdent() {\n+\t\treturn refLogIdent;\n+\t}\n+\n+\t/**\n+\t * Set the identity of the user appearing in the affected reflogs.\n+\t * <p>\n+\t * The timestamp portion of the identity is ignored. A new identity with the\n+\t * current timestamp will be created automatically when the updates occur\n+\t * and the log records are written.\n+\t * \n+\t * @param pi\n+\t *            identity of the user. If null the identity will be\n+\t *            automatically determined based on the repository\n+\t *            configuration.\n+\t */\n+\tpublic void setRefLogIdent(final PersonIdent pi) {\n+\t\trefLogIdent = pi;\n+\t}\n+\n \t/** @return get the hook invoked before updates occur. */\n \tpublic PreReceiveHook getPreReceiveHook() {\n \t\treturn preReceive;\n@@ -645,6 +670,7 @@ private void executeCommands() {\n \tprivate void execute(final ReceiveCommand cmd) {\n \t\ttry {\n \t\t\tfinal RefUpdate ru = db.updateRef(cmd.getRefName());\n+\t\t\tru.setRefLogIdent(getRefLogIdent());\n \t\t\tswitch (cmd.getType()) {\n \t\t\tcase DELETE:\n \t\t\t\tif (!ObjectId.zeroId().equals(cmd.getOldId())) {\n-- \n1.6.1.2.492.g8554a\n"},{"id":"103228","messageId":"1233789342-28079-3-git-send-email-spearce@spearce.org","threadId":"17571","inReplyTo":"1233789342-28079-2-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 3/3] Record pushes done over anonymous git:// using peer IP","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-02-04T23:15:42Z","receivedAt":"2009-02-04T23:15:42Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"The only data we have on the remote user on the other side of\na git:// network connection is their IP address/hostname.  So\nwe list that in the reflog as it is better than no data at all.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/transport/Daemon.java     |    9 +++++++++\n 1 files changed, 9 insertions(+), 0 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java b/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java\nindex 646c88d..5087533 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java\n@@ -43,6 +43,7 @@\n import java.io.IOException;\n import java.io.InputStream;\n import java.io.InterruptedIOException;\n+import java.net.InetAddress;\n import java.net.InetSocketAddress;\n import java.net.ServerSocket;\n import java.net.Socket;\n@@ -53,6 +54,7 @@\n import java.util.Map;\n import java.util.regex.Pattern;\n \n+import org.spearce.jgit.lib.PersonIdent;\n import org.spearce.jgit.lib.Repository;\n \n /** Basic daemon for the anonymous <code>git://</code> transport protocol. */\n@@ -118,8 +120,15 @@ protected void execute(final DaemonClient dc,\n \t\t\t\t\t@Override\n \t\t\t\t\tprotected void execute(final DaemonClient dc,\n \t\t\t\t\t\t\tfinal Repository db) throws IOException {\n+\t\t\t\t\t\tfinal InetAddress peer = dc.getRemoteAddress();\n+\t\t\t\t\t\tString host = peer.getCanonicalHostName();\n+\t\t\t\t\t\tif (host == null)\n+\t\t\t\t\t\t\thost = peer.getHostAddress();\n \t\t\t\t\t\tfinal ReceivePack rp = new ReceivePack(db);\n \t\t\t\t\t\tfinal InputStream in = dc.getInputStream();\n+\t\t\t\t\t\tfinal String name = \"anonymous\";\n+\t\t\t\t\t\tfinal String email = name + \"@\" + host;\n+\t\t\t\t\t\trp.setRefLogIdent(new PersonIdent(name, email));\n \t\t\t\t\t\trp.receive(in, dc.getOutputStream(), null);\n \t\t\t\t\t}\n \t\t\t\t} };\n-- \n1.6.1.2.492.g8554a\n"}]}