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 tags in this repository. The structure of this area of the repository is as 031 * follows: 032 * 033 * <pre> 034 * /tags/{tagName} 035 * </pre> 036 */ 037public class GitTags extends GitFunction { 038 039 protected static final String NAME = "tags"; 040 protected static final String ID = "/tags"; 041 042 public GitTags( 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.TAGS); 055 056 // Generate the child references to the branches ... 057 addTagsAsChildren(git, spec, writer); 058 059 } else if (spec.parameterCount() == 1) { 060 // This is a particular branch node ... 061 writer.setPrimaryType(GitLexicon.TAG); 062 String tagName = spec.parameter(0); 063 // Get the Ref, which doesn't directly know about the commit SHA1, so we have to parse the commit ... 064 Ref ref = repository.getRef(tagName); 065 if (ref == null) return null; // invalid tag name 066 RevWalk walker = new RevWalk(repository); 067 try { 068 RevCommit commit = walker.parseCommit(ref.getObjectId()); 069 // Construct the references to other nodes in this source ... 070 ObjectId objId = commit.getId(); 071 writer.addProperty(GitLexicon.OBJECT_ID, objId.name()); 072 writer.addProperty(GitLexicon.TREE, GitTree.referenceToTree(objId, objId.name(), values)); 073 writer.addProperty(GitLexicon.HISTORY, GitHistory.referenceToHistory(objId, tagName, values)); 074 writer.addProperty(GitLexicon.DETAIL, GitCommitDetails.referenceToCommit(objId, values)); 075 } finally { 076 walker.dispose(); 077 } 078 } else { 079 return null; 080 } 081 082 return writer.document(); 083 } 084}