mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-04 05:39:11 +01:00
Merge remote-tracking branch 'origin/develop' into bugfix/rest-download
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
@@ -29,6 +29,12 @@ import de.otto.edison.hal.Links;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
import static sonia.scm.repository.Branch.VALID_BRANCH_NAMES;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@@ -41,6 +47,11 @@ public class GitConfigDto extends HalRepresentation {
|
||||
|
||||
private boolean nonFastForwardDisallowed;
|
||||
|
||||
@NotEmpty
|
||||
@Length(min = 1, max = 100)
|
||||
@Pattern(regexp = VALID_BRANCH_NAMES)
|
||||
private String defaultBranch;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
|
||||
protected HalRepresentation add(Links links) {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
||||
@@ -38,6 +38,7 @@ import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.validation.Valid;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
@@ -126,7 +127,7 @@ public class GitConfigResource {
|
||||
mediaType = VndMediaType.ERROR_TYPE,
|
||||
schema = @Schema(implementation = ErrorDto.class)
|
||||
))
|
||||
public Response update(GitConfigDto configDto) {
|
||||
public Response update(@Valid GitConfigDto configDto) {
|
||||
|
||||
GitConfig config = dtoToConfigMapper.map(configDto);
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -33,7 +33,6 @@ import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@XmlRootElement(name = "config")
|
||||
@@ -49,6 +48,9 @@ public class GitConfig extends RepositoryConfig {
|
||||
@XmlElement(name = "disallow-non-fast-forward")
|
||||
private boolean nonFastForwardDisallowed;
|
||||
|
||||
@XmlElement(name = "default-branch")
|
||||
private String defaultBranch = "main";
|
||||
|
||||
public String getGcExpression() {
|
||||
return gcExpression;
|
||||
}
|
||||
@@ -65,6 +67,14 @@ public class GitConfig extends RepositoryConfig {
|
||||
this.nonFastForwardDisallowed = nonFastForwardDisallowed;
|
||||
}
|
||||
|
||||
public String getDefaultBranch() {
|
||||
return defaultBranch;
|
||||
}
|
||||
|
||||
public void setDefaultBranch(String defaultBranch) {
|
||||
this.defaultBranch = defaultBranch;
|
||||
}
|
||||
|
||||
@Override
|
||||
@XmlTransient // Only for permission checks, don't serialize to XML
|
||||
public String getId() {
|
||||
|
||||
@@ -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;
|
||||
@@ -49,20 +50,12 @@ public class GitContext implements Closeable, RepositoryProvider
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GitContext.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
* @param repository
|
||||
*/
|
||||
public GitContext(File directory, Repository repository, GitRepositoryConfigStoreProvider storeProvider)
|
||||
public GitContext(File directory, Repository repository, GitRepositoryConfigStoreProvider storeProvider, GitConfig config)
|
||||
{
|
||||
this.directory = directory;
|
||||
this.repository = repository;
|
||||
this.storeProvider = storeProvider;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -126,12 +119,17 @@ public class GitContext implements Closeable, RepositoryProvider
|
||||
storeProvider.get(repository).set(newConfig);
|
||||
}
|
||||
|
||||
GitConfig getGlobalConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final File directory;
|
||||
private final Repository repository;
|
||||
private final GitRepositoryConfigStoreProvider storeProvider;
|
||||
private final GitConfig 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,14 +30,16 @@ import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.attributes.FilterCommandRegistry;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.ConcurrentModificationException;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.NoChangesMadeException;
|
||||
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.GitWorkingCopyFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
import sonia.scm.web.lfs.LfsBlobStoreFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -49,21 +51,22 @@ import java.util.concurrent.locks.Lock;
|
||||
|
||||
public class GitModifyCommand extends AbstractGitCommand implements ModifyCommand {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GitModifyCommand.class);
|
||||
private static final Striped<Lock> REGISTER_LOCKS = Striped.lock(5);
|
||||
|
||||
private final GitWorkingCopyFactory workingCopyFactory;
|
||||
private final LfsBlobStoreFactory lfsBlobStoreFactory;
|
||||
private final GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider;
|
||||
|
||||
@Inject
|
||||
GitModifyCommand(GitContext context, GitRepositoryHandler repositoryHandler, LfsBlobStoreFactory lfsBlobStoreFactory) {
|
||||
this(context, repositoryHandler.getWorkingCopyFactory(), lfsBlobStoreFactory);
|
||||
GitModifyCommand(GitContext context, GitRepositoryHandler repositoryHandler, LfsBlobStoreFactory lfsBlobStoreFactory, GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider) {
|
||||
this(context, repositoryHandler.getWorkingCopyFactory(), lfsBlobStoreFactory, gitRepositoryConfigStoreProvider);
|
||||
}
|
||||
|
||||
GitModifyCommand(GitContext context, GitWorkingCopyFactory workingCopyFactory, LfsBlobStoreFactory lfsBlobStoreFactory) {
|
||||
GitModifyCommand(GitContext context, GitWorkingCopyFactory workingCopyFactory, LfsBlobStoreFactory lfsBlobStoreFactory, GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider) {
|
||||
super(context);
|
||||
this.workingCopyFactory = workingCopyFactory;
|
||||
this.lfsBlobStoreFactory = lfsBlobStoreFactory;
|
||||
this.gitRepositoryConfigStoreProvider = gitRepositoryConfigStoreProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,19 +88,49 @@ 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("branch", request.getBranch() == null ? "default" : request.getBranch());
|
||||
throw new ConcurrentModificationException(ContextEntry.ContextBuilder.entity("Branch", request.getBranch() == null ? "default" : request.getBranch()).in(repository).build());
|
||||
}
|
||||
for (ModifyCommandRequest.PartialRequest r : request.getRequests()) {
|
||||
r.execute(this);
|
||||
}
|
||||
failIfNotChanged(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch()));
|
||||
Optional<RevCommit> revCommit = doCommit(request.getCommitMessage(), request.getAuthor(), request.isSign());
|
||||
|
||||
if (initialCommit) {
|
||||
handleBranchForInitialCommit();
|
||||
}
|
||||
|
||||
push();
|
||||
return revCommit.orElseThrow(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch())).name();
|
||||
}
|
||||
|
||||
private void handleBranchForInitialCommit() {
|
||||
String branch = StringUtils.isNotBlank(request.getBranch()) ? request.getBranch() : context.getGlobalConfig().getDefaultBranch();
|
||||
if (StringUtils.isNotBlank(branch)) {
|
||||
try {
|
||||
getClone().checkout().setName(branch).setCreateBranch(true).call();
|
||||
setBranchInConfig(branch);
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(repository, "could not create default branch for initial commit", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setBranchInConfig(String branch) {
|
||||
ConfigurationStore<GitRepositoryConfig> store = gitRepositoryConfigStoreProvider
|
||||
.get(repository);
|
||||
GitRepositoryConfig gitRepositoryConfig = store
|
||||
.getOptional()
|
||||
.orElse(new GitRepositoryConfig());
|
||||
gitRepositoryConfig.setDefaultBranch(branch);
|
||||
store.set(gitRepositoryConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFileToScm(String name, Path file) {
|
||||
addToGitWithLfsSupport(name, file);
|
||||
|
||||
@@ -67,7 +67,11 @@ class GitWorkingCopyInitializer {
|
||||
Ref head = clone.exactRef(Constants.HEAD);
|
||||
|
||||
if (head == null || !head.isSymbolic() || (initialBranch != null && !head.getTarget().getName().endsWith(initialBranch))) {
|
||||
throw notFound(entity("Branch", initialBranch).in(context.getRepository()));
|
||||
if (clone.getRefDatabase().getRefs().isEmpty()) {
|
||||
LOG.warn("could not initialize empty clone with given branch {}; this has to be handled later on", initialBranch);
|
||||
} else {
|
||||
throw notFound(entity("Branch", initialBranch).in(context.getRepository()));
|
||||
}
|
||||
}
|
||||
|
||||
return new ParentAndClone<>(null, clone, target);
|
||||
|
||||
Reference in New Issue
Block a user