mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-01 13:19:53 +01:00
Implement git modify command
This commit is contained in:
@@ -9,7 +9,6 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import sonia.scm.BadRequestException;
|
||||
import sonia.scm.ConcurrentModificationException;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.repository.GitWorkdirFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
@@ -25,6 +24,7 @@ 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;
|
||||
import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
||||
|
||||
public class GitModifyCommand extends AbstractGitCommand implements ModifyCommand {
|
||||
@@ -79,10 +79,10 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
Path targetFile = new File(workDir, toBeCreated).toPath();
|
||||
Files.createDirectories(targetFile.getParent());
|
||||
if (overwrite) {
|
||||
Files.copy(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||
} else {
|
||||
try {
|
||||
Files.copy(file.toPath(), targetFile);
|
||||
Files.move(file.toPath(), targetFile);
|
||||
} catch (FileAlreadyExistsException e) {
|
||||
throw alreadyExists(createFileContext(toBeCreated));
|
||||
}
|
||||
@@ -94,13 +94,28 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(String path, File file) throws IOException {
|
||||
Path targetFile = new File(workDir, path).toPath();
|
||||
Files.createDirectories(targetFile.getParent());
|
||||
if (!targetFile.toFile().exists()) {
|
||||
throw notFound(createFileContext(path));
|
||||
}
|
||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||
try {
|
||||
getClone().add().addFilepattern(path).call();
|
||||
} catch (GitAPIException e) {
|
||||
throwInternalRepositoryException("could not add new file to index", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String toBeDeleted) throws IOException {
|
||||
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
||||
try {
|
||||
Files.delete(fileToBeDeleted);
|
||||
} catch (NoSuchFileException e) {
|
||||
throw NotFoundException.notFound(createFileContext(toBeDeleted));
|
||||
throw notFound(createFileContext(toBeDeleted));
|
||||
}
|
||||
try {
|
||||
getClone().rm().addFilepattern(toBeDeleted).call();
|
||||
@@ -109,8 +124,8 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
}
|
||||
}
|
||||
|
||||
private ContextEntry.ContextBuilder createFileContext(String toBeDeleted) {
|
||||
ContextEntry.ContextBuilder contextBuilder = entity("file", toBeDeleted);
|
||||
private ContextEntry.ContextBuilder createFileContext(String path) {
|
||||
ContextEntry.ContextBuilder contextBuilder = entity("file", path);
|
||||
if (!StringUtils.isEmpty(request.getBranch())) {
|
||||
contextBuilder.in("branch", request.getBranch());
|
||||
}
|
||||
@@ -118,11 +133,6 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
return contextBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(String path, File file) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(String sourcePath, String targetPath) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user