001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.connector.git;
017
018import java.io.IOException;
019import org.eclipse.jgit.api.Git;
020import org.eclipse.jgit.api.errors.GitAPIException;
021import org.eclipse.jgit.lib.ObjectId;
022import org.eclipse.jgit.lib.Ref;
023import org.eclipse.jgit.lib.Repository;
024import org.eclipse.jgit.revwalk.RevCommit;
025import org.eclipse.jgit.revwalk.RevWalk;
026import org.modeshape.schematic.document.Document;
027import org.modeshape.jcr.spi.federation.DocumentWriter;
028
029/**
030 * A {@link GitFunction} that returns the list of branches in this repository. The structure of this area of the repository is as
031 * follows:
032 * 
033 * <pre>
034 *   /branches/{branchName}
035 * </pre>
036 */
037public class GitBranches extends GitFunction {
038
039    protected static final String NAME = "branches";
040    protected static final String ID = "/branches";
041
042    public GitBranches( GitConnector connector ) {
043        super(NAME, connector);
044    }
045
046    @Override
047    public Document execute( Repository repository,
048                             Git git,
049                             CallSpecification spec,
050                             DocumentWriter writer,
051                             Values values ) throws GitAPIException, IOException {
052        if (spec.parameterCount() == 0) {
053            // This is the top-level "/branches" node
054            writer.setPrimaryType(GitLexicon.BRANCHES);
055
056            // Generate the child references to the branches ...
057            addBranchesAsChildren(git, spec, writer);
058
059        } else if (spec.parameterCount() == 1) {
060            // This is a particular branch node ...
061            writer.setPrimaryType(GitLexicon.BRANCH);
062            String branchName = spec.parameter(0);
063            String branchRefName = branchRefForName(branchName);
064            // Get the Ref, which doesn't directly know about the commit SHA1, so we have to parse the commit ...
065            Ref ref = repository.getRef(branchRefName);
066            if (ref == null) return null; // invalid branch
067            RevWalk walker = new RevWalk(repository);
068            try {
069                RevCommit commit = walker.parseCommit(ref.getObjectId());
070                // Construct the references to other nodes in this source ...
071                ObjectId objId = commit.getId();
072                writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
073                writer.addProperty(GitLexicon.TREE, GitTree.referenceToTree(objId, objId.name(), values));
074                writer.addProperty(GitLexicon.HISTORY, GitHistory.referenceToHistory(objId, branchName, values));
075                writer.addProperty(GitLexicon.DETAIL, GitCommitDetails.referenceToCommit(objId, values));
076            } finally {
077                walker.dispose();
078            }
079        } else {
080            return null;
081        }
082
083        return writer.document();
084    }
085}