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.util.List;
29  
30  import com.aragost.javahg.Repository;
31  import com.aragost.javahg.commands.flags.AddRemoveCommandFlags;
32  import com.aragost.javahg.internals.UnexpectedCommandOutputException;
33  import com.google.common.collect.Lists;
34  
35  /**
36   * Command class for executing <tt>hg addremove</tt>. Set flags from
37   * {@link AddRemoveCommandFlags} and see the {@link #execute(String...)}
38   * method for how to run the command.
39   */
40  public class AddRemoveCommand extends AddRemoveCommandFlags {
41  
42      /**
43       * @param repository
44       *            the repository associated with this command.
45       */
46      public AddRemoveCommand(Repository repository) {
47          super(repository);
48          withDebugFlag();
49      }
50  
51      /**
52       * Check if the command ended successfully.
53       * <p>
54       * In contrast with Mercurial, a return code of 1 is considered to
55       * be successful. Mercurial returns 1 if some files could not be
56       * found, but at the same time it does add the files that could be
57       * found.
58       * 
59       * @return true if the command exited with 0 or 1.
60       */
61      @Override
62      public boolean isSuccessful() {
63          return super.isSuccessful() || getReturnCode() == 1;
64      }
65  
66      /**
67      * @return List of added or removed files
68      */
69     public List<String> execute() {
70          List<String> result = Lists.newArrayList();
71          for (String line : launchIterator(new String[0])) {        	
72          	String prefix = null;
73          	
74          	final String ADDING   = "adding ";
75  			final String REMOVING = "removing ";
76  			
77  			if (line.startsWith(ADDING)) {
78  				prefix = ADDING;
79  			} else if (line.startsWith(REMOVING)) {
80  				prefix = REMOVING;
81  			}
82  				
83  			if (prefix != null) {
84                  String file = new String(line.substring(prefix.length()));
85                  result.add(file);
86              } else {
87              	if (line.startsWith("searching for exact renames")) {
88              		continue;
89              	}
90  
91              	// TODO: should clean up be called here? If so it should be for all
92              	// other uses of launchIterator in a finally block
93              	// cleanUp();
94                  throw new UnexpectedCommandOutputException(this, line);
95              }
96          }
97          return result;
98      }
99  }