public Response apply()

in java/com/google/gerrit/server/restapi/project/CreateBranch.java [83:195]


  public Response<BranchInfo> apply(ProjectResource rsrc, IdString id, BranchInput input)
      throws BadRequestException, AuthException, ResourceConflictException, IOException,
          PermissionBackendException, NoSuchProjectException {
    String ref = id.get();
    if (input == null) {
      input = new BranchInput();
    }
    if (input.ref != null && !ref.equals(input.ref)) {
      throw new BadRequestException("ref must match URL");
    }
    if (input.revision != null) {
      input.revision = input.revision.trim();
    }
    if (Strings.isNullOrEmpty(input.revision)) {
      input.revision = Constants.HEAD;
    }
    while (ref.startsWith("/")) {
      ref = ref.substring(1);
    }
    ref = RefNames.fullName(ref);
    if (!Repository.isValidRefName(ref)) {
      throw new BadRequestException("invalid branch name \"" + ref + "\"");
    }
    if (MagicBranch.isMagicBranch(ref)) {
      throw new BadRequestException(
          "not allowed to create branches under \""
              + MagicBranch.getMagicRefNamePrefix(ref)
              + "\"");
    }
    if (!isBranchAllowed(ref)) {
      throw new BadRequestException(
          "Cannot create a branch with name \""
              + ref
              + "\". Not allowed to create branches under Gerrit internal or tags refs.");
    }

    BranchNameKey name = BranchNameKey.create(rsrc.getNameKey(), ref);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
      ObjectId revid = RefUtil.parseBaseRevision(repo, rsrc.getNameKey(), input.revision);
      RevWalk rw = RefUtil.verifyConnected(repo, revid);
      RevObject object = rw.parseAny(revid);

      if (ref.startsWith(Constants.R_HEADS)) {
        // Ensure that what we start the branch from is a commit. If we
        // were given a tag, dereference to the commit instead.
        //
        object = rw.parseCommit(object);
      }

      createRefControl.checkCreateRef(identifiedUser, repo, name, object);

      RefUpdate u = repo.updateRef(ref);
      u.setExpectedOldObjectId(ObjectId.zeroId());
      u.setNewObjectId(object.copy());
      u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
      u.setRefLogMessage("created via REST from " + input.revision, false);
      refCreationValidator.validateRefOperation(rsrc.getName(), identifiedUser.get(), u);
      RefUpdate.Result result = u.update(rw);
      switch (result) {
        case FAST_FORWARD:
        case NEW:
        case NO_CHANGE:
          referenceUpdated.fire(
              name.project(), u, ReceiveCommand.Type.CREATE, identifiedUser.get().state());
          break;
        case LOCK_FAILURE:
          if (repo.getRefDatabase().exactRef(ref) != null) {
            throw new ResourceConflictException("branch \"" + ref + "\" already exists");
          }
          String refPrefix = RefUtil.getRefPrefix(ref);
          while (!Constants.R_HEADS.equals(refPrefix)) {
            if (repo.getRefDatabase().exactRef(refPrefix) != null) {
              throw new ResourceConflictException(
                  "Cannot create branch \""
                      + ref
                      + "\" since it conflicts with branch \""
                      + refPrefix
                      + "\".");
            }
            refPrefix = RefUtil.getRefPrefix(refPrefix);
          }
          throw new LockFailureException(String.format("Failed to create %s", ref), u);
        case FORCED:
        case IO_FAILURE:
        case NOT_ATTEMPTED:
        case REJECTED:
        case REJECTED_CURRENT_BRANCH:
        case RENAMED:
        case REJECTED_MISSING_OBJECT:
        case REJECTED_OTHER_REASON:
        default:
          throw new IOException(String.format("Failed to create %s: %s", ref, result.name()));
      }

      BranchInfo info = new BranchInfo();
      info.ref = ref;
      info.revision = revid.getName();

      if (isConfigRef(name.branch())) {
        // Never allow to delete the meta config branch.
        info.canDelete = null;
      } else {
        info.canDelete =
            permissionBackend.currentUser().ref(name).testOrFalse(RefPermission.DELETE)
                    && rsrc.getProjectState().statePermitsWrite()
                ? true
                : null;
      }
      return Response.created(info);
    } catch (RefUtil.InvalidRevisionException e) {
      throw new BadRequestException("invalid revision \"" + input.revision + "\"", e);
    }
  }