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