annotate src/org/tmatesoft/hg/internal/SubrepoManager.java @ 239:df9d2854d3d6

Initial access to subrepositories
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 Jun 2011 04:23:51 +0200
parents
children 981f9f50bb6c
rev   line source
239
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.BufferedReader;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.File;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.FileReader;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.IOException;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.Arrays;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.Collections;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.util.HashMap;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.util.LinkedList;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.List;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import java.util.Map;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import org.tmatesoft.hg.repo.HgRepository;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 import org.tmatesoft.hg.repo.HgSubrepoLocation;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 /**
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 *
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author TMate Software Ltd.
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 */
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public class SubrepoManager /* XXX RepoChangeNotifier, RepoChangeListener */{
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private final HgRepository repo;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private List<HgSubrepoLocation> subRepos;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 public SubrepoManager(HgRepository hgRepo) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 assert hgRepo != null;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 repo = hgRepo;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 private List<HgSubrepoLocation> readActualState() {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 File hgsubFile = new File(repo.getWorkingDir(), ".hgsub");
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 if (!hgsubFile.canRead()) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 return Collections.emptyList();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 try {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 Map<String, String> state; // path -> revision
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 File hgstateFile = new File(repo.getWorkingDir(), ".hgsubstate");
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 if (hgstateFile.canRead()) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 state = readState(new BufferedReader(new FileReader(hgstateFile)));
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 } else {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 state = Collections.emptyMap();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 BufferedReader br = new BufferedReader(new FileReader(hgsubFile));
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 return readConfig(br, state);
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 } catch (IOException ex) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 ex.printStackTrace(); // XXX log. Generally, shall not happen
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 return Collections.emptyList();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 private List<HgSubrepoLocation> readConfig(BufferedReader br, Map<String, String> substate) throws IOException {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 try {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 String line;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 LinkedList<HgSubrepoLocation> res = new LinkedList<HgSubrepoLocation>();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 while ((line = br.readLine()) != null) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 int sep = line.indexOf('=');
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 if (sep == -1) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 continue;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 // since both key and value are referenced from HgSubrepoLocation, doesn't make sense
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 // to have separate String instances (new String(line.substring()))
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 String key = line.substring(0, sep).trim();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 String value = line.substring(sep + 1).trim();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 if (value.length() == 0) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 // XXX log bad line?
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 continue;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 HgSubrepoLocation.Kind kind = HgSubrepoLocation.Kind.Hg;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 int kindEnd = value.indexOf(']', 1);
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 if (value.charAt(0) == '[' && kindEnd != -1) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 String kindStr = value.substring(1, kindEnd);
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 value = value.substring(kindEnd + 1);
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 if ("svn".equals(kindStr)) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 kind = HgSubrepoLocation.Kind.SVN;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 } else if ("git".equals(kindStr)) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 kind = HgSubrepoLocation.Kind.Git;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 // TODO respect paths mappings in config file
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 HgSubrepoLocation loc = new HgSubrepoLocation(repo, key, value, kind, substate.get(key));
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 res.add(loc);
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 return Arrays.asList(res.toArray(new HgSubrepoLocation[res.size()]));
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 } finally {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 br.close();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 private Map<String, String> readState(BufferedReader br) throws IOException {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 HashMap<String, String> rv = new HashMap<String, String>();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 try {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 String line;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 while ((line = br.readLine()) != null) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 int sep = line.trim().indexOf(' ');
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 if (sep != -1) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 rv.put(line.substring(sep+1).trim(), line.substring(0, sep).trim());
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 } finally {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 br.close();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 return rv;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 public List<HgSubrepoLocation> all(/*int revision, or TIP|WC*/) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 if (subRepos == null) {
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 subRepos = readActualState();
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 return subRepos;
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 }
df9d2854d3d6 Initial access to subrepositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 }