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.Map;
30
31 import com.aragost.javahg.Repository;
32 import com.aragost.javahg.commands.flags.CopyCommandFlags;
33 import com.aragost.javahg.internals.CopyRenameHelper;
34 import com.aragost.javahg.internals.Utils;
35 import com.google.common.annotations.VisibleForTesting;
36 import com.google.common.collect.Maps;
37
38 /**
39 * Command class for executing <tt>hg copy</tt>. Set flags from
40 * {@link CopyCommandFlags} and call the {@link #execute} method.
41 */
42 public class CopyCommand extends CopyCommandFlags {
43
44 @VisibleForTesting
45 static final String PREFIX = "copying ";
46
47 /**
48 * @param repository
49 * the repository associated with this command.
50 */
51 public CopyCommand(Repository repository) {
52 super(repository);
53 withDebugFlag();
54 }
55
56 /**
57 * @param files
58 * the files to copy. The last file in this list is the
59 * destination. If two files are given, the target must be a
60 * directory or a non-existing file. If more files are given, the
61 * destination must be an directory.
62 * @return mapping from new names to old names.
63 */
64 // TODO: this would be clearer if we overloaded execute to take
65 // either (String src, String dst) or (String[] srcs, String dst).
66 public Map<String, String> execute(String... files) {
67 Map<File, File> map = execute(Utils.stringArray2FileArray(getRepository().getDirectory(), files));
68 Map<String, String> result = Maps.newHashMap();
69 for (Map.Entry<File, File> entry : map.entrySet()) {
70 result.put(entry.getKey().getPath(), entry.getValue().getPath());
71 }
72 return result;
73 }
74
75 public Map<File, File> execute(File... files) {
76 if (files.length < 2) {
77 throw new IllegalArgumentException("must provide at least two files");
78 }
79
80 return CopyRenameHelper.parse(getRepository(), files, launchIterator(Utils.fileArray2StringArray(files)), PREFIX);
81 }
82 }