Store repository id in git config for each repository

This is needed after migration from v1 to v2 and is done in
GitV1UpdateStep.java. Therefore we hat to make the 'forAllPaths' method
in PathBasedRepositoryLocationResolver available in the interface of
RepositoryLocationResolver.
This commit is contained in:
René Pfeuffer
2019-07-03 07:48:06 +02:00
parent 8f7ca34fa2
commit c35363b79f
14 changed files with 158 additions and 52 deletions

View File

@@ -0,0 +1,21 @@
package sonia.scm.repository;
import org.eclipse.jgit.lib.StoredConfig;
import java.io.IOException;
public class GitConfigHelper {
private static final String CONFIG_SECTION_SCMM = "scmm";
private static final String CONFIG_KEY_REPOSITORY_ID = "repositoryid";
public void createScmmConfig(Repository repository, org.eclipse.jgit.lib.Repository gitRepository) throws IOException {
StoredConfig config = gitRepository.getConfig();
config.setString(CONFIG_SECTION_SCMM, null, CONFIG_KEY_REPOSITORY_ID, repository.getId());
config.save();
}
public String getRepositoryId(StoredConfig gitConfig) {
return gitConfig.getString(CONFIG_SECTION_SCMM, null, CONFIG_KEY_REPOSITORY_ID);
}
}

View File

@@ -89,8 +89,6 @@ public class GitRepositoryHandler
GitRepositoryServiceProvider.COMMANDS);
private static final Object LOCK = new Object();
private static final String CONFIG_SECTION_SCMM = "scmm";
private static final String CONFIG_KEY_REPOSITORY_ID = "repositoryid";
private final Scheduler scheduler;
@@ -185,7 +183,7 @@ public class GitRepositoryHandler
}
public String getRepositoryId(StoredConfig gitConfig) {
return gitConfig.getString(GitRepositoryHandler.CONFIG_SECTION_SCMM, null, GitRepositoryHandler.CONFIG_KEY_REPOSITORY_ID);
return new GitConfigHelper().getRepositoryId(gitConfig);
}
//~--- methods --------------------------------------------------------------
@@ -194,9 +192,7 @@ public class GitRepositoryHandler
protected void create(Repository repository, File directory) throws IOException {
try (org.eclipse.jgit.lib.Repository gitRepository = build(directory)) {
gitRepository.create(true);
StoredConfig config = gitRepository.getConfig();
config.setString(CONFIG_SECTION_SCMM, null, CONFIG_KEY_REPOSITORY_ID, repository.getId());
config.save();
new GitConfigHelper().createScmmConfig(repository, gitRepository);
}
}

View File

@@ -0,0 +1,70 @@
package sonia.scm.repository.update;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import sonia.scm.migration.UpdateException;
import sonia.scm.migration.UpdateStep;
import sonia.scm.plugin.Extension;
import sonia.scm.repository.GitConfigHelper;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.update.UpdateStepRepositoryMetadataAccess;
import sonia.scm.version.Version;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import static sonia.scm.version.Version.parse;
@Extension
public class GitV1UpdateStep implements UpdateStep {
private final RepositoryLocationResolver locationResolver;
private final UpdateStepRepositoryMetadataAccess<Path> repositoryMetadataAccess;
@Inject
public GitV1UpdateStep(RepositoryLocationResolver locationResolver, UpdateStepRepositoryMetadataAccess<Path> repositoryMetadataAccess) {
this.locationResolver = locationResolver;
this.repositoryMetadataAccess = repositoryMetadataAccess;
}
@Override
public void doUpdate() {
locationResolver.forClass(Path.class).forAllLocations(
(repositoryId, path) -> {
Repository repository = repositoryMetadataAccess.read(path);
if (isGitDirectory(repository)) {
try (org.eclipse.jgit.lib.Repository gitRepository = build(path.resolve("data").toFile())) {
new GitConfigHelper().createScmmConfig(repository, gitRepository);
} catch (IOException e) {
throw new UpdateException("could not update repository with id " + repositoryId + " in path " + path, e);
}
}
}
);
}
private org.eclipse.jgit.lib.Repository build(File directory) throws IOException {
return new FileRepositoryBuilder()
.setGitDir(directory)
.readEnvironment()
.findGitDir()
.build();
}
private boolean isGitDirectory(Repository repository) {
return GitRepositoryHandler.TYPE_NAME.equals(repository.getType());
}
@Override
public Version getTargetVersion() {
return parse("2.0.0");
}
@Override
public String getAffectedDataType() {
return "sonia.scm.plugin.git";
}
}