<commit_before>package com.holmsted.gerrit;

import com.google.common.base.Strings;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;

import com.holmsted.json.JsonUtils;

public class GerritStatReader {

    public static final int GERRIT_DEFAULT_PORT = 29418;
    public static final int NO_COMMIT_LIMIT = -1;

    private final String serverName;
    private final int port;
    private List<String> projectNames = new ArrayList<>();
    private int perQueryCommitLimit = NO_COMMIT_LIMIT;
    private int overallCommitLimit = NO_COMMIT_LIMIT;

    public static GerritStatReader fromCommandLine(String serverName, int serverPort) {
        if (serverPort != 0) {
            return new GerritStatReader(serverName, serverPort);
        } else {
            return new GerritStatReader(serverName);
        }
    }

    class GerritOutput {
        private String output;
        private int rowCount;
        private int runtimeMsec;
        private boolean moreChanges;
        private int lastLineStartIndex = -1;

        public GerritOutput(@Nonnull String output) {
            this.output = output;
            int lastLineBreak = output.lastIndexOf('\n');
            if (lastLineBreak != -1) {
                lastLineStartIndex = output.lastIndexOf('\n', lastLineBreak - 1);
                if (lastLineStartIndex != -1) {
                    JSONObject metadata = JsonUtils.readJsonString(this.output.substring(lastLineStartIndex));
                    moreChanges = metadata.optBoolean("moreChanges");
                    rowCount = metadata.optInt("rowCount");
                    runtimeMsec = metadata.optInt("runTimeMilliseconds");
                }
            }
        }

        public boolean hasMoreChanges() {
            return moreChanges;
        }

        public int getRowCount() {
            return rowCount;
        }

        public int getRuntimeMec() {
            return runtimeMsec;
        }

        @Override
        public String toString() {
            return (lastLineStartIndex != -1) ? output.substring(0, lastLineStartIndex) : output;
        }
    }

    class GerritDataReader {
        private int startOffset;

        public void setStartOffset(int startOffset) {
            this.startOffset = startOffset;
        }

        public GerritOutput readData() {
            Runtime runtime = Runtime.getRuntime();
            try {
                String projectNameList = createProjectNameList();
                String command = String.format("ssh -p %s %s gerrit query %s "
                                + "--format=JSON "
                                + "--all-approvals "
                                + "--all-reviewers "
                                + "--comments "
                                + createStartOffsetArg()
                                + createLimitArg(),
                        String.valueOf(port), serverName, projectNameList);
                System.out.println(command);

                Process exec = runtime.exec(command, null);
                BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
                StringBuilder output = new StringBuilder();
                char[] buffer = new char[1024];
                int readChars;
                while ((readChars = reader.read(buffer)) != -1) {
                    output.append(String.copyValueOf(buffer, 0, readChars));
                }
                reader.close();

                int errorCode = exec.waitFor();
                if (errorCode != 0) {
                    System.err.println("Process exited with return code " + errorCode);
                    return null;
                }

                return new GerritOutput(Strings.nullToEmpty(output.toString()));
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        private String createStartOffsetArg() {
            return startOffset != 0 ? "--start " + String.valueOf(startOffset) + " " : "";
        }
    }

    public GerritStatReader(String serverName) {<commit_msg>GerritDownloader: Show the callee's stderr in case of an error<commit_after>package com.holmsted.gerrit;

import com.google.common.base.Strings;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;

import com.holmsted.json.JsonUtils;

public class GerritStatReader {

    public static final int GERRIT_DEFAULT_PORT = 29418;
    public static final int NO_COMMIT_LIMIT = -1;

    private final String serverName;
    private final int port;
    private List<String> projectNames = new ArrayList<>();
    private int perQueryCommitLimit = NO_COMMIT_LIMIT;
    private int overallCommitLimit = NO_COMMIT_LIMIT;

    public static GerritStatReader fromCommandLine(String serverName, int serverPort) {
        if (serverPort != 0) {
            return new GerritStatReader(serverName, serverPort);
        } else {
            return new GerritStatReader(serverName);
        }
    }

    class GerritOutput {
        private String output;
        private int rowCount;
        private int runtimeMsec;
        private boolean moreChanges;
        private int lastLineStartIndex = -1;

        public GerritOutput(@Nonnull String output) {
            this.output = output;
            int lastLineBreak = output.lastIndexOf('\n');
            if (lastLineBreak != -1) {
                lastLineStartIndex = output.lastIndexOf('\n', lastLineBreak - 1);
                if (lastLineStartIndex != -1) {
                    JSONObject metadata = JsonUtils.readJsonString(this.output.substring(lastLineStartIndex));
                    moreChanges = metadata.optBoolean("moreChanges");
                    rowCount = metadata.optInt("rowCount");
                    runtimeMsec = metadata.optInt("runTimeMilliseconds");
                }
            }
        }

        public boolean hasMoreChanges() {
            return moreChanges;
        }

        public int getRowCount() {
            return rowCount;
        }

        public int getRuntimeMec() {
            return runtimeMsec;
        }

        @Override
        public String toString() {
            return (lastLineStartIndex != -1) ? output.substring(0, lastLineStartIndex) : output;
        }
    }

    class GerritDataReader {
        private int startOffset;

        public void setStartOffset(int startOffset) {
            this.startOffset = startOffset;
        }

        public GerritOutput readData() {
            Runtime runtime = Runtime.getRuntime();
            try {
                String projectNameList = createProjectNameList();
                String command = String.format("ssh -p %s %s gerrit query %s "
                                + "--format=JSON "
                                + "--all-approvals "
                                + "--all-reviewers "
                                + "--comments "
                                + createStartOffsetArg()
                                + createLimitArg(),
                        String.valueOf(port), serverName, projectNameList);
                System.out.println(command);

                Process exec = runtime.exec(command, null);

                char[] buffer = new char[1024];
                int readChars;

                BufferedReader readerOut = new BufferedReader(new InputStreamReader(exec.getInputStream()));
                StringBuilder output = new StringBuilder();
                while ((readChars = readerOut.read(buffer)) != -1) {
                    output.append(String.copyValueOf(buffer, 0, readChars));
                }
                readerOut.close();

                BufferedReader readerErr = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
                StringBuilder error = new StringBuilder();
                while ((readChars = readerErr.read(buffer)) != -1) {
                    error.append(String.copyValueOf(buffer, 0, readChars));
                }
                readerErr.close();

                int errorCode = exec.waitFor();
                if (errorCode != 0) {
                    System.err.println("Process exited with return code " + errorCode + " and output:");
                    System.err.println(error);
                    return null;
                }

                return new GerritOutput(Strings.nullToEmpty(output.toString()));
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        private String createStartOffsetArg() {
            return startOffset != 0 ? "--start " + String.valueOf(startOffset) + " " : "";
        }
    }

    public GerritStatReader(String serverName) {
{'fraction_non_alphanumeric': 0.08906882591093117, 'fraction_numerical': 0.004814531130320604, 'mean_word_length': 1.7604953186348535, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2894295', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2173, 'n_words': 527}