use of org.locationtech.geogig.api.RevTree in project GeoGig by boundlessgeo.
the class OSMUnmapTest method setUp.
@Before
public void setUp() throws Exception {
ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, new UnsupportedTerminal());
cli = new GeogigCLI(consoleReader);
File workingDirectory = tempFolder.getRoot();
TestPlatform platform = new TestPlatform(workingDirectory);
GlobalContextBuilder.builder = new CLITestContextBuilder(platform);
cli.setPlatform(platform);
cli.execute("init");
cli.execute("config", "user.name", "Gabriel Roldan");
cli.execute("config", "user.email", "groldan@boundlessgeo.com");
assertTrue(new File(workingDirectory, ".geogig").exists());
// import with mapping
String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
File file = new File(filename);
String mappingFilename = OSMMap.class.getResource("nodes_mapping_with_aliases.json").getFile();
File mappingFile = new File(mappingFilename);
cli.execute("osm", "import", file.getAbsolutePath(), "--mapping", mappingFile.getAbsolutePath());
GeoGIG geogig = cli.newGeoGIG();
Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:busstops/507464799").call(RevFeature.class);
assertTrue(revFeature.isPresent());
geogig.getRepository().workingTree().delete("node");
Optional<RevTree> tree = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node").call(RevTree.class);
assertFalse(tree.isPresent());
geogig.close();
}
use of org.locationtech.geogig.api.RevTree in project GeoGig by boundlessgeo.
the class CheckoutOp method _call.
/**
* @return the id of the new work tree
*/
@Override
protected CheckoutResult _call() {
checkState(branchOrCommit != null || !paths.isEmpty(), "No branch, tree, or path were specified");
checkArgument(!(ours && theirs), "Cannot use both --ours and --theirs.");
checkArgument((ours == theirs) || branchOrCommit == null, "--ours/--theirs is incompatible with switching branches.");
CheckoutResult result = new CheckoutResult();
List<Conflict> conflicts = stagingDatabase().getConflicts(null, null);
if (!paths.isEmpty()) {
result.setResult(CheckoutResult.Results.UPDATE_OBJECTS);
Optional<RevTree> tree = Optional.absent();
List<String> unmerged = lookForUnmerged(conflicts, paths);
if (!unmerged.isEmpty()) {
if (!(force || ours || theirs)) {
StringBuilder msg = new StringBuilder();
for (String path : unmerged) {
msg.append("error: path " + path + " is unmerged.\n");
}
throw new CheckoutException(msg.toString(), StatusCode.UNMERGED_PATHS);
}
}
if (branchOrCommit != null) {
Optional<ObjectId> id = command(ResolveTreeish.class).setTreeish(branchOrCommit).call();
checkState(id.isPresent(), "'" + branchOrCommit + "' not found in repository.");
tree = command(RevObjectParse.class).setObjectId(id.get()).call(RevTree.class);
} else {
tree = Optional.of(index().getTree());
}
Optional<RevTree> mainTree = tree;
for (String st : paths) {
if (unmerged.contains(st)) {
if (ours || theirs) {
String refspec = ours ? Ref.ORIG_HEAD : Ref.MERGE_HEAD;
Optional<ObjectId> treeId = command(ResolveTreeish.class).setTreeish(refspec).call();
if (treeId.isPresent()) {
tree = command(RevObjectParse.class).setObjectId(treeId.get()).call(RevTree.class);
}
} else {
// --force
continue;
}
} else {
tree = mainTree;
}
Optional<NodeRef> node = command(FindTreeChild.class).setParent(tree.get()).setIndex(true).setChildPath(st).call();
if ((ours || theirs) && !node.isPresent()) {
// remove the node.
command(RemoveOp.class).addPathToRemove(st).call();
} else {
checkState(node.isPresent(), "pathspec '" + st + "' didn't match a feature in the tree");
if (node.get().getType() == TYPE.TREE) {
RevTreeBuilder treeBuilder = new RevTreeBuilder(stagingDatabase(), workingTree().getTree());
treeBuilder.remove(st);
treeBuilder.put(node.get().getNode());
RevTree newRoot = treeBuilder.build();
stagingDatabase().put(newRoot);
workingTree().updateWorkHead(newRoot.getId());
} else {
ObjectId metadataId = ObjectId.NULL;
Optional<NodeRef> parentNode = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(node.get().getParentPath()).setIndex(true).call();
RevTreeBuilder treeBuilder = null;
if (parentNode.isPresent()) {
metadataId = parentNode.get().getMetadataId();
Optional<RevTree> parsed = command(RevObjectParse.class).setObjectId(parentNode.get().getNode().getObjectId()).call(RevTree.class);
checkState(parsed.isPresent(), "Parent tree couldn't be found in the repository.");
treeBuilder = new RevTreeBuilder(stagingDatabase(), parsed.get());
treeBuilder.remove(node.get().getNode().getName());
} else {
treeBuilder = new RevTreeBuilder(stagingDatabase());
}
treeBuilder.put(node.get().getNode());
ObjectId newTreeId = command(WriteBack.class).setAncestor(workingTree().getTree().builder(stagingDatabase())).setChildPath(node.get().getParentPath()).setToIndex(true).setTree(treeBuilder.build()).setMetadataId(metadataId).call();
workingTree().updateWorkHead(newTreeId);
}
}
}
} else {
if (!conflicts.isEmpty()) {
if (!(force)) {
StringBuilder msg = new StringBuilder();
for (Conflict conflict : conflicts) {
msg.append("error: " + conflict.getPath() + " needs merge.\n");
}
msg.append("You need to resolve your index first.\n");
throw new CheckoutException(msg.toString(), StatusCode.UNMERGED_PATHS);
}
}
Optional<Ref> targetRef = Optional.absent();
Optional<ObjectId> targetCommitId = Optional.absent();
Optional<ObjectId> targetTreeId = Optional.absent();
targetRef = command(RefParse.class).setName(branchOrCommit).call();
if (targetRef.isPresent()) {
ObjectId commitId = targetRef.get().getObjectId();
if (targetRef.get().getName().startsWith(Ref.REMOTES_PREFIX)) {
String remoteName = targetRef.get().getName();
remoteName = remoteName.substring(Ref.REMOTES_PREFIX.length(), targetRef.get().getName().lastIndexOf("/"));
if (branchOrCommit.contains(remoteName + '/')) {
RevCommit commit = command(RevObjectParse.class).setObjectId(commitId).call(RevCommit.class).get();
targetTreeId = Optional.of(commit.getTreeId());
targetCommitId = Optional.of(commit.getId());
targetRef = Optional.absent();
} else {
Ref branch = command(BranchCreateOp.class).setName(targetRef.get().localName()).setSource(commitId.toString()).call();
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branch.localName() + ".remote").setValue(remoteName).call();
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branch.localName() + ".merge").setValue(targetRef.get().getName()).call();
targetRef = Optional.of(branch);
result.setResult(CheckoutResult.Results.CHECKOUT_REMOTE_BRANCH);
result.setRemoteName(remoteName);
}
}
if (commitId.isNull()) {
targetTreeId = Optional.of(ObjectId.NULL);
targetCommitId = Optional.of(ObjectId.NULL);
} else {
Optional<RevCommit> parsed = command(RevObjectParse.class).setObjectId(commitId).call(RevCommit.class);
checkState(parsed.isPresent());
checkState(parsed.get() instanceof RevCommit);
RevCommit commit = parsed.get();
targetCommitId = Optional.of(commit.getId());
targetTreeId = Optional.of(commit.getTreeId());
}
} else {
final Optional<ObjectId> addressed = command(RevParse.class).setRefSpec(branchOrCommit).call();
checkArgument(addressed.isPresent(), "source '" + branchOrCommit + "' not found in repository");
RevCommit commit = command(RevObjectParse.class).setObjectId(addressed.get()).call(RevCommit.class).get();
targetTreeId = Optional.of(commit.getTreeId());
targetCommitId = Optional.of(commit.getId());
}
if (targetTreeId.isPresent()) {
if (!force) {
if (!index().isClean() || !workingTree().isClean()) {
throw new CheckoutException(StatusCode.LOCAL_CHANGES_NOT_COMMITTED);
}
}
// update work tree
ObjectId treeId = targetTreeId.get();
workingTree().updateWorkHead(treeId);
index().updateStageHead(treeId);
result.setNewTree(treeId);
if (targetRef.isPresent()) {
// update HEAD
Ref target = targetRef.get();
String refName;
if (target instanceof SymRef) {
// beware of cyclic refs, peel symrefs
refName = ((SymRef) target).getTarget();
} else {
refName = target.getName();
}
command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(refName).call();
result.setNewRef(targetRef.get());
result.setOid(targetCommitId.get());
result.setResult(CheckoutResult.Results.CHECKOUT_LOCAL_BRANCH);
} else {
// set HEAD to a dettached state
ObjectId commitId = targetCommitId.get();
command(UpdateRef.class).setName(Ref.HEAD).setNewValue(commitId).call();
result.setOid(commitId);
result.setResult(CheckoutResult.Results.DETACHED_HEAD);
}
Optional<Ref> ref = command(RefParse.class).setName(Ref.MERGE_HEAD).call();
if (ref.isPresent()) {
command(UpdateRef.class).setName(Ref.MERGE_HEAD).setDelete(true).call();
}
return result;
}
}
result.setNewTree(workingTree().getTree().getId());
return result;
}
use of org.locationtech.geogig.api.RevTree 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.RevTree in project GeoGig by boundlessgeo.
the class CommitOpTest method testCommitWithAllOption.
@Test
public void testCommitWithAllOption() throws Exception {
try {
geogig.command(AddOp.class).addPattern(".").call();
geogig.command(CommitOp.class).call();
fail("expected NothingToCommitException");
} catch (NothingToCommitException e) {
assertTrue(true);
}
insertAndAdd(points1);
geogig.command(AddOp.class).addPattern(".").call();
RevCommit commit = geogig.command(CommitOp.class).call();
ObjectId oid = insertAndAdd(points1_modified);
CommitOp commitCommand = geogig.command(CommitOp.class);
commit = commitCommand.setAll(true).call();
assertNotNull(commit);
assertNotNull(commit.getParentIds());
assertEquals(1, commit.getParentIds().size());
assertNotNull(commit.getId());
ObjectId treeId = commit.getTreeId();
assertNotNull(treeId);
RevTree root = repo.getTree(treeId);
assertNotNull(root);
Optional<Node> typeTreeId = repo.getTreeChild(root, pointsName);
assertTrue(typeTreeId.isPresent());
RevTree typeTree = repo.getTree(typeTreeId.get().getObjectId());
assertNotNull(typeTree);
String featureId = points1.getIdentifier().getID();
Optional<Node> featureBlobId = repo.getTreeChild(root, NodeRef.appendChild(pointsName, featureId));
assertTrue(featureBlobId.isPresent());
assertEquals(oid, featureBlobId.get().getObjectId());
ObjectId commitId = geogig.command(RevParse.class).setRefSpec(Ref.HEAD).call().get();
assertEquals(commit.getId(), commitId);
}
use of org.locationtech.geogig.api.RevTree in project GeoGig by boundlessgeo.
the class CommitOpTest method testCommitEmptyTreeOnNonEmptyRepo.
@Test
public void testCommitEmptyTreeOnNonEmptyRepo() throws Exception {
insertAndAdd(points1, points2);
geogig.command(CommitOp.class).call();
// insertAndAdd(lines1, lines2);
WorkingTree workingTree = geogig.getRepository().workingTree();
final String emptyTreeName = "emptyTree";
workingTree.createTypeTree(emptyTreeName, pointsType);
{
List<DiffEntry> unstaged = toList(workingTree.getUnstaged(null));
assertEquals(unstaged.toString(), 1, unstaged.size());
// assertEquals(NodeRef.ROOT, unstaged.get(0).newName());
assertEquals(emptyTreeName, unstaged.get(0).newName());
}
geogig.command(AddOp.class).call();
{
StagingArea index = geogig.getRepository().index();
List<DiffEntry> staged = toList(index.getStaged(null));
assertEquals(staged.toString(), 1, staged.size());
// assertEquals(NodeRef.ROOT, staged.get(0).newName());
assertEquals(emptyTreeName, staged.get(0).newName());
}
CommitOp commitCommand = geogig.command(CommitOp.class);
RevCommit commit = commitCommand.call();
assertNotNull(commit);
RevTree head = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call(RevTree.class).get();
Optional<NodeRef> ref = geogig.command(FindTreeChild.class).setChildPath(emptyTreeName).setParent(head).call();
assertTrue(ref.isPresent());
}
Aggregations