1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lib;
12
13 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
14 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BARE;
15 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WORKTREE;
16 import static org.eclipse.jgit.lib.Constants.DOT_GIT;
17 import static org.eclipse.jgit.lib.Constants.GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY;
18 import static org.eclipse.jgit.lib.Constants.GIT_CEILING_DIRECTORIES_KEY;
19 import static org.eclipse.jgit.lib.Constants.GIT_DIR_KEY;
20 import static org.eclipse.jgit.lib.Constants.GIT_INDEX_FILE_KEY;
21 import static org.eclipse.jgit.lib.Constants.GIT_OBJECT_DIRECTORY_KEY;
22 import static org.eclipse.jgit.lib.Constants.GIT_WORK_TREE_KEY;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.text.MessageFormat;
27 import java.util.Collection;
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import org.eclipse.jgit.annotations.NonNull;
32 import org.eclipse.jgit.api.errors.InvalidRefNameException;
33 import org.eclipse.jgit.errors.ConfigInvalidException;
34 import org.eclipse.jgit.errors.RepositoryNotFoundException;
35 import org.eclipse.jgit.internal.JGitText;
36 import org.eclipse.jgit.internal.storage.file.FileRepository;
37 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
38 import org.eclipse.jgit.storage.file.FileBasedConfig;
39 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
40 import org.eclipse.jgit.util.FS;
41 import org.eclipse.jgit.util.IO;
42 import org.eclipse.jgit.util.RawParseUtils;
43 import org.eclipse.jgit.util.StringUtils;
44 import org.eclipse.jgit.util.SystemReader;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Repository> {
60 private static boolean isSymRef(byte[] ref) {
61 if (ref.length < 9)
62 return false;
63 return ref[0] == 'g'
64 && ref[1] == 'i'
65 && ref[2] == 't'
66 && ref[3] == 'd'
67 && ref[4] == 'i'
68 && ref[5] == 'r'
69 && ref[6] == ':'
70 && ref[7] == ' ';
71 }
72
73 private static File getSymRef(File workTree, File dotGit, FS fs)
74 throws IOException {
75 byte[] content = IO.readFully(dotGit);
76 if (!isSymRef(content)) {
77 throw new IOException(MessageFormat.format(
78 JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath()));
79 }
80
81 int pathStart = 8;
82 int lineEnd = RawParseUtils.nextLF(content, pathStart);
83 while (content[lineEnd - 1] == '\n' ||
84 (content[lineEnd - 1] == '\r'
85 && SystemReader.getInstance().isWindows())) {
86 lineEnd--;
87 }
88 if (lineEnd == pathStart) {
89 throw new IOException(MessageFormat.format(
90 JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath()));
91 }
92
93 String gitdirPath = RawParseUtils.decode(content, pathStart, lineEnd);
94 File gitdirFile = fs.resolve(workTree, gitdirPath);
95 if (gitdirFile.isAbsolute()) {
96 return gitdirFile;
97 }
98 return new File(workTree, gitdirPath).getCanonicalFile();
99 }
100
101 private FS fs;
102
103 private File gitDir;
104
105 private File objectDirectory;
106
107 private List<File> alternateObjectDirectories;
108
109 private File indexFile;
110
111 private File workTree;
112
113 private String initialBranch = Constants.MASTER;
114
115
116 private List<File> ceilingDirectories;
117
118
119 private boolean bare;
120
121
122 private boolean mustExist;
123
124
125 private Config config;
126
127
128
129
130
131
132
133
134 public B setFS(FS fs) {
135 this.fs = fs;
136 return self();
137 }
138
139
140
141
142
143
144 public FS getFS() {
145 return fs;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 public B setGitDir(File gitDir) {
160 this.gitDir = gitDir;
161 this.config = null;
162 return self();
163 }
164
165
166
167
168
169
170 public File getGitDir() {
171 return gitDir;
172 }
173
174
175
176
177
178
179
180
181
182 public B setObjectDirectory(File objectDirectory) {
183 this.objectDirectory = objectDirectory;
184 return self();
185 }
186
187
188
189
190
191
192 public File getObjectDirectory() {
193 return objectDirectory;
194 }
195
196
197
198
199
200
201
202
203
204
205
206 public B addAlternateObjectDirectory(File other) {
207 if (other != null) {
208 if (alternateObjectDirectories == null)
209 alternateObjectDirectories = new LinkedList<>();
210 alternateObjectDirectories.add(other);
211 }
212 return self();
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 public B addAlternateObjectDirectories(Collection<File> inList) {
227 if (inList != null) {
228 for (File path : inList)
229 addAlternateObjectDirectory(path);
230 }
231 return self();
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245 public B addAlternateObjectDirectories(File[] inList) {
246 if (inList != null) {
247 for (File path : inList)
248 addAlternateObjectDirectory(path);
249 }
250 return self();
251 }
252
253
254
255
256
257
258 public File[] getAlternateObjectDirectories() {
259 final List<File> alts = alternateObjectDirectories;
260 if (alts == null)
261 return null;
262 return alts.toArray(new File[0]);
263 }
264
265
266
267
268
269
270
271
272
273 public B setBare() {
274 setIndexFile(null);
275 setWorkTree(null);
276 bare = true;
277 return self();
278 }
279
280
281
282
283
284
285 public boolean isBare() {
286 return bare;
287 }
288
289
290
291
292
293
294
295
296
297 public B setMustExist(boolean mustExist) {
298 this.mustExist = mustExist;
299 return self();
300 }
301
302
303
304
305
306
307 public boolean isMustExist() {
308 return mustExist;
309 }
310
311
312
313
314
315
316
317
318 public B setWorkTree(File workTree) {
319 this.workTree = workTree;
320 return self();
321 }
322
323
324
325
326
327
328 public File getWorkTree() {
329 return workTree;
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343 public B setIndexFile(File indexFile) {
344 this.indexFile = indexFile;
345 return self();
346 }
347
348
349
350
351
352
353 public File getIndexFile() {
354 return indexFile;
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 public B setInitialBranch(String branch) throws InvalidRefNameException {
372 if (StringUtils.isEmptyOrNull(branch)) {
373 this.initialBranch = Constants.MASTER;
374 } else {
375 if (!Repository.isValidRefName(Constants.R_HEADS + branch)) {
376 throw new InvalidRefNameException(MessageFormat
377 .format(JGitText.get().branchNameInvalid, branch));
378 }
379 this.initialBranch = branch;
380 }
381 return self();
382 }
383
384
385
386
387
388
389
390 public @NonNull String getInitialBranch() {
391 return initialBranch;
392 }
393
394
395
396
397
398
399
400
401
402
403
404 public B readEnvironment() {
405 return readEnvironment(SystemReader.getInstance());
406 }
407
408
409
410
411
412
413
414
415
416
417
418
419
420 public B readEnvironment(SystemReader sr) {
421 if (getGitDir() == null) {
422 String val = sr.getenv(GIT_DIR_KEY);
423 if (val != null)
424 setGitDir(new File(val));
425 }
426
427 if (getObjectDirectory() == null) {
428 String val = sr.getenv(GIT_OBJECT_DIRECTORY_KEY);
429 if (val != null)
430 setObjectDirectory(new File(val));
431 }
432
433 if (getAlternateObjectDirectories() == null) {
434 String val = sr.getenv(GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY);
435 if (val != null) {
436 for (String path : val.split(File.pathSeparator))
437 addAlternateObjectDirectory(new File(path));
438 }
439 }
440
441 if (getWorkTree() == null) {
442 String val = sr.getenv(GIT_WORK_TREE_KEY);
443 if (val != null)
444 setWorkTree(new File(val));
445 }
446
447 if (getIndexFile() == null) {
448 String val = sr.getenv(GIT_INDEX_FILE_KEY);
449 if (val != null)
450 setIndexFile(new File(val));
451 }
452
453 if (ceilingDirectories == null) {
454 String val = sr.getenv(GIT_CEILING_DIRECTORIES_KEY);
455 if (val != null) {
456 for (String path : val.split(File.pathSeparator))
457 addCeilingDirectory(new File(path));
458 }
459 }
460
461 return self();
462 }
463
464
465
466
467
468
469
470
471
472
473
474 public B addCeilingDirectory(File root) {
475 if (root != null) {
476 if (ceilingDirectories == null)
477 ceilingDirectories = new LinkedList<>();
478 ceilingDirectories.add(root);
479 }
480 return self();
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494 public B addCeilingDirectories(Collection<File> inList) {
495 if (inList != null) {
496 for (File path : inList)
497 addCeilingDirectory(path);
498 }
499 return self();
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513 public B addCeilingDirectories(File[] inList) {
514 if (inList != null) {
515 for (File path : inList)
516 addCeilingDirectory(path);
517 }
518 return self();
519 }
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 public B findGitDir() {
535 if (getGitDir() == null)
536 findGitDir(new File("").getAbsoluteFile());
537 return self();
538 }
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555 public B findGitDir(File current) {
556 if (getGitDir() == null) {
557 FS tryFS = safeFS();
558 while (current != null) {
559 File dir = new File(current, DOT_GIT);
560 if (FileKey.isGitRepository(dir, tryFS)) {
561 setGitDir(dir);
562 break;
563 } else if (dir.isFile()) {
564 try {
565 setGitDir(getSymRef(current, dir, tryFS));
566 break;
567 } catch (IOException ignored) {
568
569 }
570 } else if (FileKey.isGitRepository(current, tryFS)) {
571 setGitDir(current);
572 break;
573 }
574
575 current = current.getParentFile();
576 if (current != null && ceilingDirectories != null
577 && ceilingDirectories.contains(current))
578 break;
579 }
580 }
581 return self();
582 }
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599 public B setup() throws IllegalArgumentException, IOException {
600 requireGitDirOrWorkTree();
601 setupGitDir();
602 setupWorkTree();
603 setupInternals();
604 return self();
605 }
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623 @SuppressWarnings({ "unchecked", "resource" })
624 public R build() throws IOException {
625 R repo = (R) new FileRepository(setup());
626 if (isMustExist() && !repo.getObjectDatabase().exists())
627 throw new RepositoryNotFoundException(getGitDir());
628 return repo;
629 }
630
631
632
633
634 protected void requireGitDirOrWorkTree() {
635 if (getGitDir() == null && getWorkTree() == null)
636 throw new IllegalArgumentException(
637 JGitText.get().eitherGitDirOrWorkTreeRequired);
638 }
639
640
641
642
643
644
645
646 protected void setupGitDir() throws IOException {
647
648
649 if (getGitDir() == null && getWorkTree() != null) {
650 File dotGit = new File(getWorkTree(), DOT_GIT);
651 if (!dotGit.isFile())
652 setGitDir(dotGit);
653 else
654 setGitDir(getSymRef(getWorkTree(), dotGit, safeFS()));
655 }
656 }
657
658
659
660
661
662
663
664
665
666
667
668 protected void setupWorkTree() throws IOException {
669 if (getFS() == null)
670 setFS(FS.DETECTED);
671
672
673
674 if (!isBare() && getWorkTree() == null)
675 setWorkTree(guessWorkTreeOrFail());
676
677 if (!isBare()) {
678
679
680
681
682 if (getGitDir() == null)
683 setGitDir(getWorkTree().getParentFile());
684 if (getIndexFile() == null)
685 setIndexFile(new File(getGitDir(), "index"));
686 }
687 }
688
689
690
691
692
693
694
695 protected void setupInternals() throws IOException {
696 if (getObjectDirectory() == null && getGitDir() != null)
697 setObjectDirectory(safeFS().resolve(getGitDir(), Constants.OBJECTS));
698 }
699
700
701
702
703
704
705
706
707 protected Config getConfig() throws IOException {
708 if (config == null)
709 config = loadConfig();
710 return config;
711 }
712
713
714
715
716
717
718
719
720
721
722
723 protected Config loadConfig() throws IOException {
724 if (getGitDir() != null) {
725
726
727
728
729 File path = safeFS().resolve(getGitDir(), Constants.CONFIG);
730 FileBasedConfig cfg = new FileBasedConfig(path, safeFS());
731 try {
732 cfg.load();
733 } catch (ConfigInvalidException err) {
734 throw new IllegalArgumentException(MessageFormat.format(
735 JGitText.get().repositoryConfigFileInvalid, path
736 .getAbsolutePath(), err.getMessage()));
737 }
738 return cfg;
739 }
740 return new Config();
741 }
742
743 private File guessWorkTreeOrFail() throws IOException {
744 final Config cfg = getConfig();
745
746
747
748 String path = cfg.getString(CONFIG_CORE_SECTION, null,
749 CONFIG_KEY_WORKTREE);
750 if (path != null)
751 return safeFS().resolve(getGitDir(), path).getCanonicalFile();
752
753
754
755
756 if (cfg.getString(CONFIG_CORE_SECTION, null, CONFIG_KEY_BARE) != null) {
757 if (cfg.getBoolean(CONFIG_CORE_SECTION, CONFIG_KEY_BARE, true)) {
758 setBare();
759 return null;
760 }
761 return getGitDir().getParentFile();
762 }
763
764 if (getGitDir().getName().equals(DOT_GIT)) {
765
766
767
768 return getGitDir().getParentFile();
769 }
770
771
772
773 setBare();
774 return null;
775 }
776
777
778
779
780
781
782 protected FS safeFS() {
783 return getFS() != null ? getFS() : FS.DETECTED;
784 }
785
786
787
788
789
790
791 @SuppressWarnings("unchecked")
792 protected final B self() {
793 return (B) this;
794 }
795 }