private PrimaryVersionSpec resolvePrimaryVersionSpec(
      ProjectHistory projectHistory, PrimaryVersionSpec versionSpec)
      throws InvalidVersionSpecException {
    final int index = versionSpec.getIdentifier();
    final String branch = versionSpec.getBranch();
    final int versions = projectHistory.getVersions().size();
    if (0 > index || index >= versions || branch == null) {
      throw new InvalidVersionSpecException(
          MessageFormat.format(Messages.VersionSubInterfaceImpl_InvalidVersionRequested, index));
    }

    if (branch.equals(VersionSpec.GLOBAL)) {
      return projectHistory.getVersions().get(index).getPrimarySpec();
    }

    // Get biggest primary version of given branch which is equal or lower
    // to the given versionSpec
    for (int i = index; i >= 0; i--) {
      final Version version = projectHistory.getVersions().get(i);
      if (branch.equals(version.getPrimarySpec().getBranch())) {
        return version.getPrimarySpec();
      }
    }
    throw new InvalidVersionSpecException(Messages.VersionSubInterfaceImpl_PrimaryVersionNotFound);
  }
 private Version getVersion(ProjectHistory projectHistory, PrimaryVersionSpec baseVersionSpec) {
   if (0 > baseVersionSpec.getIdentifier()
       || baseVersionSpec.getIdentifier() > projectHistory.getVersions().size() - 1) {
     return null;
   }
   final Version version = projectHistory.getVersions().get(baseVersionSpec.getIdentifier());
   if (version == null || !version.getPrimarySpec().equals(baseVersionSpec)) {
     return null;
   }
   return version;
 }
  private PrimaryVersionSpec resolvePagedUpdateVersionSpec(
      ProjectHistory projectHistory, PagedUpdateVersionSpec baseVersion) {

    int changes = 0;
    PrimaryVersionSpec resolvedSpec = baseVersion.getBaseVersionSpec();
    int maxChanges = baseVersion.getMaxChanges();

    int i = resolvedSpec.getIdentifier();

    AbstractChangePackage cp = projectHistory.getVersions().get(i).getChanges();

    if (i == projectHistory.getVersions().size() - 1) {
      return projectHistory.getVersions().get(i).getPrimarySpec();
    }

    do {
      cp = projectHistory.getVersions().get(++i).getChanges();
    } while (cp == null && i < projectHistory.getVersions().size());

    // pull at least one change package
    if (cp.leafSize() > maxChanges) {
      maxChanges = cp.leafSize();
    }

    while (changes < maxChanges && i < projectHistory.getVersions().size()) {
      resolvedSpec = projectHistory.getVersions().get(i).getPrimarySpec();
      final Version version = projectHistory.getVersions().get(i);
      final AbstractChangePackage changePackage = version.getChanges();

      if (changePackage != null) {
        final int size = changePackage.leafSize();
        if (changes + size >= maxChanges) {
          resolvedSpec = projectHistory.getVersions().get(i).getPrimarySpec();
          break;
        }
        changes += size;
      }
      i += 1;
    }

    return resolvedSpec;
  }
  private BranchInfo createNewBranch(
      ProjectHistory projectHistory,
      PrimaryVersionSpec baseSpec,
      PrimaryVersionSpec primarySpec,
      BranchVersionSpec branch) {
    primarySpec.setBranch(branch.getBranch());

    final BranchInfo branchInfo = VersioningFactory.eINSTANCE.createBranchInfo();
    branchInfo.setName(branch.getBranch());
    branchInfo.setSource(ModelUtil.clone(baseSpec));
    branchInfo.setHead(ModelUtil.clone(primarySpec));

    projectHistory.getBranches().add(branchInfo);

    return branchInfo;
  }
  /**
   * Returns a list of versions starting from source and ending with target. This method returns the
   * version always in an ascanding order. So if you need it ordered differently you have to reverse
   * the list.
   *
   * @param projectId project id
   * @param source source
   * @param target target
   * @return list of versions
   * @throws ESException if source or target are out of range or any other problem occurs
   */
  protected List<Version> getVersions(
      ProjectId projectId, PrimaryVersionSpec source, PrimaryVersionSpec target)
      throws ESException {
    if (source.compareTo(target) < 1) {
      final ProjectHistory projectHistory =
          getSubInterface(ProjectSubInterfaceImpl.class).getProject(projectId);

      final Version sourceVersion = getVersion(projectHistory, source);
      final Version targetVersion = getVersion(projectHistory, target);

      if (sourceVersion == null || targetVersion == null) {
        throw new InvalidVersionSpecException(Messages.VersionSubInterfaceImpl_NoSourceNorTarget);
      }
      final List<Version> result = new ArrayList<Version>();

      // since the introduction of branches the versions are collected
      // in different order.
      Version currentVersion = targetVersion;
      while (currentVersion != null) {
        result.add(currentVersion);
        if (currentVersion.equals(sourceVersion)) {
          break;
        }
        if (currentVersion.getPrimarySpec().compareTo(sourceVersion.getPrimarySpec()) < 0) {
          // walked too far, invalid path.
          throw new InvalidVersionSpecException(Messages.VersionSubInterfaceImpl_InvalidPath);
        }
        // Shortcut for most common merge usecase: If you have 2
        // parallel branches and merge several times
        // from the one branch into the another.
        if (currentVersion.getMergedFromVersion().contains(sourceVersion)) {
          // add sourceVersion because #getChanges always removes
          // the first version
          result.add(sourceVersion);
          break;
        }

        currentVersion = findNextVersion(currentVersion);
      }
      // versions are collected in descending order, so the result has to be reversed.
      Collections.reverse(result);
      return result;
    }

    return getVersions(projectId, target, source);
  }
 private Version performRegularCommit(
     PrimaryVersionSpec baseVersionSpec,
     LogMessage logMessage,
     final ACUser user,
     final ProjectHistory projectHistory,
     final BranchInfo baseBranch,
     final Version baseVersion,
     final Project newProjectState)
     throws ESUpdateRequiredException, ESException {
   Version newVersion;
   // If branch is null or branch equals base branch, create new
   // version for specific branch
   if (!baseVersionSpec.equals(isHeadOfBranch(projectHistory, baseVersion.getPrimarySpec()))) {
     throw new ESUpdateRequiredException();
   }
   newVersion = createVersion(projectHistory, newProjectState, logMessage, user, baseVersion);
   newVersion.setPreviousVersion(baseVersion);
   baseBranch.setHead(ModelUtil.clone(newVersion.getPrimarySpec()));
   return newVersion;
 }
  /**
   * Returns all changes within the specified version range for a given project.
   *
   * @param projectId the ID of a project
   * @param source the source version
   * @param target the target version (inclusive)
   * @return a list of change packages containing all the changes for the specified version range
   * @throws InvalidVersionSpecException if an invalid version has been specified
   * @throws ESException in case of failure
   */
  @ESMethod(MethodId.GETCHANGES)
  public List<AbstractChangePackage> getChanges(
      ProjectId projectId, VersionSpec source, VersionSpec target)
      throws InvalidVersionSpecException, ESException {

    sanityCheckObjects(projectId, source, target);
    final PrimaryVersionSpec resolvedSource = resolveVersionSpec(projectId, source);
    final PrimaryVersionSpec resolvedTarget = resolveVersionSpec(projectId, target);
    // if target and source are equal return empty list
    if (resolvedSource.getIdentifier() == resolvedTarget.getIdentifier()) {
      return new ArrayList<AbstractChangePackage>();
    }

    synchronized (getMonitor()) {
      final boolean updateForward = resolvedTarget.getIdentifier() > resolvedSource.getIdentifier();

      // Example: if you want the changes to get from version 5 to 7, you
      // need the changes contained in version 6
      // and 7. The reason is that each version holds the changes which
      // occurred from the predecessor to the
      // version itself. Version 5 holds the changes to get from version 4
      // to 5 and therefore is irrelevant.
      // For that reason the first version is removed, since getVersions
      // always sorts ascending order.
      final List<Version> versions = getVersions(projectId, resolvedSource, resolvedTarget);
      if (versions.size() > 1) {
        versions.remove(0);
      }

      List<AbstractChangePackage> result = new ArrayList<AbstractChangePackage>();
      for (final Version version : versions) {
        final AbstractChangePackage changes = version.getChanges();
        if (changes != null) {
          changes.setLogMessage(ModelUtil.clone(version.getLogMessage()));
          result.add(changes);
        }
      }

      // if source is after target in time
      if (!updateForward) {
        // reverse list and change packages
        final List<AbstractChangePackage> resultReverse = new ArrayList<AbstractChangePackage>();
        for (final AbstractChangePackage changePackage : result) {

          final ChangePackage changePackageReverse =
              VersioningFactory.eINSTANCE.createChangePackage();
          final ESCloseableIterable<AbstractOperation> reversedOperations =
              changePackage.reversedOperations();
          final ArrayList<AbstractOperation> copiedReversedOperations =
              new ArrayList<AbstractOperation>();
          try {
            for (final AbstractOperation op : reversedOperations.iterable()) {
              copiedReversedOperations.add(op.reverse());
            }
          } finally {
            reversedOperations.close();
          }

          for (final AbstractOperation reversedOperation : copiedReversedOperations) {
            changePackageReverse.add(reversedOperation);
          }

          // copy again log message
          // reverse() created a new change package without copying
          // existent attributes
          changePackageReverse.setLogMessage(ModelUtil.clone(changePackage.getLogMessage()));
          resultReverse.add(changePackageReverse);
        }

        Collections.reverse(resultReverse);
        result = resultReverse;
      }

      return result;
    }
  }