/
InitOp.java
248 lines (215 loc) · 9.71 KB
/
InitOp.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* Copyright (c) 2012-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.porcelain;
import static java.util.Optional.empty;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import org.locationtech.geogig.di.CanRunDuringConflict;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.Ref;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.plumbing.RefParse;
import org.locationtech.geogig.plumbing.ResolveGeogigURI;
import org.locationtech.geogig.plumbing.UpdateRef;
import org.locationtech.geogig.plumbing.UpdateSymRef;
import org.locationtech.geogig.repository.AbstractGeoGigOp;
import org.locationtech.geogig.repository.Hints;
import org.locationtech.geogig.repository.Platform;
import org.locationtech.geogig.repository.Repository;
import org.locationtech.geogig.repository.RepositoryConnectionException;
import org.locationtech.geogig.repository.RepositoryResolver;
import org.locationtech.geogig.storage.ConfigDatabase;
import org.locationtech.geogig.storage.ConfigException;
import org.locationtech.geogig.storage.ObjectStore;
import org.locationtech.geogig.storage.PluginDefaults;
import org.locationtech.geogig.storage.VersionedFormat;
import org.locationtech.geogig.storage.impl.Blobs;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.inject.Inject;
/**
* Creates or "initializes" a repository in the {@link Platform#pwd() working directory}.
* <p>
* This command tries to find an existing {@code .geogig} repository directory in the current
* directory's hierarchy. It is safe to call it from inside a directory that's a child of a
* repository.
* <p>
* If no repository directory is found, then a new one is created on the current directory.
*
* @see ResolveGeogigURI
* @see RefParse
* @see UpdateRef
* @see UpdateSymRef
*/
@CanRunDuringConflict
public class InitOp extends AbstractGeoGigOp<Repository> {
private Map<String, String> config;
private String filterFile;
private Hints hints;
/**
* Constructs a new {@code InitOp} with the specified parameters.
*
* @param platform where to get the current directory from
* @param hints may contain where to get the repository from (using the
* {@link Hints#REPOSITORY_URL} argument)
*/
@Inject
public InitOp(Hints hints) {
this.config = Maps.newTreeMap();
this.hints = hints;
}
public InitOp setConfig(Map<String, String> suppliedConfiguration) {
this.config = ImmutableMap.copyOf(suppliedConfiguration);
return this;
}
/**
* @deprecated must provide repository URI in {@link Hints} instead
*/
@Deprecated
public InitOp setTarget(File targetRepoDirectory) {
return this;
}
public InitOp setFilterFile(String filterFile) {
this.filterFile = filterFile;
return this;
}
/**
* Executes the Init operation.
*
* @return the initialized repository
* @throws IllegalStateException if a repository cannot be created on the current directory or
* re-initialized in the current dir or one if its parents as determined by
* {@link ResolveGeogigURI}
*/
@Override
protected Repository _call() {
final Platform platform = platform();
Optional<URI> resolvedURI = new ResolveGeogigURI(platform, hints).call();
if (!resolvedURI.isPresent()) {
resolvedURI = Optional.of(platform.pwd().getAbsoluteFile().toURI());
}
URI repoURI = resolvedURI.get();
final RepositoryResolver repoInitializer = RepositoryResolver.lookup(repoURI);
final boolean repoExisted = repoInitializer.repoExists(repoURI);
repoInitializer.initialize(repoURI, context());
Map<String, String> effectiveConfigBuilder = Maps.newTreeMap();
Optional<Serializable> repoName = hints.get(Hints.REPOSITORY_NAME);
if (repoName.isPresent()) {
effectiveConfigBuilder.put("repo.name", String.valueOf(repoName.get()));
}
if (filterFile != null) {
try {
File oldFilterFile = new File(filterFile);
if (!oldFilterFile.exists()) {
throw new FileNotFoundException("No filter file found at " + filterFile + ".");
}
repository().blobStore().putBlob(Blobs.SPARSE_FILTER_BLOB_KEY,
Files.toByteArray(oldFilterFile));
} catch (Exception e) {
throw new IllegalStateException("Unable to copy filter file at path " + filterFile
+ " to the new repository.", e);
}
}
Repository repository;
try {
if (!repoExisted) {
// use a config database appropriate for the kind of repo URI
try (ConfigDatabase configDB = repoInitializer.getConfigDatabase(repoURI,
context)) {
PluginDefaults defaults = context.pluginDefaults();
addDefaults(configDB, defaults, effectiveConfigBuilder);
if (config != null) {
effectiveConfigBuilder.putAll(config);
}
try {
for (Entry<String, String> pair : effectiveConfigBuilder.entrySet()) {
String key = pair.getKey();
String value = pair.getValue();
configDB.put(key, value);
}
repository = repository();
repository.configure();
} catch (RepositoryConnectionException e) {
throw new IllegalStateException(
"Unable to initialize repository for the first time: "
+ e.getMessage(),
e);
}
}
} else {
repository = repository();
}
try {
repository.open();
// make sure the repo has the empty tree
ObjectStore objectDatabase = repository.objectDatabase();
objectDatabase.put(RevTree.EMPTY);
} catch (RepositoryConnectionException e) {
throw new IllegalStateException(
"Error opening repository databases: " + e.getMessage(), e);
}
} catch (ConfigException e) {
throw e;
} catch (Exception e) {
Throwables.throwIfInstanceOf(e, IllegalStateException.class);
throw new IllegalStateException("Can't access repository at '" + repoURI + "'", e);
}
if (!repoExisted) {
createDefaultRefs();
}
return repository;
}
private void addDefaults(ConfigDatabase configDB, PluginDefaults defaults,
Map<String, String> configProps) {
final String refsKey = "storage.refs";
final String objectsKey = "storage.objects";
final Map<String, String> providedConfig = configDB.getAll();
Optional<VersionedFormat> refs;
Optional<VersionedFormat> objects;
refs = providedConfig.containsKey(refsKey) ? empty() : defaults.getRefs();
objects = providedConfig.containsKey(objectsKey) ? empty() : defaults.getObjects();
if (refs.isPresent()) {
configProps.put(refsKey, refs.get().getFormat());
configProps.put(refs.get().getFormat() + ".version", refs.get().getVersion());
}
if (objects.isPresent()) {
configProps.put(objectsKey, objects.get().getFormat());
configProps.put(objects.get().getFormat() + ".version", objects.get().getVersion());
}
}
private void createDefaultRefs() {
Optional<Ref> master = command(RefParse.class).setName(Ref.MASTER).call();
Preconditions.checkState(!master.isPresent(), Ref.MASTER + " was already initialized.");
command(UpdateRef.class).setName(Ref.MASTER).setNewValue(ObjectId.NULL)
.setReason("Repository initialization").call();
Optional<Ref> head = command(RefParse.class).setName(Ref.HEAD).call();
Preconditions.checkState(!head.isPresent(), Ref.HEAD + " was already initialized.");
command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(Ref.MASTER)
.setReason("Repository initialization").call();
Optional<Ref> workhead = command(RefParse.class).setName(Ref.WORK_HEAD).call();
Preconditions.checkState(!workhead.isPresent(),
Ref.WORK_HEAD + " was already initialized.");
command(UpdateRef.class).setName(Ref.WORK_HEAD).setNewValue(RevTree.EMPTY.getId())
.setReason("Repository initialization").call();
Optional<Ref> stagehead = command(RefParse.class).setName(Ref.STAGE_HEAD).call();
Preconditions.checkState(!stagehead.isPresent(),
Ref.STAGE_HEAD + " was already initialized.");
command(UpdateRef.class).setName(Ref.STAGE_HEAD).setNewValue(RevTree.EMPTY.getId())
.setReason("Repository initialization").call();
}
}