1 /*
2 * #%L
3 * JavaHg
4 * %%
5 * Copyright (C) 2011 aragost Trifork ag
6 * %%
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 * #L%
25 */
26 package com.aragost.javahg.commands;
27
28 import java.util.List;
29
30 import com.aragost.javahg.Changeset;
31 import com.aragost.javahg.Repository;
32 import com.aragost.javahg.commands.flags.LogCommandFlags;
33 import com.aragost.javahg.internals.HgInputStream;
34 import com.aragost.javahg.internals.Utils;
35
36 /**
37 * Command class for executing <tt>hg log</tt>. Set flags from
38 * {@link LogCommandFlags} and call the {@link #execute} method.
39 */
40 public class LogCommand extends LogCommandFlags {
41
42 private String stylePath;
43 private boolean eager = false;
44
45 /**
46 * @param repository
47 * the repository associated with this command.
48 */
49 public LogCommand(Repository repository) {
50 this(repository, Changeset.CHANGESET_STYLE_PATH);
51 }
52
53 /**
54 * Construct a LogCommand using the specified template.
55 *
56 * @param repository
57 * @param template
58 */
59 protected LogCommand(Repository repository, String stylePath) {
60 super(repository);
61 withDebugFlag();
62 this.stylePath = stylePath;
63 }
64
65 /**
66 * Enable eager load for changeset file data.
67 *
68 * @return {@code this}
69 */
70 public LogCommand fileStatus(){
71 eager = true;
72 stylePath = Changeset.CHANGESET_EAGER_STYLE_PATH;
73 return this;
74 }
75
76 /**
77 * @param files
78 * an optional list of files to retrieve the log for.
79 * With no files, all changesets are considered.
80 * @return the log as a list of changesets
81 */
82 public List<Changeset> execute(String... files) {
83 if (stylePath != null) {
84 cmdAppend("--style", stylePath);
85 }
86 HgInputStream stream = launchStream(files);
87 return Changeset.readListFromStream(getRepository(), stream, eager);
88 }
89
90 /**
91 * Execute the log command and return a single Changeset.
92 * <p>
93 * If the log command gives more than one changeset an
94 * {@link IllegalArgumentException} is thrown. <code>null</code>
95 * is returned if the log command gives no Changesets.
96 *
97 * @param files
98 * @return a single Changeset
99 * @throws IllegalArgumentException
100 * if the log command returns more than one changeset
101 */
102 public Changeset single(String... files) {
103 return Utils.single(execute(files));
104 }
105
106 }