Merged in feature/editor_plugin_hg (pull request #330)

Feature/editor plugin hg
This commit is contained in:
Rene Pfeuffer
2019-10-09 09:39:10 +00:00
15 changed files with 455 additions and 126 deletions

View File

@@ -0,0 +1,103 @@
package sonia.scm.repository.spi;
import com.aragost.javahg.Changeset;
import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.CommitCommand;
import com.aragost.javahg.commands.ExecutionException;
import com.aragost.javahg.commands.PullCommand;
import com.aragost.javahg.commands.RemoveCommand;
import com.aragost.javahg.commands.StatusCommand;
import sonia.scm.NoChangesMadeException;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.util.WorkingCopy;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
public class HgModifyCommand implements ModifyCommand {
private HgCommandContext context;
private final HgWorkdirFactory workdirFactory;
public HgModifyCommand(HgCommandContext context, HgWorkdirFactory workdirFactory) {
this.context = context;
this.workdirFactory = workdirFactory;
}
@Override
public String execute(ModifyCommandRequest request) {
try (WorkingCopy<com.aragost.javahg.Repository> workingCopy = workdirFactory.createWorkingCopy(context, request.getBranch())) {
Repository workingRepository = workingCopy.getWorkingRepository();
request.getRequests().forEach(
partialRequest -> {
try {
partialRequest.execute(new ModifyWorkerHelper() {
@Override
public void addFileToScm(String name, Path file) {
try {
addFileToHg(file.toFile());
} catch (ExecutionException e) {
throwInternalRepositoryException("could not add new file to index", e);
}
}
@Override
public void doScmDelete(String toBeDeleted) {
RemoveCommand.on(workingRepository).execute(toBeDeleted);
}
@Override
public sonia.scm.repository.Repository getRepository() {
return context.getScmRepository();
}
@Override
public String getBranch() {
return request.getBranch();
}
public File getWorkDir() {
return workingRepository.getDirectory();
}
private void addFileToHg(File file) {
workingRepository.workingCopy().add(file.getAbsolutePath());
}
});
} catch (IOException e) {
throwInternalRepositoryException("could not execute command on repository", e);
}
}
);
if (StatusCommand.on(workingRepository).lines().isEmpty()) {
throw new NoChangesMadeException(context.getScmRepository());
}
CommitCommand.on(workingRepository).user(String.format("%s <%s>", request.getAuthor().getName(), request.getAuthor().getMail())).message(request.getCommitMessage()).execute();
List<Changeset> execute = pullModifyChangesToCentralRepository(request, workingCopy);
return execute.get(0).getNode();
} catch (ExecutionException e) {
throwInternalRepositoryException("could not execute command on repository", e);
return null;
}
}
private List<Changeset> pullModifyChangesToCentralRepository(ModifyCommandRequest request, WorkingCopy<com.aragost.javahg.Repository> workingCopy) {
try {
com.aragost.javahg.commands.PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository());
workdirFactory.configure(pullCommand);
return pullCommand.execute(workingCopy.getDirectory().getAbsolutePath());
} catch (Exception e) {
throw new IntegrateChangesFromWorkdirException(context.getScmRepository(),
String.format("Could not pull modify changes from working copy to central repository for branch %s", request.getBranch()),
e);
}
}
private String throwInternalRepositoryException(String message, Exception e) {
throw new InternalRepositoryException(context.getScmRepository(), message, e);
}
}

View File

@@ -66,7 +66,8 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
Command.INCOMING,
Command.OUTGOING,
Command.PUSH,
Command.PULL
Command.PULL,
Command.MODIFY
);
//J+
@@ -77,7 +78,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
//~--- constructors ---------------------------------------------------------
HgRepositoryServiceProvider(HgRepositoryHandler handler,
HgHookManager hookManager, Repository repository)
HgHookManager hookManager, Repository repository)
{
this.repository = repository;
this.handler = handler;
@@ -238,6 +239,11 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
return new HgPushCommand(handler, context, repository);
}
@Override
public ModifyCommand getModifyCommand() {
return new HgModifyCommand(context, handler.getWorkdirFactory());
}
/**
* Method description
*

View File

@@ -47,12 +47,12 @@ import sonia.scm.repository.Repository;
public class HgRepositoryServiceResolver implements RepositoryServiceResolver
{
private HgRepositoryHandler handler;
private HgHookManager hookManager;
private final HgRepositoryHandler handler;
private final HgHookManager hookManager;
@Inject
public HgRepositoryServiceResolver(HgRepositoryHandler handler,
HgHookManager hookManager)
HgHookManager hookManager)
{
this.handler = handler;
this.hookManager = hookManager;

View File

@@ -0,0 +1,164 @@
package sonia.scm.repository.spi;
import com.google.inject.util.Providers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import sonia.scm.AlreadyExistsException;
import sonia.scm.NoChangesMadeException;
import sonia.scm.NotFoundException;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgTestUtil;
import sonia.scm.repository.Person;
import sonia.scm.repository.util.WorkdirProvider;
import sonia.scm.web.HgRepositoryEnvironmentBuilder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
public class HgModifyCommandTest extends AbstractHgCommandTestBase {
private HgModifyCommand hgModifyCommand;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void initHgModifyCommand() {
HgHookManager hookManager = HgTestUtil.createHookManager();
HgRepositoryEnvironmentBuilder environmentBuilder = new HgRepositoryEnvironmentBuilder(handler, hookManager);
SimpleHgWorkdirFactory workdirFactory = new SimpleHgWorkdirFactory(Providers.of(environmentBuilder), new WorkdirProvider()) {
@Override
public void configure(com.aragost.javahg.commands.PullCommand pullCommand) {
// we do not want to configure http hooks in this unit test
}
};
hgModifyCommand = new HgModifyCommand(cmdContext, workdirFactory
);
}
@Test
public void shouldRemoveFiles() {
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.DeleteFileRequest("a.txt"));
request.setCommitMessage("this is great");
request.setAuthor(new Person("Arthur Dent", "dent@hitchhiker.com"));
String result = hgModifyCommand.execute(request);
assertThat(cmdContext.open().tip().getNode()).isEqualTo(result);
}
@Test
public void shouldCreateFilesWithoutOverwrite() throws IOException {
File testFile = temporaryFolder.newFile();
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setCommitMessage("I found the answer");
request.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
String changeSet = hgModifyCommand.execute(request);
assertThat(cmdContext.open().tip().getNode()).isEqualTo(changeSet);
assertThat(cmdContext.open().tip().getAddedFiles().size()).isEqualTo(1);
}
@Test
public void shouldOverwriteExistingFiles() throws IOException {
File testFile = temporaryFolder.newFile();
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.CreateFileRequest("a.txt", testFile, true));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
String changeSet2 = hgModifyCommand.execute(request2);
assertThat(cmdContext.open().tip().getNode()).isEqualTo(changeSet2);
assertThat(cmdContext.open().tip().getModifiedFiles().size()).isEqualTo(1);
assertThat(cmdContext.open().tip().getModifiedFiles().get(0)).isEqualTo("a.txt");
}
@Test(expected = AlreadyExistsException.class)
public void shouldThrowFileAlreadyExistsException() throws IOException {
File testFile = temporaryFolder.newFile();
new FileOutputStream(testFile).write(21);
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setCommitMessage("I found the answer");
request.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request);
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request2);
}
@Test
public void shouldModifyExistingFile() throws IOException {
File testFile = temporaryFolder.newFile("a.txt");
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.ModifyFileRequest("a.txt", testFile));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
String changeSet2 = hgModifyCommand.execute(request2);
assertThat(cmdContext.open().tip().getNode()).isEqualTo(changeSet2);
assertThat(cmdContext.open().tip().getModifiedFiles().size()).isEqualTo(1);
assertThat(cmdContext.open().tip().getModifiedFiles().get(0)).isEqualTo(testFile.getName());
}
@Test(expected = NotFoundException.class)
public void shouldThrowNotFoundExceptionIfFileDoesNotExist() throws IOException {
File testFile = temporaryFolder.newFile("Answer.txt");
new FileOutputStream(testFile).write(42);
ModifyCommandRequest request2 = new ModifyCommandRequest();
request2.addRequest(new ModifyCommandRequest.ModifyFileRequest("Answer.txt", testFile));
request2.setCommitMessage(" Now i really found the answer");
request2.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request2);
}
@Test(expected = NullPointerException.class)
public void shouldThrowNPEIfAuthorIsMissing() throws IOException {
File testFile = temporaryFolder.newFile();
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setCommitMessage("I found the answer");
hgModifyCommand.execute(request);
}
@Test(expected = NullPointerException.class)
public void shouldThrowNPEIfCommitMessageIsMissing() throws IOException {
File testFile = temporaryFolder.newFile();
ModifyCommandRequest request = new ModifyCommandRequest();
request.addRequest(new ModifyCommandRequest.CreateFileRequest("Answer.txt", testFile, false));
request.setAuthor(new Person("Trillian Astra", "trillian@hitchhiker.com"));
hgModifyCommand.execute(request);
}
@Test(expected = NoChangesMadeException.class)
public void shouldThrowNoChangesMadeExceptionIfEmptyLocalChangesetAfterRequest() {
hgModifyCommand.execute(new ModifyCommandRequest());
}
}