mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-18 03:01:05 +01:00
Remove leading slashes when changing files in git
This commit is contained in:
@@ -88,7 +88,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
getClone().add().addFilepattern(toBeCreated).call();
|
addFileToGit(toBeCreated);
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
throwInternalRepositoryException("could not add new file to index", e);
|
throwInternalRepositoryException("could not add new file to index", e);
|
||||||
}
|
}
|
||||||
@@ -103,12 +103,16 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||||
try {
|
try {
|
||||||
getClone().add().addFilepattern(path).call();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addFileToGit(String toBeCreated) throws GitAPIException {
|
||||||
|
getClone().add().addFilepattern(removeStartingPathSeparators(toBeCreated)).call();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String toBeDeleted) throws IOException {
|
public void delete(String toBeDeleted) throws IOException {
|
||||||
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
||||||
@@ -118,12 +122,19 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
throw notFound(createFileContext(toBeDeleted));
|
throw notFound(createFileContext(toBeDeleted));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
getClone().rm().addFilepattern(toBeDeleted).call();
|
getClone().rm().addFilepattern(removeStartingPathSeparators(toBeDeleted)).call();
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
throwInternalRepositoryException("could not remove file from index", e);
|
throwInternalRepositoryException("could not remove file from index", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String removeStartingPathSeparators(String path) {
|
||||||
|
while (path.startsWith(File.separator)) {
|
||||||
|
path = path.substring(1);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
private void createDirectories(Path targetFile) throws IOException {
|
private void createDirectories(Path targetFile) throws IOException {
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(targetFile.getParent());
|
Files.createDirectories(targetFile.getParent());
|
||||||
|
|||||||
@@ -97,6 +97,24 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
|
|||||||
assertInTree(assertions);
|
assertInTree(assertions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateNewFileWhenPathStartsWithSlash() throws IOException, GitAPIException {
|
||||||
|
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
|
||||||
|
|
||||||
|
GitModifyCommand command = createCommand();
|
||||||
|
|
||||||
|
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||||
|
request.setCommitMessage("test commit");
|
||||||
|
request.addRequest(new ModifyCommandRequest.CreateFileRequest("/new_file", newFile, false));
|
||||||
|
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||||
|
|
||||||
|
command.execute(request);
|
||||||
|
|
||||||
|
TreeAssertions assertions = canonicalTreeParser -> assertThat(canonicalTreeParser.findFile("new_file")).isTrue();
|
||||||
|
|
||||||
|
assertInTree(assertions);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = AlreadyExistsException.class)
|
@Test(expected = AlreadyExistsException.class)
|
||||||
public void shouldFailIfOverwritingExistingFileWithoutOverwriteFlag() throws IOException {
|
public void shouldFailIfOverwritingExistingFileWithoutOverwriteFlag() throws IOException {
|
||||||
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
|
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
|
||||||
|
|||||||
Reference in New Issue
Block a user