View Javadoc

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.io.File;
29  import java.util.List;
30  
31  import com.aragost.javahg.Repository;
32  import com.aragost.javahg.commands.flags.AddCommandFlags;
33  import com.aragost.javahg.internals.AddRemoveCommandHelper;
34  
35  /**
36   * Command class for executing <tt>hg add</tt>. Set flags from
37   * {@link AddCommandFlags} and see the {@link #execute(String...)}
38   * method for how to run the command.
39   */
40  public class AddCommand extends AddCommandFlags {
41  
42      private final AddRemoveCommandHelper helper;
43  
44      /**
45       * @param repository
46       *            the repository associated with this command.
47       */
48      public AddCommand(Repository repository) {
49          super(repository);
50          helper = new AddRemoveCommandHelper(this, "adding ");
51          withDebugFlag();
52      }
53  
54      /**
55       * Check if the command ended successfully.
56       * <p>
57       * In contrast with Mercurial, a return code of 1 is considered to
58       * be successful. Mercurial returns 1 if some files could not be
59       * found, but at the same time it does add the files that could be
60       * found.
61       * 
62       * @return true if the command exited with 0 or 1.
63       */
64      @Override
65      public boolean isSuccessful() {
66          return super.isSuccessful() || getReturnCode() == 1;
67      }
68  
69      /**
70       * Execute the add command, and return list of files added.
71       * <p>
72       * If a file could not be found, then Mercurial adds the other
73       * files and exit with a return code of 1. As described in
74       * {@link #isSuccessful()}, we don't treat that as an error and
75       * the caller should instead inspect the returned list to see if
76       * all files were added.
77       * 
78       * @param paths
79       *            the files or directories to add. Leave empty to add
80       *            all untracked files.
81       * @return list of files added actually.
82       */
83      public List<String> execute(String... paths) {
84          return this.helper.execute(paths);
85      }
86  
87      public List<File> execute(File... paths) {
88          return this.helper.execute(paths);
89      }
90  
91      public List<File> execute() {
92          return this.helper.execute();
93      }
94  }