Skip to content

Commit

Permalink
fix: last modified at after uploading media files (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
kadeksuryam authored Dec 1, 2023
1 parent 057b189 commit 84cdb0e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ 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 @@ -213,4 +213,14 @@ 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 @@ -102,4 +102,14 @@ 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
@@ -1,5 +1,8 @@
package judgels.fs.local;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
Expand All @@ -23,6 +26,7 @@
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import judgels.fs.FileInfo;
Expand Down Expand Up @@ -213,4 +217,22 @@ public byte[] readByteArrayFromFile(Path filePath) {
throw new RuntimeException(e);
}
}

@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) {
try {
Files.copy(src, dest, REPLACE_EXISTING, COPY_ATTRIBUTES);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,11 @@ 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 @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
Expand All @@ -21,6 +22,7 @@
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;

public final class LocalGit implements Git {

Expand All @@ -43,13 +45,17 @@ public void init(Path rootDirPath) {

@Override
public void clone(Path originDirPath, Path rootDirPath) {
String uri = "file://" + fs.getFile(originDirPath).getAbsolutePath();
String srcDirAbsolutePath = fs.getFile(originDirPath).getAbsolutePath();
String destDirAbsolutePath = fs.getFile(rootDirPath).getAbsolutePath();

File root = fs.getFile(rootDirPath);
fs.copyDirectory(Path.of(srcDirAbsolutePath), Path.of(destDirAbsolutePath));

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

try {
org.eclipse.jgit.api.Git.cloneRepository().setURI(uri).setDirectory(root).call().close();
} catch (GitAPIException e) {
org.eclipse.jgit.api.Git.open(destDir).remoteAdd().setName("origin").setUri(new URIish(destURI)).call();
} catch (IOException | GitAPIException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -88,12 +94,20 @@ public void addAll(Path rootDirPath) {
}

@Override
public void commit(Path rootDirPath, String committerName, String committerEmail, String title, String description) {
public void commit(
Path rootDirPath,
String committerName,
String committerEmail,
String title,
String description) {
File root = fs.getFile(rootDirPath);

try {
Repository repo = FileRepositoryBuilder.create(new File(root, ".git"));
new org.eclipse.jgit.api.Git(repo).commit().setAuthor(committerName, committerEmail).setMessage(title + "\n\n" + description).call();
new org.eclipse.jgit.api.Git(repo).commit()
.setAuthor(committerName, committerEmail)
.setMessage(title + "\n\n" + description)
.call();
repo.close();
} catch (IOException | GitAPIException e) {
throw new RuntimeException(e);
Expand All @@ -115,7 +129,6 @@ public boolean rebase(Path rootDirPath) {
}
repo.close();
return true;

} catch (IOException | GitAPIException e) {
throw new RuntimeException(e);
}
Expand All @@ -135,7 +148,6 @@ public boolean push(Path rootDirPath) {
} catch (IOException | GitAPIException e) {
throw new RuntimeException(e);
}

}

@Override
Expand Down Expand Up @@ -174,11 +186,15 @@ public List<GitCommit> getLog(Path rootDirPath) {
Iterable<RevCommit> logs = new org.eclipse.jgit.api.Git(repo).log().call();
ImmutableList.Builder<GitCommit> versions = ImmutableList.builder();
for (RevCommit rev : logs) {
versions.add(new GitCommit(rev.getName(), rev.getAuthorIdent().getName(), new Date(rev.getCommitTime() * 1000L), rev.getShortMessage(), rev.getFullMessage()));
versions.add(new GitCommit(
rev.getName(),
rev.getAuthorIdent().getName(),
new Date(rev.getCommitTime() * 1000L),
rev.getShortMessage(),
rev.getFullMessage()));
}
repo.close();
return versions.build();

} catch (IOException | GitAPIException e) {
throw new RuntimeException(e);
}
Expand All @@ -203,27 +219,30 @@ public void restore(Path rootDirPath, String hash) {
}
command.call();

new org.eclipse.jgit.api.Git(repo).rebase().setUpstream(head).runInteractively(new RebaseCommand.InteractiveHandler() {
@Override
public void prepareSteps(List<RebaseTodoLine> list) {
for (int i = 0; i < list.size(); i++) {
try {
if (i == 0) {
list.get(i).setAction(RebaseTodoLine.Action.REWORD);
} else {
list.get(i).setAction(RebaseTodoLine.Action.FIXUP);
new org.eclipse.jgit.api.Git(repo).rebase()
.setUpstream(head)
.runInteractively(new RebaseCommand.InteractiveHandler() {
@Override
public void prepareSteps(List<RebaseTodoLine> list) {
for (int i = 0; i < list.size(); i++) {
try {
if (i == 0) {
list.get(i).setAction(RebaseTodoLine.Action.REWORD);
} else {
list.get(i).setAction(RebaseTodoLine.Action.FIXUP);
}
} catch (IllegalTodoFileModification e) {
// nothing
}
}
} catch (IllegalTodoFileModification e) {
// nothing
}
}
}

@Override
public String modifyCommitMessage(String s) {
return "Revert to commit " + hash.substring(0, 7);
}
}).call();
@Override
public String modifyCommitMessage(String s) {
return "Revert to commit " + hash.substring(0, 7);
}
})
.call();

repo.close();
} catch (IOException | GitAPIException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ 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 84cdb0e

Please sign in to comment.