mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-09 02:45:37 +02:00
merge with branch issue-998
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.RefUpdate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The GitHeadModifier is able to modify the head of a git repository.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.61
|
||||
*/
|
||||
public class GitHeadModifier {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GitHeadModifier.class);
|
||||
|
||||
private final GitRepositoryHandler repositoryHandler;
|
||||
|
||||
@Inject
|
||||
public GitHeadModifier(GitRepositoryHandler repositoryHandler) {
|
||||
this.repositoryHandler = repositoryHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the repositories head points to the given branch. The method will return {@code false} if the
|
||||
* repositories head points already to the given branch.
|
||||
*
|
||||
* @param repository repository to modify
|
||||
* @param newHead branch which should be the new head of the repository
|
||||
*
|
||||
* @return {@code true} if the head has changed
|
||||
*/
|
||||
public boolean ensure(Repository repository, String newHead) {
|
||||
try (org.eclipse.jgit.lib.Repository gitRepository = open(repository)) {
|
||||
String currentHead = resolve(gitRepository);
|
||||
if (!Objects.equals(currentHead, newHead)) {
|
||||
return modify(gitRepository, newHead);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
LOG.warn("failed to change head of repository", ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String resolve(org.eclipse.jgit.lib.Repository gitRepository) throws IOException {
|
||||
Ref ref = gitRepository.getRefDatabase().getRef(Constants.HEAD);
|
||||
if ( ref.isSymbolic() ) {
|
||||
ref = ref.getTarget();
|
||||
}
|
||||
return GitUtil.getBranch(ref);
|
||||
}
|
||||
|
||||
private boolean modify(org.eclipse.jgit.lib.Repository gitRepository, String newHead) throws IOException {
|
||||
RefUpdate refUpdate = gitRepository.getRefDatabase().newUpdate(Constants.HEAD, true);
|
||||
refUpdate.setForceUpdate(true);
|
||||
RefUpdate.Result result = refUpdate.link(Constants.R_HEADS + newHead);
|
||||
return result == RefUpdate.Result.FORCED;
|
||||
}
|
||||
|
||||
private org.eclipse.jgit.lib.Repository open(Repository repository) throws IOException {
|
||||
File directory = repositoryHandler.getDirectory(repository);
|
||||
return GitUtil.open(directory);
|
||||
}
|
||||
}
|
||||
@@ -40,58 +40,72 @@ import sonia.scm.HandlerEvent;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.plugin.ext.Extension;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Repository listener which handles git related repository events.
|
||||
*
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.50
|
||||
*/
|
||||
@Extension
|
||||
@EagerSingleton
|
||||
public class GitRepositoryModifyListener {
|
||||
|
||||
|
||||
/**
|
||||
* the logger for GitRepositoryModifyListener
|
||||
*/
|
||||
private static final Logger logger = LoggerFactory.getLogger(GitRepositoryModifyListener.class);
|
||||
|
||||
|
||||
private final GitHeadModifier headModifier;
|
||||
|
||||
@Inject
|
||||
public GitRepositoryModifyListener(GitHeadModifier headModifier) {
|
||||
this.headModifier = headModifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives {@link RepositoryModificationEvent} and fires a {@link ClearRepositoryCacheEvent} if
|
||||
* the default branch of a git repository was modified.
|
||||
*
|
||||
*
|
||||
* @param event repository modification event
|
||||
*/
|
||||
@Subscribe
|
||||
public void handleEvent(RepositoryModificationEvent event){
|
||||
Repository repository = event.getItem();
|
||||
|
||||
if ( isModifyEvent(event) &&
|
||||
isGitRepository(event.getItem()) &&
|
||||
hasDefaultBranchChanged(event.getItemBeforeModification(), repository))
|
||||
|
||||
if ( isModifyEvent(event) && isGitRepository(event.getItem()) )
|
||||
{
|
||||
logger.info("git default branch of repository {} has changed, sending clear cache event", repository.getId());
|
||||
sendClearRepositoryCacheEvent(repository);
|
||||
if (hasDefaultBranchChanged(event.getItemBeforeModification(), repository)) {
|
||||
logger.info("git default branch of repository {} has changed, sending clear cache event", repository.getId());
|
||||
sendClearRepositoryCacheEvent(repository);
|
||||
}
|
||||
|
||||
String defaultBranch = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
||||
if (defaultBranch != null) {
|
||||
headModifier.ensure(repository, defaultBranch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@VisibleForTesting
|
||||
protected void sendClearRepositoryCacheEvent(Repository repository) {
|
||||
ScmEventBus.getInstance().post(new ClearRepositoryCacheEvent(repository));
|
||||
ScmEventBus.getInstance().post(new ClearRepositoryCacheEvent(repository));
|
||||
}
|
||||
|
||||
|
||||
private boolean isModifyEvent(RepositoryEvent event) {
|
||||
return event.getEventType() == HandlerEvent.MODIFY;
|
||||
}
|
||||
|
||||
|
||||
private boolean isGitRepository(Repository repository) {
|
||||
return GitRepositoryHandler.TYPE_NAME.equals(repository.getType());
|
||||
}
|
||||
|
||||
|
||||
private boolean hasDefaultBranchChanged(Repository old, Repository current) {
|
||||
return !Objects.equal(
|
||||
old.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH),
|
||||
old.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH),
|
||||
current.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitHeadModifierTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
private GitRepositoryHandler repositoryHandler;
|
||||
|
||||
@InjectMocks
|
||||
private GitHeadModifier modifier;
|
||||
|
||||
@Test
|
||||
public void testEnsure() throws IOException, GitAPIException {
|
||||
Repository repository = RepositoryTestData.createHeartOfGold("git");
|
||||
File headFile = create(repository, "master");
|
||||
|
||||
boolean result = modifier.ensure(repository, "develop");
|
||||
|
||||
assertEquals("ref: refs/heads/develop", Files.readFirstLine(headFile, Charsets.UTF_8));
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnsureWithSameBranch() throws IOException, GitAPIException {
|
||||
Repository repository = RepositoryTestData.createHeartOfGold("git");
|
||||
create(repository, "develop");
|
||||
|
||||
boolean result = modifier.ensure(repository, "develop");
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
private File create(Repository repository, String head) throws IOException, GitAPIException {
|
||||
File directory = temporaryFolder.newFolder();
|
||||
|
||||
Git.init()
|
||||
.setBare(true)
|
||||
.setDirectory(directory)
|
||||
.call();
|
||||
|
||||
File headFile = new File(directory, "HEAD");
|
||||
Files.write(String.format("ref: refs/heads/%s\n", head), headFile, Charsets.UTF_8);
|
||||
|
||||
when(repositoryHandler.getDirectory(repository)).thenReturn(directory);
|
||||
|
||||
return headFile;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
@@ -13,7 +13,7 @@
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
@@ -24,34 +24,39 @@
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.HandlerEvent;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitRepositoryModifyListener}.
|
||||
*
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitRepositoryModifyListenerTest {
|
||||
|
||||
@Mock
|
||||
private GitHeadModifier headModifier;
|
||||
|
||||
@InjectMocks
|
||||
private GitRepositoryModifyTestListener repositoryModifyListener;
|
||||
|
||||
/**
|
||||
* Set up test object.
|
||||
*/
|
||||
@Before
|
||||
public void setUpObjectUnderTest(){
|
||||
repositoryModifyListener = new GitRepositoryModifyTestListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests happy path.
|
||||
@@ -62,14 +67,14 @@ public class GitRepositoryModifyListenerTest {
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNotNull(repositoryModifyListener.repository);
|
||||
assertSame(current, repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with new default branch.
|
||||
*/
|
||||
@@ -78,14 +83,14 @@ public class GitRepositoryModifyListenerTest {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNotNull(repositoryModifyListener.repository);
|
||||
assertSame(current, repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with non git repositories.
|
||||
*/
|
||||
@@ -95,13 +100,13 @@ public class GitRepositoryModifyListenerTest {
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("hg");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests without default branch.
|
||||
*/
|
||||
@@ -109,13 +114,13 @@ public class GitRepositoryModifyListenerTest {
|
||||
public void testWithoutDefaultBranch(){
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with non modify event.
|
||||
*/
|
||||
@@ -125,13 +130,13 @@ public class GitRepositoryModifyListenerTest {
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.CREATE);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with non git repositories.
|
||||
*/
|
||||
@@ -144,20 +149,37 @@ public class GitRepositoryModifyListenerTest {
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testModifyRepositoryHead() {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
verify(headModifier).ensure(current, "develop");
|
||||
}
|
||||
|
||||
|
||||
private static class GitRepositoryModifyTestListener extends GitRepositoryModifyListener {
|
||||
|
||||
|
||||
private Repository repository;
|
||||
|
||||
|
||||
public GitRepositoryModifyTestListener(GitHeadModifier headModifier) {
|
||||
super(headModifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendClearRepositoryCacheEvent(Repository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user