1 /* 2 * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com> 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 package org.eclipse.jgit.lfs; 44 45 import java.io.BufferedReader; 46 import java.io.IOException; 47 import java.io.InputStream; 48 import java.io.InputStreamReader; 49 import java.io.OutputStream; 50 import java.io.PrintStream; 51 import java.io.UnsupportedEncodingException; 52 import java.nio.charset.StandardCharsets; 53 import java.nio.charset.UnsupportedCharsetException; 54 55 import org.eclipse.jgit.annotations.Nullable; 56 import org.eclipse.jgit.lfs.lib.AnyLongObjectId; 57 import org.eclipse.jgit.lfs.lib.Constants; 58 import org.eclipse.jgit.lfs.lib.LongObjectId; 59 60 /** 61 * Represents an LFS pointer file 62 * 63 * @since 4.6 64 */ 65 public class LfsPointer { 66 /** 67 * The version of the LfsPointer file format 68 */ 69 public static final String VERSION = "https://git-lfs.github.com/spec/v1"; //$NON-NLS-1$ 70 71 /** 72 * The name of the hash function as used in the pointer files. This will 73 * evaluate to "sha256" 74 */ 75 public static final String HASH_FUNCTION_NAME = Constants.LONG_HASH_FUNCTION 76 .toLowerCase().replace("-", ""); //$NON-NLS-1$ //$NON-NLS-2$ 77 78 private AnyLongObjectId oid; 79 80 private long size; 81 82 /** 83 * @param oid 84 * the id of the content 85 * @param size 86 * the size of the content 87 */ 88 public LfsPointer(AnyLongObjectId oid, long size) { 89 this.oid = oid; 90 this.size = size; 91 } 92 93 /** 94 * @return the id of the content 95 */ 96 public AnyLongObjectId getOid() { 97 return oid; 98 } 99 100 /** 101 * @return the size of the content 102 */ 103 public long getSize() { 104 return size; 105 } 106 107 /** 108 * Encode this object into the LFS format defined by {@link #VERSION} 109 * 110 * @param out 111 * the {@link OutputStream} into which the encoded data should be 112 * written 113 */ 114 public void encode(OutputStream out) { 115 try (PrintStream ps = new PrintStream(out, false, 116 StandardCharsets.UTF_8.name())) { 117 ps.print("version "); //$NON-NLS-1$ 118 ps.print(VERSION + "\n"); //$NON-NLS-1$ 119 ps.print("oid " + HASH_FUNCTION_NAME + ":"); //$NON-NLS-1$ //$NON-NLS-2$ 120 ps.print(oid.name() + "\n"); //$NON-NLS-1$ 121 ps.print("size "); //$NON-NLS-1$ 122 ps.print(size + "\n"); //$NON-NLS-1$ 123 } catch (UnsupportedEncodingException e) { 124 // should not happen, we are using a standard charset 125 throw new UnsupportedCharsetException( 126 StandardCharsets.UTF_8.name()); 127 } 128 } 129 130 /** 131 * Try to parse the data provided by an InputStream to the format defined by 132 * {@link #VERSION} 133 * 134 * @param in 135 * the {@link InputStream} from where to read the data 136 * @return an {@link LfsPointer} or <code>null</code> if the stream was not 137 * parseable as LfsPointer 138 * @throws IOException 139 */ 140 @Nullable 141 public static LfsPointer parseLfsPointer(InputStream in) 142 throws IOException { 143 boolean versionLine = false; 144 LongObjectId id = null; 145 long sz = -1; 146 147 try (BufferedReader br = new BufferedReader( 148 new InputStreamReader(in, StandardCharsets.UTF_8.name()))) { 149 for (String s = br.readLine(); s != null; s = br.readLine()) { 150 if (s.startsWith("#") || s.length() == 0) { //$NON-NLS-1$ 151 continue; 152 } else if (s.startsWith("version") && s.length() > 8 //$NON-NLS-1$ 153 && s.substring(8).trim().equals(VERSION)) { 154 versionLine = true; 155 } else if (s.startsWith("oid sha256:")) { //$NON-NLS-1$ 156 id = LongObjectId.fromString(s.substring(11).trim()); 157 } else if (s.startsWith("size") && s.length() > 5) { //$NON-NLS-1$ 158 sz = Long.parseLong(s.substring(5).trim()); 159 } else { 160 return null; 161 } 162 } 163 if (versionLine && id != null && sz > -1) { 164 return new LfsPointer(id, sz); 165 } 166 } 167 return null; 168 } 169 170 @Override 171 public String toString() { 172 return "LfsPointer: oid=" + oid.name() + ", size=" //$NON-NLS-1$ //$NON-NLS-2$ 173 + size; 174 } 175 } 176