Search in sources :

Example 1 with PatchFormatException

use of org.eclipse.jgit.api.errors.PatchFormatException in project repairnator by Spirals-Team.

the class GitHelper method retrieveAndApplyCommitFromGithub.

/**
 * When a commit has been force deleted it still can be retrieved from
 * GitHub API. This function intend to retrieve a patch from the Github API
 * and to apply it back on the repo
 *
 * @param git
 * @param oldCommitSha
 * @return the SHA of the commit created after applying the patch or null if
 *         an error occured.
 */
private String retrieveAndApplyCommitFromGithub(Git git, String oldCommitSha, AbstractStep step, Build build) {
    try {
        addAndCommitRepairnatorLogAndProperties(step.getInspector().getJobStatus(), git, "Commit done before retrieving a commit from GH API.");
        GitHub gh = RepairnatorConfig.getInstance().getGithub();
        GHRepository ghRepo = gh.getRepository(build.getRepository().getSlug());
        String lastKnowParent = getLastKnowParent(gh, ghRepo, git, oldCommitSha, step);
        // checkout the repo to the last known parent of the deleted commit
        git.checkout().setName(lastKnowParent).call();
        // get from github a patch between that commit and the targeted
        // commit
        // note that this patch could contain changes of multiple commits
        GHCompare compare = ghRepo.getCompare(lastKnowParent, oldCommitSha);
        showGitHubRateInformation(gh, step);
        URL patchUrl = compare.getPatchUrl();
        this.getLogger().debug("Step " + step.getName() + " - Retrieve commit patch from the following URL: " + patchUrl);
        // retrieve it through a simple HTTP request
        // some errors occurs when applying patch from snippets contained in
        // GHCompare object
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(patchUrl).build();
        Call call = client.newCall(request);
        Response response = call.execute();
        File tempFile = File.createTempFile(build.getRepository().getSlug(), "patch");
        // the referenced commit.
        if (response.code() == 200) {
            FileWriter writer = new FileWriter(tempFile);
            writer.write(response.body().string());
            writer.flush();
            writer.close();
            this.getLogger().info("Step " + step.getName() + " - Exec following command: git apply " + tempFile.getAbsolutePath());
            ProcessBuilder processBuilder = new ProcessBuilder("git", "apply", tempFile.getAbsolutePath()).directory(new File(step.getInspector().getRepoLocalPath())).inheritIO();
            Process p = processBuilder.start();
            try {
                p.waitFor();
                // Applying patch does not work as the move of file is
                // broken in JGit
                // It assumes the target directory exists.
                // ApplyResult result =
                // git.apply().setPatch(response.body().byteStream()).call();
                Commit buildCommit = build.getCommit();
                // add all file for the next commit
                git.add().addFilepattern(".").call();
                // do the commit
                RevCommit ref = git.commit().setAll(true).setAuthor(buildCommit.getAuthorName(), buildCommit.getAuthorEmail()).setCommitter(buildCommit.getCommitterName(), buildCommit.getCommitterEmail()).setMessage(buildCommit.getMessage() + "\n(This is a retrieve from the following deleted commit: " + oldCommitSha + ")").call();
                this.nbCommits++;
                tempFile.delete();
                step.getInspector().getJobStatus().setCommitRetrievedFromGithub(true);
                return ref.getName();
            } catch (InterruptedException e) {
                step.addStepError("Error while executing git command to apply patch: " + e);
            }
        }
    } catch (IOException e) {
        step.addStepError("Error while getting commit from Github: " + e);
    } catch (PatchFormatException e) {
        step.addStepError("Error while getting patch from Github: " + e);
    } catch (PatchApplyException e) {
        step.addStepError("Error while applying patch from Github: " + e);
    } catch (GitAPIException e) {
        step.addStepError("Error with Git API: " + e);
    }
    return null;
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) FileWriter(java.io.FileWriter) Request(okhttp3.Request) IOException(java.io.IOException) URL(java.net.URL) Response(okhttp3.Response) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) PatchApplyException(org.eclipse.jgit.api.errors.PatchApplyException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Commit(fr.inria.jtravis.entities.Commit) PatchFormatException(org.eclipse.jgit.api.errors.PatchFormatException) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Commit (fr.inria.jtravis.entities.Commit)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 PatchApplyException (org.eclipse.jgit.api.errors.PatchApplyException)1 PatchFormatException (org.eclipse.jgit.api.errors.PatchFormatException)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1