Search in sources :

Example 1 with MeveoUser

use of org.meveo.security.MeveoUser in project meveo by meveo-org.

the class GitClient method commit.

/**
 * Stage the files correspondings to the given pattern and create commit
 *
 * @param gitRepository Repository to commit
 * @param patterns      Pattern of the files to stage
 * @param message       Commit message
 * @throws UserNotAuthorizedException if user does not have write access to the repository
 * @throws BusinessException          if repository cannot be open or commited
 * @throws IllegalArgumentException   if pattern list is empty
 */
public void commit(GitRepository gitRepository, List<String> patterns, String message) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasWriteRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        final AddCommand add = git.add();
        if (CollectionUtils.isNotEmpty(patterns)) {
            patterns.forEach(add::addFilepattern);
            add.call();
            final Status status = git.status().call();
            final RmCommand rm = git.rm();
            boolean doRm = false;
            for (String missing : status.getMissing()) {
                if (patterns.contains(missing) || patterns.contains(".")) {
                    rm.addFilepattern(missing);
                    doRm = true;
                }
            }
            if (doRm) {
                rm.call();
            }
            if (status.hasUncommittedChanges()) {
                try {
                    RevCommit commit = git.commit().setMessage(message).setAuthor(user.getUserName(), user.getMail()).setAllowEmpty(false).setCommitter(user.getUserName(), user.getMail()).call();
                    Set<String> modifiedFiles = new HashSet<>();
                    modifiedFiles.addAll(status.getAdded());
                    modifiedFiles.addAll(status.getChanged());
                    modifiedFiles.addAll(status.getModified());
                    modifiedFiles.addAll(status.getRemoved());
                    List<DiffEntry> diffs = getDiffs(gitRepository, commit);
                    commitedEvent.fire(new CommitEvent(gitRepository, modifiedFiles, diffs));
                } catch (EmptyCommitException e) {
                // NOOP
                }
            }
        } else {
            throw new IllegalArgumentException("Cannot commit repository " + gitRepository.getCode() + " : no staged files");
        }
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } catch (GitAPIException e) {
        throw new BusinessException("Cannot create commit on repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : Status(org.eclipse.jgit.api.Status) EmptyCommitException(org.eclipse.jgit.api.errors.EmptyCommitException) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) CommitEvent(org.meveo.event.qualifier.git.CommitEvent) MeveoUser(org.meveo.security.MeveoUser) RmCommand(org.eclipse.jgit.api.RmCommand) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit) HashSet(java.util.HashSet) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 2 with MeveoUser

use of org.meveo.security.MeveoUser in project meveo by meveo-org.

the class GitClient method createGitMeveoFolder.

protected void createGitMeveoFolder(GitRepository gitRepository, File repoDir) throws BusinessException {
    MeveoUser user = currentUser.get();
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.init().setDirectory(repoDir).call()) {
        // Init repo with a dummy commit
        new File(repoDir, "README.md").createNewFile();
        git.add().addFilepattern(".").call();
        git.commit().setMessage("First commit").setAuthor(user.getUserName(), user.getMail()).setCommitter(user.getUserName(), user.getMail()).call();
    } catch (GitAPIException | IOException e) {
        repoDir.delete();
        throw new BusinessException("Error initating repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Example 3 with MeveoUser

use of org.meveo.security.MeveoUser in project meveo by meveo-org.

the class GitClient method getHeadCommit.

/**
 * Return the head commit of a repository
 *
 * @param gitRepository {@link GitRepository} to retrieve head commit from
 * @return the head commit
 * @throws BusinessException if we cannot read the repositories branches
 */
public RevCommit getHeadCommit(GitRepository gitRepository) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasReadRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        try (RevWalk rw = new RevWalk(git.getRepository())) {
            ObjectId head = git.getRepository().resolve(Constants.HEAD);
            return rw.parseCommit(head);
        }
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) ObjectId(org.eclipse.jgit.lib.ObjectId) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File)

Example 4 with MeveoUser

use of org.meveo.security.MeveoUser in project meveo by meveo-org.

the class GitClient method reset.

/**
 * Reset a repository to a given commit
 *
 * @param gitRepository {@link GitRepository} to reset
 * @param commit        Commit to reset onto
 */
public void reset(GitRepository gitRepository, RevCommit commit) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasWriteRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(commit.getId().getName()).call();
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } catch (GitAPIException e) {
        throw new BusinessException("Cannot reset repository to commit " + commit.getId().getName(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Example 5 with MeveoUser

use of org.meveo.security.MeveoUser in project meveo by meveo-org.

the class GitClient method currentBranch.

/**
 * Retrieve the name of the current branch of the repository
 *
 * @param gitRepository Repository to get branch name from
 * @return current branch name
 * @throws BusinessException          if repository cannot be opened
 * @throws UserNotAuthorizedException if user does not have read access to the repository
 */
public String currentBranch(GitRepository gitRepository) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasReadRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        return git.getRepository().getBranch();
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Aggregations

MeveoUser (org.meveo.security.MeveoUser)27 BusinessException (org.meveo.admin.exception.BusinessException)21 File (java.io.File)17 IOException (java.io.IOException)16 Git (org.eclipse.jgit.api.Git)14 UserNotAuthorizedException (org.meveo.admin.exception.UserNotAuthorizedException)14 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)11 ArrayList (java.util.ArrayList)6 TransactionAttribute (javax.ejb.TransactionAttribute)6 List (java.util.List)4 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4 SubListCreator (org.meveo.admin.async.SubListCreator)4 Date (java.util.Date)3 Interceptors (javax.interceptor.Interceptors)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)3 NoSuchObjectLocalException (javax.ejb.NoSuchObjectLocalException)2 EmptyCommitException (org.eclipse.jgit.api.errors.EmptyCommitException)2