mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-04 13:49:13 +01:00
Set default branch after initial commit
This commit is contained in:
@@ -27,6 +27,7 @@ package sonia.scm.repository.spi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
|
||||
import sonia.scm.repository.GitConfig;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.Repository;
|
||||
@@ -51,19 +52,7 @@ public class GitContext implements Closeable, RepositoryProvider
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
* @param repository
|
||||
*/
|
||||
public GitContext(File directory, Repository repository, GitRepositoryConfigStoreProvider storeProvider)
|
||||
{
|
||||
this.directory = directory;
|
||||
this.repository = repository;
|
||||
this.storeProvider = storeProvider;
|
||||
}
|
||||
private final GitConfig config;
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
@@ -122,6 +111,22 @@ public class GitContext implements Closeable, RepositoryProvider
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
* @param repository
|
||||
* @param config
|
||||
*/
|
||||
public GitContext(File directory, Repository repository, GitRepositoryConfigStoreProvider storeProvider, GitConfig config)
|
||||
{
|
||||
this.directory = directory;
|
||||
this.repository = repository;
|
||||
this.storeProvider = storeProvider;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
void setConfig(GitRepositoryConfig newConfig) {
|
||||
storeProvider.get(repository).set(newConfig);
|
||||
}
|
||||
@@ -133,6 +138,10 @@ public class GitContext implements Closeable, RepositoryProvider
|
||||
private final Repository repository;
|
||||
private final GitRepositoryConfigStoreProvider storeProvider;
|
||||
|
||||
GitConfig getGlobalConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/** Field description */
|
||||
private org.eclipse.jgit.lib.Repository gitRepository;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class GitContextFactory {
|
||||
}
|
||||
|
||||
GitContext create(Repository repository) {
|
||||
return new GitContext(handler.getDirectory(repository.getId()), repository, storeProvider);
|
||||
return new GitContext(handler.getDirectory(repository.getId()), repository, storeProvider, handler.getConfig());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -86,6 +86,9 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
@Override
|
||||
String run() throws IOException {
|
||||
getClone().getRepository().getFullBranch();
|
||||
|
||||
boolean initialCommit = getClone().getRepository().getRefDatabase().getRefs().isEmpty();
|
||||
|
||||
if (!StringUtils.isEmpty(request.getExpectedRevision())
|
||||
&& !request.getExpectedRevision().equals(getCurrentRevision().getName())) {
|
||||
throw new ConcurrentModificationException(ContextEntry.ContextBuilder.entity("Branch", request.getBranch() == null ? "default" : request.getBranch()).in(repository).build());
|
||||
@@ -95,10 +98,23 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
}
|
||||
failIfNotChanged(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch()));
|
||||
Optional<RevCommit> revCommit = doCommit(request.getCommitMessage(), request.getAuthor(), request.isSign());
|
||||
|
||||
if (initialCommit && StringUtils.isNotBlank(context.getGlobalConfig().getDefaultBranch())) {
|
||||
createDefaultBranch();
|
||||
}
|
||||
|
||||
push();
|
||||
return revCommit.orElseThrow(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch())).name();
|
||||
}
|
||||
|
||||
private void createDefaultBranch() {
|
||||
try {
|
||||
getClone().checkout().setName(context.getGlobalConfig().getDefaultBranch()).setCreateBranch(true).call();
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(repository, "could not create default branch for initial commit", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFileToScm(String name, Path file) {
|
||||
addToGitWithLfsSupport(name, file);
|
||||
|
||||
Reference in New Issue
Block a user