*/\r
public static void setDefaultHead(Repository repository, Ref ref) {\r
try {\r
- RefUpdate head = repository.updateRef(Constants.HEAD);\r
- RefUpdate.Result result = head.link(ref.getName());\r
- LOGGER.debug(MessageFormat.format("Set repository {0} default head to {1} ({2})",\r
- repository.getDirectory().getAbsolutePath(), ref.getName(), result));\r
- } catch (IOException e) {\r
- LOGGER.error("Failed to set default head!", e);\r
+ boolean detach = !ref.getName().startsWith(Constants.R_HEADS); // detach if not a branch\r
+ RefUpdate.Result result;\r
+ RefUpdate head = repository.updateRef(Constants.HEAD, detach);\r
+ if (detach) { // Tag\r
+ RevCommit commit = getCommit(repository, ref.getObjectId().getName());\r
+ head.setNewObjectId(commit.getId());\r
+ result = head.forceUpdate();\r
+ } else {\r
+ result = head.link(ref.getName());\r
+ }\r
+ switch (result) {\r
+ case NEW:\r
+ case FORCED:\r
+ case NO_CHANGE:\r
+ case FAST_FORWARD:\r
+ break;\r
+ default:\r
+ LOGGER.error(MessageFormat.format("{0} failed to set default head to {1} ({2})",\r
+ repository.getDirectory().getAbsolutePath(), ref.getName(), result));\r
+ }\r
+ } catch (Throwable t) {\r
+ error(t, repository, "{0} failed to set default head to {1}", ref.getName());\r
}\r
}\r
\r