Search in sources :

Example 6 with Commit

use of org.jooq.Commit in project jOOQ by jOOQ.

the class GitCommitProvider method provide.

@Override
public final Commits provide() {
    Commits commits = Migrations.commits(dsl.configuration());
    try (Git g = Git.open(git.repository());
        Repository r = g.getRepository()) {
        List<RevCommit> revCommits = new ArrayList<>();
        RevCommit last = null;
        try {
            for (RevCommit commit : g.log().call()) {
                if (last == null)
                    last = commit;
                revCommits.add(commit);
            }
        } catch (NoHeadException e) {
            log.debug("No HEAD exists");
        }
        // The commits seem to come in reverse order from jgit.
        Collections.reverse(revCommits);
        Commit init = commits.root();
        // TODO: We collect all the commits from git, when we could ignore the empty ones
        while (!revCommits.isEmpty()) {
            Iterator<RevCommit> it = revCommits.iterator();
            commitLoop: while (it.hasNext()) {
                RevCommit revCommit = it.next();
                if (revCommit.getParents() == null || revCommit.getParents().length == 0) {
                    commits.add(init.commit(revCommit.getName(), revCommit.getFullMessage(), editFiles(r, revCommit)));
                    it.remove();
                } else {
                    Commit[] parents = new Commit[revCommit.getParentCount()];
                    // It seems the parents are not ordered deterministically in the order of which they were merged
                    List<RevCommit> l = new ArrayList<>(asList(revCommit.getParents()));
                    l.sort(COMMIT_COMPARATOR);
                    for (int i = 0; i < parents.length; i++) if ((parents[i] = commits.get(revCommit.getParents()[i].getName())) == null)
                        continue commitLoop;
                    if (parents.length == 1)
                        commits.add(parents[0].commit(revCommit.getName(), revCommit.getFullMessage(), editFiles(r, revCommit)));
                    else if (parents.length == 2)
                        commits.add(parents[0].merge(revCommit.getName(), revCommit.getFullMessage(), parents[1], editFiles(r, revCommit)));
                    else
                        throw new UnsupportedOperationException("Merging more than two parents not yet supported");
                    it.remove();
                }
            }
        }
        Status status = g.status().call();
        if (status.hasUncommittedChanges() || !status.getUntracked().isEmpty())
            commits.add(commit(last != null ? commits.get(last.getName()) : init, status));
    } catch (Exception e) {
        throw new GitException("Error while providing git versions", e);
    }
    return commits;
}
Also used : NoHeadException(org.eclipse.jgit.api.errors.NoHeadException) Status(org.eclipse.jgit.api.Status) Commits(org.jooq.Commits) ArrayList(java.util.ArrayList) NoHeadException(org.eclipse.jgit.api.errors.NoHeadException) IOException(java.io.IOException) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Commit(org.jooq.Commit) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Commit (org.jooq.Commit)6 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)2 IOException (java.io.IOException)1 ArrayDeque (java.util.ArrayDeque)1 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 Git (org.eclipse.jgit.api.Git)1 Status (org.eclipse.jgit.api.Status)1 NoHeadException (org.eclipse.jgit.api.errors.NoHeadException)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 Commits (org.jooq.Commits)1 File (org.jooq.File)1 Meta (org.jooq.Meta)1 Schema (org.jooq.Schema)1