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