use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitRepositoryHelper method performInitialCommit.
/**
* Perform an initial commit after large changes to a site. Will not work against the global config repo.
* @param site
* @param message
* @return true if successful, false otherwise
*/
public boolean performInitialCommit(String site, String message, String sandboxBranch) {
boolean toReturn = true;
Repository repo = getRepository(site, GitRepositories.SANDBOX, sandboxBranch);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try (Git git = new Git(repo)) {
Status status = git.status().call();
if (status.hasUncommittedChanges() || !status.isClean()) {
DirCache dirCache = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS).call();
CommitCommand commitCommand = git.commit().setMessage(message);
String username = securityService.getCurrentUser();
User user = userServiceInternal.getUserByIdOrUsername(-1, username);
if (Objects.nonNull(user)) {
commitCommand = commitCommand.setAuthor(getAuthorIdent(user));
}
RevCommit commit = commitCommand.call();
// TODO: SJ: Do we need the commit id?
// commitId = commit.getName();
}
checkoutSandboxBranch(site, repo, sandboxBranch);
git.close();
} catch (GitAPIException | UserNotFoundException | ServiceLayerException err) {
logger.error("error creating initial commit for site: " + site, err);
toReturn = false;
} finally {
generalLockService.unlock(gitLockKey);
}
return toReturn;
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class AbstractUpgradeOperation method commitAllChanges.
protected void commitAllChanges(String site) throws UpgradeException {
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
Path repositoryPath = getRepositoryPath(site);
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(repositoryPath.toFile()).readEnvironment().findGitDir().build();
try (Git git = new Git(repo)) {
git.add().addFilepattern(".").call();
Status status = git.status().call();
if (status.hasUncommittedChanges() || !status.isClean()) {
git.commit().setAll(true).setMessage(getCommitMessage()).call();
}
}
} catch (IOException | GitAPIException e) {
throw new UpgradeException("Error committing changes for site " + site, e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class SiteRepositoryUpgradePipelineImpl method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(final String site) throws UpgradeException {
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
clusterSandboxRepoSyncTask.execute(site);
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(site, GitRepositories.SANDBOX);
String sandboxBranch = siteSandboxBranch;
if (repository != null) {
Git git = new Git(repository);
try {
if (!isEmpty()) {
SiteFeed siteFeed = siteService.getSite(site);
if (!StringUtils.isEmpty(siteFeed.getSandboxBranch())) {
sandboxBranch = siteFeed.getSandboxBranch();
}
createTemporaryBranch(site, git);
checkoutBranch(siteUpgradeBranch, git);
super.execute(site);
checkoutBranch(sandboxBranch, git);
mergeTemporaryBranch(repository, git);
deleteTemporaryBranch(git);
}
} catch (GitAPIException | IOException | SiteNotFoundException e) {
throw new UpgradeException("Error branching or merging upgrade branch for site " + site, e);
} finally {
if (!isEmpty()) {
try {
checkoutBranch(sandboxBranch, git);
} catch (GitAPIException e) {
logger.error("Error cleaning up repo for site " + site, e);
}
}
git.close();
}
}
} catch (CryptoException e) {
throw new UpgradeException("Unexpected error upgrading site " + site, e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
Aggregations