1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.text.MessageFormat;
15 import java.util.concurrent.Callable;
16
17 import org.eclipse.jgit.api.errors.GitAPIException;
18 import org.eclipse.jgit.api.errors.JGitInternalException;
19 import org.eclipse.jgit.internal.JGitText;
20 import org.eclipse.jgit.lib.Constants;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.lib.RepositoryBuilder;
23 import org.eclipse.jgit.util.FS;
24 import org.eclipse.jgit.util.SystemReader;
25
26
27
28
29
30
31
32 public class InitCommand implements Callable<Git> {
33 private File directory;
34
35 private File gitDir;
36
37 private boolean bare;
38
39 private FS fs;
40
41
42
43
44
45
46
47
48
49 @Override
50 public Git call() throws GitAPIException {
51 try {
52 RepositoryBuilder builder = new RepositoryBuilder();
53 if (bare)
54 builder.setBare();
55 if (fs != null) {
56 builder.setFS(fs);
57 }
58 builder.readEnvironment();
59 if (gitDir != null)
60 builder.setGitDir(gitDir);
61 else
62 gitDir = builder.getGitDir();
63 if (directory != null) {
64 if (bare)
65 builder.setGitDir(directory);
66 else {
67 builder.setWorkTree(directory);
68 if (gitDir == null)
69 builder.setGitDir(new File(directory, Constants.DOT_GIT));
70 }
71 } else if (builder.getGitDir() == null) {
72 String dStr = SystemReader.getInstance()
73 .getProperty("user.dir");
74 if (dStr == null)
75 dStr = ".";
76 File d = new File(dStr);
77 if (!bare)
78 d = new File(d, Constants.DOT_GIT);
79 builder.setGitDir(d);
80 } else {
81
82 if (!bare) {
83 String dStr = SystemReader.getInstance().getProperty(
84 "user.dir");
85 if (dStr == null)
86 dStr = ".";
87 builder.setWorkTree(new File(dStr));
88 }
89 }
90 Repository repository = builder.build();
91 if (!repository.getObjectDatabase().exists())
92 repository.create(bare);
93 return new Git(repository, true);
94 } catch (IOException e) {
95 throw new JGitInternalException(e.getMessage(), e);
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public InitCommand setDirectory(File directory)
113 throws IllegalStateException {
114 validateDirs(directory, gitDir, bare);
115 this.directory = directory;
116 return this;
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public InitCommand setGitDir(File gitDir)
133 throws IllegalStateException {
134 validateDirs(directory, gitDir, bare);
135 this.gitDir = gitDir;
136 return this;
137 }
138
139 private static void validateDirs(File directory, File gitDir, boolean bare)
140 throws IllegalStateException {
141 if (directory != null) {
142 if (bare) {
143 if (gitDir != null && !gitDir.equals(directory))
144 throw new IllegalStateException(MessageFormat.format(
145 JGitText.get().initFailedBareRepoDifferentDirs,
146 gitDir, directory));
147 } else {
148 if (gitDir != null && gitDir.equals(directory))
149 throw new IllegalStateException(MessageFormat.format(
150 JGitText.get().initFailedNonBareRepoSameDirs,
151 gitDir, directory));
152 }
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public InitCommand setBare(boolean bare) {
169 validateDirs(directory, gitDir, bare);
170 this.bare = bare;
171 return this;
172 }
173
174
175
176
177
178
179
180
181
182
183 public InitCommand setFs(FS fs) {
184 this.fs = fs;
185 return this;
186 }
187 }