mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
Reduce code redundancy
This commit is contained in:
@@ -0,0 +1,88 @@
|
|||||||
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import sonia.scm.ContextEntry;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.FileAlreadyExistsException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.NoSuchFileException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||||
|
import static sonia.scm.AlreadyExistsException.alreadyExists;
|
||||||
|
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||||
|
import static sonia.scm.NotFoundException.notFound;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This "interface" is not really intended to be used as an interface but rather as
|
||||||
|
* a base class to reduce code redundancy in Worker instances.
|
||||||
|
*/
|
||||||
|
public interface ModifyWorkerHelper extends ModifyCommand.Worker {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void delete(String toBeDeleted) throws IOException {
|
||||||
|
Path fileToBeDeleted = new File(getWorkDir(), toBeDeleted).toPath();
|
||||||
|
try {
|
||||||
|
Files.delete(fileToBeDeleted);
|
||||||
|
} catch (NoSuchFileException e) {
|
||||||
|
throw notFound(createFileContext(toBeDeleted));
|
||||||
|
}
|
||||||
|
doScmDelete(toBeDeleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
void doScmDelete(String toBeDeleted);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void create(String toBeCreated, File file, boolean overwrite) throws IOException {
|
||||||
|
Path targetFile = new File(getWorkDir(), toBeCreated).toPath();
|
||||||
|
createDirectories(targetFile);
|
||||||
|
if (overwrite) {
|
||||||
|
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
Files.move(file.toPath(), targetFile);
|
||||||
|
} catch (FileAlreadyExistsException e) {
|
||||||
|
throw alreadyExists(createFileContext(toBeCreated));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addFileToScm(toBeCreated, targetFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
default void modify(String path, File file) throws IOException {
|
||||||
|
Path targetFile = new File(getWorkDir(), path).toPath();
|
||||||
|
createDirectories(targetFile);
|
||||||
|
if (!targetFile.toFile().exists()) {
|
||||||
|
throw notFound(createFileContext(path));
|
||||||
|
}
|
||||||
|
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||||
|
addFileToScm(path, targetFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addFileToScm(String name, Path file);
|
||||||
|
|
||||||
|
default ContextEntry.ContextBuilder createFileContext(String path) {
|
||||||
|
ContextEntry.ContextBuilder contextBuilder = entity("file", path);
|
||||||
|
if (!StringUtils.isEmpty(getBranch())) {
|
||||||
|
contextBuilder.in("branch", getBranch());
|
||||||
|
}
|
||||||
|
contextBuilder.in(getRepository());
|
||||||
|
return contextBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
default void createDirectories(Path targetFile) throws IOException {
|
||||||
|
try {
|
||||||
|
Files.createDirectories(targetFile.getParent());
|
||||||
|
} catch (FileAlreadyExistsException e) {
|
||||||
|
throw alreadyExists(createFileContext(targetFile.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
File getWorkDir();
|
||||||
|
|
||||||
|
Repository getRepository();
|
||||||
|
|
||||||
|
String getBranch();
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ import org.eclipse.jgit.api.Git;
|
|||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
import org.eclipse.jgit.revwalk.RevCommit;
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
import sonia.scm.ConcurrentModificationException;
|
import sonia.scm.ConcurrentModificationException;
|
||||||
import sonia.scm.ContextEntry;
|
|
||||||
import sonia.scm.NoChangesMadeException;
|
import sonia.scm.NoChangesMadeException;
|
||||||
import sonia.scm.repository.GitWorkdirFactory;
|
import sonia.scm.repository.GitWorkdirFactory;
|
||||||
import sonia.scm.repository.InternalRepositoryException;
|
import sonia.scm.repository.InternalRepositoryException;
|
||||||
@@ -13,17 +12,9 @@ import sonia.scm.repository.Repository;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileAlreadyExistsException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.NoSuchFileException;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
|
||||||
import static sonia.scm.AlreadyExistsException.alreadyExists;
|
|
||||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
|
||||||
import static sonia.scm.NotFoundException.notFound;
|
|
||||||
|
|
||||||
public class GitModifyCommand extends AbstractGitCommand implements ModifyCommand {
|
public class GitModifyCommand extends AbstractGitCommand implements ModifyCommand {
|
||||||
|
|
||||||
private final GitWorkdirFactory workdirFactory;
|
private final GitWorkdirFactory workdirFactory;
|
||||||
@@ -38,7 +29,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
return inClone(clone -> new ModifyWorker(clone, request), workdirFactory, request.getBranch());
|
return inClone(clone -> new ModifyWorker(clone, request), workdirFactory, request.getBranch());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ModifyWorker extends GitCloneWorker<String> implements Worker {
|
private class ModifyWorker extends GitCloneWorker<String> implements ModifyWorkerHelper {
|
||||||
|
|
||||||
private final File workDir;
|
private final File workDir;
|
||||||
private final ModifyCommandRequest request;
|
private final ModifyCommandRequest request;
|
||||||
@@ -67,35 +58,9 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create(String toBeCreated, File file, boolean overwrite) throws IOException {
|
public void addFileToScm(String name, Path file) {
|
||||||
Path targetFile = new File(workDir, toBeCreated).toPath();
|
|
||||||
createDirectories(targetFile);
|
|
||||||
if (overwrite) {
|
|
||||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
|
||||||
} else {
|
|
||||||
try {
|
try {
|
||||||
Files.move(file.toPath(), targetFile);
|
addFileToGit(name);
|
||||||
} catch (FileAlreadyExistsException e) {
|
|
||||||
throw alreadyExists(createFileContext(toBeCreated));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
addFileToGit(toBeCreated);
|
|
||||||
} catch (GitAPIException e) {
|
|
||||||
throwInternalRepositoryException("could not add new file to index", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void modify(String path, File file) throws IOException {
|
|
||||||
Path targetFile = new File(workDir, path).toPath();
|
|
||||||
createDirectories(targetFile);
|
|
||||||
if (!targetFile.toFile().exists()) {
|
|
||||||
throw notFound(createFileContext(path));
|
|
||||||
}
|
|
||||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
|
||||||
try {
|
|
||||||
addFileToGit(path);
|
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
throwInternalRepositoryException("could not add new file to index", e);
|
throwInternalRepositoryException("could not add new file to index", e);
|
||||||
}
|
}
|
||||||
@@ -106,13 +71,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String toBeDeleted) throws IOException {
|
public void doScmDelete(String toBeDeleted) {
|
||||||
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
|
||||||
try {
|
|
||||||
Files.delete(fileToBeDeleted);
|
|
||||||
} catch (NoSuchFileException e) {
|
|
||||||
throw notFound(createFileContext(toBeDeleted));
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
getClone().rm().addFilepattern(removeStartingPathSeparators(toBeDeleted)).call();
|
getClone().rm().addFilepattern(removeStartingPathSeparators(toBeDeleted)).call();
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
@@ -120,29 +79,27 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getWorkDir() {
|
||||||
|
return workDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Repository getRepository() {
|
||||||
|
return repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getBranch() {
|
||||||
|
return request.getBranch();
|
||||||
|
}
|
||||||
|
|
||||||
private String removeStartingPathSeparators(String path) {
|
private String removeStartingPathSeparators(String path) {
|
||||||
while (path.startsWith(File.separator)) {
|
while (path.startsWith(File.separator)) {
|
||||||
path = path.substring(1);
|
path = path.substring(1);
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createDirectories(Path targetFile) throws IOException {
|
|
||||||
try {
|
|
||||||
Files.createDirectories(targetFile.getParent());
|
|
||||||
} catch (FileAlreadyExistsException e) {
|
|
||||||
throw alreadyExists(createFileContext(targetFile.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ContextEntry.ContextBuilder createFileContext(String path) {
|
|
||||||
ContextEntry.ContextBuilder contextBuilder = entity("file", path);
|
|
||||||
if (!StringUtils.isEmpty(request.getBranch())) {
|
|
||||||
contextBuilder.in("branch", request.getBranch());
|
|
||||||
}
|
|
||||||
contextBuilder.in(context.getRepository());
|
|
||||||
return contextBuilder;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String throwInternalRepositoryException(String message, Exception e) {
|
private String throwInternalRepositoryException(String message, Exception e) {
|
||||||
|
|||||||
@@ -7,24 +7,15 @@ import com.aragost.javahg.commands.ExecutionException;
|
|||||||
import com.aragost.javahg.commands.PullCommand;
|
import com.aragost.javahg.commands.PullCommand;
|
||||||
import com.aragost.javahg.commands.RemoveCommand;
|
import com.aragost.javahg.commands.RemoveCommand;
|
||||||
import com.aragost.javahg.commands.StatusCommand;
|
import com.aragost.javahg.commands.StatusCommand;
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import sonia.scm.ContextEntry;
|
|
||||||
import sonia.scm.NoChangesMadeException;
|
import sonia.scm.NoChangesMadeException;
|
||||||
import sonia.scm.repository.InternalRepositoryException;
|
import sonia.scm.repository.InternalRepositoryException;
|
||||||
import sonia.scm.repository.util.WorkingCopy;
|
import sonia.scm.repository.util.WorkingCopy;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileAlreadyExistsException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
|
||||||
import static sonia.scm.AlreadyExistsException.alreadyExists;
|
|
||||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
|
||||||
import static sonia.scm.NotFoundException.notFound;
|
|
||||||
|
|
||||||
public class HgModifyCommand implements ModifyCommand {
|
public class HgModifyCommand implements ModifyCommand {
|
||||||
|
|
||||||
private HgCommandContext context;
|
private HgCommandContext context;
|
||||||
@@ -43,62 +34,34 @@ public class HgModifyCommand implements ModifyCommand {
|
|||||||
request.getRequests().forEach(
|
request.getRequests().forEach(
|
||||||
partialRequest -> {
|
partialRequest -> {
|
||||||
try {
|
try {
|
||||||
partialRequest.execute(new Worker() {
|
partialRequest.execute(new ModifyWorkerHelper() {
|
||||||
@Override
|
|
||||||
public void delete(String toBeDeleted) {
|
|
||||||
RemoveCommand.on(workingRepository).execute(toBeDeleted);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create(String toBeCreated, File file, boolean overwrite) throws IOException {
|
public void addFileToScm(String name, Path file) {
|
||||||
Path targetFile = new File(workingRepository.getDirectory(), toBeCreated).toPath();
|
|
||||||
createDirectories(targetFile);
|
|
||||||
if (overwrite) {
|
|
||||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
|
||||||
} else {
|
|
||||||
try {
|
try {
|
||||||
Files.move(file.toPath(), targetFile);
|
addFileToHg(file.toFile());
|
||||||
} catch (FileAlreadyExistsException e) {
|
|
||||||
throw alreadyExists(createFileContext(toBeCreated));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
addFileToHg(targetFile.toFile());
|
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
throwInternalRepositoryException("could not add new file to index", e);
|
throwInternalRepositoryException("could not add new file to index", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void modify(String path, File file) throws IOException {
|
public void doScmDelete(String toBeDeleted) {
|
||||||
Path targetFile = new File(workingRepository.getDirectory(), path).toPath();
|
RemoveCommand.on(workingRepository).execute(toBeDeleted);
|
||||||
createDirectories(targetFile);
|
|
||||||
if (!targetFile.toFile().exists()) {
|
|
||||||
throw notFound(createFileContext(path));
|
|
||||||
}
|
|
||||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
|
||||||
try {
|
|
||||||
addFileToHg(targetFile.toFile());
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
throwInternalRepositoryException("could not modify existing file", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createDirectories(Path targetFile) throws IOException {
|
@Override
|
||||||
try {
|
public sonia.scm.repository.Repository getRepository() {
|
||||||
Files.createDirectories(targetFile.getParent());
|
return context.getScmRepository();
|
||||||
} catch (FileAlreadyExistsException e) {
|
|
||||||
throw alreadyExists(createFileContext(targetFile.toString()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ContextEntry.ContextBuilder createFileContext(String path) {
|
@Override
|
||||||
ContextEntry.ContextBuilder contextBuilder = entity("file", path);
|
public String getBranch() {
|
||||||
if (!StringUtils.isEmpty(request.getBranch())) {
|
return request.getBranch();
|
||||||
contextBuilder.in("branch", request.getBranch());
|
|
||||||
}
|
}
|
||||||
contextBuilder.in(context.getScmRepository());
|
|
||||||
return contextBuilder;
|
public File getWorkDir() {
|
||||||
|
return workingRepository.getDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addFileToHg(File file) {
|
private void addFileToHg(File file) {
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ModifyCommand getModifyCommand() {
|
public ModifyCommand getModifyCommand() {
|
||||||
return new HgModifyCommand(context, workdirFactory);
|
return new HgModifyCommand(context, handler.getWorkdirFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user