comparison src/org/tmatesoft/hg/repo/ext/MqManager.java @ 465:2078692eeb58 smartgit3

MqManager: read status/series of the active queue
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 21 Jun 2012 21:36:06 +0200
parents 1a3c18d57a8e
children 7bcfbc255f48
comparison
equal deleted inserted replaced
464:1a3c18d57a8e 465:2078692eeb58
43 * @author Artem Tikhomirov 43 * @author Artem Tikhomirov
44 * @author TMate Software Ltd. 44 * @author TMate Software Ltd.
45 */ 45 */
46 public class MqManager { 46 public class MqManager {
47 47
48 private static final String PATCHES_DIR = "patches";
49
48 private final HgRepository repo; 50 private final HgRepository repo;
49 private List<PatchRecord> applied = Collections.emptyList(); 51 private List<PatchRecord> applied = Collections.emptyList();
50 private List<PatchRecord> allKnown = Collections.emptyList(); 52 private List<PatchRecord> allKnown = Collections.emptyList();
51 private List<String> queueNames = Collections.emptyList(); 53 private List<String> queueNames = Collections.emptyList();
52 private String activeQueue = "patches"; 54 private String activeQueue = PATCHES_DIR;
53 55
54 public MqManager(HgRepository hgRepo) { 56 public MqManager(HgRepository hgRepo) {
55 repo = hgRepo; 57 repo = hgRepo;
56 } 58 }
57 59
61 public void refresh() throws HgInvalidControlFileException { 63 public void refresh() throws HgInvalidControlFileException {
62 applied = allKnown = Collections.emptyList(); 64 applied = allKnown = Collections.emptyList();
63 queueNames = Collections.emptyList(); 65 queueNames = Collections.emptyList();
64 File repoDir = HgInternals.getRepositoryDir(repo); 66 File repoDir = HgInternals.getRepositoryDir(repo);
65 final LogFacility log = HgInternals.getContext(repo).getLog(); 67 final LogFacility log = HgInternals.getContext(repo).getLog();
66 final File fileStatus = new File(repoDir, "patches/status");
67 final File fileSeries = new File(repoDir, "patches/series");
68 try { 68 try {
69 File queues = new File(repoDir, "patches.queues"); 69 File queues = new File(repoDir, "patches.queues");
70 if (queues.isFile()) { 70 if (queues.isFile()) {
71 LineReader lr = new LineReader(queues, log).trimLines(true).skipEmpty(true); 71 LineReader lr = new LineReader(queues, log).trimLines(true).skipEmpty(true);
72 lr.read(new SimpleLineCollector(), queueNames = new LinkedList<String>()); 72 lr.read(new SimpleLineCollector(), queueNames = new LinkedList<String>());
73 } 73 }
74 final String queueLocation; // path under .hg to patch queue information (status, series and diff files)
74 File activeQueueFile = new File(repoDir, "patches.queue"); 75 File activeQueueFile = new File(repoDir, "patches.queue");
75 ArrayList<String> contents = new ArrayList<String>(); 76 // file is there only if it's not default queue ('patches') that is active
76 if (activeQueueFile.isFile()) { 77 if (activeQueueFile.isFile()) {
78 ArrayList<String> contents = new ArrayList<String>();
77 new LineReader(activeQueueFile, log).read(new SimpleLineCollector(), contents); 79 new LineReader(activeQueueFile, log).read(new SimpleLineCollector(), contents);
78 if (contents.isEmpty()) { 80 if (contents.isEmpty()) {
79 log.warn(getClass(), "File %s with active queue name is empty", activeQueueFile.getName()); 81 log.warn(getClass(), "File %s with active queue name is empty", activeQueueFile.getName());
80 activeQueue = "patches"; 82 activeQueue = PATCHES_DIR;
83 queueLocation = PATCHES_DIR + '/';
81 } else { 84 } else {
82 activeQueue = contents.get(0); 85 activeQueue = contents.get(0);
86 queueLocation = PATCHES_DIR + '-' + activeQueue + '/';
83 } 87 }
84 } else { 88 } else {
85 activeQueue = "patches"; 89 activeQueue = PATCHES_DIR;
86 } 90 queueLocation = PATCHES_DIR + '/';
91 }
92 final Path.Source patchLocation = new Path.Source() {
93
94 public Path path(String p) {
95 StringBuilder sb = new StringBuilder(64);
96 sb.append(".hg/");
97 sb.append(queueLocation);
98 sb.append(p);
99 return Path.create(sb);
100 }
101 };
102 final File fileStatus = new File(repoDir, queueLocation + "status");
103 final File fileSeries = new File(repoDir, queueLocation + "series");
87 if (fileStatus.isFile()) { 104 if (fileStatus.isFile()) {
88 new LineReader(fileStatus, log).read(new LineConsumer<List<PatchRecord>>() { 105 new LineReader(fileStatus, log).read(new LineConsumer<List<PatchRecord>>() {
89 106
90 public boolean consume(String line, List<PatchRecord> result) throws IOException { 107 public boolean consume(String line, List<PatchRecord> result) throws IOException {
91 int sep = line.indexOf(':'); 108 int sep = line.indexOf(':');
93 log.warn(MqManager.class, "Bad line in %s:%s", fileStatus.getPath(), line); 110 log.warn(MqManager.class, "Bad line in %s:%s", fileStatus.getPath(), line);
94 return true; 111 return true;
95 } 112 }
96 Nodeid nid = Nodeid.fromAscii(line.substring(0, sep)); 113 Nodeid nid = Nodeid.fromAscii(line.substring(0, sep));
97 String name = new String(line.substring(sep+1)); 114 String name = new String(line.substring(sep+1));
98 result.add(new PatchRecord(nid, name, Path.create(".hg/patches/" + name))); 115 result.add(new PatchRecord(nid, name, patchLocation.path(name)));
99 return true; 116 return true;
100 } 117 }
101 }, applied = new LinkedList<PatchRecord>()); 118 }, applied = new LinkedList<PatchRecord>());
102 } 119 }
103 if (fileSeries.isFile()) { 120 if (fileSeries.isFile()) {
110 // XXX read other queues? 127 // XXX read other queues?
111 allKnown = new ArrayList<PatchRecord>(knownPatchNames.size()); 128 allKnown = new ArrayList<PatchRecord>(knownPatchNames.size());
112 for (String name : knownPatchNames) { 129 for (String name : knownPatchNames) {
113 PatchRecord pr = name2patch.get(name); 130 PatchRecord pr = name2patch.get(name);
114 if (pr == null) { 131 if (pr == null) {
115 pr = new PatchRecord(null, name, Path.create(".hg/patches/" + name)); 132 pr = new PatchRecord(null, name, patchLocation.path(name));
116 } 133 }
117 allKnown.add(pr); 134 allKnown.add(pr);
118 } 135 }
119 } 136 }
120 } catch (HgInvalidFileException ex) { 137 } catch (HgInvalidFileException ex) {
150 public List<PatchRecord> getAppliedPatches() { 167 public List<PatchRecord> getAppliedPatches() {
151 return Collections.unmodifiableList(applied); 168 return Collections.unmodifiableList(applied);
152 } 169 }
153 170
154 /** 171 /**
155 * All of the patches that MQ knows about for this repository 172 * All of the patches in the active queue that MQ knows about for this repository
156 * 173 *
157 * <p>Clients shall call {@link #refresh()} prior to first use 174 * <p>Clients shall call {@link #refresh()} prior to first use
158 * @return collection of records in no particular order, may be empty if there are no patches in the queue 175 * @return collection of records in no particular order, may be empty if there are no patches in the queue
159 */ 176 */
160 public List<PatchRecord> getAllKnownPatches() { 177 public List<PatchRecord> getAllKnownPatches() {