git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH] Evaluate short refnames into full names during push

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Mar 7, 2009, 22:18 UTC
Message-ID
<1236464299-11491-1-git-send-email-robin.rosenberg@dewire.com>
In-Reply-To
<20090307211008.GP16213@spearce.org>

With this we can use short names like master instead of refs/heads/master when pushing. This is slightly more convenient. Pushing a delete still requires the long format.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../src/org/spearce/jgit/lib/Repository.java       |   11 ++++++++++
 .../src/org/spearce/jgit/transport/Transport.java  |   21 +++++++++++++++++--
 2 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index 30bd4a3..3ab51b1 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -928,6 +928,17 @@ public String getBranch() throws IOException {
 	}
 	
 	/**
+	 * Get a ref by name.
+	 * 
+	 * @param name
+	 * @return the Ref with the given name, or null if it does not exist
+	 * @throws IOException
+	 */
+	public Ref getRef(final String name) throws IOException {
+		return refs.readRef(name);
+	}
+
+	/**
 	 * @return all known refs (heads, tags, remotes).
 	 */
 	public Map<String, Ref> getAllRefs() {
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 3aec5ca..64745a8 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Transport.java
@@ -51,6 +51,7 @@
 
 import org.spearce.jgit.errors.NotSupportedException;
 import org.spearce.jgit.errors.TransportException;
+import org.spearce.jgit.lib.Constants;
 import org.spearce.jgit.lib.NullProgressMonitor;
 import org.spearce.jgit.lib.ProgressMonitor;
 import org.spearce.jgit.lib.Ref;
@@ -243,10 +244,24 @@ else if (TransportLocal.canHandle(remote))
 		final Collection<RefSpec> procRefs = expandPushWildcardsFor(db, specs);
 
 		for (final RefSpec spec : procRefs) {
-			final String srcRef = spec.getSource();
+			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
-			final String remoteName = (spec.getDestination() == null ? spec
-					.getSource() : spec.getDestination());
+			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;
+					}
+				}
+			}
 			final boolean forceUpdate = spec.isForceUpdate();
 			final String localName = findTrackingRefName(remoteName, fetchSpecs);
 
-- 
1.6.1.285.g35d8b
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 4 of 8 in “[JGit] Push to new Amazon S3 does not work? ("funny refname")”
  1. Daniel ChengMar 7, 2009
  2. Robin RosenbergMar 7, 2009
  3. Shawn O. PearceMar 7, 2009
  4. Evaluate short refnames into full names during pushRobin Rosenberg, Mar 7, 2009
  5. Shawn O. PearceMar 7, 2009
  6. Prevent an exception if the user tries to push a non-existing ref.Robin Rosenberg, Mar 8, 2009
  7. Shawn O. PearceMar 9, 2009
  8. Robin RosenbergMar 9, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.