List of usage examples for org.eclipse.jgit.treewalk TreeWalk getRawPath
public byte[] getRawPath()
From source file:com.madgag.agit.FileListFragment.java
License:Open Source License
@Override public Loader<List<FilePath>> onCreateLoader(int id, Bundle args) { return new AsyncLoader<List<FilePath>>(getActivity()) { public List<FilePath> loadInBackground() { try { Bundle args = getArguments(); Repository repo = new FileRepository(args.getString(GITDIR)); RevCommit commit = new RevWalk(repo).parseCommit(repo.resolve(args.getString(REVISION))); Stopwatch stopwatch = new Stopwatch().start(); final List<FilePath> paths = newArrayList(); TreeWalk treeWalk = new TreeWalk(repo); treeWalk.setRecursive(true); treeWalk.addTree(commit.getTree()); while (treeWalk.next()) { paths.add(new FilePath(treeWalk.getRawPath())); }/*from w w w.j a v a2 s. c o m*/ Log.d(TAG, "Found " + paths.size() + " files " + stopwatch.stop()); new Thread(new Runnable() { @Override public void run() { // knocks around 15-30% off time-to-display the list Stopwatch stopwatch = new Stopwatch().start(); for (FilePath filePath : paths) { filePath.getPath(); } Log.d(TAG, "Converted " + paths.size() + " path byte buffs to string " + stopwatch.stop()); } }).start(); return paths; } catch (Exception e) { Log.w(TAG, "Bang", e); throw new RuntimeException(e); } } }; }
From source file:com.madgag.agit.views.TreeSummaryView.java
License:Open Source License
public void setObject(RevTree tree, View view, Repository repo) { TreeWalk treeWalk = new TreeWalk(repo); StringBuilder sb = new StringBuilder(); try {//w ww. j av a 2s . co m int treeIndex = treeWalk.addTree(tree); while (treeWalk.next()) { ObjectId newObjectId = treeWalk.getObjectId(treeIndex); String rawPath = new String(treeWalk.getRawPath()); sb.append(rawPath).append(" - "); //System.out.println(newObjectId+" rawPath="+rawPath+" subTree="+ tw.isSubtree()); } ((TextView) view.findViewById(R.id.osv_tree_description)).setText(sb); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }
From source file:org.eclipse.egit.core.synchronize.GitFolderResourceVariant.java
License:Open Source License
/** * @param progress/*from w ww . j ava 2 s. c o m*/ * @return members * @throws IOException */ public IResourceVariant[] getMembers(IProgressMonitor progress) throws IOException { if (members != null) try { return members; } finally { progress.done(); } Repository repo = getRepository(); TreeWalk tw = new TreeWalk(repo); tw.reset(); int nth = tw.addTree(getObjectId()); int iteratorNth = tw.addTree(new FileTreeIterator(repo)); tw.setFilter(new NotIgnoredFilter(iteratorNth)); IProgressMonitor monitor = SubMonitor.convert(progress); monitor.beginTask(NLS.bind(CoreText.GitFolderResourceVariant_fetchingMembers, this), tw.getTreeCount()); List<IResourceVariant> result = new ArrayList<IResourceVariant>(); try { while (tw.next()) { if (monitor.isCanceled()) throw new OperationCanceledException(); ObjectId newObjectId = tw.getObjectId(nth); String path = getPath() + "/" + new String(tw.getRawPath()); //$NON-NLS-1$ if (!newObjectId.equals(zeroId())) if (tw.isSubtree()) result.add(new GitFolderResourceVariant(repo, getRevCommit(), newObjectId, path)); else result.add(new GitBlobResourceVariant(repo, getRevCommit(), newObjectId, path)); monitor.worked(1); } members = result.toArray(new IResourceVariant[result.size()]); return members; } finally { monitor.done(); } }
From source file:org.eclipse.egit.ui.internal.synchronize.model.GitModelCache.java
License:Open Source License
private GitModelObject extractFromCache(TreeWalk tw) throws IOException { DirCacheIterator cacheIterator = tw.getTree(dirCacheIteratorNth, DirCacheIterator.class); if (cacheIterator == null) return null; DirCacheEntry cacheEntry = cacheIterator.getDirCacheEntry(); if (cacheEntry == null) return null; if (shouldIncludeEntry(tw)) { String path = new String(tw.getRawPath()); ObjectId repoId = tw.getObjectId(BASE_NTH); ObjectId cacheId = tw.getObjectId(REMOTE_NTH); if (path.split("/").length > 1) //$NON-NLS-1$ return handleCacheTree(repoId, cacheId, path); return fileFactory.createFileModel(this, baseCommit, repoId, cacheId, path); }//from w ww . j ava2s . co m return null; }
From source file:org.eclipse.orion.server.gerritfs.GerritListFile.java
License:Open Source License
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { handleAuth(req);/*from w ww .ja v a 2 s . com*/ resp.setCharacterEncoding("UTF-8"); final PrintWriter out = resp.getWriter(); try { String pathInfo = req.getPathInfo(); Pattern pattern = Pattern.compile("/([^/]*)(?:/([^/]*)(?:/(.*))?)?"); Matcher matcher = pattern.matcher(pathInfo); matcher.matches(); String projectName = null; String refName = null; String filePath = null; if (matcher.groupCount() > 0) { projectName = matcher.group(1); refName = matcher.group(2); filePath = matcher.group(3); if (projectName == null || projectName.equals("")) { projectName = null; } else { projectName = java.net.URLDecoder.decode(projectName, "UTF-8"); } if (refName == null || refName.equals("")) { refName = null; } else { refName = java.net.URLDecoder.decode(refName, "UTF-8"); } if (filePath == null || filePath.equals("")) { filePath = null; } else { filePath = java.net.URLDecoder.decode(filePath, "UTF-8"); } } if (projectName != null) { if (filePath == null) filePath = ""; NameKey projName = NameKey.parse(projectName); ProjectControl control; try { control = projControlFactory.controlFor(projName); if (!control.isVisible()) { log.debug("Project not visible!"); resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "You need to be logged in to see private projects"); return; } } catch (NoSuchProjectException e1) { } Repository repo = repoManager.openRepository(projName); if (refName == null) { ArrayList<HashMap<String, Object>> contents = new ArrayList<HashMap<String, Object>>(); List<Ref> call; try { call = new Git(repo).branchList().call(); Git git = new Git(repo); for (Ref ref : call) { HashMap<String, Object> jsonObject = new HashMap<String, Object>(); jsonObject.put("name", ref.getName()); jsonObject.put("type", "ref"); jsonObject.put("size", "0"); jsonObject.put("path", ""); jsonObject.put("project", projectName); jsonObject.put("ref", ref.getName()); lastCommit(git, null, ref.getObjectId(), jsonObject); contents.add(jsonObject); } String response = JSONUtil.write(contents); resp.setContentType("application/json"); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("ETag", "W/\"" + response.length() + "-" + response.hashCode() + "\""); log.debug(response); out.write(response); } catch (GitAPIException e) { } } else { Ref head = repo.getRef(refName); if (head == null) { ArrayList<HashMap<String, String>> contents = new ArrayList<HashMap<String, String>>(); String response = JSONUtil.write(contents); resp.setContentType("application/json"); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("ETag", "W/\"" + response.length() + "-" + response.hashCode() + "\""); log.debug(response); out.write(response); return; } RevWalk walk = new RevWalk(repo); // add try catch to catch failures Git git = new Git(repo); RevCommit commit = walk.parseCommit(head.getObjectId()); RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(repo); treeWalk.addTree(tree); treeWalk.setRecursive(false); if (!filePath.equals("")) { PathFilter pathFilter = PathFilter.create(filePath); treeWalk.setFilter(pathFilter); } if (!treeWalk.next()) { CanonicalTreeParser canonicalTreeParser = treeWalk.getTree(0, CanonicalTreeParser.class); ArrayList<HashMap<String, Object>> contents = new ArrayList<HashMap<String, Object>>(); if (canonicalTreeParser != null) { while (!canonicalTreeParser.eof()) { String path = canonicalTreeParser.getEntryPathString(); FileMode mode = canonicalTreeParser.getEntryFileMode(); listEntry(path, mode.equals(FileMode.TREE) ? "dir" : "file", "0", path, projectName, head.getName(), git, contents); canonicalTreeParser.next(); } } String response = JSONUtil.write(contents); resp.setContentType("application/json"); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("ETag", "\"" + tree.getId().getName() + "\""); log.debug(response); out.write(response); } else { // if (treeWalk.isSubtree()) { // treeWalk.enterSubtree(); // } ArrayList<HashMap<String, Object>> contents = new ArrayList<HashMap<String, Object>>(); do { if (treeWalk.isSubtree()) { String test = new String(treeWalk.getRawPath()); if (test.length() /*treeWalk.getPathLength()*/ > filePath.length()) { listEntry(treeWalk.getNameString(), "dir", "0", treeWalk.getPathString(), projectName, head.getName(), git, contents); } if (test.length() /*treeWalk.getPathLength()*/ <= filePath.length()) { treeWalk.enterSubtree(); } } else { ObjectId objId = treeWalk.getObjectId(0); ObjectLoader loader = repo.open(objId); long size = loader.getSize(); listEntry(treeWalk.getNameString(), "file", Long.toString(size), treeWalk.getPathString(), projectName, head.getName(), git, contents); } } while (treeWalk.next()); String response = JSONUtil.write(contents); resp.setContentType("application/json"); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("ETag", "\"" + tree.getId().getName() + "\""); log.debug(response); out.write(response); } walk.release(); treeWalk.release(); } } } finally { out.close(); } }
From source file:org.eclipse.orion.server.git.jobs.StashApplyCommand.java
License:Eclipse Distribution License
private void resetIndex(RevTree tree) throws IOException { DirCache dc = repo.lockDirCache();//from ww w . ja va2 s .c o m TreeWalk walk = null; try { DirCacheBuilder builder = dc.builder(); walk = new TreeWalk(repo); walk.addTree(tree); walk.addTree(new DirCacheIterator(dc)); walk.setRecursive(true); while (walk.next()) { AbstractTreeIterator cIter = walk.getTree(0, AbstractTreeIterator.class); if (cIter == null) { // Not in commit, don't add to new index continue; } final DirCacheEntry entry = new DirCacheEntry(walk.getRawPath()); entry.setFileMode(cIter.getEntryFileMode()); entry.setObjectIdFromRaw(cIter.idBuffer(), cIter.idOffset()); DirCacheIterator dcIter = walk.getTree(1, DirCacheIterator.class); if (dcIter != null && dcIter.idEqual(cIter)) { DirCacheEntry indexEntry = dcIter.getDirCacheEntry(); entry.setLastModified(indexEntry.getLastModified()); entry.setLength(indexEntry.getLength()); } builder.add(entry); } builder.commit(); } finally { dc.unlock(); if (walk != null) walk.release(); } }
From source file:org.eclipse.orion.server.git.jobs.StashApplyCommand.java
License:Eclipse Distribution License
private void resetUntracked(RevTree tree) throws CheckoutConflictException, IOException { TreeWalk walk = null; try {/*from ww w. ja va 2 s .c o m*/ walk = new TreeWalk(repo); // maybe NameConflictTreeWalk? walk.addTree(tree); walk.addTree(new FileTreeIterator(repo)); walk.setRecursive(true); final ObjectReader reader = walk.getObjectReader(); while (walk.next()) { final AbstractTreeIterator cIter = walk.getTree(0, AbstractTreeIterator.class); if (cIter == null) // Not in commit, don't create untracked continue; final DirCacheEntry entry = new DirCacheEntry(walk.getRawPath()); entry.setFileMode(cIter.getEntryFileMode()); entry.setObjectIdFromRaw(cIter.idBuffer(), cIter.idOffset()); FileTreeIterator fIter = walk.getTree(1, FileTreeIterator.class); if (fIter != null) { if (fIter.isModified(entry, true, reader)) { // file exists and is dirty throw new CheckoutConflictException(entry.getPathString()); } } checkoutPath(entry, reader); } } finally { if (walk != null) walk.release(); } }
From source file:org.gitistics.treewalk.RawPathFilter.java
License:Open Source License
@Override public boolean include(final TreeWalk walker) { for (int i = 0; i < walker.getTreeCount(); i++) { for (byte[] b : rawPaths) { if (startsWith(walker.getRawPath(), b)) { return true; }/* ww w . j a v a2 s . c o m*/ } } return false; }
From source file:org.gitistics.visitor.commit.TreeWalkVisitorStandard.java
License:Open Source License
private List<byte[]> getRawBytes(RevCommit commit) { TreeWalk walk = TreeWalkUtils.treeWalkForCommit(repository, commit); List<byte[]> rawBytes = new ArrayList<byte[]>(); try {/*from w ww. j a v a2 s . c o m*/ while (walk.next()) { rawBytes.add(walk.getRawPath()); } return rawBytes; } catch (Exception e) { throw new RuntimeException(e); } finally { walk.release(); } }
From source file:org.kuali.student.git.tools.ShowTree.java
License:Educational Community License
private static void processTreeWalk(TreeWalk tw) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { while (tw.next()) { byte[] rawPath = tw.getRawPath(); ObjectId elementObjectId = tw.getObjectId(0); String path = new String(rawPath); log.info("path = " + path + " object Id = " + elementObjectId); }/*from ww w. j a v a 2 s.c om*/ }