mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 13:35:44 +01:00
merge + refactor getStoreDirectory
This commit is contained in:
@@ -136,7 +136,7 @@ public class ScmTransportProtocol extends TransportProtocol
|
||||
*/
|
||||
@Override
|
||||
public Transport open(URIish uri, Repository local, String remoteName)
|
||||
throws NotSupportedException, TransportException
|
||||
throws TransportException
|
||||
{
|
||||
File localDirectory = local.getDirectory();
|
||||
File path = local.getFS().resolve(localDirectory, uri.getPath());
|
||||
@@ -150,7 +150,7 @@ public class ScmTransportProtocol extends TransportProtocol
|
||||
//J-
|
||||
return new TransportLocalWithHooks(
|
||||
hookEventFacadeProvider.get(),
|
||||
repositoryHandlerProvider.get(),
|
||||
repositoryHandlerProvider.get(),
|
||||
local, uri, gitDir
|
||||
);
|
||||
//J+
|
||||
|
||||
@@ -38,6 +38,7 @@ package sonia.scm.repository;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import org.eclipse.jgit.lib.StoredConfig;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -88,6 +89,8 @@ 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;
|
||||
|
||||
@@ -97,19 +100,13 @@ public class GitRepositoryHandler
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param storeFactory
|
||||
* @param fileSystem
|
||||
* @param scheduler
|
||||
* @param repositoryLocationResolver
|
||||
*/
|
||||
@Inject
|
||||
public GitRepositoryHandler(ConfigurationStoreFactory storeFactory, FileSystem fileSystem, Scheduler scheduler, RepositoryLocationResolver repositoryLocationResolver, GitWorkdirFactory workdirFactory)
|
||||
public GitRepositoryHandler(ConfigurationStoreFactory storeFactory,
|
||||
Scheduler scheduler,
|
||||
RepositoryLocationResolver repositoryLocationResolver,
|
||||
GitWorkdirFactory workdirFactory)
|
||||
{
|
||||
super(storeFactory, fileSystem, repositoryLocationResolver);
|
||||
super(storeFactory, repositoryLocationResolver);
|
||||
this.scheduler = scheduler;
|
||||
this.workdirFactory = workdirFactory;
|
||||
}
|
||||
@@ -182,15 +179,26 @@ public class GitRepositoryHandler
|
||||
return getStringFromResource(RESOURCE_VERSION, DEFAULT_VERSION_INFORMATION);
|
||||
}
|
||||
|
||||
public GitWorkdirFactory getWorkdirFactory() {
|
||||
return workdirFactory;
|
||||
}
|
||||
|
||||
public String getRepositoryId(StoredConfig gitConfig) {
|
||||
return gitConfig.getString(GitRepositoryHandler.CONFIG_SECTION_SCMM, null, GitRepositoryHandler.CONFIG_KEY_REPOSITORY_ID);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private org.eclipse.jgit.lib.Repository build(File directory) throws IOException {
|
||||
return new FileRepositoryBuilder()
|
||||
.setGitDir(directory)
|
||||
@@ -224,22 +232,4 @@ public class GitRepositoryHandler
|
||||
{
|
||||
return GitConfig.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected boolean isRepository(File directory)
|
||||
{
|
||||
return new File(directory, DIRECTORY_REFS).exists();
|
||||
}
|
||||
|
||||
public GitWorkdirFactory getWorkdirFactory() {
|
||||
return workdirFactory;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ package sonia.scm.web;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.lib.StoredConfig;
|
||||
import org.eclipse.jgit.transport.PostReceiveHook;
|
||||
import org.eclipse.jgit.transport.PreReceiveHook;
|
||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||
@@ -44,11 +45,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.RepositoryHookType;
|
||||
import sonia.scm.repository.RepositoryUtil;
|
||||
import sonia.scm.repository.spi.GitHookContextProvider;
|
||||
import sonia.scm.repository.spi.HookEventFacade;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -128,14 +127,14 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
try
|
||||
{
|
||||
Repository repository = rpack.getRepository();
|
||||
String id = resolveRepositoryId(repository);
|
||||
String repositoryId = resolveRepositoryId(repository);
|
||||
|
||||
logger.trace("resolved repository to id {}", id);
|
||||
logger.trace("resolved repository to {}", repositoryId);
|
||||
|
||||
GitHookContextProvider context = new GitHookContextProvider(rpack,
|
||||
receiveCommands);
|
||||
|
||||
hookEventFacade.handle(id).fireHookEvent(type, context);
|
||||
hookEventFacade.handle(repositoryId).fireHookEvent(type, context);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -187,20 +186,10 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private String resolveRepositoryId(Repository repository) throws IOException
|
||||
private String resolveRepositoryId(Repository repository)
|
||||
{
|
||||
File directory;
|
||||
|
||||
if (repository.isBare())
|
||||
{
|
||||
directory = repository.getDirectory();
|
||||
}
|
||||
else
|
||||
{
|
||||
directory = repository.getWorkTree();
|
||||
}
|
||||
|
||||
return RepositoryUtil.getRepositoryId(handler, directory);
|
||||
StoredConfig gitConfig = repository.getConfig();
|
||||
return handler.getRepositoryId(gitConfig);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
@@ -33,29 +33,19 @@ class GitConfigurationForm extends React.Component<Props, State> {
|
||||
this.state = { ...props.initialConfiguration };
|
||||
}
|
||||
|
||||
isValid = () => {
|
||||
return !!this.state.repositoryDirectory;
|
||||
};
|
||||
|
||||
handleChange = (value: any, name: string) => {
|
||||
this.setState({
|
||||
[name]: value
|
||||
}, () => this.props.onConfigurationChange(this.state, this.isValid()));
|
||||
}, () => this.props.onConfigurationChange(this.state, true));
|
||||
};
|
||||
|
||||
render() {
|
||||
const { repositoryDirectory, gcExpression, disabled } = this.state;
|
||||
const { gcExpression, disabled } = this.state;
|
||||
const { readOnly, t } = this.props;
|
||||
|
||||
return (
|
||||
<>
|
||||
<InputField name="repositoryDirectory"
|
||||
label={t("scm-git-plugin.config.directory")}
|
||||
helpText={t("scm-git-plugin.config.directoryHelpText")}
|
||||
value={repositoryDirectory}
|
||||
onChange={this.handleChange}
|
||||
disabled={readOnly}
|
||||
/>
|
||||
<InputField name="gcExpression"
|
||||
label={t("scm-git-plugin.config.gcExpression")}
|
||||
helpText={t("scm-git-plugin.config.gcExpressionHelpText")}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//@flow
|
||||
import React from "react";
|
||||
import { translate } from "react-i18next";
|
||||
import { Title, GlobalConfiguration } from "@scm-manager/ui-components";
|
||||
import { Title, Configuration } from "@scm-manager/ui-components";
|
||||
import GitConfigurationForm from "./GitConfigurationForm";
|
||||
|
||||
type Props = {
|
||||
@@ -22,7 +22,7 @@ class GitGlobalConfiguration extends React.Component<Props> {
|
||||
return (
|
||||
<div>
|
||||
<Title title={t("scm-git-plugin.config.title")}/>
|
||||
<GlobalConfiguration link={link} render={props => <GitConfigurationForm {...props} />}/>
|
||||
<Configuration link={link} render={props => <GitConfigurationForm {...props} />}/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
"config": {
|
||||
"link": "Git",
|
||||
"title": "Git Configuration",
|
||||
"directory": "Repository Directory",
|
||||
"directoryHelpText": "Location of the Git repositories.",
|
||||
"gcExpression": "GC Cron Expression",
|
||||
"gcExpressionHelpText": "Use Quartz Cron Expressions (SECOND MINUTE HOUR DAYOFMONTH MONTH DAYOFWEEK) to run git gc in intervals.",
|
||||
"disabled": "Disabled",
|
||||
|
||||
@@ -37,26 +37,17 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.io.DefaultFileSystem;
|
||||
import sonia.scm.schedule.Scheduler;
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
import sonia.scm.store.StoreFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -71,8 +62,8 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
||||
@Mock
|
||||
private GitWorkdirFactory gitWorkdirFactory;
|
||||
|
||||
RepositoryLocationResolver repositoryLocationResolver ;
|
||||
private Path repoDir;
|
||||
RepositoryLocationResolver repositoryLocationResolver;
|
||||
|
||||
|
||||
@Override
|
||||
protected void checkDirectory(File directory) {
|
||||
@@ -95,16 +86,10 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
||||
|
||||
@Override
|
||||
protected RepositoryHandler createRepositoryHandler(ConfigurationStoreFactory factory,
|
||||
File directory) throws RepositoryPathNotFoundException {
|
||||
DefaultFileSystem fileSystem = new DefaultFileSystem();
|
||||
PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
|
||||
InitialRepositoryLocationResolver initialRepositoryLocationResolver = new InitialRepositoryLocationResolver(contextProvider,fileSystem);
|
||||
repositoryLocationResolver = new RepositoryLocationResolver(repoDao, initialRepositoryLocationResolver);
|
||||
File directory) {
|
||||
repositoryLocationResolver = new RepositoryLocationResolver(repoDao, new InitialRepositoryLocationResolver(contextProvider));
|
||||
GitRepositoryHandler repositoryHandler = new GitRepositoryHandler(factory,
|
||||
fileSystem, scheduler, repositoryLocationResolver, gitWorkdirFactory);
|
||||
|
||||
repoDir = directory.toPath();
|
||||
when(repoDao.getPath(any())).thenReturn(repoDir);
|
||||
scheduler, repositoryLocationResolver, gitWorkdirFactory);
|
||||
repositoryHandler.init(contextProvider);
|
||||
|
||||
GitConfig config = new GitConfig();
|
||||
@@ -116,18 +101,17 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDirectory() {
|
||||
public void getDirectory() {
|
||||
GitRepositoryHandler repositoryHandler = new GitRepositoryHandler(factory,
|
||||
new DefaultFileSystem(), scheduler, repositoryLocationResolver, gitWorkdirFactory);
|
||||
Repository repository = new Repository("id", "git", "Space", "Name");
|
||||
|
||||
scheduler, repositoryLocationResolver, gitWorkdirFactory);
|
||||
GitConfig config = new GitConfig();
|
||||
config.setDisabled(false);
|
||||
config.setGcExpression("gc exp");
|
||||
|
||||
repositoryHandler.setConfig(config);
|
||||
|
||||
initRepository();
|
||||
File path = repositoryHandler.getDirectory(repository);
|
||||
assertEquals(repoDir.toString()+File.separator+InitialRepositoryLocationResolver.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
|
||||
assertEquals(repoPath.toString() + File.separator + AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user