Search in sources :

Example 1 with NothingToCommitException

use of org.locationtech.geogig.api.porcelain.NothingToCommitException 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);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) RevParse(org.locationtech.geogig.api.plumbing.RevParse) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) Node(org.locationtech.geogig.api.Node) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 2 with NothingToCommitException

use of org.locationtech.geogig.api.porcelain.NothingToCommitException in project GeoGig by boundlessgeo.

the class CommitOpTest method testCommitWithCustomAuthorAndCommitter.

@Test
public void testCommitWithCustomAuthorAndCommitter() throws Exception {
    try {
        geogig.command(AddOp.class).addPattern(".").call();
        geogig.command(CommitOp.class).call();
        fail("expected NothingToCommitException");
    } catch (NothingToCommitException e) {
        assertTrue(true);
    }
    ObjectId oid1 = insertAndAdd(points1);
    ObjectId oid2 = insertAndAdd(points2);
    geogig.command(AddOp.class).addPattern(".").call();
    CommitOp commitCommand = geogig.command(CommitOp.class);
    commitCommand.setAuthor("John Doe", "John@Doe.com");
    commitCommand.setCommitter("Jane Doe", "Jane@Doe.com");
    RevCommit commit = commitCommand.call();
    assertNotNull(commit);
    assertNotNull(commit.getParentIds());
    assertEquals(0, commit.getParentIds().size());
    assertFalse(commit.parentN(0).isPresent());
    assertNotNull(commit.getId());
    assertEquals("John Doe", commit.getAuthor().getName().get());
    assertEquals("John@Doe.com", commit.getAuthor().getEmail().get());
    assertEquals("Jane Doe", commit.getCommitter().getName().get());
    assertEquals("Jane@Doe.com", commit.getCommitter().getEmail().get());
    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(oid1, featureBlobId.get().getObjectId());
    featureId = points2.getIdentifier().getID();
    featureBlobId = repo.getTreeChild(root, NodeRef.appendChild(pointsName, featureId));
    assertTrue(featureBlobId.isPresent());
    assertEquals(oid2, featureBlobId.get().getObjectId());
    ObjectId commitId = geogig.command(RevParse.class).setRefSpec(Ref.HEAD).call().get();
    assertEquals(commit.getId(), commitId);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) RevParse(org.locationtech.geogig.api.plumbing.RevParse) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) Node(org.locationtech.geogig.api.Node) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 3 with NothingToCommitException

use of org.locationtech.geogig.api.porcelain.NothingToCommitException in project GeoGig by boundlessgeo.

the class Merge method runInternal.

/**
     * Executes the merge command using the provided options.
     */
@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(commits.size() > 0 || abort, "No commits provided to merge.");
    ConsoleReader console = cli.getConsole();
    final GeoGIG geogig = cli.getGeogig();
    Ansi ansi = newAnsi(console.getTerminal());
    if (abort) {
        Optional<Ref> ref = geogig.command(RefParse.class).setName(Ref.ORIG_HEAD).call();
        if (!ref.isPresent()) {
            throw new CommandFailedException("There is no merge to abort <ORIG_HEAD missing>.");
        }
        geogig.command(ResetOp.class).setMode(ResetMode.HARD).setCommit(Suppliers.ofInstance(ref.get().getObjectId())).call();
        console.println("Merge aborted successfully.");
        return;
    }
    RevCommit commit;
    try {
        MergeOp merge = geogig.command(MergeOp.class);
        merge.setOurs(ours).setTheirs(theirs).setNoCommit(noCommit);
        merge.setMessage(message).setProgressListener(cli.getProgressListener());
        for (String commitish : commits) {
            Optional<ObjectId> commitId;
            commitId = geogig.command(RevParse.class).setRefSpec(commitish).call();
            checkParameter(commitId.isPresent(), "Commit not found '%s'", commitish);
            merge.addCommit(Suppliers.ofInstance(commitId.get()));
        }
        MergeReport report = merge.call();
        commit = report.getMergeCommit();
    } catch (RuntimeException e) {
        if (e instanceof NothingToCommitException || e instanceof IllegalArgumentException || e instanceof IllegalStateException) {
            throw new CommandFailedException(e.getMessage(), e);
        }
        throw e;
    }
    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());
}
Also used : ConsoleReader(jline.console.ConsoleReader) ObjectId(org.locationtech.geogig.api.ObjectId) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) ResetOp(org.locationtech.geogig.api.porcelain.ResetOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) Ref(org.locationtech.geogig.api.Ref) RevParse(org.locationtech.geogig.api.plumbing.RevParse) Ansi(org.fusesource.jansi.Ansi) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 4 with NothingToCommitException

use of org.locationtech.geogig.api.porcelain.NothingToCommitException 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());
}
Also used : ConsoleReader(jline.console.ConsoleReader) ObjectId(org.locationtech.geogig.api.ObjectId) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) Ansi(org.fusesource.jansi.Ansi) TYPE(org.locationtech.geogig.api.RevObject.TYPE) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 5 with NothingToCommitException

use of org.locationtech.geogig.api.porcelain.NothingToCommitException in project GeoGig by boundlessgeo.

the class TransactionEnd method updateRefs.

private void updateRefs() {
    final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
    final String currentBranch;
    if (currHead.isPresent() && currHead.get() instanceof SymRef) {
        currentBranch = ((SymRef) currHead.get()).getTarget();
    } else {
        currentBranch = "";
    }
    ImmutableSet<Ref> changedRefs = getChangedRefs();
    // Lock the repository
    try {
        refDatabase().lock();
    } catch (TimeoutException e) {
        Throwables.propagate(e);
    }
    try {
        // Update refs
        for (Ref ref : changedRefs) {
            if (!ref.getName().startsWith(Ref.REFS_PREFIX)) {
                continue;
            }
            Ref updatedRef = ref;
            Optional<Ref> repoRef = command(RefParse.class).setName(ref.getName()).call();
            if (repoRef.isPresent() && repositoryChanged(repoRef.get())) {
                if (rebase) {
                    // Try to rebase
                    transaction.command(CheckoutOp.class).setSource(ref.getName()).setForce(true).call();
                    try {
                        transaction.command(RebaseOp.class).setUpstream(Suppliers.ofInstance(repoRef.get().getObjectId())).call();
                    } catch (RebaseConflictsException e) {
                        Throwables.propagate(e);
                    }
                    updatedRef = transaction.command(RefParse.class).setName(ref.getName()).call().get();
                } else {
                    // sync transactions have to use merge to prevent divergent history
                    transaction.command(CheckoutOp.class).setSource(ref.getName()).setForce(true).call();
                    try {
                        transaction.command(MergeOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).addCommit(Suppliers.ofInstance(repoRef.get().getObjectId())).call();
                    } catch (NothingToCommitException e) {
                    // The repo commit is already in our history, this is a fast
                    // forward.
                    }
                    updatedRef = transaction.command(RefParse.class).setName(ref.getName()).call().get();
                }
            }
            LOGGER.debug(String.format("commit %s %s -> %s", ref.getName(), ref.getObjectId(), updatedRef.getObjectId()));
            command(UpdateRef.class).setName(ref.getName()).setNewValue(updatedRef.getObjectId()).call();
            if (currentBranch.equals(ref.getName())) {
                // Update HEAD, WORK_HEAD and STAGE_HEAD
                command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(ref.getName()).call();
                command(UpdateRef.class).setName(Ref.WORK_HEAD).setNewValue(updatedRef.getObjectId()).call();
                command(UpdateRef.class).setName(Ref.STAGE_HEAD).setNewValue(updatedRef.getObjectId()).call();
            }
        }
    // TODO: What happens if there are unstaged or staged changes in the repository when
    // a transaction is committed?
    } finally {
        // Unlock the repository
        refDatabase().unlock();
    }
}
Also used : Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) RebaseConflictsException(org.locationtech.geogig.api.porcelain.RebaseConflictsException) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) CheckoutOp(org.locationtech.geogig.api.porcelain.CheckoutOp) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

NothingToCommitException (org.locationtech.geogig.api.porcelain.NothingToCommitException)15 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)10 Test (org.junit.Test)9 ObjectId (org.locationtech.geogig.api.ObjectId)8 RevCommit (org.locationtech.geogig.api.RevCommit)8 RevParse (org.locationtech.geogig.api.plumbing.RevParse)6 Node (org.locationtech.geogig.api.Node)5 RevTree (org.locationtech.geogig.api.RevTree)4 GeoGIG (org.locationtech.geogig.api.GeoGIG)3 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)3 DiffOp (org.locationtech.geogig.api.porcelain.DiffOp)3 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)3 ConsoleReader (jline.console.ConsoleReader)2 Ansi (org.fusesource.jansi.Ansi)2 Ref (org.locationtech.geogig.api.Ref)2 MergeOp (org.locationtech.geogig.api.porcelain.MergeOp)2 Optional (com.google.common.base.Optional)1 File (java.io.File)1 TimeoutException (java.util.concurrent.TimeoutException)1 Ignore (org.junit.Ignore)1