Mercurial > hg > Members > kono > jpf-core
comparison src/main/gov/nasa/jpf/util/RepositoryEntry.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
1 /* | |
2 * Copyright (C) 2014, United States Government, as represented by the | |
3 * Administrator of the National Aeronautics and Space Administration. | |
4 * All rights reserved. | |
5 * | |
6 * The Java Pathfinder core (jpf-core) platform is licensed under the | |
7 * Apache License, Version 2.0 (the "License"); you may not use this file except | |
8 * in compliance with the License. You may obtain a copy of the License at | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0. | |
11 * | |
12 * Unless required by applicable law or agreed to in writing, software | |
13 * distributed under the License is distributed on an "AS IS" BASIS, | |
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 * See the License for the specific language governing permissions and | |
16 * limitations under the License. | |
17 */ | |
18 | |
19 package gov.nasa.jpf.util; | |
20 | |
21 import java.io.BufferedReader; | |
22 import java.io.File; | |
23 import java.io.FileReader; | |
24 import java.util.HashMap; | |
25 import java.util.regex.Matcher; | |
26 import java.util.regex.Pattern; | |
27 | |
28 /** | |
29 * simple generic structure to hold repository info for source files | |
30 * | |
31 * <2do> extend this to find out what the status of the repository is, i.e. if | |
32 * there are any local modifications, update revision etc. | |
33 */ | |
34 public class RepositoryEntry { | |
35 | |
36 static HashMap<String,RepositoryEntry> dict = new HashMap<String,RepositoryEntry>(); | |
37 | |
38 String fileName; | |
39 String repositoryType; | |
40 String repository; | |
41 String revision; | |
42 | |
43 static RepositoryEntryFactory searchers[] = { | |
44 new SvnRepositoryEntryFactory(), | |
45 new HgRepositoryEntryFactory(), | |
46 new GitRepositoryEntryFactory() | |
47 }; | |
48 | |
49 public static RepositoryEntry getRepositoryEntry (String fullFileName) { | |
50 RepositoryEntry e = dict.get(fullFileName); | |
51 | |
52 if (e == null) { | |
53 for (RepositoryEntryFactory factory : searchers) { | |
54 if ((e = factory.getRepositoryEntry(fullFileName)) != null) | |
55 break; | |
56 } | |
57 } | |
58 | |
59 dict.put(fullFileName, e); // no need to look this up more than once | |
60 return e; | |
61 } | |
62 | |
63 public RepositoryEntry (String fileName, String repositoryType, String repository, String revision) { | |
64 this.fileName = fileName; | |
65 this.repositoryType = repositoryType; | |
66 this.repository = repository; | |
67 this.revision = revision; | |
68 } | |
69 | |
70 public String getFileName() { | |
71 return fileName; | |
72 } | |
73 | |
74 public String getRepositoryType() { | |
75 return repositoryType; | |
76 } | |
77 | |
78 public String getRepository() { | |
79 return repository; | |
80 } | |
81 | |
82 public String getRevision() { | |
83 return revision; | |
84 } | |
85 | |
86 } | |
87 | |
88 interface RepositoryEntryFactory { | |
89 RepositoryEntry getRepositoryEntry(String fullFileName); | |
90 } | |
91 | |
92 class SvnRepositoryEntryFactory implements RepositoryEntryFactory { | |
93 | |
94 /* | |
95 * <2do> doesn't work on Windows, where the .svn/entries is apparently | |
96 * not stored as an XML file | |
97 */ | |
98 @Override | |
99 public RepositoryEntry getRepositoryEntry(String fullFileName) { | |
100 File f = new File(fullFileName); | |
101 String fname = f.getName(); | |
102 String dName = f.getParent(); | |
103 | |
104 File fEntries = new File(dName + File.separatorChar + ".svn" + File.separatorChar + "entries"); | |
105 if (fEntries.exists()) { | |
106 String repository = "?"; | |
107 String revision = "?"; | |
108 | |
109 Pattern pName = Pattern.compile(" *name=\"([a-zA-Z0-9.]+)\""); | |
110 Pattern pRep = Pattern.compile(" *url=\"([a-zA-Z0-9.:/\\-]+)\""); | |
111 Pattern pRev = Pattern.compile(" *committed-rev=\"([0-9]+)\""); | |
112 try { | |
113 BufferedReader r = new BufferedReader(new FileReader(fEntries)); | |
114 for (String line=r.readLine(); line != null; line = r.readLine()) { | |
115 Matcher mRep = pRep.matcher(line); | |
116 if (mRep.matches()) { | |
117 repository = mRep.group(1); | |
118 } else { | |
119 Matcher mRev = pRev.matcher(line); | |
120 if (mRev.matches()) { | |
121 revision = mRev.group(1); | |
122 } else { | |
123 Matcher mName = pName.matcher(line); | |
124 if (mName.matches() && mName.group(1).equals(fname)) { | |
125 // Ok, got everything | |
126 return new RepositoryEntry(fname, "svn", repository, revision); | |
127 } | |
128 } | |
129 } | |
130 } | |
131 } catch (Throwable t) {} | |
132 } | |
133 | |
134 return null; | |
135 } | |
136 } | |
137 | |
138 class HgRepositoryEntryFactory implements RepositoryEntryFactory { | |
139 | |
140 @Override | |
141 public RepositoryEntry getRepositoryEntry(String fullFileName) { | |
142 File file = new File(fullFileName); | |
143 | |
144 if (!file.exists()) | |
145 return null; | |
146 | |
147 File currentDir = file.getParentFile(); | |
148 | |
149 searchForHg: | |
150 while (currentDir != null) { | |
151 for (String childName : currentDir.list()) | |
152 if (childName.equals(".hg")) | |
153 break searchForHg; | |
154 | |
155 currentDir = currentDir.getParentFile(); | |
156 } | |
157 | |
158 if (currentDir != null) { | |
159 try { | |
160 File hgrcFile = new File(currentDir, ".hg/hgrc"); | |
161 | |
162 String repoURL = ""; | |
163 BufferedReader r = new BufferedReader(new FileReader(hgrcFile)); | |
164 for (String line=r.readLine(); line != null; line = r.readLine()) { | |
165 String keyVal[] = line.split("="); | |
166 if (keyVal[0].trim().equals("default")) { | |
167 repoURL = keyVal[1].trim(); | |
168 break; | |
169 } | |
170 | |
171 } | |
172 | |
173 File branchHeads = new File(currentDir, ".hg/branchheads.cache"); | |
174 r = new BufferedReader(new FileReader(branchHeads)); | |
175 String revision = r.readLine().split(" ")[1]; | |
176 | |
177 return new RepositoryEntry(fullFileName, "hg", repoURL, revision); | |
178 } | |
179 catch (Exception ex) { | |
180 return null; | |
181 } | |
182 } | |
183 | |
184 return null; | |
185 } | |
186 | |
187 } | |
188 | |
189 class GitRepositoryEntryFactory implements RepositoryEntryFactory { | |
190 | |
191 @Override | |
192 public RepositoryEntry getRepositoryEntry(String fullFileName) { | |
193 File file = new File(fullFileName); | |
194 | |
195 if (!file.exists()) | |
196 return null; | |
197 | |
198 File currentDir = file.getParentFile(); | |
199 | |
200 searchForHg: | |
201 while (currentDir != null) { | |
202 for (String childName : currentDir.list()) { | |
203 if (childName.equals(".git")) { | |
204 break searchForHg; | |
205 } | |
206 } | |
207 | |
208 currentDir = currentDir.getParentFile(); | |
209 } | |
210 | |
211 if (currentDir != null) { | |
212 try { | |
213 File hgrcFile = new File(currentDir, ".git/config"); | |
214 | |
215 String repoURL = ""; | |
216 BufferedReader r = new BufferedReader(new FileReader(hgrcFile)); | |
217 for (String line = r.readLine(); line != null; line = r.readLine()) { | |
218 String keyVal[] = line.split("="); | |
219 if (keyVal[0].trim().equals("url")) { | |
220 repoURL = keyVal[1].trim(); | |
221 break; | |
222 } | |
223 | |
224 } | |
225 | |
226 // git doesn't has revision numbers so we will read last revision's hash instead | |
227 File gitHeadHash = new File(currentDir, ".git/refs/heads/master"); | |
228 r = new BufferedReader(new FileReader(gitHeadHash)); | |
229 String revision = r.readLine(); | |
230 | |
231 return new RepositoryEntry(fullFileName, "git", repoURL, revision); | |
232 | |
233 } catch (Exception ex) { | |
234 return null; | |
235 } | |
236 } | |
237 | |
238 return null; | |
239 } | |
240 } |