Package org.locationtech.geogig.api

Examples of org.locationtech.geogig.api.RevCommit


    private RevCommit createCommits(int numCommits, String branchName) {
        int largeStep = numCommits / 10;
        int smallStep = numCommits / 100;

        RevCommit commit = null;
        for (int i = 1; i <= numCommits; i++) {
            if (i % largeStep == 0) {
                System.err.print(i);
                System.err.flush();
            } else if (i % smallStep == 0) {
View Full Code Here


            createCommits(numCommits / 2, branchName);
            geogig.command(CheckoutOp.class).setSource(Ref.MASTER).call();
            geogig.command(CommitOp.class).setAllowEmpty(true)
                    .setMessage("Commit during " + branchName).call();
            geogig.command(CheckoutOp.class).setSource(branchName).call();
            RevCommit lastCommit = createCommits(numCommits / 2, branchName);
            geogig.command(CheckoutOp.class).setSource(Ref.MASTER).call();
            list.add(lastCommit.getId());
            // System.err.println("branch " + Integer.toString(i));
        }
        return list;
    }
View Full Code Here

        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()));
View Full Code Here

        // |
        // o - Points 3 added
        // |
        // o - master - HEAD - Lines 1 added
        insertAndAdd(points1);
        final RevCommit c1 = geogig.command(CommitOp.class).setMessage("commit for " + idP1).call();

        // create branch1 and checkout
        geogig.command(BranchCreateOp.class).setAutoCheckout(true).setName("branch1").call();
        insertAndAdd(points2);
        final RevCommit c2 = geogig.command(CommitOp.class).setMessage("commit for " + idP2).call();

        // checkout master
        geogig.command(CheckoutOp.class).setSource("master").call();
        insertAndAdd(points3);
        final RevCommit c3 = geogig.command(CommitOp.class).setMessage("commit for " + idP3).call();
        insertAndAdd(lines1);
        final RevCommit c4 = geogig.command(CommitOp.class).setMessage("commit for " + idL1).call();

        // Delete the graph
        database.truncate();
        database.close();
        database.open();

        // Rebuild the graph
        ImmutableList<ObjectId> updated = geogig.command(RebuildGraphOp.class).call();
        assertEquals(4, updated.size());
        assertTrue(updated.contains(c1.getId()));
        assertTrue(updated.contains(c2.getId()));
        assertTrue(updated.contains(c3.getId()));
        assertTrue(updated.contains(c4.getId()));
    }
View Full Code Here

        // |
        // o - Points 3 added
        // |
        // o - master - HEAD - Lines 1 added
        insertAndAdd(points1);
        final RevCommit c1 = geogig.command(CommitOp.class).setMessage("commit for " + idP1).call();

        // create branch1 and checkout
        geogig.command(BranchCreateOp.class).setAutoCheckout(true).setName("branch1").call();
        insertAndAdd(points2);
        final RevCommit c2 = geogig.command(CommitOp.class).setMessage("commit for " + idP2).call();

        // Delete the graph
        database.truncate();
        database.close();
        database.open();

        // checkout master
        geogig.command(CheckoutOp.class).setSource("master").call();
        insertAndAdd(points3);
        final RevCommit c3 = geogig.command(CommitOp.class).setMessage("commit for " + idP3).call();
        insertAndAdd(lines1);
        final RevCommit c4 = geogig.command(CommitOp.class).setMessage("commit for " + idL1).call();

        // Rebuild the graph
        ImmutableList<ObjectId> updated = geogig.command(RebuildGraphOp.class).call();
        assertEquals(2, updated.size());
        assertTrue(updated.contains(c1.getId()));
        assertTrue(updated.contains(c2.getId()));
        assertFalse(updated.contains(c3.getId()));
        assertFalse(updated.contains(c4.getId()));
    }
View Full Code Here

        Preconditions.checkState(conflicts.isEmpty() || skip || abort,
                "Cannot run operation while merge conflicts exist.");

        Optional<Ref> ref = command(RefParse.class).setName(Ref.ORIG_HEAD).call();
        File branchFile = new File(getRebaseFolder(), "branch");
        RevCommit squashCommit = readSquashCommit();
        if (abort) {
            Preconditions.checkState(ref.isPresent() && branchFile.exists(),
                    "Cannot abort. You are not in the middle of a rebase process.");
            command(ResetOp.class).setMode(ResetMode.HARD)
                    .setCommit(Suppliers.ofInstance(ref.get().getObjectId())).call();
            command(UpdateRef.class).setDelete(true).setName(Ref.ORIG_HEAD).call();
            branchFile.delete();
            return true;
        } else if (continueRebase) {
            Preconditions.checkState(ref.isPresent() && branchFile.exists(),
                    "Cannot continue. You are not in the middle of a rebase process.");
            try {
                currentBranch = Files.readFirstLine(branchFile, Charsets.UTF_8);
            } catch (IOException e) {
                throw new IllegalStateException("Cannot read current branch info file");
            }
            rebaseHead = currHead.get().getObjectId();
            if (squashCommit == null) {
                // Commit the manually-merged changes with the info of the commit that caused the
                // conflict
                applyNextCommit(false);
                // Commit files should already be prepared, so we do nothing else
            } else {
                applyCommit(squashCommit, false);
            }
        } else if (skip) {
            Preconditions.checkState(ref.isPresent() && branchFile.exists(),
                    "Cannot skip. You are not in the middle of a rebase process.");
            try {
                currentBranch = Files.readFirstLine(branchFile, Charsets.UTF_8);
            } catch (IOException e) {
                throw new IllegalStateException("Cannot read current branch info file");
            }
            rebaseHead = currHead.get().getObjectId();
            command(ResetOp.class).setCommit(Suppliers.ofInstance(rebaseHead))
                    .setMode(ResetMode.HARD).call();
            if (squashCommit == null) {
                skipCurrentCommit();
                applyNextCommit(true);
            } else {
                return true;
            }
        } else {
            Preconditions
                    .checkState(!ref.isPresent(),
                            "You are currently in the middle of a merge or rebase project <ORIG_HEAD is present>.");

            getProgressListener().started();

            command(UpdateRef.class).setName(Ref.ORIG_HEAD)
                    .setNewValue(currHead.get().getObjectId()).call();

            // Here we prepare the files with the info about the commits to apply or, if that's not
            // needed, do a fast-forward

            final SymRef headRef = (SymRef) currHead.get();
            currentBranch = headRef.getTarget();

            if (ObjectId.NULL.equals(headRef.getObjectId())) {
                // Fast-forward
                command(UpdateRef.class).setName(currentBranch).setNewValue(upstream.get()).call();
                command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();
                workingTree().updateWorkHead(upstream.get());
                index().updateStageHead(upstream.get());
                getProgressListener().complete();
                return true;
            }

            Repository repository = repository();
            final RevCommit headCommit = repository.getCommit(headRef.getObjectId());
            final RevCommit targetCommit = repository.getCommit(upstream.get());

            command(UpdateRef.class).setName(Ref.ORIG_HEAD).setNewValue(headCommit.getId());

            Optional<ObjectId> ancestorCommit = command(FindCommonAncestor.class)
                    .setLeft(headCommit).setRight(targetCommit)
                    .setProgressListener(subProgress(10.f)).call();

            Preconditions.checkState(ancestorCommit.isPresent(),
                    "No ancestor commit could be found.");

            if (ancestorCommit.get().equals(headCommit.getId())) {
                // Fast-forward
                command(UpdateRef.class).setName(currentBranch).setNewValue(upstream.get()).call();
                command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();

                workingTree().updateWorkHead(upstream.get());
                index().updateStageHead(upstream.get());
                getProgressListener().complete();
                return true;
            }

            // Get all commits between the head commit and the ancestor.
            Iterator<RevCommit> commitIterator = command(LogOp.class).call();

            List<RevCommit> commitsToRebase = new ArrayList<RevCommit>();

            RevCommit commit = commitIterator.next();
            while (!commit.getId().equals(ancestorCommit.get())) {
                commitsToRebase.add(commit);
                commit = commitIterator.next();
            }

            // rewind the HEAD
View Full Code Here

        try {
            String idx = Files.readFirstLine(nextFile, Charsets.UTF_8);
            File commitFile = new File(rebaseFolder, idx);
            if (commitFile.exists()) {
                String commitId = Files.readFirstLine(commitFile, Charsets.UTF_8);
                RevCommit commit = repository().getCommit(ObjectId.valueOf(commitId));
                applyCommit(commit, useCommitChanges);
                commitFile.delete();
                int newIdx = Integer.parseInt(idx) + 1;
                Files.write(Integer.toString(newIdx), nextFile, Charsets.UTF_8);
                return true;
View Full Code Here

                builder.setParentIds(Arrays.asList(rebaseHead));
                builder.setTreeId(newTreeId);
                builder.setCommitterTimestamp(timestamp);
                builder.setCommitterTimeZoneOffset(platform.timeZoneOffset(timestamp));

                RevCommit newCommit = builder.build();
                repository.objectDatabase().put(newCommit);

                rebaseHead = newCommit.getId();

                command(UpdateRef.class).setName(currentBranch).setNewValue(rebaseHead).call();
                command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();

                workingTree().updateWorkHead(newTreeId);
                index().updateStageHead(newTreeId);

            } else {
                Iterator<DiffEntry> unconflicted = report.getUnconflicted().iterator();
                // stage unconflicted changes
                index().stage(getProgressListener(), unconflicted, 0);
                workingTree().updateWorkHead(index().getTree().getId());

                // mark conflicted elements
                command(ConflictsWriteOp.class).setConflicts(report.getConflicts()).call();

                // created exception message
                StringBuilder msg = new StringBuilder();
                msg.append("error: could not apply ");
                msg.append(commitToApply.getId().toString().substring(0, 7));
                msg.append(" " + commitToApply.getMessage() + "\n");

                for (Conflict conflict : report.getConflicts()) {
                    msg.append("CONFLICT: conflict in " + conflict.getPath() + "\n");
                }

                File branchFile = new File(getRebaseFolder(), "branch");
                try {
                    Files.write(currentBranch, branchFile, Charsets.UTF_8);
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot create current branch info file");
                }

                throw new RebaseConflictsException(msg.toString());

            }
        } else {
            // write new tree
            ObjectId newTreeId = command(WriteTree2.class).call();

            long timestamp = platform.currentTimeMillis();
            // Create new commit
            CommitBuilder builder = new CommitBuilder(commitToApply);
            builder.setParentIds(Arrays.asList(rebaseHead));
            builder.setTreeId(newTreeId);
            builder.setCommitterTimestamp(timestamp);
            builder.setCommitterTimeZoneOffset(platform.timeZoneOffset(timestamp));

            RevCommit newCommit = builder.build();
            repository.objectDatabase().put(newCommit);

            rebaseHead = newCommit.getId();

            command(UpdateRef.class).setName(currentBranch).setNewValue(rebaseHead).call();
            command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();

            workingTree().updateWorkHead(newTreeId);
View Full Code Here

        ObjectId id = ObjectId.valueOf(lines.get(0).split("\t")[1].trim());
        String commitString = Joiner.on("\n").join(lines.subList(1, lines.size()));
        ByteArrayInputStream stream = new ByteArrayInputStream(
                commitString.getBytes(Charsets.UTF_8));
        ObjectReader<RevCommit> reader = new TextSerializationFactory().createCommitReader();
        RevCommit revCommit = reader.read(id, stream);
        return revCommit;

    }
View Full Code Here

                    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);
View Full Code Here

TOP

Related Classes of org.locationtech.geogig.api.RevCommit

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.