Remove leading slashes when changing files in git

This commit is contained in:
Rene Pfeuffer
2019-09-11 10:51:26 +02:00
parent 66c977b08d
commit 87a4fa8b74
2 changed files with 32 additions and 3 deletions

View File

@@ -88,7 +88,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
}
}
try {
getClone().add().addFilepattern(toBeCreated).call();
addFileToGit(toBeCreated);
} catch (GitAPIException 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);
try {
getClone().add().addFilepattern(path).call();
addFileToGit(path);
} catch (GitAPIException e) {
throwInternalRepositoryException("could not add new file to index", e);
}
}
private void addFileToGit(String toBeCreated) throws GitAPIException {
getClone().add().addFilepattern(removeStartingPathSeparators(toBeCreated)).call();
}
@Override
public void delete(String toBeDeleted) throws IOException {
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
@@ -118,12 +122,19 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
throw notFound(createFileContext(toBeDeleted));
}
try {
getClone().rm().addFilepattern(toBeDeleted).call();
getClone().rm().addFilepattern(removeStartingPathSeparators(toBeDeleted)).call();
} catch (GitAPIException 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 {
try {
Files.createDirectories(targetFile.getParent());