Re: [EGIT PATCH] Prevent an exception if the user tries to push a non-existing ref.
Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> Instead of a StringIndexOutOfBoundsException we now get an error telling
> us that the ref could not be resolved.
Show 13 quoted lines
> 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 a0a2575..8a25213 100644
> --- a/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java
> +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java
> @@ -255,7 +255,7 @@ else if (TransportLocal.canHandle(remote))
> } else {
> if (!remoteName.startsWith(Constants.R_REFS)) {
> // null source is another special case (delete)
> - if (srcRef != null) {
> + if (src != null) {
> // assume the same type of ref at the destination
> String srcPrefix = srcRef.substring(0, srcRef.indexOf('/', Constants.R_REFS.length()));
> remoteName = srcPrefix + "/" + remoteName;After reading that code again, I'm tempted to apply this instead. Its a much larger patch, but I think the result is a lot easier to follow.
--8<-- 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 <robin.rosenberg@dewire.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
.../src/org/spearce/jgit/transport/Transport.java | 45 ++++++++++---------
1 files 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 a0a2575..1068f50 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 @@ else if (TransportLocal.canHandle(remote))
final Collection<RefSpec> 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;
--
1.6.2.185.g8b635
--
Shawn.