Line data Source code
1 : // Copyright (C) 2010 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd.commands; 16 : 17 : import com.google.gerrit.server.DynamicOptions; 18 : import com.google.gerrit.server.query.change.OutputStreamQuery; 19 : import com.google.gerrit.server.query.change.OutputStreamQuery.OutputFormat; 20 : import com.google.gerrit.sshd.CommandMetaData; 21 : import com.google.gerrit.sshd.SshCommand; 22 : import com.google.inject.Inject; 23 : import java.util.List; 24 : import org.kohsuke.args4j.Argument; 25 : import org.kohsuke.args4j.Option; 26 : 27 : @CommandMetaData(name = "query", description = "Query the change database") 28 3 : public class Query extends SshCommand implements DynamicOptions.BeanReceiver { 29 : @Inject private OutputStreamQuery processor; 30 : 31 : @Option(name = "--format", metaVar = "FMT", usage = "Output display format") 32 : void setFormat(OutputFormat format) { 33 2 : processor.setOutput(out, format); 34 2 : } 35 : 36 : @Option(name = "--current-patch-set", usage = "Include information about current patch set") 37 : void setCurrentPatchSet(boolean on) { 38 1 : processor.setIncludeCurrentPatchSet(on); 39 1 : } 40 : 41 : @Option(name = "--patch-sets", usage = "Include information about all patch sets") 42 : void setPatchSets(boolean on) { 43 1 : processor.setIncludePatchSets(on); 44 1 : } 45 : 46 : @Option( 47 : name = "--all-approvals", 48 : usage = "Include information about all patch sets and approvals") 49 : void setApprovals(boolean on) { 50 1 : if (on) { 51 1 : processor.setIncludePatchSets(on); 52 : } 53 1 : processor.setIncludeApprovals(on); 54 1 : } 55 : 56 : @Option(name = "--comments", usage = "Include patch set and inline comments") 57 : void setComments(boolean on) { 58 1 : processor.setIncludeComments(on); 59 1 : } 60 : 61 : @Option(name = "--files", usage = "Include file list on patch sets") 62 : void setFiles(boolean on) { 63 1 : processor.setIncludeFiles(on); 64 1 : } 65 : 66 : @Option(name = "--commit-message", usage = "Include the full commit message for a change") 67 : void setCommitMessage(boolean on) { 68 1 : processor.setIncludeCommitMessage(on); 69 1 : } 70 : 71 : @Option(name = "--dependencies", usage = "Include depends-on and needed-by information") 72 : void setDependencies(boolean on) { 73 1 : processor.setIncludeDependencies(on); 74 1 : } 75 : 76 : @Option(name = "--all-reviewers", usage = "Include all reviewers") 77 : void setAllReviewers(boolean on) { 78 1 : processor.setIncludeAllReviewers(on); 79 1 : } 80 : 81 : @Option(name = "--submit-records", usage = "Include submit and label status") 82 : void setSubmitRecords(boolean on) { 83 1 : processor.setIncludeSubmitRecords(on); 84 1 : } 85 : 86 : @Option( 87 : name = "--start", 88 : aliases = {"-S"}, 89 : usage = "Number of changes to skip") 90 : void setStart(int start) { 91 1 : processor.setStart(start); 92 1 : } 93 : 94 : @Option(name = "--no-limit", usage = "Return all results, overriding the default limit") 95 : void setNoLimit(boolean on) { 96 0 : processor.setNoLimit(on); 97 0 : } 98 : 99 : @Argument( 100 : index = 0, 101 : required = true, 102 : multiValued = true, 103 : metaVar = "QUERY", 104 : usage = "Query to execute") 105 : private List<String> query; 106 : 107 : @Override 108 : protected void run() throws Exception { 109 3 : enableGracefulStop(); 110 3 : processor.query(join(query, " ")); 111 3 : } 112 : 113 : @Override 114 : public void setDynamicBean(String plugin, DynamicOptions.DynamicBean dynamicBean) { 115 2 : processor.setDynamicBean(plugin, dynamicBean); 116 2 : } 117 : 118 : @Override 119 : protected void parseCommandLine(DynamicOptions pluginOptions) throws UnloggedFailure { 120 3 : processor.setOutput(out, OutputFormat.TEXT); 121 3 : super.parseCommandLine(pluginOptions); 122 3 : if (processor.getIncludeFiles() 123 1 : && !(processor.getIncludePatchSets() || processor.getIncludeCurrentPatchSet())) { 124 1 : throw die("--files option needs --patch-sets or --current-patch-set"); 125 : } 126 3 : } 127 : 128 : private static String join(List<String> list, String sep) { 129 3 : StringBuilder r = new StringBuilder(); 130 3 : for (int i = 0; i < list.size(); i++) { 131 3 : if (i > 0) { 132 2 : r.append(sep); 133 : } 134 3 : r.append(list.get(i)); 135 : } 136 3 : return r.toString(); 137 : } 138 : }