use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getSubtreeItems.
@Override
public List<String> getSubtreeItems(String site, String path) {
final List<String> retItems = new ArrayList<String>();
String rootPath;
if (path.endsWith(FILE_SEPARATOR + INDEX_FILE)) {
int lastIdx = path.lastIndexOf(FILE_SEPARATOR + INDEX_FILE);
rootPath = path.substring(0, lastIdx);
} else {
rootPath = path;
}
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
RevTree tree = helper.getTreeForLastCommit(repo);
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(rootPath), tree)) {
if (tw != null) {
// Loop for all children and gather path of item excluding the item, file/folder name, and
// whether or not it's a folder
ObjectLoader loader = repo.open(tw.getObjectId(0));
if (loader.getType() == OBJ_TREE) {
tw.enterSubtree();
tw.setRecursive(true);
while (tw.next()) {
String name = tw.getNameString();
String childPath = FILE_SEPARATOR + tw.getPathString();
if (!ArrayUtils.contains(IGNORE_FILES, name) && !childPath.equals(path)) {
retItems.add(childPath);
}
}
tw.close();
} else {
logger.debug("Object is not tree for site: " + site + " path: " + path + " - it does not have children");
}
} else {
String gitPath = helper.getGitPath(rootPath);
if (StringUtils.isEmpty(gitPath) || gitPath.equals(".")) {
try (TreeWalk treeWalk = new TreeWalk(repo)) {
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
while (treeWalk.next()) {
String name = treeWalk.getNameString();
String childPath = FILE_SEPARATOR + treeWalk.getPathString();
if (!ArrayUtils.contains(IGNORE_FILES, name) && !childPath.equals(path)) {
retItems.add(childPath);
}
}
} catch (IOException e) {
logger.error("Error while getting children for site: " + site + " path: " + path, e);
}
}
}
} catch (IOException e) {
logger.error("Error while getting children for site: " + site + " path: " + path, e);
}
} catch (IOException | CryptoException e) {
logger.error("Failed to create RevTree for site: " + site + " path: " + path, e);
}
return retItems;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method createSiteFromBlueprint.
@Override
public boolean createSiteFromBlueprint(String blueprintLocation, String site, String sandboxBranch, Map<String, String> params) {
boolean toReturn;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
// create git repository for site content
toReturn = helper.createSandboxRepository(site, sandboxBranch);
if (toReturn) {
// copy files from blueprint
toReturn = helper.copyContentFromBlueprint(blueprintLocation, site);
}
if (toReturn) {
// update site name variable inside config files
toReturn = helper.updateSiteNameConfigVar(site);
}
if (toReturn) {
toReturn = helper.replaceParameters(site, params);
}
if (toReturn) {
toReturn = helper.addGitIgnoreFile(site);
}
if (toReturn) {
// commit everything so it is visible
toReturn = helper.performInitialCommit(site, helper.getCommitMessage(REPO_INITIAL_COMMIT_COMMIT_MESSAGE), sandboxBranch);
}
} catch (CryptoException e) {
logger.error("Error creating site from blueprint", e);
toReturn = false;
} finally {
generalLockService.unlock(gitLockKey);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getDeploymentHistory.
@Override
public List<DeploymentSyncHistory> getDeploymentHistory(String site, List<String> environmentNames, ZonedDateTime fromDate, ZonedDateTime toDate, DmFilterWrapper dmFilterWrapper, String filterType, int numberOfItems) {
List<DeploymentSyncHistory> toRet = new ArrayList<DeploymentSyncHistory>();
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository publishedRepo = helper.getRepository(site, PUBLISHED);
if (Objects.nonNull(publishedRepo)) {
int counter = 0;
try (Git git = new Git(publishedRepo)) {
// List all environments
List<Ref> environments = git.branchList().call();
for (int i = 0; i < environments.size() && counter < numberOfItems; i++) {
Ref env = environments.get(i);
String environment = env.getName();
environment = environment.replace(R_HEADS, "");
if (environmentNames.contains(environment)) {
List<RevFilter> filters = new ArrayList<RevFilter>();
filters.add(CommitTimeRevFilter.after(fromDate.toInstant().toEpochMilli()));
filters.add(CommitTimeRevFilter.before(toDate.toInstant().toEpochMilli()));
filters.add(NotRevFilter.create(MessageRevFilter.create("Initial commit.")));
Iterable<RevCommit> branchLog = git.log().add(env.getObjectId()).setRevFilter(AndRevFilter.create(filters)).call();
Iterator<RevCommit> iterator = branchLog.iterator();
while (iterator.hasNext() && counter < numberOfItems) {
RevCommit revCommit = iterator.next();
List<String> files = helper.getFilesInCommit(publishedRepo, revCommit);
for (int j = 0; j < files.size() && counter < numberOfItems; j++) {
String file = files.get(j);
Path path = Paths.get(file);
String fileName = path.getFileName().toString();
if (!ArrayUtils.contains(IGNORE_FILES, fileName)) {
if (dmFilterWrapper.accept(site, file, filterType)) {
DeploymentSyncHistory dsh = new DeploymentSyncHistory();
dsh.setSite(site);
dsh.setPath(file);
dsh.setSyncDate(Instant.ofEpochSecond(revCommit.getCommitTime()).atZone(UTC));
dsh.setUser(revCommit.getAuthorIdent().getName());
dsh.setEnvironment(environment.replace(R_HEADS, ""));
toRet.add(dsh);
counter++;
}
}
}
}
}
}
git.close();
toRet.sort((o1, o2) -> o2.getSyncDate().compareTo(o1.getSyncDate()));
}
}
} catch (CryptoException | IOException | GitAPIException e) {
logger.error("Error while getting deployment history for site " + site, e);
}
return toRet;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getChangeSetPathsFromDelta.
@Override
public Map<String, String> getChangeSetPathsFromDelta(String site, String commitIdFrom, String commitIdTo) {
Map<String, String> changeSet = new TreeMap<>();
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repository != null) {
synchronized (repository) {
try {
// Get the sandbox repo, and then get a reference to the commitId we received and another for head
boolean fromEmptyRepo = StringUtils.isEmpty(commitIdFrom);
String firstCommitId = getRepoFirstCommitId(site);
if (fromEmptyRepo) {
commitIdFrom = firstCommitId;
}
Repository repo = helper.getRepository(site, SANDBOX);
ObjectId objCommitIdFrom = repo.resolve(commitIdFrom);
ObjectId objCommitIdTo = repo.resolve(commitIdTo);
try (Git git = new Git(repo)) {
if (fromEmptyRepo) {
CanonicalTreeParser firstCommitTreeParser = new CanonicalTreeParser();
// reset(reader, firstCommitTree.getId());
firstCommitTreeParser.reset();
// Diff the two commit Ids
List<DiffEntry> diffEntries = git.diff().setOldTree(firstCommitTreeParser).setNewTree(null).call();
// Now that we have a diff, let's itemize the file changes, pack them into a TO
// and add them to the list of RepoOperations to return to the caller
// also include date/time of commit by taking number of seconds and multiply by 1000 and
// convert to java date before sending over
changeSet = getChangeSetFromDiff(diffEntries);
}
// let's do it
if (!objCommitIdFrom.equals(objCommitIdTo)) {
// Compare HEAD with commitId we're given
// Get list of commits between commitId and HEAD in chronological order
RevTree fromTree = helper.getTreeForCommit(repo, objCommitIdFrom.getName());
RevTree toTree = helper.getTreeForCommit(repo, objCommitIdTo.getName());
if (fromTree != null && toTree != null) {
try (ObjectReader reader = repo.newObjectReader()) {
CanonicalTreeParser fromCommitTreeParser = new CanonicalTreeParser();
CanonicalTreeParser toCommitTreeParser = new CanonicalTreeParser();
fromCommitTreeParser.reset(reader, fromTree.getId());
toCommitTreeParser.reset(reader, toTree.getId());
// Diff the two commit Ids
List<DiffEntry> diffEntries = git.diff().setOldTree(fromCommitTreeParser).setNewTree(toCommitTreeParser).call();
// Now that we have a diff, let's itemize the file changes, pack them into a TO
// and add them to the list of RepoOperations to return to the caller
// also include date/time of commit by taking number of seconds and multiply by 1000 and
// convert to java date before sending over
changeSet = getChangeSetFromDiff(diffEntries);
}
}
}
} catch (GitAPIException e) {
logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
}
} catch (IOException e) {
logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
}
}
}
} catch (CryptoException e) {
logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
}
return changeSet;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper 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