Mercurial > jhg
comparison src/com/tmate/hgkit/ll/HgDirstate.java @ 18:02ee376bee79
status operation against current working directory
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 03 Jan 2011 20:42:52 +0100 |
parents | 442dc6ee647b |
children | b771e94a4f7c |
comparison
equal
deleted
inserted
replaced
17:571e1b2cc3f7 | 18:02ee376bee79 |
---|---|
4 package com.tmate.hgkit.ll; | 4 package com.tmate.hgkit.ll; |
5 | 5 |
6 import java.io.File; | 6 import java.io.File; |
7 import java.io.IOException; | 7 import java.io.IOException; |
8 import java.util.Collections; | 8 import java.util.Collections; |
9 import java.util.LinkedList; | 9 import java.util.LinkedHashMap; |
10 import java.util.List; | 10 import java.util.Map; |
11 import java.util.TreeSet; | |
11 | 12 |
12 import com.tmate.hgkit.fs.DataAccess; | 13 import com.tmate.hgkit.fs.DataAccess; |
13 import com.tmate.hgkit.fs.DataAccessProvider; | 14 import com.tmate.hgkit.fs.DataAccessProvider; |
14 | 15 |
15 /** | 16 /** |
19 */ | 20 */ |
20 public class HgDirstate { | 21 public class HgDirstate { |
21 | 22 |
22 private final LocalHgRepo repo; | 23 private final LocalHgRepo repo; |
23 private final File dirstateFile; | 24 private final File dirstateFile; |
24 private List<Record> normal; | 25 private Map<String, Record> normal; |
25 private List<Record> added; | 26 private Map<String, Record> added; |
26 private List<Record> removed; | 27 private Map<String, Record> removed; |
27 private List<Record> merged; | 28 private Map<String, Record> merged; |
28 | 29 |
29 public HgDirstate(LocalHgRepo hgRepo, File dirstate) { | 30 public HgDirstate(LocalHgRepo hgRepo, File dirstate) { |
30 this.repo = hgRepo; | 31 this.repo = hgRepo; |
31 this.dirstateFile = dirstate; | 32 this.dirstateFile = dirstate; |
32 } | 33 } |
33 | 34 |
34 private void read() { | 35 private void read() { |
35 normal = added = removed = merged = Collections.emptyList(); | 36 normal = added = removed = merged = Collections.<String, Record>emptyMap(); |
36 if (!dirstateFile.exists()) { | 37 if (!dirstateFile.exists()) { |
37 return; | 38 return; |
38 } | 39 } |
39 DataAccessProvider dap = repo.getDataAccess(); | 40 DataAccessProvider dap = repo.getDataAccess(); |
40 DataAccess da = dap.create(dirstateFile); | 41 DataAccess da = dap.create(dirstateFile); |
41 if (da.isEmpty()) { | 42 if (da.isEmpty()) { |
42 return; | 43 return; |
43 } | 44 } |
44 normal = new LinkedList<Record>(); | 45 // not sure linked is really needed here, just for ease of debug |
45 added = new LinkedList<Record>(); | 46 normal = new LinkedHashMap<String, Record>(); |
46 removed = new LinkedList<Record>(); | 47 added = new LinkedHashMap<String, Record>(); |
47 merged = new LinkedList<Record>(); | 48 removed = new LinkedHashMap<String, Record>(); |
49 merged = new LinkedHashMap<String, Record>(); | |
48 try { | 50 try { |
49 // XXX skip(40) if we don't need these? | 51 // XXX skip(40) if we don't need these? |
50 byte[] parents = new byte[40]; | 52 byte[] parents = new byte[40]; |
51 da.readBytes(parents, 0, 40); | 53 da.readBytes(parents, 0, 40); |
52 parents = null; | 54 parents = null; |
69 if (fn1 == null) { | 71 if (fn1 == null) { |
70 fn1 = new String(name); | 72 fn1 = new String(name); |
71 } | 73 } |
72 Record r = new Record(fmode, size, time, fn1, fn2); | 74 Record r = new Record(fmode, size, time, fn1, fn2); |
73 if (state == 'n') { | 75 if (state == 'n') { |
74 normal.add(r); | 76 normal.put(r.name1, r); |
75 } else if (state == 'a') { | 77 } else if (state == 'a') { |
76 added.add(r); | 78 added.put(r.name1, r); |
77 } else if (state == 'r') { | 79 } else if (state == 'r') { |
78 removed.add(r); | 80 removed.put(r.name1, r); |
79 } else if (state == 'm') { | 81 } else if (state == 'm') { |
80 merged.add(r); | 82 merged.put(r.name1, r); |
81 } else { | 83 } else { |
82 // FIXME log error? | 84 // FIXME log error? |
83 } | 85 } |
84 } while (!da.isEmpty()); | 86 } while (!da.isEmpty()); |
85 } catch (IOException ex) { | 87 } catch (IOException ex) { |
87 } finally { | 89 } finally { |
88 da.done(); | 90 da.done(); |
89 } | 91 } |
90 } | 92 } |
91 | 93 |
94 // new, modifiable collection | |
95 /*package-local*/ TreeSet<String> all() { | |
96 read(); | |
97 TreeSet<String> rv = new TreeSet<String>(); | |
98 @SuppressWarnings("unchecked") | |
99 Map<String, Record>[] all = new Map[] { normal, added, removed, merged }; | |
100 for (int i = 0; i < all.length; i++) { | |
101 for (Record r : all[i].values()) { | |
102 rv.add(r.name1); | |
103 } | |
104 } | |
105 return rv; | |
106 } | |
107 | |
108 /*package-local*/ Record checkNormal(String fname) { | |
109 return normal.get(fname); | |
110 } | |
111 | |
112 /*package-local*/ Record checkAdded(String fname) { | |
113 return added.get(fname); | |
114 } | |
115 /*package-local*/ Record checkRemoved(String fname) { | |
116 return removed.get(fname); | |
117 } | |
118 /*package-local*/ Record checkMerged(String fname) { | |
119 return merged.get(fname); | |
120 } | |
121 | |
122 | |
123 | |
124 | |
92 public void dump() { | 125 public void dump() { |
93 read(); | 126 read(); |
94 @SuppressWarnings("unchecked") | 127 @SuppressWarnings("unchecked") |
95 List<Record>[] all = new List[] { normal, added, removed, merged }; | 128 Map<String, Record>[] all = new Map[] { normal, added, removed, merged }; |
96 char[] x = new char[] {'n', 'a', 'r', 'm' }; | 129 char[] x = new char[] {'n', 'a', 'r', 'm' }; |
97 for (int i = 0; i < all.length; i++) { | 130 for (int i = 0; i < all.length; i++) { |
98 for (Record r : all[i]) { | 131 for (Record r : all[i].values()) { |
99 System.out.printf("%c %3o%6d %30tc\t\t%s", x[i], r.mode, r.size, (long) r.time * 1000, r.name1); | 132 System.out.printf("%c %3o%6d %30tc\t\t%s", x[i], r.mode, r.size, (long) r.time * 1000, r.name1); |
100 if (r.name2 != null) { | 133 if (r.name2 != null) { |
101 System.out.printf(" --> %s", r.name2); | 134 System.out.printf(" --> %s", r.name2); |
102 } | 135 } |
103 System.out.println(); | 136 System.out.println(); |
104 } | 137 } |
105 System.out.println(); | 138 System.out.println(); |
106 } | 139 } |
107 } | 140 } |
108 | 141 |
109 private static class Record { | 142 /*package-local*/ static class Record { |
110 final int mode; | 143 final int mode; |
111 final int size; | 144 final int size; |
112 final int time; | 145 final int time; |
113 final String name1; | 146 final String name1; |
114 final String name2; | 147 final String name2; |