use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class Clean method runInternal.
@Override
public void runInternal(GeogigCLI cli) throws IOException {
final ConsoleReader console = cli.getConsole();
final GeoGIG geogig = cli.getGeogig();
String pathFilter = null;
if (!path.isEmpty()) {
pathFilter = path.get(0);
}
if (dryRun) {
if (pathFilter != null) {
// check that is a valid path
Repository repository = cli.getGeogig().getRepository();
NodeRef.checkValidPath(pathFilter);
Optional<NodeRef> ref = repository.command(FindTreeChild.class).setIndex(true).setParent(repository.workingTree().getTree()).setChildPath(pathFilter).call();
checkParameter(ref.isPresent(), "pathspec '%s' did not match any tree", pathFilter);
checkParameter(ref.get().getType() == TYPE.TREE, "pathspec '%s' did not resolve to a tree", pathFilter);
}
Iterator<DiffEntry> unstaged = geogig.command(DiffWorkTree.class).setFilter(pathFilter).call();
while (unstaged.hasNext()) {
DiffEntry entry = unstaged.next();
if (entry.changeType() == ChangeType.ADDED) {
console.println("Would remove " + entry.newPath());
}
}
} else {
geogig.command(CleanOp.class).setPath(pathFilter).call();
console.println("Clean operation completed succesfully.");
}
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class Commit method runInternal.
/**
* Executes the commit command using the provided options.
*
* @param cli
* @see org.locationtech.geogig.cli.AbstractCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
*/
@Override
public void runInternal(GeogigCLI cli) throws IOException {
final GeoGIG geogig = cli.getGeogig();
if (message == null || Strings.isNullOrEmpty(message)) {
message = geogig.command(ReadMergeCommitMessageOp.class).call();
}
checkParameter(!Strings.isNullOrEmpty(message) || commitToReuse != null || amend, "No commit message provided");
ConsoleReader console = cli.getConsole();
Ansi ansi = newAnsi(console.getTerminal());
RevCommit commit;
try {
CommitOp commitOp = geogig.command(CommitOp.class).setMessage(message).setAmend(amend);
if (commitTimestamp != null && !Strings.isNullOrEmpty(commitTimestamp)) {
Long millis = geogig.command(ParseTimestamp.class).setString(commitTimestamp).call();
commitOp.setCommitterTimestamp(millis.longValue());
}
if (commitToReuse != null) {
Optional<ObjectId> commitId = geogig.command(RevParse.class).setRefSpec(commitToReuse).call();
checkParameter(commitId.isPresent(), "Provided reference does not exist");
TYPE type = geogig.command(ResolveObjectType.class).setObjectId(commitId.get()).call();
checkParameter(TYPE.COMMIT.equals(type), "Provided reference does not resolve to a commit");
commitOp.setCommit(geogig.getRepository().getCommit(commitId.get()));
}
commit = commitOp.setPathFilters(pathFilters).setProgressListener(cli.getProgressListener()).call();
} catch (NothingToCommitException noChanges) {
throw new CommandFailedException(noChanges.getMessage(), noChanges);
}
final ObjectId parentId = commit.parentN(0).or(ObjectId.NULL);
console.println("[" + commit.getId() + "] " + commit.getMessage());
console.print("Committed, counting objects...");
Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).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.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.print(ansi.toString());
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class Status method print.
/**
* Prints the list of changes using the specified options
*
* @param console the output console
* @param changes an iterator of differences to print
* @param color the color to use for the changes if color use is enabled
* @param total the total number of changes
* @throws IOException
* @see DiffEntry
*/
private void print(final ConsoleReader console, final Iterator<DiffEntry> changes, final Color color, final long total) throws IOException {
final int limit = all || this.limit == null ? Integer.MAX_VALUE : this.limit.intValue();
StringBuilder sb = new StringBuilder();
Ansi ansi = newAnsi(console.getTerminal(), sb);
DiffEntry entry;
ChangeType type;
String path;
int cnt = 0;
if (limit > 0) {
Iterator<DiffEntry> changesIterator = changes;
while (changesIterator.hasNext() && cnt < limit) {
++cnt;
entry = changesIterator.next();
type = entry.changeType();
path = formatPath(entry);
sb.setLength(0);
ansi.a("# ").fg(color).a(type.toString().toLowerCase()).a(" ").a(path).reset();
console.println(ansi.toString());
}
}
sb.setLength(0);
ansi.a("# ").a(String.format("%,d", total)).a(" total.");
console.println(ansi.toString());
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class HttpMappedRemoteRepo method pushSparseCommit.
/**
* Pushes a sparse commit to a remote repository and updates all mappings.
*
* @param commitId the commit to push
*/
@Override
protected void pushSparseCommit(ObjectId commitId) {
Repository from = localRepository;
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.command(FindCommonAncestor.class).setLeftId(commit.getParentIds().get(0)).setRightId(parentId).call();
if (commonAncestor.isPresent()) {
if (from.command(CheckSparsePath.class).setStart(parentId).setEnd(commonAncestor.get()).call()) {
// This should be the base commit to preserve changes that were filtered
// out.
newParents.add(0, from.graphDatabase().getMapping(parentId));
continue;
}
}
}
newParents.add(from.graphDatabase().getMapping(parentId));
}
if (newParents.size() > 0) {
parent = from.graphDatabase().getMapping(newParents.get(0));
}
Iterator<DiffEntry> diffIter = from.command(DiffOp.class).setNewVersion(commitId).setOldVersion(parent).setReportTrees(true).call();
// connect and send packed changes
final URL resourceURL;
try {
resourceURL = new URL(repositoryURL.toString() + "/repo/applychanges");
} catch (MalformedURLException e) {
throw Throwables.propagate(e);
}
final HttpURLConnection connection;
final OutputStream out;
try {
connection = (HttpURLConnection) resourceURL.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
out = connection.getOutputStream();
// pack the commit object
final ObjectSerializingFactory factory = DataStreamSerializationFactoryV1.INSTANCE;
final ObjectWriter<RevCommit> commitWriter = factory.createObjectWriter(TYPE.COMMIT);
commitWriter.write(commit, out);
// write the new parents
out.write(newParents.size());
for (ObjectId parentId : newParents) {
out.write(parentId.getRawValue());
}
// pack the changes
BinaryPackedChanges changes = new BinaryPackedChanges(from);
changes.write(out, diffIter);
} catch (IOException e) {
throw Throwables.propagate(e);
}
final InputStream in;
try {
in = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
String line = rd.readLine();
if (line != null) {
ObjectId remoteCommitId = ObjectId.valueOf(line);
from.graphDatabase().map(commit.getId(), remoteCommitId);
from.graphDatabase().map(remoteCommitId, commit.getId());
}
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class RemoveOp method _call.
/**
* @see java.util.concurrent.Callable#call()
*/
protected WorkingTree _call() {
// Check that all paths are valid and exist
for (String pathToRemove : pathsToRemove) {
NodeRef.checkValidPath(pathToRemove);
Optional<NodeRef> node;
node = command(FindTreeChild.class).setParent(workingTree().getTree()).setIndex(true).setChildPath(pathToRemove).call();
List<Conflict> conflicts = index().getConflicted(pathToRemove);
if (conflicts.size() > 0) {
for (Conflict conflict : conflicts) {
stagingDatabase().removeConflict(null, conflict.getPath());
}
} else {
Preconditions.checkArgument(node.isPresent(), "pathspec '%s' did not match any feature or tree", pathToRemove);
}
}
// separate trees from features an delete accordingly
for (String pathToRemove : pathsToRemove) {
Optional<NodeRef> node = command(FindTreeChild.class).setParent(workingTree().getTree()).setIndex(true).setChildPath(pathToRemove).call();
if (!node.isPresent()) {
continue;
}
switch(node.get().getType()) {
case TREE:
workingTree().delete(pathToRemove);
break;
case FEATURE:
String parentPath = NodeRef.parentPath(pathToRemove);
String name = node.get().name();
workingTree().delete(parentPath, name);
break;
default:
break;
}
final long numChanges = workingTree().countUnstaged(pathToRemove).count();
Iterator<DiffEntry> unstaged = workingTree().getUnstaged(pathToRemove);
index().stage(getProgressListener(), unstaged, numChanges);
}
return workingTree();
}
Aggregations