Search in sources :

Example 1 with Watchdog

use of com.atlassian.utils.process.Watchdog in project stashbot by palantir.

the class CommandOutputHandlerFactory method getBranchContainsOutputHandler.

/**
 * Returns an output handler which provides an ImmutableList of branches in the form "refs/heads/foo" when the
 * command prints out a list of the form "XXfoo\n" where X doesn't matter.
 *
 * @return ImmutableList<String> branchNames
 */
public CommandOutputHandler<Object> getBranchContainsOutputHandler() {
    return new CommandOutputHandler<Object>() {

        private ArrayList<String> branches;

        private LineReader lr;

        private boolean processed = false;

        @Override
        public void complete() throws ProcessException {
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public Object getOutput() {
            if (processed == false) {
                throw new IllegalStateException("getOutput() called before process()");
            }
            return ImmutableList.copyOf(branches);
        }

        @Override
        public void process(InputStream output) throws ProcessException {
            processed = true;
            if (branches == null) {
                branches = new ArrayList<String>();
                lr = new LineReader(new InputStreamReader(output));
            }
            String branch = "";
            while (branch != null) {
                try {
                    branch = lr.readLine();
                } catch (IOException e) {
                    // dear god, why is this happening?
                    throw new RuntimeException(e);
                }
                // format is "  somebranch\n* branchIamOn\n  someOtherBranch"
                if (branch != null) {
                    branches.add("refs/heads/" + branch.substring(2));
                }
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Watchdog(com.atlassian.utils.process.Watchdog) InputStream(java.io.InputStream) LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler)

Example 2 with Watchdog

use of com.atlassian.utils.process.Watchdog in project stash-codesearch-plugin by palantir.

the class GitVersionValidator method validateVersion.

private String validateVersion(GitScm gitScm) {
    GitCommandBuilderFactory gcbf = gitScm.getCommandBuilderFactory();
    CommandOutputHandler<String> coh = new CommandOutputHandler<String>() {

        private final StringBuilder sb = new StringBuilder();

        private boolean finished = false;

        @Override
        public void process(InputStream output) throws ProcessException {
            final LineReader lr = new LineReader(new InputStreamReader(output));
            String line = null;
            try {
                while ((line = lr.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error while determining git version:", e);
            }
        }

        @Override
        public void complete() throws ProcessException {
            finished = true;
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public String getOutput() {
            if (!finished) {
                throw new IllegalStateException("Called getOutput() before complete()");
            }
            return sb.toString();
        }
    };
    Command<String> versionCommand = gcbf.builder().version(coh);
    versionCommand.call();
    String versionString = coh.getOutput();
    // version string looks like this: git version 1.7.9.5
    String versionNumber = versionString.split(" ")[2];
    String[] versionParts = versionNumber.split("\\.");
    int min = (versionParts.length <= REQUIRED_GIT_VERSION.length) ? versionParts.length : REQUIRED_GIT_VERSION.length;
    for (int i = 0; i < min; ++i) {
        try {
            int test = Integer.parseInt(versionParts[i]);
            if (test > REQUIRED_GIT_VERSION[i]) {
                return versionNumber;
            } else if (test < REQUIRED_GIT_VERSION[i]) {
                throw new IllegalStateException("Invalid Git Version '" + versionNumber + "', must be equal to or greater than '" + REQUIRED_GIT_VERSION_STRING + "'");
            }
        // else: keep checking next number
        } catch (NumberFormatException e) {
            throw new RuntimeException("Error while determining git version:", e);
        }
    }
    // if we get here, we had a perfect match
    return versionNumber;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler) Watchdog(com.atlassian.utils.process.Watchdog) GitCommandBuilderFactory(com.atlassian.stash.scm.git.GitCommandBuilderFactory) LineReader(com.google.common.io.LineReader)

Example 3 with Watchdog

use of com.atlassian.utils.process.Watchdog in project stashbot by palantir.

the class CommandOutputHandlerFactory method getRevlistOutputHandler.

public CommandOutputHandler<Object> getRevlistOutputHandler() {
    return new CommandOutputHandler<Object>() {

        private ArrayList<String> changesets;

        private LineReader lr;

        private boolean processed = false;

        @Override
        public void complete() throws ProcessException {
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public Object getOutput() {
            if (processed == false) {
                throw new IllegalStateException("getOutput() called before process()");
            }
            return ImmutableList.copyOf(changesets).reverse();
        }

        @Override
        public void process(InputStream output) throws ProcessException {
            processed = true;
            if (changesets == null) {
                changesets = new ArrayList<String>();
                lr = new LineReader(new InputStreamReader(output));
            }
            String sha1 = "";
            while (sha1 != null) {
                try {
                    sha1 = lr.readLine();
                } catch (IOException e) {
                    // dear god, why is this happening?
                    throw new RuntimeException(e);
                }
                if (sha1 != null && sha1.matches("[0-9a-fA-F]{40}")) {
                    changesets.add(sha1);
                }
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Watchdog(com.atlassian.utils.process.Watchdog) InputStream(java.io.InputStream) LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler)

Aggregations

CommandOutputHandler (com.atlassian.stash.scm.CommandOutputHandler)3 Watchdog (com.atlassian.utils.process.Watchdog)3 LineReader (com.google.common.io.LineReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 ArrayList (java.util.ArrayList)2 GitCommandBuilderFactory (com.atlassian.stash.scm.git.GitCommandBuilderFactory)1