use of org.locationtech.geogig.api.ObjectId in project GeoGig by boundlessgeo.
the class CommitOp method _call.
/**
* Executes the commit operation.
*
* @return the commit just applied, or {@code null} if
* {@code getProgressListener().isCanceled()}
* @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
* @throws NothingToCommitException if there are no staged changes by comparing the index
* staging tree and the repository HEAD tree.
*/
@Override
protected RevCommit _call() throws RuntimeException {
final String committer = resolveCommitter();
final String committerEmail = resolveCommitterEmail();
final String author = resolveAuthor();
final String authorEmail = resolveAuthorEmail();
final Long authorTime = getAuthorTimeStamp();
final Long committerTime = getCommitterTimeStamp();
final Integer authorTimeZoneOffset = getAuthorTimeZoneOffset();
final Integer committerTimeZoneOffset = getCommitterTimeZoneOffset();
getProgressListener().started();
float writeTreeProgress = 99f;
if (all) {
writeTreeProgress = 50f;
AddOp op = command(AddOp.class);
for (String st : pathFilters) {
op.addPattern(st);
}
op.setUpdateOnly(true).setProgressListener(subProgress(49f)).call();
}
if (getProgressListener().isCanceled()) {
return null;
}
final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
checkState(currHead.isPresent(), "Repository has no HEAD, can't commit");
final Ref headRef = currHead.get();
checkState(//
headRef instanceof SymRef, "HEAD is in a dettached state, cannot commit. Create a branch from it before committing");
final String currentBranch = ((SymRef) headRef).getTarget();
final ObjectId currHeadCommitId = headRef.getObjectId();
Supplier<RevTree> oldRoot = resolveOldRoot();
if (!currHeadCommitId.isNull()) {
if (amend) {
RevCommit headCommit = command(RevObjectParse.class).setObjectId(currHeadCommitId).call(RevCommit.class).get();
parents.addAll(headCommit.getParentIds());
if (message == null || message.isEmpty()) {
message = headCommit.getMessage();
}
RevTree commitTree = command(RevObjectParse.class).setObjectId(headCommit.getTreeId()).call(RevTree.class).get();
oldRoot = Suppliers.ofInstance(commitTree);
} else {
parents.add(0, currHeadCommitId);
}
} else {
Preconditions.checkArgument(!amend, "Cannot amend. There is no previous commit to amend");
}
// additional operations in case we are committing after a conflicted merge
final Optional<Ref> mergeHead = command(RefParse.class).setName(Ref.MERGE_HEAD).call();
if (mergeHead.isPresent()) {
ObjectId mergeCommitId = mergeHead.get().getObjectId();
if (!mergeCommitId.isNull()) {
parents.add(mergeCommitId);
}
if (message == null) {
message = command(ReadMergeCommitMessageOp.class).call();
}
}
ObjectId newTreeId;
{
WriteTree2 writeTree = command(WriteTree2.class);
writeTree.setOldRoot(oldRoot).setProgressListener(subProgress(writeTreeProgress));
if (!pathFilters.isEmpty()) {
writeTree.setPathFilter(pathFilters);
}
newTreeId = writeTree.call();
}
if (getProgressListener().isCanceled()) {
return null;
}
final ObjectId currentRootTreeId = command(ResolveTreeish.class).setTreeish(currHeadCommitId).call().or(RevTree.EMPTY_TREE_ID);
if (currentRootTreeId.equals(newTreeId)) {
if (!allowEmpty) {
throw new NothingToCommitException("Nothing to commit after " + currHeadCommitId);
}
}
final RevCommit commit;
if (this.commit == null) {
CommitBuilder cb = new CommitBuilder();
cb.setAuthor(author);
cb.setAuthorEmail(authorEmail);
cb.setCommitter(committer);
cb.setCommitterEmail(committerEmail);
cb.setMessage(message);
cb.setParentIds(parents);
cb.setTreeId(newTreeId);
cb.setCommitterTimestamp(committerTime);
cb.setAuthorTimestamp(authorTime);
cb.setCommitterTimeZoneOffset(committerTimeZoneOffset);
cb.setAuthorTimeZoneOffset(authorTimeZoneOffset);
commit = cb.build();
} else {
CommitBuilder cb = new CommitBuilder(this.commit);
cb.setParentIds(parents);
cb.setTreeId(newTreeId);
cb.setCommitterTimestamp(committerTime);
cb.setCommitterTimeZoneOffset(committerTimeZoneOffset);
if (message != null) {
cb.setMessage(message);
}
commit = cb.build();
}
if (getProgressListener().isCanceled()) {
return null;
}
final ObjectDatabase objectDb = objectDatabase();
objectDb.put(commit);
// set the HEAD pointing to the new commit
final Optional<Ref> branchHead = command(UpdateRef.class).setName(currentBranch).setNewValue(commit.getId()).call();
checkState(commit.getId().equals(branchHead.get().getObjectId()));
final Optional<Ref> newHead = command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();
checkState(currentBranch.equals(((SymRef) newHead.get()).getTarget()));
Optional<ObjectId> treeId = command(ResolveTreeish.class).setTreeish(branchHead.get().getObjectId()).call();
checkState(treeId.isPresent());
checkState(newTreeId.equals(treeId.get()));
getProgressListener().setProgress(100f);
getProgressListener().complete();
// TODO: maybe all this "heads cleaning" should be put in an independent operation
if (mergeHead.isPresent()) {
command(UpdateRef.class).setDelete(true).setName(Ref.MERGE_HEAD).call();
command(UpdateRef.class).setDelete(true).setName(Ref.ORIG_HEAD).call();
}
final Optional<Ref> cherrypickHead = command(RefParse.class).setName(Ref.CHERRY_PICK_HEAD).call();
if (cherrypickHead.isPresent()) {
command(UpdateRef.class).setDelete(true).setName(Ref.CHERRY_PICK_HEAD).call();
command(UpdateRef.class).setDelete(true).setName(Ref.ORIG_HEAD).call();
}
return commit;
}
use of org.locationtech.geogig.api.ObjectId in project GeoGig by boundlessgeo.
the class BlameOp method _call.
@Override
protected BlameReport _call() {
String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
if (!id.isPresent()) {
throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
}
TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
if (!type.equals(TYPE.FEATURE)) {
throw new BlameException(StatusCode.PATH_NOT_FEATURE);
}
Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
BlameReport report = new BlameReport(featureType.get());
Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
RevCommit commit = log.next();
RevObjectParse revObjectParse = command(RevObjectParse.class);
DiffOp diffOp = command(DiffOp.class);
DiffFeature diffFeature = command(DiffFeature.class);
while (!report.isComplete()) {
if (!log.hasNext()) {
String refSpec = commit.getId().toString() + ":" + path;
RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
report.setFirstVersion(feature, commit);
break;
}
RevCommit commitB = log.next();
Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (path.equals(diff.newPath())) {
if (diff.isAdd()) {
String refSpec = commit.getId().toString() + ":" + path;
RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
report.setFirstVersion(feature, commit);
break;
}
FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
while (iter.hasNext()) {
PropertyDescriptor key = iter.next();
Optional<?> value = attribDiffs.get(key).getNewValue();
String attribute = key.getName().toString();
report.addDiff(attribute, value, commit);
}
}
}
commit = commitB;
}
return report;
}
use of org.locationtech.geogig.api.ObjectId in project GeoGig by boundlessgeo.
the class BranchCreateOp method _call.
protected Ref _call() {
checkState(branchName != null, "branch name was not provided");
final String branchRefPath = Ref.append(Ref.HEADS_PREFIX, branchName);
checkArgument(force || !command(RefParse.class).setName(branchRefPath).call().isPresent(), "A branch named '" + branchName + "' already exists.");
Optional<Ref> branchRef;
if (orphan) {
branchRef = command(UpdateRef.class).setName(branchRefPath).setNewValue(ObjectId.NULL).call();
} else {
final String branchOrigin = Optional.fromNullable(commit_ish).or(Ref.HEAD);
final ObjectId branchOriginCommitId = resolveOriginCommitId(branchOrigin);
branchRef = command(UpdateRef.class).setName(branchRefPath).setNewValue(branchOriginCommitId).call();
checkState(branchRef.isPresent());
}
if (checkout) {
command(CheckoutOp.class).setSource(branchRefPath).call();
}
return branchRef.get();
}
use of org.locationtech.geogig.api.ObjectId in project GeoGig by boundlessgeo.
the class BranchCreateOp method resolveOriginCommitId.
private ObjectId resolveOriginCommitId(String branchOrigin) {
Optional<Ref> ref = command(RefParse.class).setName(branchOrigin).call();
if (ref.isPresent()) {
ObjectId commitId = ref.get().getObjectId();
checkArgument(!commitId.isNull(), branchOrigin + " has no commits yet, branch cannot be created.");
return commitId;
}
Optional<ObjectId> objectId = command(RevParse.class).setRefSpec(branchOrigin).call();
checkArgument(objectId.isPresent(), branchOrigin + " does not resolve to a repository object");
ObjectId commitId = objectId.get();
TYPE objectType = command(ResolveObjectType.class).setObjectId(commitId).call();
checkArgument(TYPE.COMMIT.equals(objectType), branchOrigin + " does not resolve to a commit: " + objectType);
return commitId;
}
use of org.locationtech.geogig.api.ObjectId in project GeoGig by boundlessgeo.
the class FetchOp method updateLocalRef.
private Ref updateLocalRef(Ref remoteRef, Remote remote, ImmutableSet<Ref> localRemoteRefs) {
final String refName;
if (remoteRef.getName().startsWith(Ref.TAGS_PREFIX)) {
refName = remoteRef.getName();
} else {
refName = Ref.REMOTES_PREFIX + remote.getName() + "/" + remoteRef.localName();
}
Ref updatedRef = remoteRef;
if (remoteRef instanceof SymRef) {
String targetBranch = Ref.localName(((SymRef) remoteRef).getTarget());
String newTarget = Ref.REMOTES_PREFIX + remote.getName() + "/" + targetBranch;
command(UpdateSymRef.class).setName(refName).setNewValue(newTarget).call();
} else {
ObjectId effectiveId = remoteRef.getObjectId();
if (remote.getMapped() && !repository().commitExists(remoteRef.getObjectId())) {
effectiveId = graphDatabase().getMapping(effectiveId);
updatedRef = new Ref(remoteRef.getName(), effectiveId);
}
command(UpdateRef.class).setName(refName).setNewValue(effectiveId).call();
}
return updatedRef;
}
Aggregations