1 /* 2 * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com> 3 * Copyright (C) 2009, Google Inc. 4 * Copyright (C) 2009, JetBrains s.r.o. 5 * and other copyright owners as documented in the project's IP log. 6 * 7 * This program and the accompanying materials are made available 8 * under the terms of the Eclipse Distribution License v1.0 which 9 * accompanies this distribution, is reproduced below, and is 10 * available at http://www.eclipse.org/org/documents/edl-v10.php 11 * 12 * All rights reserved. 13 * 14 * Redistribution and use in source and binary forms, with or 15 * without modification, are permitted provided that the following 16 * conditions are met: 17 * 18 * - Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 21 * - Redistributions in binary form must reproduce the above 22 * copyright notice, this list of conditions and the following 23 * disclaimer in the documentation and/or other materials provided 24 * with the distribution. 25 * 26 * - Neither the name of the Eclipse Foundation, Inc. nor the 27 * names of its contributors may be used to endorse or promote 28 * products derived from this software without specific prior 29 * written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 40 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 43 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44 */ 45 46 package org.eclipse.jgit.lib; 47 48 import static org.eclipse.jgit.lib.Constants.CHARSET; 49 50 import java.io.FileNotFoundException; 51 import java.io.IOException; 52 import java.text.MessageFormat; 53 54 import org.eclipse.jgit.errors.ConfigInvalidException; 55 import org.eclipse.jgit.errors.IncorrectObjectTypeException; 56 import org.eclipse.jgit.errors.MissingObjectException; 57 import org.eclipse.jgit.internal.JGitText; 58 import org.eclipse.jgit.revwalk.RevCommit; 59 import org.eclipse.jgit.revwalk.RevTree; 60 import org.eclipse.jgit.revwalk.RevWalk; 61 import org.eclipse.jgit.treewalk.TreeWalk; 62 import org.eclipse.jgit.util.RawParseUtils; 63 64 /** 65 * Configuration file based on the blobs stored in the repository. 66 * 67 * This implementation currently only provides reading support, and is primarily 68 * useful for supporting the {@code .gitmodules} file. 69 */ 70 public class BlobBasedConfig extends Config { 71 /** 72 * Parse a configuration from a byte array. 73 * 74 * @param base 75 * the base configuration file 76 * @param blob 77 * the byte array, should be UTF-8 encoded text. 78 * @throws org.eclipse.jgit.errors.ConfigInvalidException 79 * the byte array is not a valid configuration format. 80 */ 81 public BlobBasedConfig(Config base, byte[] blob) 82 throws ConfigInvalidException { 83 super(base); 84 final String decoded; 85 if (isUtf8(blob)) { 86 decoded = RawParseUtils.decode(CHARSET, blob, 3, blob.length); 87 } else { 88 decoded = RawParseUtils.decode(blob); 89 } 90 fromText(decoded); 91 } 92 93 /** 94 * Load a configuration file from a blob. 95 * 96 * @param base 97 * the base configuration file 98 * @param db 99 * the repository 100 * @param objectId 101 * the object identifier 102 * @throws java.io.IOException 103 * the blob cannot be read from the repository. 104 * @throws org.eclipse.jgit.errors.ConfigInvalidException 105 * the blob is not a valid configuration format. 106 */ 107 public BlobBasedConfig(Config base, Repository db, AnyObjectId objectId) 108 throws IOException, ConfigInvalidException { 109 this(base, read(db, objectId)); 110 } 111 112 private static byte[] read(Repository db, AnyObjectId blobId) 113 throws MissingObjectException, IncorrectObjectTypeException, 114 IOException { 115 try (ObjectReader or = db.newObjectReader()) { 116 return read(or, blobId); 117 } 118 } 119 120 private static byte[] read(ObjectReader or, AnyObjectId blobId) 121 throws MissingObjectException, IncorrectObjectTypeException, 122 IOException { 123 ObjectLoader loader = or.open(blobId, Constants.OBJ_BLOB); 124 return loader.getCachedBytes(Integer.MAX_VALUE); 125 } 126 127 /** 128 * Load a configuration file from a blob stored in a specific commit. 129 * 130 * @param base 131 * the base configuration file 132 * @param db 133 * the repository containing the objects. 134 * @param treeish 135 * the tree (or commit) that contains the object 136 * @param path 137 * the path within the tree 138 * @throws java.io.FileNotFoundException 139 * the path does not exist in the commit's tree. 140 * @throws java.io.IOException 141 * the tree and/or blob cannot be accessed. 142 * @throws org.eclipse.jgit.errors.ConfigInvalidException 143 * the blob is not a valid configuration format. 144 */ 145 public BlobBasedConfig(Config base, Repository db, AnyObjectId treeish, 146 String path) throws FileNotFoundException, IOException, 147 ConfigInvalidException { 148 this(base, read(db, treeish, path)); 149 } 150 151 private static byte[] read(Repository db, AnyObjectId treeish, String path) 152 throws MissingObjectException, IncorrectObjectTypeException, 153 IOException { 154 try (ObjectReader or = db.newObjectReader()) { 155 TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish)); 156 if (tree == null) 157 throw new FileNotFoundException(MessageFormat.format(JGitText 158 .get().entryNotFoundByPath, path)); 159 return read(or, tree.getObjectId(0)); 160 } 161 } 162 163 private static AnyObjectId asTree(ObjectReader or, AnyObjectId treeish) 164 throws MissingObjectException, IncorrectObjectTypeException, 165 IOException { 166 if (treeish instanceof RevTree) 167 return treeish; 168 169 if (treeish instanceof RevCommit 170 && ((RevCommit) treeish).getTree() != null) 171 return ((RevCommit) treeish).getTree(); 172 173 try (RevWalk rw = new RevWalk(or)) { 174 return rw.parseTree(treeish).getId(); 175 } 176 } 177 }