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());
}
}
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());
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations