View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.dfs;
12  
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.text.MessageFormat;
16  import java.util.Collections;
17  
18  import org.eclipse.jgit.attributes.AttributesNode;
19  import org.eclipse.jgit.attributes.AttributesNodeProvider;
20  import org.eclipse.jgit.attributes.AttributesRule;
21  import org.eclipse.jgit.internal.JGitText;
22  import org.eclipse.jgit.lib.Constants;
23  import org.eclipse.jgit.lib.RefUpdate;
24  import org.eclipse.jgit.lib.ReflogReader;
25  import org.eclipse.jgit.lib.Repository;
26  import org.eclipse.jgit.lib.StoredConfig;
27  
28  /**
29   * A Git repository on a DFS.
30   */
31  public abstract class DfsRepository extends Repository {
32  	private final DfsConfig config;
33  
34  	private final DfsRepositoryDescription description;
35  
36  	/**
37  	 * Initialize a DFS repository.
38  	 *
39  	 * @param builder
40  	 *            description of the repository.
41  	 */
42  	protected DfsRepository(DfsRepositoryBuilder builder) {
43  		super(builder);
44  		this.config = new DfsConfig();
45  		this.description = builder.getRepositoryDescription();
46  	}
47  
48  	/** {@inheritDoc} */
49  	@Override
50  	public abstract DfsObjDatabase getObjectDatabase();
51  
52  	/**
53  	 * Get the description of this repository.
54  	 *
55  	 * @return the description of this repository.
56  	 */
57  	public DfsRepositoryDescription getDescription() {
58  		return description;
59  	}
60  
61  	/**
62  	 * Check if the repository already exists.
63  	 *
64  	 * @return true if the repository exists; false if it is new.
65  	 * @throws java.io.IOException
66  	 *             the repository cannot be checked.
67  	 */
68  	public boolean exists() throws IOException {
69  		if (getRefDatabase() instanceof DfsRefDatabase) {
70  			return ((DfsRefDatabase) getRefDatabase()).exists();
71  		}
72  		return true;
73  	}
74  
75  	/** {@inheritDoc} */
76  	@Override
77  	public void create(boolean bare) throws IOException {
78  		if (exists())
79  			throw new IOException(MessageFormat.format(
80  					JGitText.get().repositoryAlreadyExists, "")); //$NON-NLS-1$
81  
82  		String master = Constants.R_HEADS + Constants.MASTER;
83  		RefUpdate.Result result = updateRef(Constants.HEAD, true).link(master);
84  		if (result != RefUpdate.Result.NEW)
85  			throw new IOException(result.name());
86  	}
87  
88  	/** {@inheritDoc} */
89  	@Override
90  	public StoredConfig getConfig() {
91  		return config;
92  	}
93  
94  	/** {@inheritDoc} */
95  	@Override
96  	public String getIdentifier() {
97  		return getDescription().getRepositoryName();
98  	}
99  
100 	/** {@inheritDoc} */
101 	@Override
102 	public void scanForRepoChanges() throws IOException {
103 		getRefDatabase().refresh();
104 		getObjectDatabase().clearCache();
105 	}
106 
107 	/** {@inheritDoc} */
108 	@Override
109 	public void notifyIndexChanged(boolean internal) {
110 		// Do not send notifications.
111 		// There is no index, as there is no working tree.
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public ReflogReader getReflogReader(String refName) throws IOException {
117 		throw new UnsupportedOperationException();
118 	}
119 
120 	/** {@inheritDoc} */
121 	@Override
122 	public AttributesNodeProvider createAttributesNodeProvider() {
123 		// TODO Check if the implementation used in FileRepository can be used
124 		// for this kind of repository
125 		return new EmptyAttributesNodeProvider();
126 	}
127 
128 	private static class EmptyAttributesNodeProvider implements
129 			AttributesNodeProvider {
130 		private EmptyAttributesNode emptyAttributesNode = new EmptyAttributesNode();
131 
132 		@Override
133 		public AttributesNode getInfoAttributesNode() throws IOException {
134 			return emptyAttributesNode;
135 		}
136 
137 		@Override
138 		public AttributesNode getGlobalAttributesNode() throws IOException {
139 			return emptyAttributesNode;
140 		}
141 
142 		private static class EmptyAttributesNode extends AttributesNode {
143 
144 			public EmptyAttributesNode() {
145 				super(Collections.<AttributesRule> emptyList());
146 			}
147 
148 			@Override
149 			public void parse(InputStream in) throws IOException {
150 				// Do nothing
151 			}
152 		}
153 	}
154 }