List of usage examples for org.eclipse.jgit.treewalk TreeWalk getObjectReader
public ObjectReader getObjectReader()
From source file:com.gitblit.servlet.RawServlet.java
License:Apache License
protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository, RevCommit commit, String requestedPath) throws IOException { boolean served = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try {//from w w w. j a va 2s . c o m tw.reset(); tw.addTree(commit.getTree()); PathFilter f = PathFilter.create(requestedPath); tw.setFilter(f); tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); ObjectReader reader = tw.getObjectReader(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); String filename = StringUtils.getLastPathElement(requestedPath); try { String userAgent = request.getHeader("User-Agent"); if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) { response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\""); } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) { response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\""); } else { response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\""); } } catch (UnsupportedEncodingException e) { response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); } long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB); setContentType(response, "application/octet-stream"); response.setIntHeader("Content-Length", (int) len); ObjectLoader ldr = repository.open(id); ldr.copyTo(response.getOutputStream()); served = true; } } finally { tw.close(); rw.dispose(); } response.flushBuffer(); return served; }
From source file:com.gitblit.utils.CompressionUtils.java
License:Apache License
/** * Zips the contents of the tree at the (optionally) specified revision and * the (optionally) specified basepath to the supplied outputstream. * * @param repository/* w w w . j av a 2s . com*/ * @param basePath * if unspecified, entire repository is assumed. * @param objectId * if unspecified, HEAD is assumed. * @param os * @return true if repository was successfully zipped to supplied output * stream */ public static boolean zip(Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) { RevCommit commit = JGitUtils.getCommit(repository, objectId); if (commit == null) { return false; } boolean success = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { tw.reset(); tw.addTree(commit.getTree()); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setComment("Generated by Gitblit"); if (!StringUtils.isEmpty(basePath)) { PathFilter f = PathFilter.create(basePath); tw.setFilter(f); } tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); ObjectReader reader = tw.getObjectReader(); long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); ObjectLoader loader = repository.open(id); ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString()); FilestoreModel filestoreItem = null; if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) { filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id)); } final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize(); entry.setSize(size); entry.setComment(commit.getName()); entry.setUnixMode(mode.getBits()); entry.setTime(modified); zos.putArchiveEntry(entry); if (filestoreItem == null) { //Copy repository stored file loader.copyTo(zos); } else { //Copy filestore file try (FileInputStream streamIn = new FileInputStream( filestoreManager.getStoragePath(filestoreItem.oid))) { IOUtils.copyLarge(streamIn, zos); } catch (Throwable e) { LOGGER.error( MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e); //Handle as per other errors throw e; } } zos.closeArchiveEntry(); } zos.finish(); success = true; } catch (IOException e) { error(e, repository, "{0} failed to zip files from commit {1}", commit.getName()); } finally { tw.close(); rw.dispose(); } return success; }
From source file:com.gitblit.utils.CompressionUtils.java
License:Apache License
/** * Compresses/archives the contents of the tree at the (optionally) * specified revision and the (optionally) specified basepath to the * supplied outputstream./*from w w w. j a v a 2 s . c om*/ * * @param algorithm * compression algorithm for tar (optional) * @param repository * @param basePath * if unspecified, entire repository is assumed. * @param objectId * if unspecified, HEAD is assumed. * @param os * @return true if repository was successfully zipped to supplied output * stream */ private static boolean tar(String algorithm, Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) { RevCommit commit = JGitUtils.getCommit(repository, objectId); if (commit == null) { return false; } OutputStream cos = os; if (!StringUtils.isEmpty(algorithm)) { try { cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os); } catch (CompressorException e1) { error(e1, repository, "{0} failed to open {1} stream", algorithm); } } boolean success = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { tw.reset(); tw.addTree(commit.getTree()); TarArchiveOutputStream tos = new TarArchiveOutputStream(cos); tos.setAddPaxHeadersForNonAsciiNames(true); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); if (!StringUtils.isEmpty(basePath)) { PathFilter f = PathFilter.create(basePath); tw.setFilter(f); } tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); ObjectLoader loader = repository.open(id); if (FileMode.SYMLINK == mode) { TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK); ByteArrayOutputStream bos = new ByteArrayOutputStream(); loader.copyTo(bos); entry.setLinkName(bos.toString()); entry.setModTime(modified); tos.putArchiveEntry(entry); tos.closeArchiveEntry(); } else { TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString()); entry.setMode(mode.getBits()); entry.setModTime(modified); FilestoreModel filestoreItem = null; if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) { filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id)); } final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize(); entry.setSize(size); tos.putArchiveEntry(entry); if (filestoreItem == null) { //Copy repository stored file loader.copyTo(tos); } else { //Copy filestore file try (FileInputStream streamIn = new FileInputStream( filestoreManager.getStoragePath(filestoreItem.oid))) { IOUtils.copyLarge(streamIn, tos); } catch (Throwable e) { LOGGER.error( MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e); //Handle as per other errors throw e; } } tos.closeArchiveEntry(); } } tos.finish(); tos.close(); cos.close(); success = true; } catch (IOException e) { error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName()); } finally { tw.close(); rw.dispose(); } return success; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns the list of files changed in a specified commit. If the * repository does not exist or is empty, an empty list is returned. * * @param repository//from w ww . ja v a 2s .com * @param commit * if null, HEAD is assumed. * @param calculateDiffStat * if true, each PathChangeModel will have insertions/deletions * @return list of files changed in a commit */ public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit, boolean calculateDiffStat) { List<PathChangeModel> list = new ArrayList<PathChangeModel>(); if (!hasCommits(repository)) { return list; } RevWalk rw = new RevWalk(repository); try { if (commit == null) { ObjectId object = getDefaultBranch(repository); commit = rw.parseCommit(object); } if (commit.getParentCount() == 0) { TreeWalk tw = new TreeWalk(repository); tw.reset(); tw.setRecursive(true); tw.addTree(commit.getTree()); while (tw.next()) { long size = 0; FilestoreModel filestoreItem = null; ObjectId objectId = tw.getObjectId(0); try { if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) { size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB); if (isPossibleFilestoreItem(size)) { filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId)); } } } catch (Throwable t) { error(t, null, "failed to retrieve blob size for " + tw.getPathString()); } list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), filestoreItem, size, tw.getRawMode(0), objectId.getName(), commit.getId().getName(), ChangeType.ADD)); } tw.close(); } else { RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository); df.setRepository(repository); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree()); for (DiffEntry diff : diffs) { // create the path change model PathChangeModel pcm = PathChangeModel.from(diff, commit.getName(), repository); if (calculateDiffStat) { // update file diffstats df.format(diff); PathChangeModel pathStat = df.getDiffStat().getPath(pcm.path); if (pathStat != null) { pcm.insertions = pathStat.insertions; pcm.deletions = pathStat.deletions; } } list.add(pcm); } } } catch (Throwable t) { error(t, repository, "{0} failed to determine files in commit!"); } finally { rw.dispose(); } return list; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns a path model of the current file in the treewalk. * * @param tw//from w w w. j a va 2 s . c o m * @param basePath * @param commit * @return a path model of the current file in the treewalk */ private static PathModel getPathModel(TreeWalk tw, String basePath, RevCommit commit) { String name; long size = 0; if (StringUtils.isEmpty(basePath)) { name = tw.getPathString(); } else { name = tw.getPathString().substring(basePath.length() + 1); } ObjectId objectId = tw.getObjectId(0); FilestoreModel filestoreItem = null; try { if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) { size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB); if (isPossibleFilestoreItem(size)) { filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId)); } } } catch (Throwable t) { error(t, null, "failed to retrieve blob size for " + tw.getPathString()); } return new PathModel(name, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), objectId.getName(), commit.getName()); }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns a path model by path string//from ww w . ja v a2 s .co m * * @param repo * @param path * @param filter * @param commit * @return a path model of the specified object */ private static PathModel getPathModel(Repository repo, String path, String filter, RevCommit commit) throws IOException { long size = 0; FilestoreModel filestoreItem = null; TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); String pathString = path; if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) { pathString = PathUtils.getLastPathComponent(pathString); size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB); if (isPossibleFilestoreItem(size)) { filestoreItem = getFilestoreItem(tw.getObjectReader().open(tw.getObjectId(0))); } } else if (tw.isSubtree()) { // do not display dirs that are behind in the path if (!Strings.isNullOrEmpty(filter)) { pathString = path.replaceFirst(filter + "/", ""); } // remove the last slash from path in displayed link if (pathString != null && pathString.charAt(pathString.length() - 1) == '/') { pathString = pathString.substring(0, pathString.length() - 1); } } return new PathModel(pathString, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), tw.getObjectId(0).getName(), commit.getName()); }
From source file:i5.las2peer.services.codeGenerationService.generators.MicroserviceGenerator.java
/** * /*from ww w . ja v a 2 s . c o m*/ * Creates source code from a CAE microservice model and pushes it to GitHub. * * @param microservice the microservice model * @param templateRepositoryName the name of the template repository on GitHub * @param gitHubOrganization the organization that is used in the CAE * @param gitHubUser the CAE user * @param gitHubUserMail the mail of the CAE user * @param gitHubPassword the password of the CAE user * * @throws GitHubException thrown if anything goes wrong during this process. Wraps around all * other exceptions and prints their message. * */ public static void createSourceCode(Microservice microservice, String templateRepositoryName, String gitHubOrganization, String gitHubUser, String gitHubUserMail, String gitHubPassword) throws GitHubException { // variables to be closed in the final block Repository microserviceRepository = null; TreeWalk treeWalk = null; // helper variables String packageName = microservice.getResourceName().substring(0, 1).toLowerCase() + microservice.getResourceName().substring(1); // get the port: skip first 6 characters for search (http: / https:) String port = microservice.getPath().substring(microservice.getPath().indexOf(":", 6) + 1, microservice.getPath().indexOf("/", microservice.getPath().indexOf(":", 6))); // variables holding content to be modified and added to repository later String projectFile = null; BufferedImage logo = null; String readMe = null; String license = null; String buildFile = null; String startScriptWindows = null; String startScriptUnix = null; String userAgentGeneratorWindows = null; String userAgentGeneratorUnix = null; String nodeInfo = null; String antServiceProperties = null; String antUserProperties = null; String ivy = null; String ivySettings = null; String serviceProperties = null; String webConnectorConfig = null; String gitignore = null; String classpath = null; String databaseManager = null; String serviceClass = null; String serviceTest = null; String genericHttpMethod = null; String genericApiResponse = null; String genericHttpResponse = null; String genericTestCase = null; String databaseConfig = null; String databaseInstantiation = null; String serviceInvocation = null; String databaseScript = null; String genericTable = null; try { PersonIdent caeUser = new PersonIdent(gitHubUser, gitHubUserMail); String repositoryName = "microservice-" + microservice.getName().replace(" ", "-"); microserviceRepository = generateNewRepository(repositoryName, gitHubOrganization, gitHubUser, gitHubPassword); try { // now load the TreeWalk containing the template repository content treeWalk = getTemplateRepositoryContent(templateRepositoryName, gitHubOrganization); treeWalk.setFilter(PathFilter.create("backend/")); ObjectReader reader = treeWalk.getObjectReader(); // walk through the tree and retrieve the needed templates while (treeWalk.next()) { ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = reader.open(objectId); switch (treeWalk.getNameString()) { // start with the "easy" replacements, and store the other template files for later case ".project": projectFile = new String(loader.getBytes(), "UTF-8"); projectFile = projectFile.replace("$Microservice_Name$", microservice.getName()); break; case "logo_services.png": logo = ImageIO.read(loader.openStream()); break; case "README.md": readMe = new String(loader.getBytes(), "UTF-8"); readMe = readMe.replace("$Repository_Name$", repositoryName); readMe = readMe.replace("$Organization_Name$", gitHubOrganization); readMe = readMe.replace("$Microservice_Name$", microservice.getName()); break; case "LICENSE.txt": license = new String(loader.getBytes(), "UTF-8"); break; case "build.xml": buildFile = new String(loader.getBytes(), "UTF-8"); buildFile = buildFile.replace("$Microservice_Name$", microservice.getName()); break; case "start_network.bat": startScriptWindows = new String(loader.getBytes(), "UTF-8"); startScriptWindows = startScriptWindows.replace("$Resource_Name$", microservice.getResourceName()); startScriptWindows = startScriptWindows.replace("$Lower_Resource_Name$", packageName); startScriptWindows = startScriptWindows.replace("$Microservice_Version$", microservice.getVersion() + ""); break; case "start_network.sh": startScriptUnix = new String(loader.getBytes(), "UTF-8"); startScriptUnix = startScriptUnix.replace("$Resource_Name$", microservice.getResourceName()); startScriptUnix = startScriptUnix.replace("$Lower_Resource_Name$", packageName); startScriptUnix = startScriptUnix.replace("$Microservice_Version$", microservice.getVersion() + ""); break; case "start_UserAgentGenerator.bat": userAgentGeneratorWindows = new String(loader.getBytes(), "UTF-8"); break; case "start_UserAgentGenerator.sh": userAgentGeneratorUnix = new String(loader.getBytes(), "UTF-8"); break; case "nodeInfo.xml": nodeInfo = new String(loader.getBytes(), "UTF-8"); nodeInfo = nodeInfo.replace("$Developer$", microservice.getDeveloper()); nodeInfo = nodeInfo.replace("$Resource_Name$", microservice.getResourceName()); break; case "service.properties": antServiceProperties = new String(loader.getBytes(), "UTF-8"); antServiceProperties = antServiceProperties.replace("$Microservice_Version$", microservice.getVersion() + ""); antServiceProperties = antServiceProperties.replace("$Lower_Resource_Name$", packageName); antServiceProperties = antServiceProperties.replace("$Resource_Name$", microservice.getResourceName()); antServiceProperties = antServiceProperties.replace("$Microservice_Version$", microservice.getVersion() + ""); break; case "user.properties": antUserProperties = new String(loader.getBytes(), "UTF-8"); break; case "ivy.xml": ivy = new String(loader.getBytes(), "UTF-8"); // add mysql dependency only if a database exists if (microservice.getDatabase() != null) { ivy = ivy.replace("$MySQL_Dependencies$", "<dependency org=\"mysql\" name=\"mysql-connector-java\" rev=\"5.1.6\" />\n" + " <dependency org=\"org.apache.commons\" name=\"commons-pool2\" rev=\"2.2\" />\n" + " <dependency org=\"org.apache.commons\" name=\"commons-dbcp2\" rev=\"2.0\" />"); } else { ivy = ivy.replace(" $MySQL_Dependencies$\n", ""); } break; case "ivysettings.xml": ivySettings = new String(loader.getBytes(), "UTF-8"); break; case "i5.las2peer.services.servicePackage.ServiceClass.properties": serviceProperties = new String(loader.getBytes(), "UTF-8"); // if database does not exist, clear the file if (microservice.getDatabase() == null) { serviceProperties = ""; } else { serviceProperties = serviceProperties.replace("$Database_Address$", microservice.getDatabase().getAddress()); serviceProperties = serviceProperties.replace("$Database_Schema$", microservice.getDatabase().getSchema()); serviceProperties = serviceProperties.replace("$Database_User$", microservice.getDatabase().getLoginName()); serviceProperties = serviceProperties.replace("$Database_Password$", microservice.getDatabase().getLoginPassword()); } case "i5.las2peer.webConnector.WebConnector.properties": webConnectorConfig = new String(loader.getBytes(), "UTF-8"); webConnectorConfig = webConnectorConfig.replace("$HTTP_Port$", port); break; case ".gitignore": gitignore = new String(loader.getBytes(), "UTF-8"); break; case ".classpath": classpath = new String(loader.getBytes(), "UTF-8"); if (microservice.getDatabase() != null) { classpath = classpath.replace("$Database_Libraries$", "<classpathentry kind=\"lib\" path=\"lib/mysql-connector-java-5.1.6.jar\"/>\n" + " <classpathentry kind=\"lib\" path=\"lib/commons-dbcp2-2.0.jar\"/>"); } else { classpath = classpath.replace("$Database_Libraries$\n", ""); } break; case "DatabaseManager.java": if (microservice.getDatabase() != null) { databaseManager = new String(loader.getBytes(), "UTF-8"); databaseManager = databaseManager.replace("$Lower_Resource_Name$", packageName); } break; case "ServiceClass.java": serviceClass = new String(loader.getBytes(), "UTF-8"); break; case "genericHTTPMethod.txt": genericHttpMethod = new String(loader.getBytes(), "UTF-8"); break; case "genericHTTPResponse.txt": genericHttpResponse = new String(loader.getBytes(), "UTF-8"); break; case "genericApiResponse.txt": genericApiResponse = new String(loader.getBytes(), "UTF-8"); break; case "ServiceTest.java": serviceTest = new String(loader.getBytes(), "UTF-8"); break; case "genericTestMethod.txt": genericTestCase = new String(loader.getBytes(), "UTF-8"); break; case "databaseConfig.txt": databaseConfig = new String(loader.getBytes(), "UTF-8"); break; case "databaseInstantiation.txt": databaseInstantiation = new String(loader.getBytes(), "UTF-8"); break; case "genericServiceInvocation.txt": serviceInvocation = new String(loader.getBytes(), "UTF-8"); break; case "database.sql": databaseScript = new String(loader.getBytes(), "UTF-8"); break; case "genericTable.txt": genericTable = new String(loader.getBytes(), "UTF-8"); break; } } } catch (Exception e) { logger.printStackTrace(e); throw new GitHubException(e.getMessage()); } // generate service class and test String repositoryLocation = "https://github.com/" + gitHubOrganization + "/" + repositoryName; serviceClass = generateNewServiceClass(serviceClass, microservice, repositoryLocation, genericHttpMethod, genericApiResponse, genericHttpResponse, databaseConfig, databaseInstantiation, serviceInvocation); serviceTest = generateNewServiceTest(serviceTest, microservice, genericTestCase); if (microservice.getDatabase() != null) { databaseScript = generateDatabaseScript(databaseScript, genericTable, microservice); } // add files to new repository // configuration and build stuff microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/ivy/", "ivy.xml", ivy); microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/ivy/", "ivysettings.xml", ivySettings); microserviceRepository = createTextFileInRepository(microserviceRepository, "", "build.xml", buildFile); microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/ant_configuration/", "user.properties", antUserProperties); microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/ant_configuration/", "service.properties", antServiceProperties); microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/", "nodeInfo.xml", nodeInfo); microserviceRepository = createTextFileInRepository(microserviceRepository, "", ".project", projectFile); microserviceRepository = createTextFileInRepository(microserviceRepository, "", ".gitignore", gitignore); microserviceRepository = createTextFileInRepository(microserviceRepository, "", ".classpath", classpath); // property files microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/", "i5.las2peer.services." + packageName + "." + microservice.getResourceName() + ".properties", serviceProperties); microserviceRepository = createTextFileInRepository(microserviceRepository, "etc/", "i5.las2peer.webConnector.WebConnector.properties", webConnectorConfig); // scripts microserviceRepository = createTextFileInRepository(microserviceRepository, "bin/", "start_network.bat", startScriptWindows); microserviceRepository = createTextFileInRepository(microserviceRepository, "bin/", "start_network.sh", startScriptUnix); microserviceRepository = createTextFileInRepository(microserviceRepository, "bin/", "start_UserAgentGenerator.bat", userAgentGeneratorWindows); microserviceRepository = createTextFileInRepository(microserviceRepository, "bin/", "start_UserAgentGenerator.sh", userAgentGeneratorUnix); // doc microserviceRepository = createTextFileInRepository(microserviceRepository, "", "README.md", readMe); microserviceRepository = createTextFileInRepository(microserviceRepository, "", "LICENSE.txt", license); microserviceRepository = createImageFileInRepository(microserviceRepository, "img/", "logo.png", logo); // source code if (databaseManager != null) { microserviceRepository = createTextFileInRepository(microserviceRepository, "src/main/i5/las2peer/services/" + packageName + "/database/", "DatabaseManager.java", databaseManager); // database script (replace spaces in filename for better usability later on) microserviceRepository = createTextFileInRepository(microserviceRepository, "db/", microservice.getName().replace(" ", "_") + "_create_tables.sql", databaseScript); } microserviceRepository = createTextFileInRepository(microserviceRepository, "src/main/i5/las2peer/services/" + packageName + "/", microservice.getResourceName() + ".java", serviceClass); microserviceRepository = createTextFileInRepository(microserviceRepository, "src/test/i5/las2peer/services/" + packageName + "/", microservice.getResourceName() + "Test.java", serviceTest); // commit files try { Git.wrap(microserviceRepository).commit() .setMessage("Generated microservice version " + microservice.getVersion()) .setCommitter(caeUser).call(); } catch (Exception e) { logger.printStackTrace(e); throw new GitHubException(e.getMessage()); } // push (local) repository content to GitHub repository try { pushToRemoteRepository(microserviceRepository, gitHubUser, gitHubPassword); } catch (Exception e) { logger.printStackTrace(e); throw new GitHubException(e.getMessage()); } // close all open resources } finally { microserviceRepository.close(); treeWalk.close(); } }
From source file:i5.las2peer.services.gamificationGamifierService.GamificationGamifierService.java
License:Open Source License
@POST @Path("/repo") @Produces(MediaType.APPLICATION_JSON)/*w w w . j av a2 s .c o m*/ @ApiOperation(value = "memberLoginValidation", notes = "Simple function to validate a member login.") @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Member is registered"), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "Unauthorized"), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "User data error to be retrieved"), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Cannot connect to database"), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "User data error to be retrieved. Not JSON object") }) public HttpResponse updateRepository( @ApiParam(value = "Data in JSON", required = true) @ContentParam byte[] contentB) { // Request log L2pLogger.logEvent(this, Event.SERVICE_CUSTOM_MESSAGE_99, "POST " + "gamification/gamifier/repo"); long randomLong = new Random().nextLong(); //To be able to match UserAgent userAgent = (UserAgent) getContext().getMainAgent(); // take username as default name String name = userAgent.getLoginName(); System.out.println("User name : " + name); if (name.equals("anonymous")) { return unauthorizedMessage(); } L2pLogger.logEvent(Event.SERVICE_CUSTOM_MESSAGE_9, "" + randomLong); JSONObject objResponse = new JSONObject(); String content = new String(contentB); if (content.equals(null)) { objResponse.put("message", "Cannot update repository. Cannot parse json data into string"); //L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); L2pLogger.logEvent(this, Event.AGENT_UPLOAD_FAILED, (String) objResponse.get("message")); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); } // if(!initializeDBConnection()){ // objResponse.put("message", "Cannot connect to database"); // L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); // return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); // } JSONObject obj; String originRepositoryName; String newRepositoryName; String fileContent; String appId; String epURL; String aopScript; try { obj = (JSONObject) JSONValue.parseWithException(content); originRepositoryName = stringfromJSON(obj, "originRepositoryName"); newRepositoryName = stringfromJSON(obj, "newRepositoryName"); //fileContent = stringfromJSON(obj,"fileContent"); appId = stringfromJSON(obj, "appId"); epURL = stringfromJSON(obj, "epURL"); aopScript = stringfromJSON(obj, "aopScript"); } catch (ParseException e) { e.printStackTrace(); objResponse.put("message", "Cannot update repository. Cannot parse json data into string. " + e.getMessage()); L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); } catch (IOException e) { e.printStackTrace(); objResponse.put("message", "Cannot update repository. Cannot parse json data into string. " + e.getMessage()); L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); } // check if repo exist TreeWalk treeWalk = null; Repository newRepository = null; Repository originRepository = null; // helper variables // variables holding content to be modified and added to repository later String widget = null; try { RepositoryHelper.deleteRemoteRepository(newRepositoryName, gitHubOrganizationNewRepo, gitHubUserNewRepo, gitHubPasswordNewRepo); } catch (GitHubException e) { //e.printStackTrace(); } try { PersonIdent caeUser = new PersonIdent(gitHubUserNewRepo, gitHubUserMailNewRepo); originRepository = RepositoryHelper.getRemoteRepository(originRepositoryName, gitHubOrganizationOrigin); newRepository = RepositoryHelper.generateNewRepository(newRepositoryName, gitHubOrganizationNewRepo, gitHubUserNewRepo, gitHubPasswordNewRepo); File originDir = originRepository.getDirectory(); // now load the TreeWalk containing the origin repository content treeWalk = RepositoryHelper.getRepositoryContent(originRepositoryName, gitHubOrganizationOrigin); //System.out.println("PATH " + treeWalk.getPathString()); System.out.println("PATH2 " + originDir.getParent()); System.out.println("PATH3 " + newRepository.getDirectory().getParent()); // treeWalk.setFilter(PathFilter.create("frontend/")); ObjectReader reader = treeWalk.getObjectReader(); // walk through the tree and retrieve the needed templates while (treeWalk.next()) { ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = reader.open(objectId); switch (treeWalk.getNameString()) { case "widget.xml": widget = new String(loader.getBytes(), "UTF-8"); break; } } // replace widget.xml //widget = createWidgetCode(widget, htmlElementTemplate, yjsImports, gitHubOrganization, repositoryName, frontendComponent); widget = RepositoryHelper.appendWidget(widget, gitHubOrganizationNewRepo, newRepositoryName); RepositoryHelper.copyFolder(originRepository.getDirectory().getParentFile(), newRepository.getDirectory().getParentFile()); String aopfilestring = RepositoryHelper.readFile("../GamificationGamifierService/jsfiles/aop.pack.js", Charset.forName("UTF-8")); String oidcwidgetfilestring = RepositoryHelper .readFile("../GamificationGamifierService/jsfiles/oidc-widget.js", Charset.forName("UTF-8")); String gamifierstring = RepositoryHelper.readFile("../GamificationGamifierService/jsfiles/gamifier.js", Charset.forName("UTF-8")); gamifierstring = gamifierstring.replace("$Application_Id$", appId); gamifierstring = gamifierstring.replace("$Endpoint_URL$", epURL); gamifierstring = gamifierstring.replace("$AOP_Script$", aopScript); // add files to new repository newRepository = RepositoryHelper.createTextFileInRepository(newRepository, "", "widget.xml", widget); newRepository = RepositoryHelper.createTextFileInRepository(newRepository, "gamification/", "aop.pack.js", aopfilestring); newRepository = RepositoryHelper.createTextFileInRepository(newRepository, "gamification/", "oidc-widget.js", oidcwidgetfilestring); newRepository = RepositoryHelper.createTextFileInRepository(newRepository, "gamification/", "gamifier.js", gamifierstring); // stage file Git.wrap(newRepository).add().addFilepattern(".").call(); // commit files Git.wrap(newRepository).commit().setMessage("Generated new repo ").setCommitter(caeUser).call(); // push (local) repository content to GitHub repository "gh-pages" branch RepositoryHelper.pushToRemoteRepository(newRepository, gitHubUserNewRepo, gitHubPasswordNewRepo, "master", "gh-pages"); // close all open resources } catch (GitHubException e1) { // TODO Auto-generated catch block e1.printStackTrace(); objResponse.put("message", "Cannot update repository. Github exception. " + e1.getMessage()); L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); } catch (IOException e1) { e1.printStackTrace(); objResponse.put("message", "Cannot update repository. Github exception. " + e1.getMessage()); L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); } catch (Exception e) { objResponse.put("message", "Cannot update repository. Github exception. " + e.getMessage()); L2pLogger.logEvent(this, Event.SERVICE_ERROR, (String) objResponse.get("message")); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_INTERNAL_ERROR); } finally { newRepository.close(); originRepository.close(); treeWalk.close(); } objResponse.put("message", "Updated"); L2pLogger.logEvent(Event.SERVICE_CUSTOM_MESSAGE_10, "" + randomLong); L2pLogger.logEvent(Event.SERVICE_CUSTOM_MESSAGE_22, "" + appId); L2pLogger.logEvent(Event.SERVICE_CUSTOM_MESSAGE_23, "" + name); return new HttpResponse(objResponse.toJSONString(), HttpURLConnection.HTTP_OK); }
From source file:io.liveoak.git.HTTPGitResourceTest.java
License:Open Source License
@Test public void rootResource() throws Exception { // Test #1 - Git Repo present after install File appDir = new File(testApplication.directory().getParentFile(), "newApp"); File appGitDir = new File(appDir, ".git"); // Verify app and git dir do not exist assertThat(appDir.exists()).isFalse(); assertThat(appGitDir.exists()).isFalse(); // Create new app assertThat(execPost(ADMIN_ROOT, "{ \"id\": \"newApp\" }")).hasStatus(201); awaitStability();/*from w w w . ja va 2s . c om*/ // Verify app and git dirs exist assertThat(appDir.exists()).isTrue(); assertThat(appGitDir.exists()).isTrue(); assertThat(new File(appGitDir, ".git").exists()).isFalse(); // Verify REST endpoints assertThat(execGet(GIT_ADMIN_ROOT)).hasStatus(200); assertThat(execGet(GIT_PUBLIC_ROOT)).hasStatus(404).hasNoSuchResource(); // Test #2 - Post a commit, with auto add files to index // Create a file in the application directory File testFile = new File(appDir, "test.txt"); assertThat(testFile.createNewFile()).isTrue(); Files.write(testFile.toPath(), "content".getBytes()); Git gitRepo = Git.open(appDir); // Execute a commit assertThat( execPost("/admin/applications/newApp/resources/git/commits", "{ \"msg\": \"my commit message\" }")) .hasStatus(201); assertThat(gitRepo.status().call().hasUncommittedChanges()).isFalse(); Iterator<RevCommit> iterator = gitRepo.log().all().call().iterator(); int commitSize = 0; RevCommit latestCommit = null; while (iterator.hasNext()) { RevCommit commit = iterator.next(); if (commitSize == 0) { latestCommit = commit; } commitSize++; } assertThat(commitSize).isEqualTo(2); assertThat(latestCommit.getFullMessage()).isEqualTo("my commit message"); TreeWalk treeWalk = new TreeWalk(gitRepo.getRepository()); treeWalk.addTree(latestCommit.getTree()); treeWalk.setFilter(PathFilter.create("test.txt")); assertThat(treeWalk.next()).isTrue(); String fileContent = new String(treeWalk.getObjectReader().open(treeWalk.getObjectId(0)).getBytes()); treeWalk.release(); assertThat(fileContent).isEqualTo("content"); // Test #3 - Post a commit, with auto add files to index off File anotherFile = new File(appDir, "another.txt"); assertThat(anotherFile.createNewFile()).isTrue(); Files.write(anotherFile.toPath(), "another content".getBytes()); Files.write(testFile.toPath(), "updated content".getBytes()); // Execute a commit assertThat(execPost("/admin/applications/newApp/resources/git/commits", "{ \"msg\": \"another commit\", \"include-untracked\": \"false\" }")).hasStatus(201); assertThat(gitRepo.status().call().isClean()).isFalse(); iterator = gitRepo.log().all().call().iterator(); commitSize = 0; while (iterator.hasNext()) { RevCommit commit = iterator.next(); if (commitSize == 0) { latestCommit = commit; } commitSize++; } assertThat(commitSize).isEqualTo(3); assertThat(latestCommit.getFullMessage()).isEqualTo("another commit"); treeWalk = new TreeWalk(gitRepo.getRepository()); treeWalk.addTree(latestCommit.getTree()); treeWalk.setFilter(PathFilter.create("another.txt")); assertThat(treeWalk.next()).isFalse(); treeWalk.release(); treeWalk = new TreeWalk(gitRepo.getRepository()); treeWalk.addTree(latestCommit.getTree()); treeWalk.setFilter(PathFilter.create("test.txt")); assertThat(treeWalk.next()).isTrue(); fileContent = new String(treeWalk.getObjectReader().open(treeWalk.getObjectId(0)).getBytes()); treeWalk.release(); assertThat(fileContent).isEqualTo("updated content"); // Test #4 - Verify PUT on commit is not supported assertThat(execPut("/admin/applications/newApp/resources/git/commits/" + latestCommit.getName(), "{ \"bad\": \"request\" }")).hasStatus(500).isInternalError(); }
From source file:org.eclipse.egit.bc.BeyondCompareRepositoryActionHandler.java
License:Open Source License
protected String getPreviousPath(Repository repository, ObjectReader reader, RevCommit headCommit, RevCommit previousCommit, String path) throws IOException { TreeWalk walk = new TreeWalk(reader); walk.setRecursive(true);/* www .jav a2 s .c o m*/ walk.addTree(previousCommit.getTree()); walk.addTree(headCommit.getTree()); List<DiffEntry> entries = DiffEntry.scan(walk); if (entries.size() < 2) return path; for (DiffEntry diff : entries) if (diff.getChangeType() == ChangeType.MODIFY && path.equals(diff.getNewPath())) return path; RenameDetector detector = new RenameDetector(repository); detector.addAll(entries); List<DiffEntry> renames = detector.compute(walk.getObjectReader(), NullProgressMonitor.INSTANCE); for (DiffEntry diff : renames) if (diff.getChangeType() == ChangeType.RENAME && path.equals(diff.getNewPath())) return diff.getOldPath(); return path; }