com.aragost.javahg.commands.CopyCommand Maven / Gradle / Ivy
/*
* #%L
* JavaHg
* %%
* Copyright (C) 2011 aragost Trifork ag
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
package com.aragost.javahg.commands;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.flags.CopyCommandFlags;
import com.aragost.javahg.internals.UnexpectedCommandOutputException;
import com.aragost.javahg.internals.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
/**
* Command class for executing hg copy. Set flags from
* {@link CopyCommandFlags} and call the {@link #execute} method.
*/
public class CopyCommand extends CopyCommandFlags {
private static final String PREFIX = "copying ";
/**
* @param repository
* the repository associated with this command.
*/
public CopyCommand(Repository repository) {
super(repository);
withDebugFlag();
}
/**
* @param files
* the files to copy. The last file in this list is the
* destination. Two files are given, the target must be
* an existing directory or a non-existing file. If
* more files are given, the destination must be an
* existing directory.
* @return mapping from new names to old names.
* @throws IOException
*/
// TODO: this would be clearer if we overloaded execute to take
// either (String src, String dst) or (String[] srcs, String dst).
public Map execute(String... files) {
Map map = execute(Utils.stringArray2FileArray(getRepository().getDirectory(), files));
Map result = Maps.newHashMap();
for (Map.Entry entry : map.entrySet()) {
result.put(entry.getKey().getPath(), entry.getValue().getPath());
}
return result;
}
public Map execute(File... files) {
if (files.length < 2) {
throw new IllegalArgumentException("must copy at least two files");
}
Map result = Maps.newHashMap();
// Assume multiple source files by default, in which case the
// destination must be a directory.
boolean dstIsDirectory = true;
if (files.length == 2) {
dstIsDirectory = files[1].isDirectory();
}
// Make all files relative to repository
for (int i = 0; i < files.length; i++) {
File file = files[i];
files[i] = getRepository().relativeFile(file);
}
int currentSrc = 0;
File src = files[currentSrc];
File dst = files[files.length - 1];
for (String line : launchIterator(Utils.fileArray2StringArray(files))) {
if (line.startsWith(PREFIX)) {
if (!line.substring(PREFIX.length()).startsWith(src.getPath())) {
currentSrc++;
src = files[currentSrc];
}
String[] names = parseCopyLine(src.getPath(), dst.getPath(), line, dstIsDirectory);
result.put(new File(names[1]), new File(names[0]));
} else {
throw new UnexpectedCommandOutputException("could not parse '" + line + "'");
}
}
return result;
}
@VisibleForTesting
static String[] parseCopyLine(String src, String dst, String line, boolean dstIsDirectory) {
if (src.charAt(src.length() - 1) == File.separatorChar) {
src = src.substring(0, src.length() - 1);
}
if (dst.charAt(dst.length() - 1) == File.separatorChar) {
dst = dst.substring(0, dst.length() - 1);
}
line = line.substring(PREFIX.length()).trim();
int lineLength = line.length();
int srcLength = src.length();
int dstLength = dst.length();
boolean srcIsDirectory = line.charAt(srcLength) == File.separatorChar;
if (dstIsDirectory) {
dstLength += 1;
}
if (srcIsDirectory) {
srcLength += 1;
}
int pathLength = lineLength - 4 - srcLength - dstLength;
if (dstIsDirectory) {
pathLength -= srcLength;
}
if (pathLength % 2 != 0) {
throw new IllegalStateException("pathLength not even");
}
pathLength /= 2;
String oldName = line.substring(0, srcLength + pathLength);
String newName = line.substring(srcLength + pathLength + 4, lineLength);
return new String[] { oldName, newName };
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy