From 9057f1bc4679f93535f6ae13a716c41b4ece2630 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 9 Mar 2009 23:11:19 +0100 Subject: [PATCH] Fix DWIMery for push to handle non-existant source refs Instead of a StringIndexOutOfBoundsException we now get an error telling us that the ref could not be resolved. Found-by: Robin Rosenberg Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../src/org/spearce/jgit/transport/Transport.java | 45 ++++++++++++---------- 1 file changed, 24 insertions(+), 21 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 a0a25751..1068f50f 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java @@ -244,29 +244,32 @@ public abstract class Transport { final Collection procRefs = expandPushWildcardsFor(db, specs); for (final RefSpec spec : procRefs) { - String srcRef = spec.getSource(); - final Ref src = db.getRef(srcRef); - if (src != null) - srcRef = src.getName(); - String remoteName = spec.getDestination(); - // null destination (no-colon in ref-spec) is a special case - if (remoteName == null) { - remoteName = srcRef; - } else { - if (!remoteName.startsWith(Constants.R_REFS)) { - // null source is another special case (delete) - if (srcRef != null) { - // assume the same type of ref at the destination - String srcPrefix = srcRef.substring(0, srcRef.indexOf('/', Constants.R_REFS.length())); - remoteName = srcPrefix + "/" + remoteName; - } - } + String srcSpec = spec.getSource(); + final Ref srcRef = db.getRef(srcSpec); + if (srcRef != null) + srcSpec = srcRef.getName(); + + String destSpec = spec.getDestination(); + if (destSpec == null) { + // No destination (no-colon in ref-spec), DWIMery assumes src + // + destSpec = srcSpec; } - final boolean forceUpdate = spec.isForceUpdate(); - final String localName = findTrackingRefName(remoteName, fetchSpecs); - final RemoteRefUpdate rru = new RemoteRefUpdate(db, srcRef, - remoteName, forceUpdate, localName, null); + if (srcRef != null && !destSpec.startsWith(Constants.R_REFS)) { + // Assume the same kind of ref at the destination, e.g. + // "refs/heads/foo:master", DWIMery assumes master is also + // under "refs/heads/". + // + final String n = srcRef.getName(); + final int kindEnd = n.indexOf('/', Constants.R_REFS.length()); + destSpec = n.substring(0, kindEnd + 1) + destSpec; + } + + final boolean forceUpdate = spec.isForceUpdate(); + final String localName = findTrackingRefName(destSpec, fetchSpecs); + final RemoteRefUpdate rru = new RemoteRefUpdate(db, srcSpec, + destSpec, forceUpdate, localName, null); result.add(rru); } return result; -- 2.11.4.GIT