mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 11:35:57 +01:00
Detect merges where branch has been deleted, too
This commit is contained in:
@@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.repository.api;
|
|
||||||
|
|
||||||
import org.eclipse.jgit.lib.Repository;
|
|
||||||
import sonia.scm.repository.ChangesetPagingResult;
|
|
||||||
import sonia.scm.repository.spi.GitLogComputer;
|
|
||||||
import sonia.scm.repository.spi.HookMergeDetectionProvider;
|
|
||||||
import sonia.scm.repository.spi.LogCommandRequest;
|
|
||||||
|
|
||||||
public class GitPostReceiveHookMergeDetectionProvider implements HookMergeDetectionProvider {
|
|
||||||
private final Repository repository;
|
|
||||||
private final String repositoryId;
|
|
||||||
|
|
||||||
public GitPostReceiveHookMergeDetectionProvider(Repository repository, String repositoryId) {
|
|
||||||
this.repository = repository;
|
|
||||||
this.repositoryId = repositoryId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean branchesMerged(String target, String branch) {
|
|
||||||
LogCommandRequest request = new LogCommandRequest();
|
|
||||||
request.setBranch(branch);
|
|
||||||
request.setAncestorChangeset(target);
|
|
||||||
request.setPagingLimit(1);
|
|
||||||
|
|
||||||
ChangesetPagingResult changesets = new GitLogComputer(repositoryId, repository).compute(request);
|
|
||||||
return changesets.getTotal() == 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,33 +24,58 @@
|
|||||||
|
|
||||||
package sonia.scm.repository.api;
|
package sonia.scm.repository.api;
|
||||||
|
|
||||||
|
import org.eclipse.jgit.lib.AnyObjectId;
|
||||||
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
|
import sonia.scm.repository.GitUtil;
|
||||||
import sonia.scm.repository.spi.GitLogComputer;
|
import sonia.scm.repository.spi.GitLogComputer;
|
||||||
import sonia.scm.repository.spi.HookMergeDetectionProvider;
|
import sonia.scm.repository.spi.HookMergeDetectionProvider;
|
||||||
import sonia.scm.repository.spi.LogCommandRequest;
|
import sonia.scm.repository.spi.LogCommandRequest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class GitReceiveHookMergeDetectionProvider implements HookMergeDetectionProvider {
|
public class GitReceiveHookMergeDetectionProvider implements HookMergeDetectionProvider {
|
||||||
private final Repository repository;
|
private final Repository repository;
|
||||||
private final String repositoryId;
|
private final String repositoryId;
|
||||||
private final BranchMapper branchMapper;
|
private final List<ReceiveCommand> receiveCommands;
|
||||||
|
|
||||||
public GitReceiveHookMergeDetectionProvider(Repository repository, String repositoryId, BranchMapper branchMapper) {
|
public GitReceiveHookMergeDetectionProvider(Repository repository, String repositoryId, List<ReceiveCommand> receiveCommands) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.repositoryId = repositoryId;
|
this.repositoryId = repositoryId;
|
||||||
this.branchMapper = branchMapper;
|
this.receiveCommands = receiveCommands;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean branchesMerged(String target, String branch) {
|
public boolean branchesMerged(String target, String branch) {
|
||||||
LogCommandRequest request = new LogCommandRequest();
|
LogCommandRequest request = new LogCommandRequest();
|
||||||
request.setBranch(branchMapper.map(branch));
|
request.setBranch(findRelevantRevisionForBranchIfToBeUpdated(branch));
|
||||||
request.setAncestorChangeset(branchMapper.map(target));
|
request.setAncestorChangeset(findRelevantRevisionForBranchIfToBeUpdated(target));
|
||||||
request.setPagingLimit(1);
|
request.setPagingLimit(1);
|
||||||
|
|
||||||
return new GitLogComputer(repositoryId, repository).compute(request).getTotal() == 0;
|
return new GitLogComputer(repositoryId, repository).compute(request).getTotal() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface BranchMapper {
|
private String findRelevantRevisionForBranchIfToBeUpdated(String branch) {
|
||||||
String map(String branch);
|
return receiveCommands
|
||||||
|
.stream()
|
||||||
|
.filter(receiveCommand -> isReceiveCommandForBranch(branch, receiveCommand))
|
||||||
|
.map(this::getRelevantRevision)
|
||||||
|
.map(AnyObjectId::getName)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(branch);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isReceiveCommandForBranch(String branch, ReceiveCommand receiveCommand) {
|
||||||
|
return receiveCommand.getType() != ReceiveCommand.Type.CREATE
|
||||||
|
&& GitUtil.getBranch(receiveCommand.getRef()).equals(branch);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ObjectId getRelevantRevision(ReceiveCommand receiveCommand) {
|
||||||
|
if (receiveCommand.getType() == ReceiveCommand.Type.DELETE) {
|
||||||
|
return receiveCommand.getOldId();
|
||||||
|
} else {
|
||||||
|
return receiveCommand.getNewId();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,12 +26,9 @@ package sonia.scm.repository.spi;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import org.eclipse.jgit.lib.AnyObjectId;
|
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
import org.eclipse.jgit.transport.ReceivePack;
|
import org.eclipse.jgit.transport.ReceivePack;
|
||||||
import sonia.scm.repository.GitUtil;
|
|
||||||
import sonia.scm.repository.RepositoryHookType;
|
|
||||||
import sonia.scm.repository.api.GitHookBranchProvider;
|
import sonia.scm.repository.api.GitHookBranchProvider;
|
||||||
import sonia.scm.repository.api.GitHookMessageProvider;
|
import sonia.scm.repository.api.GitHookMessageProvider;
|
||||||
import sonia.scm.repository.api.GitHookTagProvider;
|
import sonia.scm.repository.api.GitHookTagProvider;
|
||||||
@@ -69,18 +66,15 @@ public class GitHookContextProvider extends HookContextProvider
|
|||||||
* Constructs a new instance
|
* Constructs a new instance
|
||||||
* @param receivePack git receive pack
|
* @param receivePack git receive pack
|
||||||
* @param receiveCommands received commands
|
* @param receiveCommands received commands
|
||||||
* @param type
|
|
||||||
*/
|
*/
|
||||||
public GitHookContextProvider(
|
public GitHookContextProvider(
|
||||||
ReceivePack receivePack,
|
ReceivePack receivePack,
|
||||||
List<ReceiveCommand> receiveCommands,
|
List<ReceiveCommand> receiveCommands,
|
||||||
RepositoryHookType type,
|
|
||||||
Repository repository,
|
Repository repository,
|
||||||
String repositoryId
|
String repositoryId
|
||||||
) {
|
) {
|
||||||
this.receivePack = receivePack;
|
this.receivePack = receivePack;
|
||||||
this.receiveCommands = receiveCommands;
|
this.receiveCommands = receiveCommands;
|
||||||
this.type = type;
|
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.repositoryId = repositoryId;
|
this.repositoryId = repositoryId;
|
||||||
this.changesetProvider = new GitHookChangesetProvider(receivePack,
|
this.changesetProvider = new GitHookChangesetProvider(receivePack,
|
||||||
@@ -116,25 +110,7 @@ public class GitHookContextProvider extends HookContextProvider
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HookMergeDetectionProvider getMergeDetectionProvider() {
|
public HookMergeDetectionProvider getMergeDetectionProvider() {
|
||||||
if (type == RepositoryHookType.POST_RECEIVE) {
|
return new GitReceiveHookMergeDetectionProvider(repository, repositoryId, receiveCommands);
|
||||||
return new GitReceiveHookMergeDetectionProvider(repository, repositoryId, branch -> branch);
|
|
||||||
} else {
|
|
||||||
return new GitReceiveHookMergeDetectionProvider(repository, repositoryId, this::findNewRevisionForBranchIfToBeUpdated);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String findNewRevisionForBranchIfToBeUpdated(String branch) {
|
|
||||||
return receiveCommands
|
|
||||||
.stream()
|
|
||||||
.filter(receiveCommand -> isReceiveCommandForBranch(branch, receiveCommand))
|
|
||||||
.map(ReceiveCommand::getNewId)
|
|
||||||
.map(AnyObjectId::getName)
|
|
||||||
.findFirst()
|
|
||||||
.orElse(branch);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isReceiveCommandForBranch(String branch, ReceiveCommand receiveCommand) {
|
|
||||||
return GitUtil.getBranch(receiveCommand.getRef()).equals(branch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -146,7 +122,6 @@ public class GitHookContextProvider extends HookContextProvider
|
|||||||
private final GitHookChangesetProvider changesetProvider;
|
private final GitHookChangesetProvider changesetProvider;
|
||||||
private final List<ReceiveCommand> receiveCommands;
|
private final List<ReceiveCommand> receiveCommands;
|
||||||
private final ReceivePack receivePack;
|
private final ReceivePack receivePack;
|
||||||
private final RepositoryHookType type;
|
|
||||||
private final Repository repository;
|
private final Repository repository;
|
||||||
private final String repositoryId;
|
private final String repositoryId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
|||||||
|
|
||||||
logger.trace("resolved repository to {}", repositoryId);
|
logger.trace("resolved repository to {}", repositoryId);
|
||||||
|
|
||||||
GitHookContextProvider context = new GitHookContextProvider(rpack, receiveCommands, type, repository, repositoryId);
|
GitHookContextProvider context = new GitHookContextProvider(rpack, receiveCommands, repository, repositoryId);
|
||||||
|
|
||||||
hookEventFacade.handle(repositoryId).fireHookEvent(type, context);
|
hookEventFacade.handle(repositoryId).fireHookEvent(type, context);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user