2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
19 package gov.nasa.jpf.util;
21 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.util.HashMap;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
29 * simple generic structure to hold repository info for source files
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.
34 public class RepositoryEntry {
36 static HashMap<String,RepositoryEntry> dict = new HashMap<String,RepositoryEntry>();
39 String repositoryType;
43 static RepositoryEntryFactory searchers[] = {
44 new SvnRepositoryEntryFactory(),
45 new HgRepositoryEntryFactory(),
46 new GitRepositoryEntryFactory()
49 public static RepositoryEntry getRepositoryEntry (String fullFileName) {
50 RepositoryEntry e = dict.get(fullFileName);
53 for (RepositoryEntryFactory factory : searchers) {
54 if ((e = factory.getRepositoryEntry(fullFileName)) != null)
59 dict.put(fullFileName, e); // no need to look this up more than once
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;
70 public String getFileName() {
74 public String getRepositoryType() {
75 return repositoryType;
78 public String getRepository() {
82 public String getRevision() {
88 interface RepositoryEntryFactory {
89 RepositoryEntry getRepositoryEntry(String fullFileName);
92 class SvnRepositoryEntryFactory implements RepositoryEntryFactory {
95 * <2do> doesn't work on Windows, where the .svn/entries is apparently
96 * not stored as an XML file
99 public RepositoryEntry getRepositoryEntry(String fullFileName) {
100 File f = new File(fullFileName);
101 String fname = f.getName();
102 String dName = f.getParent();
104 File fEntries = new File(dName + File.separatorChar + ".svn" + File.separatorChar + "entries");
105 if (fEntries.exists()) {
106 String repository = "?";
107 String revision = "?";
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]+)\"");
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);
119 Matcher mRev = pRev.matcher(line);
120 if (mRev.matches()) {
121 revision = mRev.group(1);
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);
131 } catch (Throwable t) {}
138 class HgRepositoryEntryFactory implements RepositoryEntryFactory {
141 public RepositoryEntry getRepositoryEntry(String fullFileName) {
142 File file = new File(fullFileName);
147 File currentDir = file.getParentFile();
150 while (currentDir != null) {
151 for (String childName : currentDir.list())
152 if (childName.equals(".hg"))
155 currentDir = currentDir.getParentFile();
158 if (currentDir != null) {
160 File hgrcFile = new File(currentDir, ".hg/hgrc");
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();
173 File branchHeads = new File(currentDir, ".hg/branchheads.cache");
174 r = new BufferedReader(new FileReader(branchHeads));
175 String revision = r.readLine().split(" ")[1];
177 return new RepositoryEntry(fullFileName, "hg", repoURL, revision);
179 catch (Exception ex) {
189 class GitRepositoryEntryFactory implements RepositoryEntryFactory {
192 public RepositoryEntry getRepositoryEntry(String fullFileName) {
193 File file = new File(fullFileName);
198 File currentDir = file.getParentFile();
201 while (currentDir != null) {
202 for (String childName : currentDir.list()) {
203 if (childName.equals(".git")) {
208 currentDir = currentDir.getParentFile();
211 if (currentDir != null) {
213 File hgrcFile = new File(currentDir, ".git/config");
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();
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();
231 return new RepositoryEntry(fullFileName, "git", repoURL, revision);
233 } catch (Exception ex) {