comparison src/org/tmatesoft/hg/repo/HgBranches.java @ 220:8de327242aa0

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