@Test public void testResetPathFixesConflict() throws Exception { Feature points1Modified = feature(pointsType, idP1, "StringProp1_2", new Integer(1000), "POINT(1 1)"); Feature points1ModifiedB = feature(pointsType, idP1, "StringProp1_3", new Integer(2000), "POINT(1 1)"); insertAndAdd(points1); RevCommit resetCommit = geogit.command(CommitOp.class).call(); geogit.command(BranchCreateOp.class).setName("TestBranch").call(); insertAndAdd(points1Modified); geogit.command(CommitOp.class).call(); geogit.command(CheckoutOp.class).setSource("TestBranch").call(); insertAndAdd(points1ModifiedB); insertAndAdd(points2); geogit.command(CommitOp.class).call(); geogit.command(CheckoutOp.class).setSource("master").call(); Ref branch = geogit.command(RefParse.class).setName("TestBranch").call().get(); try { geogit.command(MergeOp.class).addCommit(Suppliers.ofInstance(branch.getObjectId())).call(); fail(); } catch (MergeConflictsException e) { assertTrue(e.getMessage().contains("conflict")); } geogit .command(ResetOp.class) .addPattern(pointsName + "/" + idP1) .setCommit(Suppliers.ofInstance(resetCommit.getId())) .call(); List<Conflict> conflicts = geogit.getRepository().getIndex().getDatabase().getConflicts(null, null); assertTrue(conflicts.isEmpty()); }
/** * Updates the remote ref that matches the given refspec. * * @param refspec the ref to update * @param commitId the new value of the ref * @param delete if true, the remote ref will be deleted * @return the updated ref */ @Override protected Ref updateRemoteRef(String refspec, ObjectId commitId, boolean delete) { Ref updatedRef = remoteGeoGit .command(UpdateRef.class) .setName(refspec) .setNewValue(commitId) .setDelete(delete) .call() .get(); Ref remoteHead = headRef(); if (remoteHead instanceof SymRef) { if (((SymRef) remoteHead).getTarget().equals(updatedRef.getName())) { remoteGeoGit .command(UpdateSymRef.class) .setName(Ref.HEAD) .setNewValue(updatedRef.getName()) .call(); RevCommit commit = remoteGeoGit.getRepository().getCommit(commitId); remoteGeoGit.getRepository().getWorkingTree().updateWorkHead(commit.getTreeId()); remoteGeoGit.getRepository().getIndex().updateStageHead(commit.getTreeId()); } } return updatedRef; }
@Override public void print(RevCommit commit) throws IOException { Ansi ansi = AnsiDecorator.newAnsi(useColor); ansi.fg(Color.YELLOW).a(getIdAsString(commit.getId())).reset(); String message = Strings.nullToEmpty(commit.getMessage()); String title = Splitter.on('\n').split(message).iterator().next(); ansi.a(" ").a(title); console.println(ansi.toString()); }
@Test public void testGetAtRevision() throws Exception { Iterator<RevCommit> it = ws.log("master").call(); assertTrue(it.hasNext()); it.next(); assertTrue(it.hasNext()); RevCommit second = it.next(); RevCommit last = Iterators.getLast(it); assertNotNull(ws.get("point")); assertNotNull(ws.get("point@" + second.getId().toString())); assertNull(ws.get("point@" + last.getId().toString())); }
@Override protected void print(RevCommit commit, Writer w) throws IOException { println(w, "tree\t", commit.getTreeId().toString()); print(w, "parents\t"); for (Iterator<ObjectId> it = commit.getParentIds().iterator(); it.hasNext(); ) { print(w, it.next().toString()); if (it.hasNext()) { print(w, " "); } } println(w); printPerson(w, "author", commit.getAuthor()); printPerson(w, "committer", commit.getCommitter()); println(w, "message\t", Optional.fromNullable(commit.getMessage()).or("")); w.flush(); }
public void testWriteTree() throws Exception { insertAndAdd(points1, lines1); Tuple<ObjectId, BoundingBox> result = index.writeTree(repo.getHead()); // this new root tree must exist on the repo db, but is not set as the current head. In // fact, it is headless, as there's no commit pointing to it. CommitOp does that. ObjectId newRootTreeId = result.getFirst(); assertNotNull(newRootTreeId); assertFalse(repo.getRootTreeId().equals(newRootTreeId)); // but the index staged root shall be pointing to it // assertEquals(newRootTreeId, index.getStaged().getId()); RevTree tree = repo.getTree(newRootTreeId); assertEquals(2, tree.size().intValue()); ObjectDatabase odb = repo.getObjectDatabase(); assertNotNull(odb.getTreeChild(tree, pointsNs, pointsName, points1.getIdentifier().getID())); assertNotNull(odb.getTreeChild(tree, linesNs, linesName, lines1.getIdentifier().getID())); // simulate a commit so the repo head points to this new tree ObjectInserter objectInserter = repo.newObjectInserter(); RevCommit commit = new RevCommit(ObjectId.NULL); commit.setTreeId(newRootTreeId); ObjectId commitId = objectInserter.insert(WrappedSerialisingFactory.getInstance().createCommitWriter(commit)); final Ref newHead = repo.updateRef(new Ref(Ref.HEAD, commitId, TYPE.COMMIT)); index.deleted(linesNs, linesName, lines1.getIdentifier().getID()); index.stage(new NullProgressListener()); result = index.writeTree(new Ref("", newRootTreeId, TYPE.TREE)); newRootTreeId = result.getFirst(); assertNotNull(newRootTreeId); assertFalse(repo.getRootTreeId().equals(newRootTreeId)); tree = repo.getTree(newRootTreeId); assertNotNull(odb.getTreeChild(tree, pointsNs, pointsName, points1.getIdentifier().getID())); assertNull(odb.getTreeChild(tree, linesNs, linesName, lines1.getIdentifier().getID())); }
/** * Gets all of the changes from the target commit that should be applied to the sparse clone. * * @param commit the commit to get changes from * @return an iterator for changes that match the repository filter */ @Override protected FilteredDiffIterator getFilteredChanges(RevCommit commit) { ObjectId parent = ObjectId.NULL; if (commit.getParentIds().size() > 0) { parent = commit.getParentIds().get(0); } Iterator<DiffEntry> changes = remoteGeoGit .command(DiffOp.class) .setNewVersion(commit.getId()) .setOldVersion(parent) .setReportTrees(true) .call(); return new LocalFilteredDiffIterator( changes, remoteGeoGit.getRepository(), localRepository, filter); }
private void writeCSV(GeoGIT geogit, Writer out, Iterator<RevCommit> log) throws Exception { String response = "ChangeType,FeatureId,CommitId,Parent CommitIds,Author Name,Author Email,Author Commit Time,Committer Name,Committer Email,Committer Commit Time,Commit Message"; out.write(response); response = ""; String path = paths.get(0); // This is the feature type object Optional<NodeRef> ref = geogit .command(FindTreeChild.class) .setChildPath(path) .setParent(geogit.getRepository().workingTree().getTree()) .call(); Optional<RevObject> type = Optional.absent(); if (ref.isPresent()) { type = geogit .command(RevObjectParse.class) .setRefSpec(ref.get().getMetadataId().toString()) .call(); } else { throw new CommandSpecException("Couldn't resolve the given path."); } if (type.isPresent() && type.get() instanceof RevFeatureType) { RevFeatureType featureType = (RevFeatureType) type.get(); Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors(); int attributeLength = attribs.size(); for (PropertyDescriptor attrib : attribs) { response += "," + escapeCsv(attrib.getName().toString()); } response += '\n'; out.write(response); response = ""; RevCommit commit = null; while (log.hasNext()) { commit = log.next(); String parentId = commit.getParentIds().size() >= 1 ? commit.getParentIds().get(0).toString() : ObjectId.NULL.toString(); Iterator<DiffEntry> diff = geogit .command(DiffOp.class) .setOldVersion(parentId) .setNewVersion(commit.getId().toString()) .setFilter(path) .call(); while (diff.hasNext()) { DiffEntry entry = diff.next(); response += entry.changeType().toString() + ","; String fid = ""; if (entry.newPath() != null) { if (entry.oldPath() != null) { fid = entry.oldPath() + " -> " + entry.newPath(); } else { fid = entry.newPath(); } } else if (entry.oldPath() != null) { fid = entry.oldPath(); } response += fid + ","; response += commit.getId().toString() + ","; response += parentId; if (commit.getParentIds().size() > 1) { for (int index = 1; index < commit.getParentIds().size(); index++) { response += " " + commit.getParentIds().get(index).toString(); } } response += ","; if (commit.getAuthor().getName().isPresent()) { response += escapeCsv(commit.getAuthor().getName().get()); } response += ","; if (commit.getAuthor().getEmail().isPresent()) { response += escapeCsv(commit.getAuthor().getEmail().get()); } response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z") .format(new Date(commit.getAuthor().getTimestamp())) + ","; if (commit.getCommitter().getName().isPresent()) { response += escapeCsv(commit.getCommitter().getName().get()); } response += ","; if (commit.getCommitter().getEmail().isPresent()) { response += escapeCsv(commit.getCommitter().getEmail().get()); } response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z") .format(new Date(commit.getCommitter().getTimestamp())) + ","; String message = escapeCsv(commit.getMessage()); response += message; if (entry.newObjectId() == ObjectId.NULL) { // Feature was removed so we need to fill out blank attribute values for (int index = 0; index < attributeLength; index++) { response += ","; } } else { // Feature was added or modified so we need to write out the // attribute // values from the feature Optional<RevObject> feature = geogit.command(RevObjectParse.class).setObjectId(entry.newObjectId()).call(); RevFeature revFeature = (RevFeature) feature.get(); List<Optional<Object>> values = revFeature.getValues(); for (int index = 0; index < values.size(); index++) { Optional<Object> value = values.get(index); PropertyDescriptor attrib = (PropertyDescriptor) attribs.toArray()[index]; String stringValue = ""; if (value.isPresent()) { FieldType attributeType = FieldType.forBinding(attrib.getType().getBinding()); switch (attributeType) { case DATE: stringValue = new SimpleDateFormat("MM/dd/yyyy z").format((java.sql.Date) value.get()); break; case DATETIME: stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format((Date) value.get()); break; case TIME: stringValue = new SimpleDateFormat("HH:mm:ss z").format((Time) value.get()); break; case TIMESTAMP: stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z") .format((Timestamp) value.get()); break; default: stringValue = escapeCsv(value.get().toString()); } response += "," + stringValue; } else { response += ","; } } } response += '\n'; out.write(response); response = ""; } } } else { // Couldn't resolve FeatureType throw new CommandSpecException("Couldn't resolve the given path to a feature type."); } }
public void testWriteTree2() throws Exception { final ObjectDatabase repoDb = repo.getObjectDatabase(); // insert and commit feature1_1 final ObjectId oId1_1 = insertAndAdd(points1); final ObjectId newRepoTreeId1; { Tuple<ObjectId, BoundingBox> writeResult; writeResult = index.writeTree(repo.getHead()); newRepoTreeId1 = writeResult.getFirst(); // assertEquals(index.getDatabase().getStagedRootRef().getObjectId(), newRepoTreeId1); RevTree newRepoTree = repoDb.getTree(newRepoTreeId1); System.err.println("++++++++++ new repo tree 1: " + newRepoTreeId1 + " ++++++++++++"); newRepoTree.accept(new PrintVisitor(repoDb, new PrintWriter(System.err))); // check feature1_1 is there assertEquals( oId1_1, repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP1).getObjectId()); } // insert and add (stage) points2, points3, and lines1 final ObjectId oId1_2 = insertAndAdd(points2); final ObjectId oId1_3 = insertAndAdd(points3); final ObjectId oId2_1 = insertAndAdd(lines1); { // simulate a commit so the repo head points to this new tree ObjectInserter objectInserter = repo.newObjectInserter(); RevCommit commit = new RevCommit(ObjectId.NULL); commit.setTreeId(newRepoTreeId1); ObjectId commitId = objectInserter.insert(WrappedSerialisingFactory.getInstance().createCommitWriter(commit)); final Ref newHead = repo.updateRef(new Ref(Ref.HEAD, commitId, TYPE.COMMIT)); } final ObjectId newRepoTreeId2; { Tuple<ObjectId, BoundingBox> writeResult; // write comparing the the previously generated tree instead of the repository HEAD, as // it was not updated (no commit op was performed) Ref repoRootRef = new Ref("", newRepoTreeId1, TYPE.TREE); writeResult = index.writeTree(repoRootRef); newRepoTreeId2 = writeResult.getFirst(); // assertEquals(index.getDatabase().getStagedRootRef().getObjectId(), newRepoTreeId2); System.err.println("++++++++ new root 2:" + newRepoTreeId2 + " ++++++++++"); RevTree newRepoTree = repoDb.getTree(newRepoTreeId2); newRepoTree.accept(new PrintVisitor(repoDb, new PrintWriter(System.err))); // check feature1_2, feature1_2 and feature2_1 Ref treeChild; assertNotNull(treeChild = repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP2)); assertEquals(oId1_2, treeChild.getObjectId()); assertNotNull(treeChild = repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP3)); assertEquals(oId1_3, treeChild.getObjectId()); assertNotNull(treeChild = repoDb.getTreeChild(newRepoTree, linesNs, linesName, idL1)); assertEquals(oId2_1, treeChild.getObjectId()); // as well as feature1_1 from the previous commit assertNotNull(treeChild = repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP1)); assertEquals(oId1_1, treeChild.getObjectId()); } { // simulate a commit so the repo head points to this new tree ObjectInserter objectInserter = repo.newObjectInserter(); RevCommit commit = new RevCommit(ObjectId.NULL); commit.setTreeId(newRepoTreeId2); ObjectId commitId = objectInserter.insert(WrappedSerialisingFactory.getInstance().createCommitWriter(commit)); final Ref newHead = repo.updateRef(new Ref(Ref.HEAD, commitId, TYPE.COMMIT)); } // delete feature1_1, feature1_3, and feature2_1 assertTrue(deleteAndAdd(points1)); assertTrue(deleteAndAdd(points3)); assertTrue(deleteAndAdd(lines1)); // and insert feature2_2 final ObjectId oId2_2 = insertAndAdd(lines2); final ObjectId newRepoTreeId3; { Tuple<ObjectId, BoundingBox> writeResult; // write comparing the the previously generated tree instead of the repository HEAD, as // it was not updated (no commit op was performed) Ref repoRootRef = new Ref("", newRepoTreeId2, TYPE.TREE); writeResult = index.writeTree(repoRootRef); newRepoTreeId3 = writeResult.getFirst(); // assertEquals(index.getDatabase().getStagedRootRef().getObjectId(), newRepoTreeId3); System.err.println("++++++++ new root 3:" + newRepoTreeId3 + " ++++++++++"); RevTree newRepoTree = repoDb.getTree(newRepoTreeId3); newRepoTree.accept(new PrintVisitor(repoDb, new PrintWriter(System.err))); // and check only feature1_2 and feature2_2 remain assertNull(repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP1)); assertNull(repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP3)); assertNull(repoDb.getTreeChild(newRepoTree, linesNs, linesName, idL3)); assertEquals( oId1_2, repoDb.getTreeChild(newRepoTree, pointsNs, pointsName, idP2).getObjectId()); assertEquals( oId2_2, repoDb.getTreeChild(newRepoTree, linesNs, linesName, idL2).getObjectId()); } }
/** * This function takes all of the changes introduced by a commit on the sparse repository and * creates a new commit on the full repository with those changes. * * @param commitId the commit id of commit from the sparse repository * @param from the sparse repository * @param to the full repository */ protected void pushSparseCommit(ObjectId commitId) { Repository from = localRepository; Repository to = remoteGeoGit.getRepository(); Optional<RevObject> object = from.command(RevObjectParse.class).setObjectId(commitId).call(); if (object.isPresent() && object.get().getType().equals(TYPE.COMMIT)) { RevCommit commit = (RevCommit) object.get(); ObjectId parent = ObjectId.NULL; List<ObjectId> newParents = new LinkedList<ObjectId>(); for (int i = 0; i < commit.getParentIds().size(); i++) { ObjectId parentId = commit.getParentIds().get(i); if (i != 0) { Optional<ObjectId> commonAncestor = from.getGraphDatabase() .findLowestCommonAncestor(commit.getParentIds().get(0), parentId); if (commonAncestor.isPresent()) { if (from.getGraphDatabase().isSparsePath(parentId, commonAncestor.get())) { // This should be the base commit to preserve the sparse changes that // were filtered // out. newParents.add(0, from.getGraphDatabase().getMapping(parentId)); continue; } } } newParents.add(from.getGraphDatabase().getMapping(parentId)); } if (newParents.size() > 0) { parent = from.getGraphDatabase().getMapping(newParents.get(0)); } Iterator<DiffEntry> diffIter = from.command(DiffOp.class) .setNewVersion(commitId) .setOldVersion(parent) .setReportTrees(true) .call(); LocalCopyingDiffIterator changes = new LocalCopyingDiffIterator(diffIter, from, to); RevTree rootTree = RevTree.EMPTY; if (newParents.size() > 0) { ObjectId mappedCommit = newParents.get(0); Optional<ObjectId> treeId = to.command(ResolveTreeish.class).setTreeish(mappedCommit).call(); if (treeId.isPresent()) { rootTree = to.getTree(treeId.get()); } } // Create new commit ObjectId newTreeId = to.command(WriteTree.class) .setOldRoot(Suppliers.ofInstance(rootTree)) .setDiffSupplier(Suppliers.ofInstance((Iterator<DiffEntry>) changes)) .call(); CommitBuilder builder = new CommitBuilder(commit); builder.setParentIds(newParents); builder.setTreeId(newTreeId); RevCommit mapped = builder.build(); to.getObjectDatabase().put(mapped); from.getGraphDatabase().map(commit.getId(), mapped.getId()); from.getGraphDatabase().map(mapped.getId(), commit.getId()); } }
@Override public void print(RevCommit commit) throws IOException { Ansi ansi = AnsiDecorator.newAnsi(useColor); ansi.a("Commit: ").fg(Color.YELLOW).a(getIdAsString(commit.getId())).reset().newline(); if (commit.getParentIds().size() > 1) { ansi.a("Merge: "); for (ObjectId parent : commit.getParentIds()) { ansi.a(parent.toString().substring(0, 7)).a(" "); } ansi.newline(); } ansi.a("Author: ").fg(Color.GREEN).a(formatPerson(commit.getAuthor())).reset().newline(); final long timestamp = commit.getAuthor().getTimestamp(); final int timeZoneOffset = commit.getAuthor().getTimeZoneOffset(); String friendlyString = estimateSince(now, timestamp); DATE_FORMAT.getCalendar().getTimeZone().setRawOffset(timeZoneOffset); String formattedDate = DATE_FORMAT.format(timestamp); ansi.a("Date: (") .fg(Color.RED) .a(friendlyString) .reset() .a(") ") .a(formattedDate) .newline(); ansi.a("Subject: ").a(commit.getMessage()).newline(); if ((detail.equals(LOG_DETAIL.NAMES_ONLY)) && commit.getParentIds().size() == 1) { ansi.a("Affected paths:").newline(); Iterator<DiffEntry> diff = geogit .command(DiffOp.class) .setOldVersion(commit.parentN(0).get()) .setNewVersion(commit.getId()) .call(); DiffEntry diffEntry; while (diff.hasNext()) { diffEntry = diff.next(); ansi.a("\t" + diffEntry.newPath()).newline(); } } if (detail.equals(LOG_DETAIL.STATS) && commit.getParentIds().size() == 1) { Iterator<DiffEntry> diff = geogit .command(DiffOp.class) .setOldVersion(commit.parentN(0).get()) .setNewVersion(commit.getId()) .call(); int adds = 0, deletes = 0, changes = 0; DiffEntry diffEntry; while (diff.hasNext()) { diffEntry = diff.next(); switch (diffEntry.changeType()) { case ADDED: ++adds; break; case REMOVED: ++deletes; break; case MODIFIED: ++changes; break; } } ansi.a("Changes:"); ansi.fg(Color.GREEN) .a(adds) .reset() .a(" features added, ") .fg(Color.YELLOW) .a(changes) .reset() .a(" changed, ") .fg(Color.RED) .a(deletes) .reset() .a(" deleted.") .reset() .newline(); } console.println(ansi.toString()); if (detail.equals(LOG_DETAIL.SUMMARY) && commit.getParentIds().size() == 1) { ansi.a("Changes:").newline(); Iterator<DiffEntry> diff = geogit .command(DiffOp.class) .setOldVersion(commit.parentN(0).get()) .setNewVersion(commit.getId()) .call(); DiffEntry diffEntry; while (diff.hasNext()) { diffEntry = diff.next(); if (detail.equals(LOG_DETAIL.SUMMARY)) { new FullDiffPrinter(true, false).print(geogit, console, diffEntry); } } } }