| 1 | |
package com.aragost.javahg.internals; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.util.Map; |
| 5 | |
|
| 6 | |
import com.aragost.javahg.Repository; |
| 7 | |
import com.google.common.annotations.VisibleForTesting; |
| 8 | |
import com.google.common.collect.Maps; |
| 9 | |
|
| 10 | 0 | public class CopyRenameHelper { |
| 11 | |
|
| 12 | |
public static Map<File, File> parse(Repository repo, File[] files, LineIterator output, String prefix){ |
| 13 | 21 | Map<File, File> result = Maps.newHashMap(); |
| 14 | |
|
| 15 | |
|
| 16 | 65 | for (int i = 0; i < files.length; i++) { |
| 17 | 44 | File file = files[i]; |
| 18 | 44 | files[i] = repo.relativeFile(file); |
| 19 | |
} |
| 20 | |
|
| 21 | 21 | int currentSrc = 0; |
| 22 | 21 | File src = files[currentSrc]; |
| 23 | 21 | for (String line : output) { |
| 24 | 26 | if (line.startsWith(prefix)) { |
| 25 | 26 | if (!line.substring(prefix.length()).startsWith(src.getPath())) { |
| 26 | 2 | currentSrc++; |
| 27 | 2 | src = files[currentSrc]; |
| 28 | |
} |
| 29 | 26 | String[] names = CopyRenameHelper.parseCopyLine(line, prefix); |
| 30 | |
|
| 31 | 26 | if (names != null) { |
| 32 | 26 | result.put(new File(names[1]), new File(names[0])); |
| 33 | |
} |
| 34 | 26 | } else { |
| 35 | 0 | throw new UnexpectedCommandOutputException("could not parse '" + line + "'"); |
| 36 | |
} |
| 37 | |
} |
| 38 | 21 | return result; |
| 39 | |
|
| 40 | |
} |
| 41 | |
|
| 42 | |
@VisibleForTesting |
| 43 | |
public static String[] parseCopyLine(String line, String prefix) { |
| 44 | 32 | line = line.substring(prefix.length()).trim(); |
| 45 | |
|
| 46 | 32 | int nCut = line.indexOf(" to "); |
| 47 | |
|
| 48 | 32 | if (nCut < 0) { |
| 49 | 0 | return null; |
| 50 | |
} |
| 51 | |
|
| 52 | 32 | return new String[] { line.substring(0, nCut), line.substring(nCut + 4)}; |
| 53 | |
} |
| 54 | |
|
| 55 | |
} |