| | |
| | | }
|
| | |
|
| | | /**
|
| | | * Returns the default HEAD for a repository. Normally returns the ref HEAD points to, but if HEAD points to nothing
|
| | | * it returns null.
|
| | | *
|
| | | * @param repository
|
| | | * @return the refmodel for HEAD or null
|
| | | */
|
| | | public static RefModel getDefaultHead(Repository repository) {
|
| | | RefModel ref = null;
|
| | | try {
|
| | | Ref head = repository.getRef(Constants.HEAD);
|
| | | if (head != null) {
|
| | | Ref target = head.getTarget();
|
| | | RevWalk rw = new RevWalk(repository);
|
| | | ObjectId targetId = target.getObjectId();
|
| | | if (targetId != null) {
|
| | | RevObject object = rw.parseAny(targetId);
|
| | | ref = new RefModel(target.getName(), target, object);
|
| | | }
|
| | | rw.dispose();
|
| | | }
|
| | | } catch (Throwable t) {
|
| | | LOGGER.error("Failed to get default head!", t);
|
| | | }
|
| | | return ref;
|
| | | }
|
| | |
|
| | | /**
|
| | | * Sets the default HEAD symbolic ref for a repository.
|
| | | *
|
| | | * @param repository
|
| | | * @param ref
|
| | | */
|
| | | public static void setDefaultHead(Repository repository, Ref ref) {
|
| | | try {
|
| | | 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());
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * Returns all refs grouped by their associated object id.
|
| | | *
|
| | | * @param repository
|