Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c19c5d587003cf266b9c95595367170faf9e6c5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (C) 2006, 2012 Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 * Copyright (C) 2011, Dariusz Luksza <dariusz@luksza.org>
 * Copyright (C) 2014, Obeo
 * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.storage;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import org.eclipse.core.resources.IEncodedStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.attributes.Filtering;
import org.eclipse.egit.core.internal.CompareCoreUtils;
import org.eclipse.egit.core.internal.CoreText;
import org.eclipse.jgit.dircache.DirCacheCheckout.CheckoutMetadata;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.WorkingTreeOptions;
import org.eclipse.jgit.util.io.EolStreamTypeUtil;
import org.eclipse.osgi.util.NLS;

/**
 * Provides access to a git blob.
 *
 * @since 4.0
 */
public class GitBlobStorage implements IEncodedStorage {


	/** Repository containing the object this storage provides access to. */
	protected final Repository db;

	/** Repository-relative path of the underlying object. */
	protected final String path;

	/** Id of this object in its repository. */
	protected final ObjectId blobId;

	/** Checkout metadata: smudge filters, EOL stream type. */
	private final CheckoutMetadata metadata;

	private String charset;

	/**
	 * @param repository
	 *            The repository containing this object.
	 * @param path
	 *            Repository-relative path of the underlying object. This path
	 *            is not validated by this class, i.e. it's returned as is by
	 *            {@code #getAbsolutePath()} and {@code #getFullPath()} without
	 *            validating if the blob is reachable using this path.
	 * @param blob
	 *            Id of this object in its repository.
	 * @deprecated Use
	 *             {@link #GitBlobStorage(Repository, String, ObjectId, CheckoutMetadata)}
	 *             instead.
	 */
	@Deprecated
	public GitBlobStorage(final Repository repository, final String path,
			final ObjectId blob) {
		this(repository, path, blob, null);
	}

	/**
	 * @param repository
	 *                       The repository containing this object.
	 * @param path
	 *                       Repository-relative path of the underlying object.
	 *                       This path is not validated by this class, i.e. it's
	 *                       returned as is by {@code #getAbsolutePath()} and
	 *                       {@code #getFullPath()} without validating if the
	 *                       blob is reachable using this path.
	 * @param blob
	 *                       Id of this object in its repository.
	 * @param metadata
	 *                       Smudge filters and EOL stream type to apply when
	 *                       the content is to be gotten.
	 * @since 5.0
	 */
	public GitBlobStorage(final Repository repository, final String path,
			final ObjectId blob, final CheckoutMetadata metadata) {
		this.db = repository;
		this.path = path;
		this.blobId = blob;
		this.metadata = metadata;
	}

	@Override
	public InputStream getContents() throws CoreException {
		try {
			return open();
		} catch (IOException e) {
			throw new CoreException(Activator.error(
					NLS.bind(CoreText.BlobStorage_errorReadingBlob, blobId
							.name(), path), e));
		}
	}

	private InputStream open() throws IOException, CoreException,
			IncorrectObjectTypeException {
		if (blobId == null) {
			return new ByteArrayInputStream(new byte[0]);
		}
		try {
			WorkingTreeOptions workingTreeOptions = db.getConfig().get(WorkingTreeOptions.KEY);
			InputStream objectInputStream = db.open(blobId,
					Constants.OBJ_BLOB).openStream();
			InputStream filteredInputStream = objectInputStream;
			if (metadata != null) {
				filteredInputStream = Filtering.filter(db, path,
						objectInputStream, metadata.smudgeFilterCommand);
			}
			EolStreamType streamType;
			if (metadata != null && metadata.eolStreamType != null) {
				streamType = metadata.eolStreamType;
			} else if (workingTreeOptions.getAutoCRLF() == AutoCRLF.TRUE) {
				streamType = EolStreamType.AUTO_CRLF;
			} else {
				streamType = EolStreamType.DIRECT;
			}
			return EolStreamTypeUtil.wrapInputStream(filteredInputStream,
					streamType);
		} catch (MissingObjectException notFound) {
			throw new CoreException(Activator.error(NLS.bind(
					CoreText.BlobStorage_blobNotFound, blobId.name(), path),
					notFound));
		}
	}

	@Override
	public IPath getFullPath() {
		return Path.fromPortableString(path);
	}

	@Override
	public String getName() {
		final int last = path.lastIndexOf('/');
		return last >= 0 ? path.substring(last + 1) : path;
	}

	@Override
	public boolean isReadOnly() {
		return true;
	}

	@Override
	public Object getAdapter(final Class adapter) {
		return null;
	}

	@Override
	public int hashCode() {
		return Arrays.hashCode(new Object[] { blobId, db, path });
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GitBlobStorage other = (GitBlobStorage) obj;
		if (blobId == null) {
			if (other.blobId != null)
				return false;
		} else if (!blobId.equals(other.blobId))
			return false;
		if (db == null) {
			if (other.db != null)
				return false;
		} else if (!db.equals(other.db))
			return false;
		if (path == null) {
			if (other.path != null)
				return false;
		} else if (!path.equals(other.path))
			return false;
		return true;
	}

	/**
	 * Returns the absolute path on disk of the underlying object.
	 * <p>
	 * The returned path may not point to an existing file if the object does
	 * not exist locally.
	 * </p>
	 *
	 * @return The absolute path on disk of the underlying object.
	 */
	public IPath getAbsolutePath() {
		if (db.isBare()) {
			return null;
		}
		return new Path(db.getWorkTree().getAbsolutePath() + File.separatorChar
				+ path);
	}

	/**
	 * @since 4.1
	 */
	@Override
	public String getCharset() throws CoreException {
		if (charset == null) {
			charset = CompareCoreUtils.getResourceEncoding(db, path);
		}
		return charset;
	}
}

Back to the top