List of usage examples for org.eclipse.jgit.lib Repository open
@NonNull public ObjectLoader open(AnyObjectId objectId) throws MissingObjectException, IOException
From source file:com.googlesource.gerrit.plugins.xdocs.XDocServlet.java
License:Apache License
private Resource getImageResource(Repository repo, DiffMode diffMode, ObjectId revId, ObjectId revIdB, String file) {//from w w w. j a v a 2 s. c o m ObjectId id = diffMode == DiffMode.NO_DIFF || diffMode == DiffMode.SIDEBYSIDE_A ? revId : revIdB; try (RevWalk rw = new RevWalk(repo)) { RevCommit commit = rw.parseCommit(id); RevTree tree = commit.getTree(); try (TreeWalk tw = new TreeWalk(repo)) { tw.addTree(tree); tw.setRecursive(true); tw.setFilter(PathFilter.create(file)); if (!tw.next()) { return Resource.NOT_FOUND; } ObjectId objectId = tw.getObjectId(0); ObjectLoader loader = repo.open(objectId); byte[] content = loader.getBytes(Integer.MAX_VALUE); MimeType mimeType = fileTypeRegistry.getMimeType(file, content); if (!isSafeImage(mimeType)) { return Resource.NOT_FOUND; } return new SmallResource(content).setContentType(mimeType.toString()) .setCharacterEncoding(UTF_8.name()).setLastModified(commit.getCommitTime()); } } catch (IOException e) { return Resource.NOT_FOUND; } }
From source file:com.madgag.agit.views.BlobSummaryView.java
License:Open Source License
public void setObject(RevBlob blob, View view, Repository repo) { try {/* w w w.j a v a 2 s. com*/ long size = repo.open(blob).getSize(); ((TextView) view.findViewById(blob_size)).setText(size + " bytes"); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.mangosolutions.rcloud.rawgist.repository.git.ReadGistOperation.java
private FileContent readContent(Repository repository, TreeWalk treeWalk) { ObjectId objectId = treeWalk.getObjectId(0); String fileName = treeWalk.getPathString(); FileContent content = fileContentCache.load(objectId.getName(), fileName); if (content == null) { content = new FileContent(); try {//from w ww. ja va 2s . co m content.setFilename(fileName); ObjectLoader loader = repository.open(objectId); content.setContent(new String(loader.getBytes(), Charsets.UTF_8)); content.setSize(loader.getSize()); content.setTruncated(false); String language = FilenameUtils.getExtension(fileName); if (!GitGistRepository.B64_BINARY_EXTENSION.equals(language) && !StringUtils.isEmpty(language)) { content.setLanguage(language); } content.setType(MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(fileName)); fileContentCache.save(objectId.getName(), fileName, content); } catch (IOException e) { GistError error = new GistError(GistErrorCode.ERR_GIST_CONTENT_NOT_READABLE, "Could not read content of {} for gist {}", fileName, gistId); logger.error(error.getFormattedMessage() + " with path {}", this.layout.getRootFolder(), e); throw new GistRepositoryError(error, e); } } return content; }
From source file:com.nexus.git.GetFileAttributes.java
License:Apache License
private static void printFile(Repository repository, RevTree tree) throws IOException { // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree);// w w w. j a v a 2 s .c o m treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("README.md")); // if (!treeWalk.next()) { // throw new IllegalStateException("Did not find expected file // 'README.md'"); // } // FileMode specifies the type of file, FileMode.REGULAR_FILE for // normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } }
From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java
License:Open Source License
public static Trees getTrees(Repository r, String revision, String path, boolean history, int recursion) throws IOException, GitAPIException, EntityNotFoundException { if (path.startsWith("/")) { path = path.substring(1);/* ww w . j a va2 s .c o m*/ } RevObject revId = resolveRevision(r, revision); MutableObjectId revTree = new MutableObjectId(); TreeWalk treeWalk = getTreeOnPath(revTree, r, revId, path); if (treeWalk == null) { throw new EntityNotFoundException("Revision: " + revision + ", path: " + path + " not found."); } Trees trees = new Trees(revTree.name()); Git git = history ? new Git(r) : null; while (treeWalk.next()) { ObjectLoader loader = r.open(treeWalk.getObjectId(0)); Trees.Tree t = new Trees.Tree(treeWalk.getObjectId(0).getName(), // sha treeWalk.getPathString(), // path treeWalk.getNameString(), // name getType(loader.getType()), // type treeWalk.getRawMode(0), // mode loader.getSize() // size ); trees.getTree().add(t); if (recursion == -2 && t.getType() == Item.Type.TREE) { t.setEmpty(getEmptyPath(r, treeWalk.getObjectId(0))); } if (history) { addHistory(git, t, revId, /* path + ( path.length() == 0 ? "" : "/") + */t.getPath()); } } return trees; }
From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java
License:Open Source License
public static Item getItem(Repository r, String revision, String path) throws IOException, EntityNotFoundException { if (path.startsWith("/")) { path = path.substring(1);// w ww . ja v a2 s .c om } String id = resolve(r, r.resolve(revision), path); if (id == null) { throw new EntityNotFoundException(); } ObjectId objectId = ObjectId.fromString(id); ObjectLoader loader = r.open(objectId); return new Item(id, getType(loader.getType())); }
From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java
License:Open Source License
private static String getEmptyPath(Repository r, ObjectId id) throws IOException { TreeWalk treeWalk = new TreeWalk(r); treeWalk.addTree(new RevWalk(r).parseTree(id)); String path = null;/*from w ww .j av a 2 s .co m*/ // Find tree of interrest it has to be alone there ObjectId iId = null; String iPath = null; while (treeWalk.next()) { if (iId == null) { iId = treeWalk.getObjectId(0); iPath = treeWalk.getPathString(); } else { iId = null; break; } } if (iId != null) { ObjectLoader loader = r.open(iId); if (loader.getType() == Constants.OBJ_TREE) { // It is alone there and it is a folder path = "/" + iPath; String sep = getEmptyPath(r, iId); if (sep != null) { path = path + sep; } } } return path; }
From source file:edu.nju.cs.inform.jgit.api.GetFileAttributes.java
License:Apache License
private static void printFile(Repository repository, RevTree tree) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree);//from ww w. j av a2s .c om treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } }
From source file:eu.trentorise.opendata.josman.test.GitTest.java
private static void printFile(Repository repository, RevTree tree, String filter) { try {/* w w w. ja v a 2s . c o m*/ // now try to find a specific file TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create(filter)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file " + filter); } // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println(treeWalk.getPathString() + ": " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } catch (Exception ex) { throw new RuntimeException("Error while walkinf files", ex); } }
From source file:eu.trentorise.opendata.josman.test.GitTest.java
private static void printDirectory(Repository repo, String revStr, String prefix) { checkNotNull(repo);//w w w . jav a2 s .co m checkNotNull(prefix); checkNotEmpty(revStr, "invalid revisiong string!"); try { // find the HEAD ObjectId lastCommitId = repo.resolve(revStr); // a RevWalk allows to walk over commits based on some filtering that is defined RevWalk revWalk = new RevWalk(repo, DEPTH); RevCommit commit = revWalk.parseCommit(lastCommitId); // and using commit's tree find the path RevTree tree = commit.getTree(); LOG.log(Level.INFO, "Having tree: {0}", tree); // look at directory, this has FileMode.TREE TreeWalk treeWalk = new TreeWalk(repo); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(prefix));// careful this looks for *exact* dir name (i.e. 'docs' will work for docs/a.txt but 'doc' won't work) while (treeWalk.next()) { // FileMode now indicates that this is a directory, i.e. FileMode.TREE.equals(fileMode) holds true String pathString = treeWalk.getPathString(); if (pathString.startsWith(prefix)) { FileMode fileMode = treeWalk.getFileMode(0); System.out.println(pathString + ": " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode); ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repo.open(objectId); InputStream stream = loader.openStream(); File outputFile = File.createTempFile("bla", "bla"); FileUtils.copyInputStreamToFile(stream, outputFile); stream.close(); System.out.println("content:\n" + FileUtils.readFileToString(outputFile)); } } } catch (Exception ex) { throw new RuntimeException("Error while walking directory!", ex); } }