From 6ba06c111ba9557cab73cda0d8053d150951556b Mon Sep 17 00:00:00 2001 From: Marek Zawirski Date: Sat, 16 Aug 2008 15:45:52 +0200 Subject: [PATCH] Extract Transport findRemoteRefUpdatesFor() as static method This method could be used outside of specific URI scope, so let it be static. Otherwise, if someone want to generate remote ref updates from refspecs he/she may have to create some dummy transport just for that. Signed-off-by: Marek Zawirski --- .../src/org/spearce/jgit/transport/Transport.java | 147 +++++++++++++-------- 1 file changed, 90 insertions(+), 57 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java b/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java index 98853e61..e986e485 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java @@ -211,7 +211,92 @@ public abstract class Transport { throw new NotSupportedException("URI not supported: " + remote); } + + /** + * Convert push remote refs update specification from {@link RefSpec} form + * to {@link RemoteRefUpdate}. Conversion expands wildcards by matching + * source part to local refs. expectedOldObjectId in RemoteRefUpdate is + * always set as null. Tracking branch is configured if RefSpec destination + * matches source of any fetch ref spec for this transport remote + * configuration. + * + * @param db + * local database. + * @param specs + * collection of RefSpec to convert. + * @param fetchSpecs + * fetch specifications used for finding localtracking refs. May + * be null or empty collection. + * @return collection of set up {@link RemoteRefUpdate}. + * @throws TransportException + * when problem occurred during conversion or specification set + * up: most probably, missing objects or refs. + */ + public static Collection findRemoteRefUpdatesFor( + final Repository db, final Collection specs, + Collection fetchSpecs) throws TransportException { + if (fetchSpecs == null) + fetchSpecs = Collections.emptyList(); + final List result = new LinkedList(); + final Collection procRefs = expandPushWildcardsFor(db, specs); + + for (final RefSpec spec : procRefs) { + try { + final String srcRef = spec.getSource(); + // null destination (no-colon in ref-spec) is a special case + final String remoteName = (spec.getDestination() == null ? spec + .getSource() : spec.getDestination()); + final boolean forceUpdate = spec.isForceUpdate(); + final String localName = findTrackingRefName(remoteName, + fetchSpecs); + + final RemoteRefUpdate rru = new RemoteRefUpdate(db, srcRef, + remoteName, forceUpdate, localName, null); + result.add(rru); + } catch (TransportException x) { + throw x; + } catch (Exception x) { + throw new TransportException( + "Problem with resolving push ref spec \"" + spec + + "\" locally: " + x.getMessage(), x); + } + } + return result; + } + + private static Collection expandPushWildcardsFor( + final Repository db, final Collection specs) { + final Map localRefs = db.getAllRefs(); + final Collection procRefs = new HashSet(); + for (final RefSpec spec : specs) { + if (spec.isWildcard()) { + for (final Ref localRef : localRefs.values()) { + if (spec.matchSource(localRef)) + procRefs.add(spec.expandFromSource(localRef)); + } + } else { + procRefs.add(spec); + } + } + return procRefs; + } + + private static String findTrackingRefName(final String remoteName, + final Collection fetchSpecs) { + // try to find matching tracking refs + for (final RefSpec fetchSpec : fetchSpecs) { + if (fetchSpec.matchSource(remoteName)) { + if (fetchSpec.isWildcard()) + return fetchSpec.expandFromSource(remoteName) + .getDestination(); + else + return fetchSpec.getDestination(); + } + } + return null; + } + /** * Default setting for {@link #fetchThin} option. */ @@ -573,7 +658,10 @@ public abstract class Transport { * always set as null. Tracking branch is configured if RefSpec destination * matches source of any fetch ref spec for this transport remote * configuration. - * + *

+ * Conversion is performed for context of this transport (database, fetch + * specifications). + * * @param specs * collection of RefSpec to convert. * @return collection of set up {@link RemoteRefUpdate}. @@ -583,30 +671,7 @@ public abstract class Transport { */ public Collection findRemoteRefUpdatesFor( final Collection specs) throws TransportException { - final List result = new LinkedList(); - final Collection procRefs = expandPushWildcardsFor(specs); - - for (final RefSpec spec : procRefs) { - try { - final String srcRef = spec.getSource(); - // null destination (no-colon in ref-spec) is a special case - final String remoteName = (spec.getDestination() == null ? spec - .getSource() : spec.getDestination()); - final boolean forceUpdate = spec.isForceUpdate(); - final String localName = findTrackingRefName(remoteName); - - final RemoteRefUpdate rru = new RemoteRefUpdate(local, srcRef, - remoteName, forceUpdate, localName, null); - result.add(rru); - } catch (TransportException x) { - throw x; - } catch (Exception x) { - throw new TransportException( - "Problem with resolving push ref spec \"" + spec - + "\" locally: " + x.getMessage(), x); - } - } - return result; + return findRemoteRefUpdatesFor(local, specs, fetch); } /** @@ -642,36 +707,4 @@ public abstract class Transport { * any open file handles used to read the "remote" repository. */ public abstract void close(); - - private Collection expandPushWildcardsFor( - final Collection specs) { - final Map localRefs = local.getAllRefs(); - final Collection procRefs = new HashSet(); - - for (final RefSpec spec : specs) { - if (spec.isWildcard()) { - for (final Ref localRef : localRefs.values()) { - if (spec.matchSource(localRef)) - procRefs.add(spec.expandFromSource(localRef)); - } - } else { - procRefs.add(spec); - } - } - return procRefs; - } - - private String findTrackingRefName(final String remoteName) { - // try to find matching tracking refs - for (final RefSpec fetchSpec : fetch) { - if (fetchSpec.matchSource(remoteName)) { - if (fetchSpec.isWildcard()) - return fetchSpec.expandFromSource(remoteName) - .getDestination(); - else - return fetchSpec.getDestination(); - } - } - return null; - } } -- 2.11.4.GIT