Implement file lock for git (#1838)

Adds a "file lock" command that can be used to mark files as locked by a specific user. This command is implemented for git using a store to keep the locks.

Additionally, the Git LFS locking API is implemented.

To display locks, the scm-manager/scm-file-lock-plugin can be used.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
René Pfeuffer
2021-11-01 16:54:58 +01:00
committed by GitHub
parent 87aea1936b
commit e1a2d27256
44 changed files with 4970 additions and 787 deletions

View File

@@ -0,0 +1,40 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.rest;
import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper;
import sonia.scm.repository.api.FileLockedException;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
@Provider
public class FileLockedExceptionMapper extends ContextualExceptionMapper<FileLockedException> {
@Inject
public FileLockedExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) {
super(FileLockedException.class, Response.Status.CONFLICT, mapper);
}
}

View File

@@ -40,6 +40,7 @@ import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.spi.HttpScmProtocol;
import sonia.scm.security.Authentications;
import sonia.scm.util.HttpUtil;
import sonia.scm.web.ScmClientDetector;
import sonia.scm.web.UserAgent;
import sonia.scm.web.UserAgentParser;
@@ -49,6 +50,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;
import java.util.Set;
@Singleton
@WebElement(value = HttpProtocolServlet.PATTERN)
@@ -63,21 +65,27 @@ public class HttpProtocolServlet extends HttpServlet {
private final NamespaceAndNameFromPathExtractor pathExtractor;
private final PushStateDispatcher dispatcher;
private final UserAgentParser userAgentParser;
private final Set<ScmClientDetector> scmClientDetectors;
@Inject
public HttpProtocolServlet(ScmConfiguration configuration, RepositoryServiceFactory serviceFactory, NamespaceAndNameFromPathExtractor pathExtractor, PushStateDispatcher dispatcher, UserAgentParser userAgentParser) {
public HttpProtocolServlet(ScmConfiguration configuration,
RepositoryServiceFactory serviceFactory,
NamespaceAndNameFromPathExtractor pathExtractor,
PushStateDispatcher dispatcher,
UserAgentParser userAgentParser,
Set<ScmClientDetector> scmClientDetectors) {
this.configuration = configuration;
this.serviceFactory = serviceFactory;
this.pathExtractor = pathExtractor;
this.dispatcher = dispatcher;
this.userAgentParser = userAgentParser;
this.scmClientDetectors = scmClientDetectors;
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
UserAgent userAgent = userAgentParser.parse(request);
if (userAgent.isScmClient()) {
if (isScmClient(userAgent, request)) {
String pathInfo = request.getPathInfo();
Optional<NamespaceAndName> namespaceAndName = pathExtractor.fromUri(pathInfo);
if (namespaceAndName.isPresent()) {
@@ -92,6 +100,10 @@ public class HttpProtocolServlet extends HttpServlet {
}
}
private boolean isScmClient(UserAgent userAgent, HttpServletRequest request) {
return userAgent.isScmClient() || scmClientDetectors.stream().anyMatch(detector -> detector.isScmClient(request, userAgent));
}
private void service(HttpServletRequest req, HttpServletResponse resp, NamespaceAndName namespaceAndName) throws IOException, ServletException {
try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
req.setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repositoryService.getRepository());