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 java.io.FileNotFoundException; 49 import java.io.IOException; 50 import java.text.MessageFormat; 51 52 import org.eclipse.jgit.errors.ConfigInvalidException; 53 import org.eclipse.jgit.errors.IncorrectObjectTypeException; 54 import org.eclipse.jgit.errors.MissingObjectException; 55 import org.eclipse.jgit.internal.JGitText; 56 import org.eclipse.jgit.revwalk.RevCommit; 57 import org.eclipse.jgit.revwalk.RevTree; 58 import org.eclipse.jgit.revwalk.RevWalk; 59 import org.eclipse.jgit.treewalk.TreeWalk; 60 import org.eclipse.jgit.util.RawParseUtils; 61 62 /** 63 * Configuration file based on the blobs stored in the repository. 64 * 65 * This implementation currently only provides reading support, and is primarily 66 * useful for supporting the {@code .gitmodules} file. 67 */ 68 public class BlobBasedConfig extends Config { 69 /** 70 * Parse a configuration from a byte array. 71 * 72 * @param base 73 * the base configuration file 74 * @param blob 75 * the byte array, should be UTF-8 encoded text. 76 * @throws ConfigInvalidException 77 * the byte array is not a valid configuration format. 78 */ 79 public BlobBasedConfig(Config base, final byte[] blob) 80 throws ConfigInvalidException { 81 super(base); 82 final String decoded; 83 if (isUtf8(blob)) { 84 decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET, 85 blob, 3, blob.length); 86 } else { 87 decoded = RawParseUtils.decode(blob); 88 } 89 fromText(decoded); 90 } 91 92 /** 93 * Load a configuration file from a blob. 94 * 95 * @param base 96 * the base configuration file 97 * @param db 98 * the repository 99 * @param objectId 100 * the object identifier 101 * @throws IOException 102 * the blob cannot be read from the repository. 103 * @throws ConfigInvalidException 104 * the blob is not a valid configuration format. 105 */ 106 public BlobBasedConfig(Config base, Repository db, AnyObjectId objectId) 107 throws IOException, ConfigInvalidException { 108 this(base, read(db, objectId)); 109 } 110 111 private static byte[] read(Repository db, AnyObjectId blobId) 112 throws MissingObjectException, IncorrectObjectTypeException, 113 IOException { 114 try (ObjectReader or = db.newObjectReader()) { 115 return read(or, blobId); 116 } 117 } 118 119 private static byte[] read(ObjectReader or, AnyObjectId blobId) 120 throws MissingObjectException, IncorrectObjectTypeException, 121 IOException { 122 ObjectLoader loader = or.open(blobId, Constants.OBJ_BLOB); 123 return loader.getCachedBytes(Integer.MAX_VALUE); 124 } 125 126 /** 127 * Load a configuration file from a blob stored in a specific commit. 128 * 129 * @param base 130 * the base configuration file 131 * @param db 132 * the repository containing the objects. 133 * @param treeish 134 * the tree (or commit) that contains the object 135 * @param path 136 * the path within the tree 137 * @throws FileNotFoundException 138 * the path does not exist in the commit's tree. 139 * @throws IOException 140 * the tree and/or blob cannot be accessed. 141 * @throws ConfigInvalidException 142 * the blob is not a valid configuration format. 143 */ 144 public BlobBasedConfig(Config base, Repository db, AnyObjectId treeish, 145 String path) throws FileNotFoundException, IOException, 146 ConfigInvalidException { 147 this(base, read(db, treeish, path)); 148 } 149 150 private static byte[] read(Repository db, AnyObjectId treeish, String path) 151 throws MissingObjectException, IncorrectObjectTypeException, 152 IOException { 153 try (ObjectReader or = db.newObjectReader()) { 154 TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish)); 155 if (tree == null) 156 throw new FileNotFoundException(MessageFormat.format(JGitText 157 .get().entryNotFoundByPath, path)); 158 return read(or, tree.getObjectId(0)); 159 } 160 } 161 162 private static AnyObjectId asTree(ObjectReader or, AnyObjectId treeish) 163 throws MissingObjectException, IncorrectObjectTypeException, 164 IOException { 165 if (treeish instanceof RevTree) 166 return treeish; 167 168 if (treeish instanceof RevCommit 169 && ((RevCommit) treeish).getTree() != null) 170 return ((RevCommit) treeish).getTree(); 171 172 try (RevWalk rw = new RevWalk(or)) { 173 return rw.parseTree(treeish).getId(); 174 } 175 } 176 }