1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.lib;
14
15 import static java.nio.charset.StandardCharsets.UTF_8;
16
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.text.MessageFormat;
20
21 import org.eclipse.jgit.errors.ConfigInvalidException;
22 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
23 import org.eclipse.jgit.errors.MissingObjectException;
24 import org.eclipse.jgit.internal.JGitText;
25 import org.eclipse.jgit.revwalk.RevCommit;
26 import org.eclipse.jgit.revwalk.RevTree;
27 import org.eclipse.jgit.revwalk.RevWalk;
28 import org.eclipse.jgit.treewalk.TreeWalk;
29 import org.eclipse.jgit.util.RawParseUtils;
30
31
32
33
34
35
36
37 public class BlobBasedConfig extends Config {
38
39
40
41
42
43
44
45
46
47
48 public BlobBasedConfig(Config base, byte[] blob)
49 throws ConfigInvalidException {
50 super(base);
51 final String decoded;
52 if (isUtf8(blob)) {
53 decoded = RawParseUtils.decode(UTF_8, blob, 3, blob.length);
54 } else {
55 decoded = RawParseUtils.decode(blob);
56 }
57 fromText(decoded);
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public BlobBasedConfig(Config base, Repository db, AnyObjectId objectId)
75 throws IOException, ConfigInvalidException {
76 this(base, read(db, objectId));
77 }
78
79 private static byte[] read(Repository db, AnyObjectId blobId)
80 throws MissingObjectException, IncorrectObjectTypeException,
81 IOException {
82 try (ObjectReader or = db.newObjectReader()) {
83 return read(or, blobId);
84 }
85 }
86
87 private static byte[] read(ObjectReader or, AnyObjectId blobId)
88 throws MissingObjectException, IncorrectObjectTypeException,
89 IOException {
90 ObjectLoader loader = or.open(blobId, Constants.OBJ_BLOB);
91 return loader.getCachedBytes(Integer.MAX_VALUE);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public BlobBasedConfig(Config base, Repository db, AnyObjectId treeish,
113 String path) throws FileNotFoundException, IOException,
114 ConfigInvalidException {
115 this(base, read(db, treeish, path));
116 }
117
118 private static byte[] read(Repository db, AnyObjectId treeish, String path)
119 throws MissingObjectException, IncorrectObjectTypeException,
120 IOException {
121 try (ObjectReader or = db.newObjectReader()) {
122 TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish));
123 if (tree == null)
124 throw new FileNotFoundException(MessageFormat.format(JGitText
125 .get().entryNotFoundByPath, path));
126 return read(or, tree.getObjectId(0));
127 }
128 }
129
130 private static AnyObjectId asTree(ObjectReader or, AnyObjectId treeish)
131 throws MissingObjectException, IncorrectObjectTypeException,
132 IOException {
133 if (treeish instanceof RevTree)
134 return treeish;
135
136 if (treeish instanceof RevCommit
137 && ((RevCommit) treeish).getTree() != null)
138 return ((RevCommit) treeish).getTree();
139
140 try (RevWalk rw = new RevWalk(or)) {
141 return rw.parseTree(treeish).getId();
142 }
143 }
144 }