Skip to content

Commit

Permalink
fix(server): mkdirs before cloning problems
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Feb 3, 2024
1 parent 4bc943f commit b7e8939
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public interface FileSystem {
void createDirectory(Path dirPath);
boolean directoryExists(Path dirPath);
void copyDirectory(Path srcPath, Path destPath);
void createFile(Path filePath);
void removeFile(Path filePath);
File getFile(Path filePath);
Expand All @@ -28,7 +29,4 @@ default void writeToFile(Path filePath, String content) {
default String readFromFile(Path filePath) {
return new String(readByteArrayFromFile(filePath));
}

void copyDirectory(Path src, Path dest);
void copy(Path src, Path dest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public boolean directoryExists(Path dirPath) {
throw new UnsupportedOperationException();
}

@Override
public void copyDirectory(Path srcPath, Path destPath) {
throw new UnsupportedOperationException();
}

@Override
public void createFile(Path filePath) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -213,14 +218,4 @@ public byte[] readByteArrayFromFile(Path filePath) {
throw new RuntimeException(e);
}
}

@Override
public void copyDirectory(Path src, Path dest) {
throw new UnsupportedOperationException();
}

@Override
public void copy(Path src, Path dest) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public boolean directoryExists(Path dirPath) {
return local.directoryExists(dirPath);
}

@Override
public void copyDirectory(Path srcPath, Path destPath) {
local.copyDirectory(srcPath, destPath);
}

@Override
public void createFile(Path filePath) {
local.createFile(filePath);
Expand Down Expand Up @@ -102,14 +107,4 @@ public byte[] readByteArrayFromFile(Path filePath) {
return aws.readByteArrayFromFile(filePath);
}
}

@Override
public void copyDirectory(Path src, Path dest) {
local.copyDirectory(src, dest);
}

@Override
public void copy(Path src, Path dest) {
local.copy(src, dest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public boolean directoryExists(Path dirPath) {
return Files.isDirectory(baseDir.resolve(dirPath));
}

@Override
public void copyDirectory(Path srcPath, Path destPath) {
try (Stream<Path> stream = Files.walk(baseDir.resolve(srcPath))) {
stream.forEach(src -> copy(src, baseDir.resolve(destPath).resolve(baseDir.resolve(srcPath).relativize(src))));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void createFile(Path filePath) {
writeByteArrayToFile(filePath, new byte[0]);
Expand Down Expand Up @@ -218,20 +227,10 @@ public byte[] readByteArrayFromFile(Path filePath) {
}
}

@Override
public void copyDirectory(Path src, Path dest) {
try (Stream<Path> stream = Files.walk(src)) {
stream.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void copy(Path src, Path dest) {
private static void copy(Path src, Path dest) {
try {
Files.copy(src, dest, REPLACE_EXISTING, COPY_ATTRIBUTES);
} catch (Exception e) {
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public boolean directoryExists(Path dirPath) {
return false;
}

@Override
public void copyDirectory(Path srcPath, Path destPath) {}

@Override
public void createFile(Path filePath) {}

Expand Down Expand Up @@ -78,11 +81,5 @@ public void writeByteArrayToFile(Path filePath, byte[] content) {}
public byte[] readByteArrayFromFile(Path filePath) {
return new byte[0];
}

@Override
public void copyDirectory(Path src, Path dest) {}

@Override
public void copy(Path src, Path dest) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ public void init(Path rootDirPath) {

@Override
public void clone(Path originDirPath, Path rootDirPath) {
String srcDirAbsolutePath = fs.getFile(originDirPath).getAbsolutePath();
String destDirAbsolutePath = fs.getFile(rootDirPath).getAbsolutePath();

fs.copyDirectory(Path.of(srcDirAbsolutePath), Path.of(destDirAbsolutePath));
fs.createDirectory(rootDirPath);
fs.copyDirectory(originDirPath, rootDirPath);

File destDir = fs.getFile(rootDirPath);
String destURI = "file://" + srcDirAbsolutePath;
String destURI = "file://" + fs.getFile(originDirPath).getAbsolutePath();

try {
org.eclipse.jgit.api.Git.open(destDir).remoteAdd().setName("origin").setUri(new URIish(destURI)).call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public boolean directoryExists(Path dirPath) {
throw new UnsupportedOperationException();
}

@Override
public void copyDirectory(Path srcPath, Path destPath) {}

@Override
public void createFile(Path filePath) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -135,10 +138,4 @@ public void writeByteArrayToFile(Path filePath, byte[] content) {}
public byte[] readByteArrayFromFile(Path filePath) {
return fs.get(filePath);
}

@Override
public void copyDirectory(Path src, Path dest) {}

@Override
public void copy(Path src, Path dest) {}
}

0 comments on commit b7e8939

Please sign in to comment.