package uk.ac.ic.wlgitbridge.bridge.repo;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.data.filestore.GitDirectoryContents;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Optional;

/**
 * We compose because we don't want to deal with the static constructors
 */
public class WalkOverrideGitRepo implements ProjectRepo {

    private final GitProjectRepo gitRepo;

    private final Optional<Long> maxFileSize;

    private final Optional<ObjectId> commitId;

    public WalkOverrideGitRepo(
            GitProjectRepo gitRepo,
            Optional<Long> maxFileSize,
            Optional<ObjectId> commitId
    ) {
        this.gitRepo = gitRepo;
        this.maxFileSize = maxFileSize;
        this.commitId = commitId;
    }

    @Override
    public String getProjectName() {
        return gitRepo.getProjectName();
    }

    @Override
    public void initRepo(RepoStore repoStore) throws IOException {
        gitRepo.initRepo(repoStore);
    }

    @Override
    public void useExistingRepository(RepoStore repoStore) throws IOException {
        gitRepo.useExistingRepository(repoStore);
    }

    @Override
    public RawDirectory getDirectory() throws IOException, GitUserException {
        Repository repo = gitRepo.getJGitRepository();
        RepositoryObjectTreeWalker walker;
        if (commitId.isPresent()) {
            walker = new RepositoryObjectTreeWalker(repo, commitId.get());
        } else {
            walker = new RepositoryObjectTreeWalker(repo);
        }
        return walker.getDirectoryContents(maxFileSize);
    }

    @Override
    public Collection<String> commitAndGetMissing(
            GitDirectoryContents gitDirectoryContents
    ) throws GitUserException, IOException {
        return gitRepo.commitAndGetMissing(gitDirectoryContents);
    }

    @Override
    public void runGC() throws IOException {
        gitRepo.runGC();
    }

    @Override
    public void deleteIncomingPacks() throws IOException {
        gitRepo.deleteIncomingPacks();
    }

    @Override
    public File getProjectDir() {
        return gitRepo.getProjectDir();
    }

    @Override
    public Repository getJGitRepository() {
        return gitRepo.getJGitRepository();
    }

}

@@ -479,53 +479,213 @@
 %0A * 
-We compose because we don't want to deal with
+This class takes a GitProjectRepo and delegates all calls to it.%0A *%0A * The purpose is to insert a file size check in %7B@link #getDirectory()%7D.%0A *%0A * We delegate instead of subclass because we can't override
  the
@@ -691,16 +691,19 @@
 e static
+%0A *
  constru
@@ -707,16 +707,43 @@
 tructors
+ in %7B@link GitProjectRepo%7D.
 %0A */%0Apub

Add some JavaDoc to WalkOverrideGitRepo