From 30f9d25d77ccb5cd978d4cf8fa389ec819e90e95 Mon Sep 17 00:00:00 2001
From: Philip L. McMahon <philip.l.mcmahon@gmail.com>
Date: Fri, 27 Jan 2012 02:02:19 -0500
Subject: [PATCH] Correct update of HEAD symbolic reference when target is a tag.

---
 src/com/gitblit/utils/JGitUtils.java |   28 ++++++++++++++++++++++------
 1 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java
index 0750b07..05c0852 100644
--- a/src/com/gitblit/utils/JGitUtils.java
+++ b/src/com/gitblit/utils/JGitUtils.java
@@ -1190,12 +1190,28 @@
 	 */
 	public static void setDefaultHead(Repository repository, Ref ref) {
 		try {
-			RefUpdate head = repository.updateRef(Constants.HEAD);
-			RefUpdate.Result result = head.link(ref.getName());
-			LOGGER.debug(MessageFormat.format("Set repository {0} default head to {1} ({2})",
-					repository.getDirectory().getAbsolutePath(), ref.getName(), result));
-		} catch (IOException e) {
-			LOGGER.error("Failed to set default head!", e);
+			boolean detach = !ref.getName().startsWith(Constants.R_HEADS); // detach if not a branch
+			RefUpdate.Result result;
+			RefUpdate head = repository.updateRef(Constants.HEAD, detach);
+			if (detach) { // Tag
+				RevCommit commit = getCommit(repository, ref.getObjectId().getName());
+				head.setNewObjectId(commit.getId());
+				result = head.forceUpdate();
+			} else {
+				result = head.link(ref.getName());
+			}
+			switch (result) {
+			case NEW:
+			case FORCED:
+			case NO_CHANGE:
+			case FAST_FORWARD:
+				break;
+			default:
+				LOGGER.error(MessageFormat.format("{0} failed to set default head to {1} ({2})",
+						repository.getDirectory().getAbsolutePath(), ref.getName(), result));
+			}
+		} catch (Throwable t) {
+			error(t, repository, "{0} failed to set default head to {1}", ref.getName());
 		}
 	}
 

--
Gitblit v1.9.1