annotate src/org/tmatesoft/hg/repo/ext/MqManager.java @ 475:0e34b8f3946a

Tests for MqManager
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 12 Jul 2012 16:57:40 +0200
parents 7bcfbc255f48
children f3fab7a20841
rev   line source
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012 TMate Software Ltd
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.repo.ext;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
471
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
20
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.BufferedReader;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.File;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.FileReader;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.io.IOException;
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
25 import java.util.ArrayList;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
26 import java.util.Collection;
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.Collections;
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
28 import java.util.HashMap;
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import java.util.LinkedList;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import java.util.List;
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
31 import java.util.Map;
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 import org.tmatesoft.hg.core.Nodeid;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 import org.tmatesoft.hg.repo.HgInternals;
471
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
35 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
36 import org.tmatesoft.hg.repo.HgInvalidFileException;
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 import org.tmatesoft.hg.repo.HgRepository;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 import org.tmatesoft.hg.util.LogFacility;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 import org.tmatesoft.hg.util.Path;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 /**
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 * Mercurial Queues Support.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 * Access to MqExtension functionality.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 * @author Artem Tikhomirov
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 * @author TMate Software Ltd.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 public class MqManager {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
50 private static final String PATCHES_DIR = "patches";
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
51
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 private final HgRepository repo;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 private List<PatchRecord> applied = Collections.emptyList();
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 private List<PatchRecord> allKnown = Collections.emptyList();
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
55 private List<String> queueNames = Collections.emptyList();
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
56 private String activeQueue = PATCHES_DIR;
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 public MqManager(HgRepository hgRepo) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 repo = hgRepo;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 /**
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 * Updates manager with up-to-date state of the mercurial queues.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 public void refresh() throws HgInvalidControlFileException {
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
66 applied = allKnown = Collections.emptyList();
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
67 queueNames = Collections.emptyList();
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 File repoDir = HgInternals.getRepositoryDir(repo);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 final LogFacility log = HgInternals.getContext(repo).getLog();
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 try {
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
71 File queues = new File(repoDir, "patches.queues");
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
72 if (queues.isFile()) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
73 LineReader lr = new LineReader(queues, log).trimLines(true).skipEmpty(true);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
74 lr.read(new SimpleLineCollector(), queueNames = new LinkedList<String>());
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
75 }
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
76 final String queueLocation; // path under .hg to patch queue information (status, series and diff files)
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
77 File activeQueueFile = new File(repoDir, "patches.queue");
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
78 // file is there only if it's not default queue ('patches') that is active
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
79 if (activeQueueFile.isFile()) {
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
80 ArrayList<String> contents = new ArrayList<String>();
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
81 new LineReader(activeQueueFile, log).read(new SimpleLineCollector(), contents);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
82 if (contents.isEmpty()) {
471
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
83 log.dump(getClass(), Warn, "File %s with active queue name is empty", activeQueueFile.getName());
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
84 activeQueue = PATCHES_DIR;
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
85 queueLocation = PATCHES_DIR + '/';
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
86 } else {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
87 activeQueue = contents.get(0);
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
88 queueLocation = PATCHES_DIR + '-' + activeQueue + '/';
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
89 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
90 } else {
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
91 activeQueue = PATCHES_DIR;
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
92 queueLocation = PATCHES_DIR + '/';
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
93 }
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
94 final Path.Source patchLocation = new Path.Source() {
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
95
471
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
96 public Path path(CharSequence p) {
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
97 StringBuilder sb = new StringBuilder(64);
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
98 sb.append(".hg/");
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
99 sb.append(queueLocation);
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
100 sb.append(p);
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
101 return Path.create(sb);
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
102 }
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
103 };
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
104 final File fileStatus = new File(repoDir, queueLocation + "status");
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
105 final File fileSeries = new File(repoDir, queueLocation + "series");
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 if (fileStatus.isFile()) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 new LineReader(fileStatus, log).read(new LineConsumer<List<PatchRecord>>() {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 public boolean consume(String line, List<PatchRecord> result) throws IOException {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 int sep = line.indexOf(':');
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 if (sep == -1) {
471
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
112 log.dump(MqManager.class, Warn, "Bad line in %s:%s", fileStatus.getPath(), line);
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 return true;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 Nodeid nid = Nodeid.fromAscii(line.substring(0, sep));
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 String name = new String(line.substring(sep+1));
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
117 result.add(new PatchRecord(nid, name, patchLocation.path(name)));
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 return true;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 }, applied = new LinkedList<PatchRecord>());
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 if (fileSeries.isFile()) {
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
123 final Map<String,PatchRecord> name2patch = new HashMap<String, PatchRecord>();
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
124 for (PatchRecord pr : applied) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
125 name2patch.put(pr.getName(), pr);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
126 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
127 LinkedList<String> knownPatchNames = new LinkedList<String>();
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
128 new LineReader(fileSeries, log).read(new SimpleLineCollector(), knownPatchNames);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
129 // XXX read other queues?
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
130 allKnown = new ArrayList<PatchRecord>(knownPatchNames.size());
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
131 for (String name : knownPatchNames) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
132 PatchRecord pr = name2patch.get(name);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
133 if (pr == null) {
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
134 pr = new PatchRecord(null, name, patchLocation.path(name));
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 }
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
136 allKnown.add(pr);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
137 }
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 } catch (HgInvalidFileException ex) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 HgInvalidControlFileException th = new HgInvalidControlFileException(ex.getMessage(), ex.getCause(), ex.getFile());
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 th.setStackTrace(ex.getStackTrace());
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 throw th;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 }
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
145
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
146 static class SimpleLineCollector implements LineConsumer<Collection<String>> {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
147
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
148 public boolean consume(String line, Collection<String> result) throws IOException {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
149 result.add(line);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
150 return true;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
151 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
152 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
153
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
154 /**
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
155 * Number of patches not yet applied
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
156 * @return positive value when there are
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
157 */
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
158 public int getQueueSize() {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
159 return getAllKnownPatches().size() - getAppliedPatches().size();
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
160 }
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 /**
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 * Subset of the patches from the queue that were already applied to the repository
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
164 * <p>Analog of 'hg qapplied'
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 * <p>Clients shall call {@link #refresh()} prior to first use
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 * @return collection of records in no particular order, may be empty if none applied
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 public List<PatchRecord> getAppliedPatches() {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
170 return Collections.unmodifiableList(applied);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 /**
465
2078692eeb58 MqManager: read status/series of the active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 464
diff changeset
174 * All of the patches in the active queue that MQ knows about for this repository
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
175 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176 * <p>Clients shall call {@link #refresh()} prior to first use
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 * @return collection of records in no particular order, may be empty if there are no patches in the queue
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 public List<PatchRecord> getAllKnownPatches() {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180 return Collections.unmodifiableList(allKnown);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
183 /**
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
184 * Name of the patch queue <code>hg qqueue --active</code> which is active now.
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
185 * @return patch queue name
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
186 */
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
187 public String getActiveQueueName() {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
188 return activeQueue;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
189 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
190
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
191 /**
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
192 * Patch queues known in the repository, <code>hg qqueue -l</code> analog.
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
193 * There's at least one patch queue (default one names 'patches'). Only one patch queue at a time is active.
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
194 *
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
195 * @return names of patch queues
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
196 */
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
197 public List<String> getQueueNames() {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
198 return Collections.unmodifiableList(queueNames);
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
199 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
200
475
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
201 public final class PatchRecord {
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
202 private final Nodeid nodeid;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
203 private final String name;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
204 private final Path location;
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
205
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
206 // hashCode/equals might be useful if cons becomes public
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 PatchRecord(Nodeid revision, String name, Path diffLocation) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 nodeid = revision;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
210 this.name = name;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
211 this.location = diffLocation;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
212 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
213
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
214 /**
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
215 * Identifies changeset of the patch that has been applied to the repository
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
216 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
217 * @return changeset revision or <code>null</code> if this patch is not yet applied
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
218 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
219 public Nodeid getRevision() {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
220 return nodeid;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
221 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
222
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
223 /**
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
224 * Identifies patch, either based on a user-supplied name (<code>hg qnew <i>patch-name</i></code>) or
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225 * an automatically generated name (like <code><i>revisionIndex</i>.diff</code> for imported changesets).
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
226 * Clients shall not rely on this naming scheme, though.
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
227 *
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
228 * @return never <code>null</code>
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
229 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
230 public String getName() {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
231 return name;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
232 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
233
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
234 /**
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
235 * Location of diff file with the patch, relative to repository root
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
236 * @return path to the patch, never <code>null</code>
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
237 */
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
238 public Path getPatchLocation() {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
239 return location;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
240 }
475
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
241
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
242 @Override
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
243 public String toString() {
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
244 String fmt = "mq.PatchRecord[name:%s; %spath:%s]";
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
245 String ni = nodeid != null ? String.format("applied as: %s; ", nodeid.shortNotation()) : "";
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
246 return String.format(fmt, name, ni, location);
0e34b8f3946a Tests for MqManager
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 471
diff changeset
247 }
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
248 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
249
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
250 // TODO refine API and extract into separate classes
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
251
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
252 interface LineConsumer<T> {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
253 // boolean begin(File f, T paramObj) throws IOException;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
254 boolean consume(String line, T paramObj) throws IOException;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
255 // boolean end(File f, T paramObj) throws IOException;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
257
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
258 class LineReader {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
259
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
260 private final File file;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
261 private final LogFacility log;
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
262 private boolean trimLines = true;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
263 private boolean skipEmpty = true;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
264 private String ignoreThatStars = null;
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
265
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
266 LineReader(File f, LogFacility logFacility) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
267 file = f;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
268 log = logFacility;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
269 }
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
270
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
271 /**
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
272 * default: <code>true</code>
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
273 * <code>false</code> to return line as is
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
274 */
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
275 LineReader trimLines(boolean trim) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
276 trimLines = trim;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
277 return this;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
278 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
279
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
280 /**
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
281 * default: <code>true</code>
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
282 * <code>false</code> to pass empty lines to consumer
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
283 */
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
284 LineReader skipEmpty(boolean skip) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
285 skipEmpty = skip;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
286 return this;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
287 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
288
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
289 /**
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
290 * default: doesn't skip any line.
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
291 * set e.g. to "#" or "//" to skip lines that start with such prefix
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
292 */
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
293 LineReader ignoreLineComments(String lineStart) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
294 ignoreThatStars = lineStart;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
295 return this;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
296 }
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
297
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
298 <T> void read(LineConsumer<T> consumer, T paramObj) throws HgInvalidFileException {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
299 BufferedReader statusFileReader = null;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
300 try {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
301 // consumer.begin(file, paramObj);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
302 statusFileReader = new BufferedReader(new FileReader(file));
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
303 String line;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
304 boolean ok = true;
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
305 while (ok && (line = statusFileReader.readLine()) != null) {
464
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
306 if (trimLines) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
307 line = line.trim();
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
308 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
309 if (ignoreThatStars != null && line.startsWith(ignoreThatStars)) {
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
310 continue;
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
311 }
1a3c18d57a8e MqManager evolution: same PatchRecord instances, list patch queues, detect active queue
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 463
diff changeset
312 if (!skipEmpty || line.length() > 0) {
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
313 ok = consumer.consume(line, paramObj);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
314 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
315 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
316 } catch (IOException ex) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
317 throw new HgInvalidFileException(ex.getMessage(), ex, file);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
318 } finally {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
319 try {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
320 statusFileReader.close();
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
321 } catch (IOException ex) {
471
7bcfbc255f48 Merge changes from smartgit3 branch into 1.1 stream
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 465
diff changeset
322 log.dump(MqManager.class, Warn, ex, null);
463
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
323 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
324 // try {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
325 // consumer.end(file, paramObj);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
326 // } catch (IOException ex) {
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
327 // log.warn(MqManager.class, ex, null);
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
328 // }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
329 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
330 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
331 }
a0507a9f3da0 Initial support for MqExtension
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
332 }