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