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

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;

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

/**
 * Created by winston on 20/08/2016.
 */
public interface RepoStore {

    /* Still need to get rid of these two methods.
       Main dependency: GitRepoStore needs a Repository which needs a directory.
       Instead, use a visitor or something. */
    String getRepoStorePath();

    File getRootDirectory();

    ProjectRepo initRepo(String project) throws IOException;

    ProjectRepo initRepoFromExisting(String project, String fromProject) throws IOException;

    ProjectRepo getExistingRepo(String project) throws IOException;

    ProjectRepo useJGitRepo(Repository repo, ObjectId commitId);

    void purgeNonexistentProjects(
            Collection<String> existingProjectNames
    );

    long totalSize();

    /**
     * Tars and bzip2s the .git directory of the given project. Throws an
     * IOException if the project doesn't exist. The returned stream is a copy
     * of the original .git directory, which must be deleted using remove().
     */
    InputStream bzip2Project(
            String projectName,
            long[] sizePtr
    ) throws IOException;

    default InputStream bzip2Project(
            String projectName
    ) throws IOException {
        return bzip2Project(projectName, null);
    }

    void gcProject(String projectName) throws IOException;

    /**
     * Called after {@link #bzip2Project(String, long[])}'s has been safely
     * uploaded to the swap store. Removes all traces of the project from disk,
     * i.e. not just its .git, but the whole project's git directory.
     * @param projectName
     * @throws IOException
     */
    void remove(String projectName) throws IOException;

    /**
     * Unbzip2s the given data stream into a .git directory for projectName.
     * Creates the project's git directory.
     * If projectName already exists, throws an IOException.
     * @param projectName the name of the project, e.g. abc123
     * @param dataStream the data stream containing the bzipped contents.
     */
    void unbzip2Project(
            String projectName,
            InputStream dataStream
    ) throws IOException;

}

@@ -1460,16 +1460,513 @@
 %0A    %7D%0A%0A
+    /**%0A     * Tars and gzips the .git directory of the given project. Throws an%0A     * IOException if the project doesn't exist. The returned stream is a copy%0A     * of the original .git directory, which must be deleted using remove().%0A     */%0A    InputStream gzipProject(%0A        String projectName,%0A        long%5B%5D sizePtr%0A    ) throws IOException;%0A%0A    default InputStream gzipProject(%0A        String projectName%0A    ) throws IOException %7B%0A        return gzipProject(projectName, null);%0A    %7D%0A%0A
     void
@@ -2823,10 +2823,451 @@
 ption;%0A%0A
+    /**%0A     * Ungzips the given data stream into a .git directory for projectName.%0A     * Creates the project's git directory.%0A     * If projectName already exists, throws an IOException.%0A     * @param projectName the name of the project, e.g. abc123%0A     * @param dataStream the data stream containing the gzip contents.%0A     */%0A    void ungzipProject(%0A        String projectName,%0A        InputStream dataStream%0A    ) throws IOException;%0A%0A
 %7D%0A

Add gzip to RepoStore interface