Mercurial > hg4j
comparison 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 |
comparison
equal
deleted
inserted
replaced
238:4817d4ccc50e | 239:df9d2854d3d6 |
---|---|
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.internal; | |
18 | |
19 import java.io.BufferedReader; | |
20 import java.io.File; | |
21 import java.io.FileReader; | |
22 import java.io.IOException; | |
23 import java.util.Arrays; | |
24 import java.util.Collections; | |
25 import java.util.HashMap; | |
26 import java.util.LinkedList; | |
27 import java.util.List; | |
28 import java.util.Map; | |
29 | |
30 import org.tmatesoft.hg.repo.HgRepository; | |
31 import org.tmatesoft.hg.repo.HgSubrepoLocation; | |
32 | |
33 /** | |
34 * | |
35 * @author Artem Tikhomirov | |
36 * @author TMate Software Ltd. | |
37 */ | |
38 public class SubrepoManager /* XXX RepoChangeNotifier, RepoChangeListener */{ | |
39 | |
40 private final HgRepository repo; | |
41 private List<HgSubrepoLocation> subRepos; | |
42 | |
43 public SubrepoManager(HgRepository hgRepo) { | |
44 assert hgRepo != null; | |
45 repo = hgRepo; | |
46 } | |
47 | |
48 private List<HgSubrepoLocation> readActualState() { | |
49 File hgsubFile = new File(repo.getWorkingDir(), ".hgsub"); | |
50 if (!hgsubFile.canRead()) { | |
51 return Collections.emptyList(); | |
52 } | |
53 try { | |
54 Map<String, String> state; // path -> revision | |
55 File hgstateFile = new File(repo.getWorkingDir(), ".hgsubstate"); | |
56 if (hgstateFile.canRead()) { | |
57 state = readState(new BufferedReader(new FileReader(hgstateFile))); | |
58 } else { | |
59 state = Collections.emptyMap(); | |
60 } | |
61 BufferedReader br = new BufferedReader(new FileReader(hgsubFile)); | |
62 return readConfig(br, state); | |
63 } catch (IOException ex) { | |
64 ex.printStackTrace(); // XXX log. Generally, shall not happen | |
65 } | |
66 return Collections.emptyList(); | |
67 } | |
68 | |
69 private List<HgSubrepoLocation> readConfig(BufferedReader br, Map<String, String> substate) throws IOException { | |
70 try { | |
71 String line; | |
72 LinkedList<HgSubrepoLocation> res = new LinkedList<HgSubrepoLocation>(); | |
73 while ((line = br.readLine()) != null) { | |
74 int sep = line.indexOf('='); | |
75 if (sep == -1) { | |
76 continue; | |
77 } | |
78 // since both key and value are referenced from HgSubrepoLocation, doesn't make sense | |
79 // to have separate String instances (new String(line.substring())) | |
80 String key = line.substring(0, sep).trim(); | |
81 String value = line.substring(sep + 1).trim(); | |
82 if (value.length() == 0) { | |
83 // XXX log bad line? | |
84 continue; | |
85 } | |
86 HgSubrepoLocation.Kind kind = HgSubrepoLocation.Kind.Hg; | |
87 int kindEnd = value.indexOf(']', 1); | |
88 if (value.charAt(0) == '[' && kindEnd != -1) { | |
89 String kindStr = value.substring(1, kindEnd); | |
90 value = value.substring(kindEnd + 1); | |
91 if ("svn".equals(kindStr)) { | |
92 kind = HgSubrepoLocation.Kind.SVN; | |
93 } else if ("git".equals(kindStr)) { | |
94 kind = HgSubrepoLocation.Kind.Git; | |
95 } | |
96 } | |
97 // TODO respect paths mappings in config file | |
98 HgSubrepoLocation loc = new HgSubrepoLocation(repo, key, value, kind, substate.get(key)); | |
99 res.add(loc); | |
100 } | |
101 return Arrays.asList(res.toArray(new HgSubrepoLocation[res.size()])); | |
102 } finally { | |
103 br.close(); | |
104 } | |
105 } | |
106 | |
107 private Map<String, String> readState(BufferedReader br) throws IOException { | |
108 HashMap<String, String> rv = new HashMap<String, String>(); | |
109 try { | |
110 String line; | |
111 while ((line = br.readLine()) != null) { | |
112 int sep = line.trim().indexOf(' '); | |
113 if (sep != -1) { | |
114 rv.put(line.substring(sep+1).trim(), line.substring(0, sep).trim()); | |
115 } | |
116 } | |
117 } finally { | |
118 br.close(); | |
119 } | |
120 return rv; | |
121 } | |
122 | |
123 public List<HgSubrepoLocation> all(/*int revision, or TIP|WC*/) { | |
124 if (subRepos == null) { | |
125 subRepos = readActualState(); | |
126 } | |
127 return subRepos; | |
128 } | |
129 } |