View Javadoc
1   /*
2    * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3    * Copyright (C) 2008-2010, Google Inc.
4    * Copyright (C) 2006-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
5    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.internal.storage.file;
48  
49  import static org.eclipse.jgit.lib.RefDatabase.ALL;
50  
51  import java.io.File;
52  import java.io.FileInputStream;
53  import java.io.FileNotFoundException;
54  import java.io.IOException;
55  import java.text.MessageFormat;
56  import java.util.HashSet;
57  import java.util.Set;
58  
59  import org.eclipse.jgit.attributes.AttributesNode;
60  import org.eclipse.jgit.attributes.AttributesNodeProvider;
61  import org.eclipse.jgit.errors.ConfigInvalidException;
62  import org.eclipse.jgit.events.ConfigChangedEvent;
63  import org.eclipse.jgit.events.ConfigChangedListener;
64  import org.eclipse.jgit.events.IndexChangedEvent;
65  import org.eclipse.jgit.internal.JGitText;
66  import org.eclipse.jgit.internal.storage.reftree.RefTreeDatabase;
67  import org.eclipse.jgit.internal.storage.file.ObjectDirectory.AlternateHandle;
68  import org.eclipse.jgit.internal.storage.file.ObjectDirectory.AlternateRepository;
69  import org.eclipse.jgit.lib.BaseRepositoryBuilder;
70  import org.eclipse.jgit.lib.ConfigConstants;
71  import org.eclipse.jgit.lib.Constants;
72  import org.eclipse.jgit.lib.CoreConfig.HideDotFiles;
73  import org.eclipse.jgit.lib.CoreConfig.SymLinks;
74  import org.eclipse.jgit.lib.ObjectId;
75  import org.eclipse.jgit.lib.Ref;
76  import org.eclipse.jgit.lib.RefDatabase;
77  import org.eclipse.jgit.lib.RefUpdate;
78  import org.eclipse.jgit.lib.ReflogReader;
79  import org.eclipse.jgit.lib.Repository;
80  import org.eclipse.jgit.storage.file.FileBasedConfig;
81  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
82  import org.eclipse.jgit.util.FS;
83  import org.eclipse.jgit.util.FileUtils;
84  import org.eclipse.jgit.util.StringUtils;
85  import org.eclipse.jgit.util.SystemReader;
86  
87  /**
88   * Represents a Git repository. A repository holds all objects and refs used for
89   * managing source code (could by any type of file, but source code is what
90   * SCM's are typically used for).
91   *
92   * In Git terms all data is stored in GIT_DIR, typically a directory called
93   * .git. A work tree is maintained unless the repository is a bare repository.
94   * Typically the .git directory is located at the root of the work dir.
95   *
96   * <ul>
97   * <li>GIT_DIR
98   * 	<ul>
99   * 		<li>objects/ - objects</li>
100  * 		<li>refs/ - tags and heads</li>
101  * 		<li>config - configuration</li>
102  * 		<li>info/ - more configurations</li>
103  * 	</ul>
104  * </li>
105  * </ul>
106  * <p>
107  * This class is thread-safe.
108  * <p>
109  * This implementation only handles a subtly undocumented subset of git features.
110  *
111  */
112 public class FileRepository extends Repository {
113 	private final FileBasedConfig systemConfig;
114 
115 	private final FileBasedConfig userConfig;
116 
117 	private final FileBasedConfig repoConfig;
118 
119 	private final RefDatabase refs;
120 
121 	private final ObjectDirectory objectDatabase;
122 
123 	private FileSnapshot snapshot;
124 
125 	/**
126 	 * Construct a representation of a Git repository.
127 	 * <p>
128 	 * The work tree, object directory, alternate object directories and index
129 	 * file locations are deduced from the given git directory and the default
130 	 * rules by running {@link FileRepositoryBuilder}. This constructor is the
131 	 * same as saying:
132 	 *
133 	 * <pre>
134 	 * new FileRepositoryBuilder().setGitDir(gitDir).build()
135 	 * </pre>
136 	 *
137 	 * @param gitDir
138 	 *            GIT_DIR (the location of the repository metadata).
139 	 * @throws IOException
140 	 *             the repository appears to already exist but cannot be
141 	 *             accessed.
142 	 * @see FileRepositoryBuilder
143 	 */
144 	public FileRepository(final File gitDir) throws IOException {
145 		this(new FileRepositoryBuilder().setGitDir(gitDir).setup());
146 	}
147 
148 	/**
149 	 * A convenience API for {@link #FileRepository(File)}.
150 	 *
151 	 * @param gitDir
152 	 *            GIT_DIR (the location of the repository metadata).
153 	 * @throws IOException
154 	 *             the repository appears to already exist but cannot be
155 	 *             accessed.
156 	 * @see FileRepositoryBuilder
157 	 */
158 	public FileRepository(final String gitDir) throws IOException {
159 		this(new File(gitDir));
160 	}
161 
162 	/**
163 	 * Create a repository using the local file system.
164 	 *
165 	 * @param options
166 	 *            description of the repository's important paths.
167 	 * @throws IOException
168 	 *             the user configuration file or repository configuration file
169 	 *             cannot be accessed.
170 	 */
171 	public FileRepository(final BaseRepositoryBuilder options) throws IOException {
172 		super(options);
173 
174 		if (StringUtils.isEmptyOrNull(SystemReader.getInstance().getenv(
175 				Constants.GIT_CONFIG_NOSYSTEM_KEY)))
176 			systemConfig = SystemReader.getInstance().openSystemConfig(null,
177 					getFS());
178 		else
179 			systemConfig = new FileBasedConfig(null, FS.DETECTED) {
180 				public void load() {
181 					// empty, do not load
182 				}
183 
184 				public boolean isOutdated() {
185 					// regular class would bomb here
186 					return false;
187 				}
188 			};
189 		userConfig = SystemReader.getInstance().openUserConfig(systemConfig,
190 				getFS());
191 		repoConfig = new FileBasedConfig(userConfig, getFS().resolve(
192 				getDirectory(), Constants.CONFIG),
193 				getFS());
194 
195 		loadSystemConfig();
196 		loadUserConfig();
197 		loadRepoConfig();
198 
199 		repoConfig.addChangeListener(new ConfigChangedListener() {
200 			public void onConfigChanged(ConfigChangedEvent event) {
201 				fireEvent(event);
202 			}
203 		});
204 
205 		final long repositoryFormatVersion = getConfig().getLong(
206 				ConfigConstants.CONFIG_CORE_SECTION, null,
207 				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
208 
209 		String reftype = repoConfig.getString(
210 				"extensions", null, "refsStorage"); //$NON-NLS-1$ //$NON-NLS-2$
211 		if (repositoryFormatVersion >= 1 && reftype != null) {
212 			if (StringUtils.equalsIgnoreCase(reftype, "reftree")) { //$NON-NLS-1$
213 				refs = new RefTreeDatabase(this, new RefDirectory(this));
214 			} else {
215 				throw new IOException(JGitText.get().unknownRepositoryFormat);
216 			}
217 		} else {
218 			refs = new RefDirectory(this);
219 		}
220 
221 		objectDatabase = new ObjectDirectory(repoConfig, //
222 				options.getObjectDirectory(), //
223 				options.getAlternateObjectDirectories(), //
224 				getFS(), //
225 				new File(getDirectory(), Constants.SHALLOW));
226 
227 		if (objectDatabase.exists()) {
228 			if (repositoryFormatVersion > 1)
229 				throw new IOException(MessageFormat.format(
230 						JGitText.get().unknownRepositoryFormat2,
231 						Long.valueOf(repositoryFormatVersion)));
232 		}
233 
234 		if (!isBare())
235 			snapshot = FileSnapshot.save(getIndexFile());
236 	}
237 
238 	private void loadSystemConfig() throws IOException {
239 		try {
240 			systemConfig.load();
241 		} catch (ConfigInvalidException e1) {
242 			IOException e2 = new IOException(MessageFormat.format(JGitText
243 					.get().systemConfigFileInvalid, systemConfig.getFile()
244 					.getAbsolutePath(), e1));
245 			e2.initCause(e1);
246 			throw e2;
247 		}
248 	}
249 
250 	private void loadUserConfig() throws IOException {
251 		try {
252 			userConfig.load();
253 		} catch (ConfigInvalidException e1) {
254 			IOException e2 = new IOException(MessageFormat.format(JGitText
255 					.get().userConfigFileInvalid, userConfig.getFile()
256 					.getAbsolutePath(), e1));
257 			e2.initCause(e1);
258 			throw e2;
259 		}
260 	}
261 
262 	private void loadRepoConfig() throws IOException {
263 		try {
264 			repoConfig.load();
265 		} catch (ConfigInvalidException e1) {
266 			IOException e2 = new IOException(JGitText.get().unknownRepositoryFormat);
267 			e2.initCause(e1);
268 			throw e2;
269 		}
270 	}
271 
272 	/**
273 	 * Create a new Git repository initializing the necessary files and
274 	 * directories.
275 	 *
276 	 * @param bare
277 	 *            if true, a bare repository is created.
278 	 *
279 	 * @throws IOException
280 	 *             in case of IO problem
281 	 */
282 	public void create(boolean bare) throws IOException {
283 		final FileBasedConfig cfg = getConfig();
284 		if (cfg.getFile().exists()) {
285 			throw new IllegalStateException(MessageFormat.format(
286 					JGitText.get().repositoryAlreadyExists, getDirectory()));
287 		}
288 		FileUtils.mkdirs(getDirectory(), true);
289 		HideDotFiles hideDotFiles = getConfig().getEnum(
290 				ConfigConstants.CONFIG_CORE_SECTION, null,
291 				ConfigConstants.CONFIG_KEY_HIDEDOTFILES,
292 				HideDotFiles.DOTGITONLY);
293 		if (hideDotFiles != HideDotFiles.FALSE && !isBare()
294 				&& getDirectory().getName().startsWith(".")) //$NON-NLS-1$
295 			getFS().setHidden(getDirectory(), true);
296 		refs.create();
297 		objectDatabase.create();
298 
299 		FileUtils.mkdir(new File(getDirectory(), "branches")); //$NON-NLS-1$
300 		FileUtils.mkdir(new File(getDirectory(), "hooks")); //$NON-NLS-1$
301 
302 		RefUpdate head = updateRef(Constants.HEAD);
303 		head.disableRefLog();
304 		head.link(Constants.R_HEADS + Constants.MASTER);
305 
306 		final boolean fileMode;
307 		if (getFS().supportsExecute()) {
308 			File tmp = File.createTempFile("try", "execute", getDirectory()); //$NON-NLS-1$ //$NON-NLS-2$
309 
310 			getFS().setExecute(tmp, true);
311 			final boolean on = getFS().canExecute(tmp);
312 
313 			getFS().setExecute(tmp, false);
314 			final boolean off = getFS().canExecute(tmp);
315 			FileUtils.delete(tmp);
316 
317 			fileMode = on && !off;
318 		} else {
319 			fileMode = false;
320 		}
321 
322 		SymLinks symLinks = SymLinks.FALSE;
323 		if (getFS().supportsSymlinks()) {
324 			File tmp = new File(getDirectory(), "tmplink"); //$NON-NLS-1$
325 			try {
326 				getFS().createSymLink(tmp, "target"); //$NON-NLS-1$
327 				symLinks = null;
328 				FileUtils.delete(tmp);
329 			} catch (IOException e) {
330 				// Normally a java.nio.file.FileSystemException
331 			}
332 		}
333 		if (symLinks != null)
334 			cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
335 					ConfigConstants.CONFIG_KEY_SYMLINKS, symLinks.name()
336 							.toLowerCase());
337 		cfg.setInt(ConfigConstants.CONFIG_CORE_SECTION, null,
338 				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
339 		cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
340 				ConfigConstants.CONFIG_KEY_FILEMODE, fileMode);
341 		if (bare)
342 			cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
343 					ConfigConstants.CONFIG_KEY_BARE, true);
344 		cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
345 				ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, !bare);
346 		if (SystemReader.getInstance().isMacOS())
347 			// Java has no other way
348 			cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
349 					ConfigConstants.CONFIG_KEY_PRECOMPOSEUNICODE, true);
350 		if (!bare) {
351 			File workTree = getWorkTree();
352 			if (!getDirectory().getParentFile().equals(workTree)) {
353 				cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
354 						ConfigConstants.CONFIG_KEY_WORKTREE, getWorkTree()
355 								.getAbsolutePath());
356 				LockFile dotGitLockFile = new LockFile(new File(workTree,
357 						Constants.DOT_GIT));
358 				try {
359 					if (dotGitLockFile.lock()) {
360 						dotGitLockFile.write(Constants.encode(Constants.GITDIR
361 								+ getDirectory().getAbsolutePath()));
362 						dotGitLockFile.commit();
363 					}
364 				} finally {
365 					dotGitLockFile.unlock();
366 				}
367 			}
368 		}
369 		cfg.save();
370 	}
371 
372 	/**
373 	 * @return the directory containing the objects owned by this repository.
374 	 */
375 	public File getObjectsDirectory() {
376 		return objectDatabase.getDirectory();
377 	}
378 
379 	/**
380 	 * @return the object database which stores this repository's data.
381 	 */
382 	public ObjectDirectory getObjectDatabase() {
383 		return objectDatabase;
384 	}
385 
386 	/** @return the reference database which stores the reference namespace. */
387 	public RefDatabase getRefDatabase() {
388 		return refs;
389 	}
390 
391 	/**
392 	 * @return the configuration of this repository
393 	 */
394 	public FileBasedConfig getConfig() {
395 		if (systemConfig.isOutdated()) {
396 			try {
397 				loadSystemConfig();
398 			} catch (IOException e) {
399 				throw new RuntimeException(e);
400 			}
401 		}
402 		if (userConfig.isOutdated()) {
403 			try {
404 				loadUserConfig();
405 			} catch (IOException e) {
406 				throw new RuntimeException(e);
407 			}
408 		}
409 		if (repoConfig.isOutdated()) {
410 				try {
411 					loadRepoConfig();
412 				} catch (IOException e) {
413 					throw new RuntimeException(e);
414 				}
415 		}
416 		return repoConfig;
417 	}
418 
419 	/**
420 	 * Objects known to exist but not expressed by {@link #getAllRefs()}.
421 	 * <p>
422 	 * When a repository borrows objects from another repository, it can
423 	 * advertise that it safely has that other repository's references, without
424 	 * exposing any other details about the other repository.  This may help
425 	 * a client trying to push changes avoid pushing more than it needs to.
426 	 *
427 	 * @return unmodifiable collection of other known objects.
428 	 */
429 	public Set<ObjectId> getAdditionalHaves() {
430 		HashSet<ObjectId> r = new HashSet<ObjectId>();
431 		for (AlternateHandle d : objectDatabase.myAlternates()) {
432 			if (d instanceof AlternateRepository) {
433 				Repository repo;
434 
435 				repo = ((AlternateRepository) d).repository;
436 				for (Ref ref : repo.getAllRefs().values()) {
437 					if (ref.getObjectId() != null)
438 						r.add(ref.getObjectId());
439 					if (ref.getPeeledObjectId() != null)
440 						r.add(ref.getPeeledObjectId());
441 				}
442 				r.addAll(repo.getAdditionalHaves());
443 			}
444 		}
445 		return r;
446 	}
447 
448 	/**
449 	 * Add a single existing pack to the list of available pack files.
450 	 *
451 	 * @param pack
452 	 *            path of the pack file to open.
453 	 * @throws IOException
454 	 *             index file could not be opened, read, or is not recognized as
455 	 *             a Git pack file index.
456 	 */
457 	public void openPack(final File pack) throws IOException {
458 		objectDatabase.openPack(pack);
459 	}
460 
461 	@Override
462 	public void scanForRepoChanges() throws IOException {
463 		getRefDatabase().getRefs(ALL); // This will look for changes to refs
464 		detectIndexChanges();
465 	}
466 
467 	/**
468 	 * Detect index changes.
469 	 */
470 	private void detectIndexChanges() {
471 		if (isBare())
472 			return;
473 
474 		File indexFile = getIndexFile();
475 		if (snapshot == null)
476 			snapshot = FileSnapshot.save(indexFile);
477 		else if (snapshot.isModified(indexFile))
478 			notifyIndexChanged();
479 	}
480 
481 	@Override
482 	public void notifyIndexChanged() {
483 		snapshot = FileSnapshot.save(getIndexFile());
484 		fireEvent(new IndexChangedEvent());
485 	}
486 
487 	/**
488 	 * @param refName
489 	 * @return a {@link ReflogReader} for the supplied refname, or null if the
490 	 *         named ref does not exist.
491 	 * @throws IOException the ref could not be accessed.
492 	 */
493 	public ReflogReader getReflogReader(String refName) throws IOException {
494 		Ref ref = findRef(refName);
495 		if (ref != null)
496 			return new ReflogReaderImpl(this, ref.getName());
497 		return null;
498 	}
499 
500 	@Override
501 	public AttributesNodeProvider createAttributesNodeProvider() {
502 		return new AttributesNodeProviderImpl(this);
503 	}
504 
505 	/**
506 	 * Implementation a {@link AttributesNodeProvider} for a
507 	 * {@link FileRepository}.
508 	 *
509 	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
510 	 *
511 	 */
512 	static class AttributesNodeProviderImpl implements
513 			AttributesNodeProvider {
514 
515 		private AttributesNode infoAttributesNode;
516 
517 		private AttributesNode globalAttributesNode;
518 
519 		/**
520 		 * Constructor.
521 		 *
522 		 * @param repo
523 		 *            {@link Repository} that will provide the attribute nodes.
524 		 */
525 		protected AttributesNodeProviderImpl(Repository repo) {
526 			infoAttributesNode = new InfoAttributesNode(repo);
527 			globalAttributesNode = new GlobalAttributesNode(repo);
528 		}
529 
530 		public AttributesNode getInfoAttributesNode() throws IOException {
531 			if (infoAttributesNode instanceof InfoAttributesNode)
532 				infoAttributesNode = ((InfoAttributesNode) infoAttributesNode)
533 						.load();
534 			return infoAttributesNode;
535 		}
536 
537 		public AttributesNode getGlobalAttributesNode() throws IOException {
538 			if (globalAttributesNode instanceof GlobalAttributesNode)
539 				globalAttributesNode = ((GlobalAttributesNode) globalAttributesNode)
540 						.load();
541 			return globalAttributesNode;
542 		}
543 
544 		static void loadRulesFromFile(AttributesNode r, File attrs)
545 				throws FileNotFoundException, IOException {
546 			if (attrs.exists()) {
547 				FileInputStream in = new FileInputStream(attrs);
548 				try {
549 					r.parse(in);
550 				} finally {
551 					in.close();
552 				}
553 			}
554 		}
555 
556 	}
557 
558 }