git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>,
	Marek Zawirski <marek.zawirski@gmail.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 14/21] Extract readPackedRefs from TransportSftp for reuse
Date: Sun, 29 Jun 2008 03:59:24 -0400	[thread overview]
Message-ID: <1214726371-93520-15-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1214726371-93520-14-git-send-email-spearce@spearce.org>

Other dumb transports may need this functionality available to them.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/transport/TransportHttp.java  |    5 ++
 .../org/spearce/jgit/transport/TransportSftp.java  |   47 ++--------------
 .../src/org/spearce/jgit/transport/URIish.java     |   22 ++++++++
 .../jgit/transport/WalkRemoteObjectDatabase.java   |   58 ++++++++++++++++++++
 4 files changed, 91 insertions(+), 41 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportHttp.java b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportHttp.java
index 4655950..2f28f2c 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportHttp.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportHttp.java
@@ -119,6 +119,11 @@ class TransportHttp extends WalkTransport {
 		}
 
 		@Override
+		URIish getURI() {
+			return new URIish(objectsUrl);
+		}
+
+		@Override
 		Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
 			try {
 				return readAlternates(INFO_HTTP_ALTERNATES);
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java
index e5db6cc..a33406b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java
@@ -193,6 +193,11 @@ class TransportSftp extends WalkTransport {
 		}
 
 		@Override
+		URIish getURI() {
+			return uri.setPath(objectsPath);
+		}
+
+		@Override
 		Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
 			try {
 				return readAlternates(INFO_ALTERNATES);
@@ -355,52 +360,12 @@ class TransportSftp extends WalkTransport {
 
 		Map<String, Ref> readAdvertisedRefs() throws TransportException {
 			final TreeMap<String, Ref> avail = new TreeMap<String, Ref>();
-			try {
-				final BufferedReader br = openReader(PACKED_REFS);
-				try {
-					readPackedRefs(avail, br);
-				} finally {
-					br.close();
-				}
-			} catch (FileNotFoundException notPacked) {
-				// Perhaps it wasn't worthwhile, or is just an older repository.
-			} catch (IOException e) {
-				throw new TransportException(uri, "error in packed-refs", e);
-			}
+			readPackedRefs(avail);
 			readRef(avail, "../HEAD", "HEAD");
 			readLooseRefs(avail, "../refs", "refs/");
 			return avail;
 		}
 
-		private void readPackedRefs(final TreeMap<String, Ref> avail,
-				final BufferedReader br) throws IOException {
-			Ref last = null;
-			for (;;) {
-				String line = br.readLine();
-				if (line == null)
-					break;
-				if (line.charAt(0) == '#')
-					continue;
-				if (line.charAt(0) == '^') {
-					if (last == null)
-						throw new TransportException("Peeled line before ref.");
-					final ObjectId id = ObjectId.fromString(line + 1);
-					last = new Ref(Ref.Storage.PACKED, last.getName(), last
-							.getObjectId(), id);
-					avail.put(last.getName(), last);
-					continue;
-				}
-
-				final int sp = line.indexOf(' ');
-				if (sp < 0)
-					throw new TransportException("Unrecognized ref: " + line);
-				final ObjectId id = ObjectId.fromString(line.substring(0, sp));
-				final String name = line.substring(sp + 1);
-				last = new Ref(Ref.Storage.PACKED, name, id);
-				avail.put(last.getName(), last);
-			}
-		}
-
 		private void readLooseRefs(final TreeMap<String, Ref> avail,
 				final String dir, final String prefix)
 				throws TransportException {
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/URIish.java b/org.spearce.jgit/src/org/spearce/jgit/transport/URIish.java
index 307b591..9e7ca83 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/URIish.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/URIish.java
@@ -39,6 +39,7 @@
 package org.spearce.jgit.transport;
 
 import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -100,6 +101,27 @@ public class URIish {
 		}
 	}
 
+	/**
+	 * Construct a URIish from a standard URL.
+	 * 
+	 * @param u
+	 *            the source URL to convert from.
+	 */
+	public URIish(final URL u) {
+		scheme = u.getProtocol();
+		path = u.getPath();
+
+		final String ui = u.getUserInfo();
+		if (ui != null) {
+			final int d = ui.indexOf(':');
+			user = d < 0 ? ui : ui.substring(0, d);
+			pass = d < 0 ? null : ui.substring(d + 1);
+		}
+
+		port = u.getPort();
+		host = u.getHost();
+	}
+
 	/** Create an empty, non-configured URI. */
 	public URIish() {
 		// Configure nothing.
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/WalkRemoteObjectDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/transport/WalkRemoteObjectDatabase.java
index 57d525f..4f5a1cb 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/WalkRemoteObjectDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/WalkRemoteObjectDatabase.java
@@ -47,7 +47,9 @@ import java.io.OutputStream;
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Map;
 
+import org.spearce.jgit.errors.TransportException;
 import org.spearce.jgit.lib.Constants;
 import org.spearce.jgit.lib.ObjectId;
 import org.spearce.jgit.lib.Ref;
@@ -76,6 +78,8 @@ abstract class WalkRemoteObjectDatabase {
 
 	static final String PACKED_REFS = "../packed-refs";
 
+	abstract URIish getURI();
+
 	/**
 	 * Obtain the list of available packs (if any).
 	 * <p>
@@ -469,6 +473,60 @@ abstract class WalkRemoteObjectDatabase {
 		}
 	}
 
+	/**
+	 * Read a standard Git packed-refs file to discover known references.
+	 * 
+	 * @param avail
+	 *            return collection of references. Any existing entries will be
+	 *            replaced if they are found in the packed-refs file.
+	 * @throws TransportException
+	 *             an error occurred reading from the packed refs file.
+	 */
+	protected void readPackedRefs(final Map<String, Ref> avail)
+			throws TransportException {
+		try {
+			final BufferedReader br = openReader(PACKED_REFS);
+			try {
+				readPackedRefsImpl(avail, br);
+			} finally {
+				br.close();
+			}
+		} catch (FileNotFoundException notPacked) {
+			// Perhaps it wasn't worthwhile, or is just an older repository.
+		} catch (IOException e) {
+			throw new TransportException(getURI(), "error in packed-refs", e);
+		}
+	}
+
+	private void readPackedRefsImpl(final Map<String, Ref> avail,
+			final BufferedReader br) throws IOException {
+		Ref last = null;
+		for (;;) {
+			String line = br.readLine();
+			if (line == null)
+				break;
+			if (line.charAt(0) == '#')
+				continue;
+			if (line.charAt(0) == '^') {
+				if (last == null)
+					throw new TransportException("Peeled line before ref.");
+				final ObjectId id = ObjectId.fromString(line + 1);
+				last = new Ref(Ref.Storage.PACKED, last.getName(), last
+						.getObjectId(), id);
+				avail.put(last.getName(), last);
+				continue;
+			}
+
+			final int sp = line.indexOf(' ');
+			if (sp < 0)
+				throw new TransportException("Unrecognized ref: " + line);
+			final ObjectId id = ObjectId.fromString(line.substring(0, sp));
+			final String name = line.substring(sp + 1);
+			last = new Ref(Ref.Storage.PACKED, name, id);
+			avail.put(last.getName(), last);
+		}
+	}
+
 	static final class FileStream {
 		final InputStream in;
 
-- 
1.5.6.74.g8a5e

  reply	other threads:[~2008-06-29  8:02 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-29  7:59 [JGIT PATCH 00/21] Push support over SFTP and (encrypted) Amazon S3 Shawn O. Pearce
2008-06-29  7:59 ` [JGIT PATCH 01/21] Remove unused index files when WalkFetchConnection closes Shawn O. Pearce
2008-06-29  7:59   ` [JGIT PATCH 02/21] Do not show URIish passwords in TransportExceptions Shawn O. Pearce
2008-06-29  7:59     ` [JGIT PATCH 03/21] Use PackedObjectInfo as a base class for PackWriter's ObjectToPack Shawn O. Pearce
2008-06-29  7:59       ` [JGIT PATCH 04/21] Refactor PackWriter to hold onto the sorted object list Shawn O. Pearce
2008-06-29  7:59         ` [JGIT PATCH 05/21] Save the pack checksum after computing it in PackWriter Shawn O. Pearce
2008-06-29  7:59           ` [JGIT PATCH 06/21] Allow PackIndexWriter to use any subclass of PackedObjectInfo Shawn O. Pearce
2008-06-29  7:59             ` [JGIT PATCH 07/21] Allow PackWriter to create a corresponding index file Shawn O. Pearce
2008-06-29  7:59               ` [JGIT PATCH 08/21] Allow PackWriter to prepare object list and compute name before writing Shawn O. Pearce
2008-06-29  7:59                 ` [JGIT PATCH 09/21] Remember how a Ref was read in from disk and created Shawn O. Pearce
2008-06-29  7:59                   ` [JGIT PATCH 10/21] Simplify walker transport ref advertisement setup Shawn O. Pearce
2008-06-29  7:59                     ` [JGIT PATCH 11/21] Indicate the protocol jgit doesn't support push over Shawn O. Pearce
2008-06-29  7:59                       ` [JGIT PATCH 12/21] WalkTransport must allow subclasses to implement openPush Shawn O. Pearce
2008-06-29  7:59                         ` [JGIT PATCH 13/21] Support push over the sftp:// dumb transport Shawn O. Pearce
2008-06-29  7:59                           ` Shawn O. Pearce [this message]
2008-06-29  7:59                             ` [JGIT PATCH 15/21] Specialized byte array output stream for large files Shawn O. Pearce
2008-06-29  7:59                               ` [JGIT PATCH 16/21] Add Robert Harder's public domain Base64 encoding utility Shawn O. Pearce
2008-06-29  7:59                                 ` [JGIT PATCH 17/21] Misc. documentation fixes to Base64 utility Shawn O. Pearce
2008-06-29  7:59                                   ` [JGIT PATCH 18/21] Extract the basic HTTP proxy support to its own class Shawn O. Pearce
2008-06-29  7:59                                     ` [JGIT PATCH 19/21] Create a really simple Amazon S3 REST client Shawn O. Pearce
2008-06-29  7:59                                       ` [JGIT PATCH 20/21] Add client side encryption to Amazon S3 client library Shawn O. Pearce
2008-06-29  7:59                                         ` [JGIT PATCH 21/21] Bidirectional protocol support for Amazon S3 Shawn O. Pearce
2008-06-29 13:51                                 ` [JGIT PATCH 16/21] Add Robert Harder's public domain Base64 encoding utility Robin Rosenberg
2008-06-29 18:06                                   ` Shawn O. Pearce
2008-06-29 13:51                   ` [JGIT PATCH 09/21] Remember how a Ref was read in from disk and created Robin Rosenberg
2008-06-29 14:17                     ` Johannes Schindelin
2008-06-29 18:00                       ` Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1214726371-93520-15-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=marek.zawirski@gmail.com \
    --cc=robin.rosenberg@dewire.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).