annotate src/org/tmatesoft/hg/repo/HgBranches.java @ 227:0fd10e5016dd

IOException on empty repository with 40-byte dirstate
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 25 May 2011 13:13:36 +0200
parents 8de327242aa0
children 883300108179
rev   line source
220
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.repo;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.util.ArrayList;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.util.Arrays;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.util.Collections;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.HashMap;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.HashSet;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.LinkedList;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.util.List;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.util.Map;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.TreeMap;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import org.tmatesoft.hg.core.Nodeid;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 import org.tmatesoft.hg.util.ProgressSupport;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 /**
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 *
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author TMate Software Ltd.
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 */
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public class HgBranches {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private final HgRepository repo;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 HgBranches(HgRepository hgRepo) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 repo = hgRepo;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 void collect(final ProgressSupport ps) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 ps.start(1 + repo.getChangelog().getRevisionCount() * 2);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 final HgChangelog.ParentWalker pw = repo.getChangelog().new ParentWalker();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 pw.init();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 ps.worked(repo.getChangelog().getRevisionCount());
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 final HashMap<String, Nodeid> branchStart = new HashMap<String, Nodeid>();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 final HashMap<String, Nodeid> branchLastSeen = new HashMap<String, Nodeid>();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 final HashMap<String, List<Nodeid>> branchHeads = new HashMap<String, List<Nodeid>>();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 final HashSet<String> closedBranches = new HashSet<String>();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 HgChangelog.Inspector insp = new HgChangelog.Inspector() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 String branchName = cset.branch();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 if (!branchStart.containsKey(branchName)) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 branchStart.put(branchName, nodeid);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 branchHeads.put(branchName, new LinkedList<Nodeid>());
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 branchLastSeen.remove(branchName);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 if ("1".equals(cset.extras().get("close"))) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 branchHeads.get(branchName).add(nodeid); // XXX what if it still has children?
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 closedBranches.add(branchName);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 } else {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 if (pw.hasChildren(nodeid)) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 // children may be in another branch
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 // and unless we later came across another element from this branch,
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 // we need to record all these as valid heads
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 // XXX what about next case: head1 with children in different branch, and head2 without children
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 // head1 would get lost
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 branchLastSeen.put(branchName, nodeid);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 } else {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 // no more children known for this node, it's (one of the) head of the branch
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 branchHeads.get(branchName).add(nodeid);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 ps.worked(1);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 };
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 repo.getChangelog().all(insp);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 for (String bn : branchLastSeen.keySet()) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 branchHeads.get(bn).add(branchLastSeen.get(bn));
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 for (String bn : branchStart.keySet()) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 Nodeid[] heads = branchHeads.get(bn).toArray(new Nodeid[0]);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 BranchInfo bi = new BranchInfo(bn, branchStart.get(bn), heads, closedBranches.contains(bn));
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 branches.put(bn, bi);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 ps.done();
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 public List<BranchInfo> getAllBranches() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 return new LinkedList<BranchInfo>(branches.values());
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 public BranchInfo getBranch(String name) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 return branches.get(name);
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 public static class BranchInfo {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 private final String name;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 private final List<Nodeid> heads;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 private final boolean closed;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 private final Nodeid start;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 BranchInfo(String branchName, Nodeid first, Nodeid[] branchHeads, boolean isClosed) {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 name = branchName;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 start = first;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 heads = Collections.unmodifiableList(new ArrayList<Nodeid>(Arrays.asList(branchHeads)));
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 closed = isClosed;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 public String getName() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 return name;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 public boolean isClosed() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 return closed;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 public List<Nodeid> getHeads() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 return heads;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 // public Nodeid getTip() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 // }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 public Nodeid getStart() {
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 // first node where branch appears
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 return start;
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 }
8de327242aa0 Basic information about branches
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 }