Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/RepoInitializer.java @ 493:ba36f66c32b4
Refactor to keep knowledge about repository control files and their location in respect to .hg/ in a single place (facilitate future adoption of shared repositories)
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 18 Oct 2012 18:36:13 +0200 |
parents | b3c16d1aede0 |
children | 4e6179bde4fc |
comparison
equal
deleted
inserted
replaced
492:e4eaa23e3442 | 493:ba36f66c32b4 |
---|---|
22 import java.io.FileOutputStream; | 22 import java.io.FileOutputStream; |
23 import java.io.IOException; | 23 import java.io.IOException; |
24 import java.nio.charset.Charset; | 24 import java.nio.charset.Charset; |
25 | 25 |
26 import org.tmatesoft.hg.core.SessionContext; | 26 import org.tmatesoft.hg.core.SessionContext; |
27 import org.tmatesoft.hg.repo.HgInvalidControlFileException; | |
27 import org.tmatesoft.hg.util.PathRewrite; | 28 import org.tmatesoft.hg.util.PathRewrite; |
28 | 29 |
29 /** | 30 /** |
30 * Responsible of `requires` processing both on repo read and repo write | 31 * Responsible of `requires` processing both on repo read and repo write |
31 * XXX needs better name, perhaps | 32 * XXX needs better name, perhaps |
37 private int requiresFlags; | 38 private int requiresFlags; |
38 | 39 |
39 public RepoInitializer() { | 40 public RepoInitializer() { |
40 } | 41 } |
41 | 42 |
43 public RepoInitializer initRequiresFromFile(File repoDir) throws HgInvalidControlFileException { | |
44 File requiresFile = new File(repoDir, "requires"); // not #getFileFromRepoDir() just in case it gets modified later | |
45 try { | |
46 int flags = new RequiresFile().parse(requiresFile); | |
47 return setRequires(flags); | |
48 } catch (IOException ex) { | |
49 throw new HgInvalidControlFileException("Parse failed", ex, requiresFile); | |
50 } | |
51 } | |
52 | |
42 public RepoInitializer setRequires(int flags) { | 53 public RepoInitializer setRequires(int flags) { |
43 requiresFlags = flags; | 54 requiresFlags = flags; |
44 return this; | 55 return this; |
45 } | 56 } |
46 | 57 |
47 public int getRequires() { | 58 public int getRequires() { |
48 return requiresFlags; | 59 return requiresFlags; |
49 } | 60 } |
50 | 61 |
51 public void initEmptyRepository(File hgDir) throws IOException { | 62 public void initEmptyRepository(File repoDir) throws IOException { |
52 hgDir.mkdir(); | 63 repoDir.mkdirs(); |
53 FileOutputStream requiresFile = new FileOutputStream(new File(hgDir, "requires")); | 64 FileOutputStream requiresFile = new FileOutputStream(new File(repoDir, "requires")); |
54 StringBuilder sb = new StringBuilder(40); | 65 StringBuilder sb = new StringBuilder(40); |
55 sb.append("revlogv1\n"); | 66 sb.append("revlogv1\n"); |
56 if ((requiresFlags & STORE) != 0) { | 67 if ((requiresFlags & STORE) != 0) { |
57 sb.append("store\n"); | 68 sb.append("store\n"); |
58 } | 69 } |
62 if ((requiresFlags & DOTENCODE) != 0) { | 73 if ((requiresFlags & DOTENCODE) != 0) { |
63 sb.append("dotencode\n"); | 74 sb.append("dotencode\n"); |
64 } | 75 } |
65 requiresFile.write(sb.toString().getBytes()); | 76 requiresFile.write(sb.toString().getBytes()); |
66 requiresFile.close(); | 77 requiresFile.close(); |
67 new File(hgDir, "store").mkdir(); // with that, hg verify says ok. | 78 new File(repoDir, "store").mkdir(); // with that, hg verify says ok. |
68 } | 79 } |
69 | 80 |
70 public PathRewrite buildDataFilesHelper(SessionContext ctx) { | 81 public PathRewrite buildDataFilesHelper(SessionContext ctx) { |
71 Charset cs = Internals.getFileEncoding(ctx); | 82 Charset cs = Internals.getFileEncoding(ctx); |
72 // StoragePathHelper needs fine-grained control over char encoding, hence doesn't use EncodingHelper | 83 // StoragePathHelper needs fine-grained control over char encoding, hence doesn't use EncodingHelper |
73 return new StoragePathHelper((requiresFlags & STORE) != 0, (requiresFlags & FNCACHE) != 0, (requiresFlags & DOTENCODE) != 0, cs); | 84 return new StoragePathHelper((requiresFlags & STORE) != 0, (requiresFlags & FNCACHE) != 0, (requiresFlags & DOTENCODE) != 0, cs); |
74 } | 85 } |
86 | |
87 public PathRewrite buildStoreFilesHelper() { | |
88 if ((requiresFlags & STORE) != 0) { | |
89 return new PathRewrite() { | |
90 public CharSequence rewrite(CharSequence path) { | |
91 return "store/" + path; | |
92 } | |
93 }; | |
94 } else { | |
95 return new PathRewrite.Empty(); | |
96 } | |
97 } | |
75 } | 98 } |