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 static java.nio.charset.StandardCharsets.UTF_8;
46
47 import java.io.BufferedReader;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.io.InputStreamReader;
51 import java.io.OutputStream;
52 import java.io.PrintStream;
53 import java.io.UnsupportedEncodingException;
54 import java.nio.charset.UnsupportedCharsetException;
55 import java.util.Locale;
56
57 import org.eclipse.jgit.annotations.Nullable;
58 import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
59 import org.eclipse.jgit.lfs.lib.Constants;
60 import org.eclipse.jgit.lfs.lib.LongObjectId;
61
62 /**
63 * Represents an LFS pointer file
64 *
65 * @since 4.6
66 */
67 public class LfsPointer implements Comparable<LfsPointer> {
68 /**
69 * The version of the LfsPointer file format
70 */
71 public static final String VERSION = "https://git-lfs.github.com/spec/v1"; //$NON-NLS-1$
72
73 /**
74 * The version of the LfsPointer file format using legacy URL
75 * @since 4.7
76 */
77 public static final String VERSION_LEGACY = "https://hawser.github.com/spec/v1"; //$NON-NLS-1$
78
79 /**
80 * Don't inspect files that are larger than this threshold to avoid
81 * excessive reading. No pointer file should be larger than this.
82 * @since 4.11
83 */
84 public static final int SIZE_THRESHOLD = 200;
85
86 /**
87 * The name of the hash function as used in the pointer files. This will
88 * evaluate to "sha256"
89 */
90 public static final String HASH_FUNCTION_NAME = Constants.LONG_HASH_FUNCTION
91 .toLowerCase(Locale.ROOT).replace("-", ""); //$NON-NLS-1$ //$NON-NLS-2$
92
93 private AnyLongObjectId oid;
94
95 private long size;
96
97 /**
98 * <p>Constructor for LfsPointer.</p>
99 *
100 * @param oid
101 * the id of the content
102 * @param size
103 * the size of the content
104 */
105 public LfsPointer(AnyLongObjectId oid, long size) {
106 this.oid = oid;
107 this.size = size;
108 }
109
110 /**
111 * <p>Getter for the field <code>oid</code>.</p>
112 *
113 * @return the id of the content
114 */
115 public AnyLongObjectId getOid() {
116 return oid;
117 }
118
119 /**
120 * <p>Getter for the field <code>size</code>.</p>
121 *
122 * @return the size of the content
123 */
124 public long getSize() {
125 return size;
126 }
127
128 /**
129 * Encode this object into the LFS format defined by {@link #VERSION}
130 *
131 * @param out
132 * the {@link java.io.OutputStream} into which the encoded data should be
133 * written
134 */
135 public void encode(OutputStream out) {
136 try (PrintStream ps = new PrintStream(out, false,
137 UTF_8.name())) {
138 ps.print("version "); //$NON-NLS-1$
139 ps.print(VERSION + "\n"); //$NON-NLS-1$
140 ps.print("oid " + HASH_FUNCTION_NAME + ":"); //$NON-NLS-1$ //$NON-NLS-2$
141 ps.print(oid.name() + "\n"); //$NON-NLS-1$
142 ps.print("size "); //$NON-NLS-1$
143 ps.print(size + "\n"); //$NON-NLS-1$
144 } catch (UnsupportedEncodingException e) {
145 // should not happen, we are using a standard charset
146 throw new UnsupportedCharsetException(UTF_8.name());
147 }
148 }
149
150 /**
151 * Try to parse the data provided by an InputStream to the format defined by
152 * {@link #VERSION}
153 *
154 * @param in
155 * the {@link java.io.InputStream} from where to read the data
156 * @return an {@link org.eclipse.jgit.lfs.LfsPointer} or <code>null</code>
157 * if the stream was not parseable as LfsPointer
158 * @throws java.io.IOException
159 */
160 @Nullable
161 public static LfsPointer parseLfsPointer(InputStream in)
162 throws IOException {
163 boolean versionLine = false;
164 LongObjectId id = null;
165 long sz = -1;
166
167 try (BufferedReader br = new BufferedReader(
168 new InputStreamReader(in, UTF_8))) {
169 for (String s = br.readLine(); s != null; s = br.readLine()) {
170 if (s.startsWith("#") || s.length() == 0) { //$NON-NLS-1$
171 continue;
172 } else if (s.startsWith("version") && s.length() > 8 //$NON-NLS-1$
173 && (s.substring(8).trim().equals(VERSION) ||
174 s.substring(8).trim().equals(VERSION_LEGACY))) {
175 versionLine = true;
176 } else if (s.startsWith("oid sha256:")) { //$NON-NLS-1$
177 id = LongObjectId.fromString(s.substring(11).trim());
178 } else if (s.startsWith("size") && s.length() > 5) { //$NON-NLS-1$
179 sz = Long.parseLong(s.substring(5).trim());
180 }
181 }
182 if (versionLine && id != null && sz > -1) {
183 return new LfsPointer(id, sz);
184 }
185 }
186 return null;
187 }
188
189 /** {@inheritDoc} */
190 @Override
191 public String toString() {
192 return "LfsPointer: oid=" + oid.name() + ", size=" //$NON-NLS-1$ //$NON-NLS-2$
193 + size;
194 }
195
196 /**
197 * @since 4.11
198 */
199 @Override
200 public int compareTo(LfsPointer o) {
201 int x = getOid().compareTo(o.getOid());
202 if (x != 0) {
203 return x;
204 }
205
206 return (int) (getSize() - o.getSize());
207 }
208 }
209