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 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.IOException;
53 import java.text.MessageFormat;
54 import java.util.HashSet;
55 import java.util.Set;
56
57 import org.eclipse.jgit.errors.ConfigInvalidException;
58 import org.eclipse.jgit.events.ConfigChangedEvent;
59 import org.eclipse.jgit.events.ConfigChangedListener;
60 import org.eclipse.jgit.events.IndexChangedEvent;
61 import org.eclipse.jgit.internal.JGitText;
62 import org.eclipse.jgit.internal.storage.file.ObjectDirectory.AlternateHandle;
63 import org.eclipse.jgit.internal.storage.file.ObjectDirectory.AlternateRepository;
64 import org.eclipse.jgit.lib.BaseRepositoryBuilder;
65 import org.eclipse.jgit.lib.ConfigConstants;
66 import org.eclipse.jgit.lib.Constants;
67 import org.eclipse.jgit.lib.CoreConfig.HideDotFiles;
68 import org.eclipse.jgit.lib.CoreConfig.SymLinks;
69 import org.eclipse.jgit.lib.ObjectId;
70 import org.eclipse.jgit.lib.Ref;
71 import org.eclipse.jgit.lib.RefDatabase;
72 import org.eclipse.jgit.lib.RefUpdate;
73 import org.eclipse.jgit.lib.ReflogReader;
74 import org.eclipse.jgit.lib.Repository;
75 import org.eclipse.jgit.storage.file.FileBasedConfig;
76 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
77 import org.eclipse.jgit.util.FS;
78 import org.eclipse.jgit.util.FileUtils;
79 import org.eclipse.jgit.util.StringUtils;
80 import org.eclipse.jgit.util.SystemReader;
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 public class FileRepository extends Repository {
108 private final FileBasedConfig systemConfig;
109
110 private final FileBasedConfig userConfig;
111
112 private final FileBasedConfig repoConfig;
113
114 private final RefDatabase refs;
115
116 private final ObjectDirectory objectDatabase;
117
118 private FileSnapshot snapshot;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public FileRepository(final File gitDir) throws IOException {
140 this(new FileRepositoryBuilder().setGitDir(gitDir).setup());
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public FileRepository(final String gitDir) throws IOException {
154 this(new File(gitDir));
155 }
156
157
158
159
160
161
162
163
164
165
166 public FileRepository(final BaseRepositoryBuilder options) throws IOException {
167 super(options);
168
169 if (StringUtils.isEmptyOrNull(SystemReader.getInstance().getenv(
170 Constants.GIT_CONFIG_NOSYSTEM_KEY)))
171 systemConfig = SystemReader.getInstance().openSystemConfig(null,
172 getFS());
173 else
174 systemConfig = new FileBasedConfig(null, FS.DETECTED) {
175 public void load() {
176
177 }
178
179 public boolean isOutdated() {
180
181 return false;
182 }
183 };
184 userConfig = SystemReader.getInstance().openUserConfig(systemConfig,
185 getFS());
186 repoConfig = new FileBasedConfig(userConfig, getFS().resolve(
187 getDirectory(), Constants.CONFIG),
188 getFS());
189
190 loadSystemConfig();
191 loadUserConfig();
192 loadRepoConfig();
193
194 repoConfig.addChangeListener(new ConfigChangedListener() {
195 public void onConfigChanged(ConfigChangedEvent event) {
196 fireEvent(event);
197 }
198 });
199
200 refs = new RefDirectory(this);
201 objectDatabase = new ObjectDirectory(repoConfig,
202 options.getObjectDirectory(),
203 options.getAlternateObjectDirectories(),
204 getFS(),
205 new File(getDirectory(), Constants.SHALLOW));
206
207 if (objectDatabase.exists()) {
208 final long repositoryFormatVersion = getConfig().getLong(
209 ConfigConstants.CONFIG_CORE_SECTION, null,
210 ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
211 if (repositoryFormatVersion > 0)
212 throw new IOException(MessageFormat.format(
213 JGitText.get().unknownRepositoryFormat2,
214 Long.valueOf(repositoryFormatVersion)));
215 }
216
217 if (!isBare())
218 snapshot = FileSnapshot.save(getIndexFile());
219 }
220
221 private void loadSystemConfig() throws IOException {
222 try {
223 systemConfig.load();
224 } catch (ConfigInvalidException e1) {
225 IOException e2 = new IOException(MessageFormat.format(JGitText
226 .get().systemConfigFileInvalid, systemConfig.getFile()
227 .getAbsolutePath(), e1));
228 e2.initCause(e1);
229 throw e2;
230 }
231 }
232
233 private void loadUserConfig() throws IOException {
234 try {
235 userConfig.load();
236 } catch (ConfigInvalidException e1) {
237 IOException e2 = new IOException(MessageFormat.format(JGitText
238 .get().userConfigFileInvalid, userConfig.getFile()
239 .getAbsolutePath(), e1));
240 e2.initCause(e1);
241 throw e2;
242 }
243 }
244
245 private void loadRepoConfig() throws IOException {
246 try {
247 repoConfig.load();
248 } catch (ConfigInvalidException e1) {
249 IOException e2 = new IOException(JGitText.get().unknownRepositoryFormat);
250 e2.initCause(e1);
251 throw e2;
252 }
253 }
254
255
256
257
258
259
260
261
262
263
264
265 public void create(boolean bare) throws IOException {
266 final FileBasedConfig cfg = getConfig();
267 if (cfg.getFile().exists()) {
268 throw new IllegalStateException(MessageFormat.format(
269 JGitText.get().repositoryAlreadyExists, getDirectory()));
270 }
271 FileUtils.mkdirs(getDirectory(), true);
272 HideDotFiles hideDotFiles = getConfig().getEnum(
273 ConfigConstants.CONFIG_CORE_SECTION, null,
274 ConfigConstants.CONFIG_KEY_HIDEDOTFILES,
275 HideDotFiles.DOTGITONLY);
276 if (hideDotFiles != HideDotFiles.FALSE && !isBare()
277 && getDirectory().getName().startsWith("."))
278 getFS().setHidden(getDirectory(), true);
279 refs.create();
280 objectDatabase.create();
281
282 FileUtils.mkdir(new File(getDirectory(), "branches"));
283 FileUtils.mkdir(new File(getDirectory(), "hooks"));
284
285 RefUpdate head = updateRef(Constants.HEAD);
286 head.disableRefLog();
287 head.link(Constants.R_HEADS + Constants.MASTER);
288
289 final boolean fileMode;
290 if (getFS().supportsExecute()) {
291 File tmp = File.createTempFile("try", "execute", getDirectory());
292
293 getFS().setExecute(tmp, true);
294 final boolean on = getFS().canExecute(tmp);
295
296 getFS().setExecute(tmp, false);
297 final boolean off = getFS().canExecute(tmp);
298 FileUtils.delete(tmp);
299
300 fileMode = on && !off;
301 } else {
302 fileMode = false;
303 }
304
305 SymLinks symLinks = SymLinks.FALSE;
306 if (getFS().supportsSymlinks()) {
307 File tmp = new File(getDirectory(), "tmplink");
308 try {
309 getFS().createSymLink(tmp, "target");
310 symLinks = null;
311 FileUtils.delete(tmp);
312 } catch (IOException e) {
313
314 }
315 }
316 if (symLinks != null)
317 cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
318 ConfigConstants.CONFIG_KEY_SYMLINKS, symLinks.name()
319 .toLowerCase());
320 cfg.setInt(ConfigConstants.CONFIG_CORE_SECTION, null,
321 ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
322 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
323 ConfigConstants.CONFIG_KEY_FILEMODE, fileMode);
324 if (bare)
325 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
326 ConfigConstants.CONFIG_KEY_BARE, true);
327 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
328 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, !bare);
329 if (SystemReader.getInstance().isMacOS())
330
331 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
332 ConfigConstants.CONFIG_KEY_PRECOMPOSEUNICODE, true);
333 if (!bare) {
334 File workTree = getWorkTree();
335 if (!getDirectory().getParentFile().equals(workTree)) {
336 cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
337 ConfigConstants.CONFIG_KEY_WORKTREE, getWorkTree()
338 .getAbsolutePath());
339 LockFile dotGitLockFile = new LockFile(new File(workTree,
340 Constants.DOT_GIT), getFS());
341 try {
342 if (dotGitLockFile.lock()) {
343 dotGitLockFile.write(Constants.encode(Constants.GITDIR
344 + getDirectory().getAbsolutePath()));
345 dotGitLockFile.commit();
346 }
347 } finally {
348 dotGitLockFile.unlock();
349 }
350 }
351 }
352 cfg.save();
353 }
354
355
356
357
358 public File getObjectsDirectory() {
359 return objectDatabase.getDirectory();
360 }
361
362
363
364
365 public ObjectDirectory getObjectDatabase() {
366 return objectDatabase;
367 }
368
369
370 public RefDatabase getRefDatabase() {
371 return refs;
372 }
373
374
375
376
377 public FileBasedConfig getConfig() {
378 if (systemConfig.isOutdated()) {
379 try {
380 loadSystemConfig();
381 } catch (IOException e) {
382 throw new RuntimeException(e);
383 }
384 }
385 if (userConfig.isOutdated()) {
386 try {
387 loadUserConfig();
388 } catch (IOException e) {
389 throw new RuntimeException(e);
390 }
391 }
392 if (repoConfig.isOutdated()) {
393 try {
394 loadRepoConfig();
395 } catch (IOException e) {
396 throw new RuntimeException(e);
397 }
398 }
399 return repoConfig;
400 }
401
402
403
404
405
406
407
408
409
410
411
412 public Set<ObjectId> getAdditionalHaves() {
413 HashSet<ObjectId> r = new HashSet<ObjectId>();
414 for (AlternateHandle d : objectDatabase.myAlternates()) {
415 if (d instanceof AlternateRepository) {
416 Repository repo;
417
418 repo = ((AlternateRepository) d).repository;
419 for (Ref ref : repo.getAllRefs().values()) {
420 if (ref.getObjectId() != null)
421 r.add(ref.getObjectId());
422 if (ref.getPeeledObjectId() != null)
423 r.add(ref.getPeeledObjectId());
424 }
425 r.addAll(repo.getAdditionalHaves());
426 }
427 }
428 return r;
429 }
430
431
432
433
434
435
436
437
438
439
440 public void openPack(final File pack) throws IOException {
441 objectDatabase.openPack(pack);
442 }
443
444 @Override
445 public void scanForRepoChanges() throws IOException {
446 getRefDatabase().getRefs(ALL);
447 detectIndexChanges();
448 }
449
450
451
452
453 private void detectIndexChanges() {
454 if (isBare())
455 return;
456
457 File indexFile = getIndexFile();
458 if (snapshot == null)
459 snapshot = FileSnapshot.save(indexFile);
460 else if (snapshot.isModified(indexFile))
461 notifyIndexChanged();
462 }
463
464 @Override
465 public void notifyIndexChanged() {
466 snapshot = FileSnapshot.save(getIndexFile());
467 fireEvent(new IndexChangedEvent());
468 }
469
470
471
472
473
474
475
476 public ReflogReader getReflogReader(String refName) throws IOException {
477 Ref ref = getRef(refName);
478 if (ref != null)
479 return new ReflogReaderImpl(this, ref.getName());
480 return null;
481 }
482 }