private AbstractTreeIterator getTreeIterator(Repository db, String name) throws IOException { final ObjectId id = db.resolve(name); if (id == null) throw new IllegalArgumentException(name); final CanonicalTreeParser p = new CanonicalTreeParser(); final ObjectReader or = db.newObjectReader(); try { p.reset(or, new RevWalk(db).parseTree(id)); return p; } finally { or.release(); } }
private void _testMapsWithLinefeeds(boolean useBytes) throws Exception { String CSV = "A,B,C\n" + "data11,data12\n" + "data21,data22,data23\r\n" + "data31,\"data32 data32\ndata32 data32\",data33\n" + "data41,\"data42 data42\r\ndata42\",data43\n"; CsvSchema cs = CsvSchema.emptySchema().withHeader(); ObjectReader or = MAPPER.readerFor(HashMap.class).with(cs); MappingIterator<Map<String, String>> mi; if (useBytes) { mi = or.readValues(CSV.getBytes("UTF-8")); } else { mi = or.readValues(CSV); } assertTrue(mi.hasNext()); Map<String, String> map = mi.nextValue(); assertNotNull(map); assertEquals("data11", map.get("A")); assertEquals("data12", map.get("B")); assertEquals(2, map.size()); assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); // then entries with linefeeds assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); assertEquals("data31", map.get("A")); assertEquals("data32 data32\ndata32 data32", map.get("B")); assertEquals("data33", map.get("C")); assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); assertEquals("data41", map.get("A")); assertEquals("data42 data42\r\ndata42", map.get("B")); assertEquals("data43", map.get("C")); assertFalse(mi.hasNext()); mi.close(); }
private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Exception { r = r.forType(FiveMinuteUser.class); FiveMinuteUser user; final String INPUT = "Bob,Robertson,MALE,AQIDBAU=,false\n"; if (useBytes) { user = r.readValue(INPUT); } else { user = r.readValue(INPUT.getBytes("UTF-8")); } assertEquals("Bob", user.firstName); assertEquals("Robertson", user.lastName); assertEquals(Gender.MALE, user.getGender()); assertFalse(user.isVerified()); assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage()); }
@NotNull private InputStream openAttributes(@NotNull ObjectReader reader, @Nullable ObjectId id) throws IOException { if (ObjectId.zeroId().equals(id)) { return new ByteArrayInputStream(new byte[0]); } return reader.open(id, Constants.OBJ_BLOB).openStream(); }
public void testSimpleExplicitWithBOM() throws Exception { ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA); r = r.forType(FiveMinuteUser.class); FiveMinuteUser user; ByteArrayOutputStream b = new ByteArrayOutputStream(); // first, UTF-8 BOM: b.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}); b.write("Bob,Robertson,MALE,AQIDBAU=,false\n".getBytes("UTF-8")); b.close(); user = r.readValue(b.toByteArray()); String fn = user.firstName; if (!fn.equals("Bob")) { fail("Expected 'Bob' (3), got '" + fn + "' (" + fn.length() + ")"); } assertEquals("Robertson", user.lastName); assertEquals(Gender.MALE, user.getGender()); assertFalse(user.isVerified()); assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage()); }
@NotNull public ConvertTask convertTask(@NotNull ObjectReader reader, @NotNull TaskKey key) throws IOException { switch (key.getType()) { case Simple: { if (!reader.has(key.getObjectId())) { return keepMissingTask(key.getObjectId()); } final RevObject revObject = new RevWalk(reader).parseAny(key.getObjectId()); if (revObject instanceof RevCommit) { return convertCommitTask((RevCommit) revObject); } if (revObject instanceof RevTree) { return convertTreeTask(reader, revObject, false); } if (revObject instanceof RevBlob) { return copyTask(reader, revObject); } if (revObject instanceof RevTag) { return convertTagTask((RevTag) revObject); } throw new IllegalStateException( "Unsupported object type: " + key + " (" + revObject.getClass().getName() + ")"); } case Root: { final RevObject revObject = new RevWalk(reader).parseAny(key.getObjectId()); if (revObject instanceof RevTree) { return convertTreeTask(reader, revObject, true); } throw new IllegalStateException( "Unsupported object type: " + key + " (" + revObject.getClass().getName() + ")"); } case Attribute: return createAttributesTask(reader, key.getObjectId()); case UploadLfs: return convertLfsTask(reader, key.getObjectId()); default: throw new IllegalStateException("Unknwon task key type: " + key.getType()); } }
private boolean handleGetDiff( HttpServletRequest request, HttpServletResponse response, Repository db, String scope, String pattern, OutputStream out) throws Exception { Git git = new Git(db); DiffCommand diff = git.diff(); diff.setOutputStream(new BufferedOutputStream(out)); AbstractTreeIterator oldTree; AbstractTreeIterator newTree = new FileTreeIterator(db); if (scope.contains("..")) { // $NON-NLS-1$ String[] commits = scope.split("\\.\\."); // $NON-NLS-1$ if (commits.length != 2) { String msg = NLS.bind("Failed to generate diff for {0}", scope); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } oldTree = getTreeIterator(db, commits[0]); newTree = getTreeIterator(db, commits[1]); } else if (scope.equals(GitConstants.KEY_DIFF_CACHED)) { ObjectId head = db.resolve(Constants.HEAD + "^{tree}"); // $NON-NLS-1$ if (head == null) { String msg = NLS.bind("Failed to generate diff for {0}, no HEAD", scope); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } CanonicalTreeParser p = new CanonicalTreeParser(); ObjectReader reader = db.newObjectReader(); try { p.reset(reader, head); } finally { reader.release(); } oldTree = p; newTree = new DirCacheIterator(db.readDirCache()); } else if (scope.equals(GitConstants.KEY_DIFF_DEFAULT)) { oldTree = new DirCacheIterator(db.readDirCache()); } else { oldTree = getTreeIterator(db, scope); } String[] paths = request.getParameterValues(ProtocolConstants.KEY_PATH); TreeFilter filter = null; TreeFilter pathFilter = null; if (paths != null) { if (paths.length > 1) { Set<TreeFilter> pathFilters = new HashSet<TreeFilter>(paths.length); for (String path : paths) { pathFilters.add(PathFilter.create(path)); } pathFilter = OrTreeFilter.create(pathFilters); } else if (paths.length == 1) { pathFilter = PathFilter.create(paths[0]); } } if (pattern != null) { PathFilter patternFilter = PathFilter.create(pattern); if (pathFilter != null) filter = AndTreeFilter.create(patternFilter, pathFilter); else filter = patternFilter; } else { filter = pathFilter; } if (filter != null) diff.setPathFilter(filter); diff.setOldTree(oldTree); diff.setNewTree(newTree); diff.call(); return true; }