List of usage examples for org.eclipse.jgit.treewalk TreeWalk next
public boolean next() throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java
License:Open Source License
/** * builds a dir cache by copying entries from an existing tree, skipping * files if specified.//w w w . j av a 2s. c o m * * @param tree * the tree to copy from. * @param repository * the repository to work upon. * @param dirCacheBuilder * the dir builder instance used to add entries to the dir cache. * @param ignoredFilePaths * a list of file paths to ignore during copying. * @throws MissingObjectException * @throws IncorrectObjectTypeException * @throws CorruptObjectException * @throws IOException */ private void buildDirCacheFromTree(RevTree tree, Repository repository, DirCacheBuilder dirCacheBuilder, List<String> ignoredFilePaths) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { // TODO improve exception handling TreeWalk treeWalk = new TreeWalk(repository); int treeId = treeWalk.addTree(tree); treeWalk.setRecursive(true); while (treeWalk.next()) { String path = treeWalk.getPathString(); CanonicalTreeParser prevTreeParser = treeWalk.getTree(treeId, CanonicalTreeParser.class); if (prevTreeParser != null && !ignoredFilePaths.contains(path)) { // create a new DirCacheEntry with data from the previous commit final DirCacheEntry dcEntry = new DirCacheEntry(path); dcEntry.setObjectId(prevTreeParser.getEntryObjectId()); dcEntry.setFileMode(prevTreeParser.getEntryFileMode()); dirCacheBuilder.add(dcEntry); } } treeWalk.close(); }
From source file:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java
License:Open Source License
/** * Find the tree that contains the required identity. * * @return The ObjectId of the tree (directory) that contains the matching * identity within the supplied hierarchy. *//*from www. j a v a 2s. com*/ private ObjectId findMatchingIdentity(IdentityValidator identityValidator, ObjectId tree) throws IOException { TreeWalk treeWalk = new TreeWalk(repo.getRepository()); try { treeWalk.setRecursive(false); treeWalk.addTree(tree); while (treeWalk.next()) { if (treeWalk.isSubtree()) { ObjectId candidateId = findMatchingIdentity(identityValidator, treeWalk.getObjectId(0)); if (ObjectId.zeroId().equals(candidateId)) { // Keep searching } else { return candidateId; } } else if (identityValidator.getIdentityFileName().equals(treeWalk.getNameString())) { // Read the identity file ObjectLoader loader = repo.getRepository().open(treeWalk.getObjectId(0)); ObjectStream stream = loader.openStream(); InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); if (identityValidator.isIdentityMatched(new BufferedReader(reader))) { // We found it return tree; } } } return ObjectId.zeroId(); } finally { treeWalk.release(); } }
From source file:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java
License:Open Source License
@Override public Optional<BufferedReader> getBufferedReader(IdentityValidator identityValidator, String filename, Optional<VersionInfo> version) throws IOException { if (version.isPresent()) { Pair<String, ObjectId> diff = diffCache.get(); if (!version.get().getId().equals(diff.getKey())) { // Grab the id of the commit we are trying to diff against ObjectId id = ObjectId.fromString(version.get().getId()); RevWalk revWalk = new RevWalk(repo.getRepository()); try { // Read the commit RevCommit commit = revWalk.parseCommit(id); ObjectId matchedDirectory = findMatchingIdentity(identityValidator, commit.getTree()); diff = new Pair<>(version.get().getId(), matchedDirectory); diffCache.set(diff);//from www . j av a 2 s .co m } finally { revWalk.release(); } } if (ObjectId.zeroId().equals(diff.getValue())) { // No such tree return Optional.empty(); } else { // Find the file in this tree TreeWalk treeWalk = new TreeWalk(repo.getRepository()); try { treeWalk.setRecursive(false); treeWalk.addTree(diff.getValue()); while (treeWalk.next()) { if (filename.equals(treeWalk.getNameString())) { // Read the file ObjectLoader loader = repo.getRepository().open(treeWalk.getObjectId(0)); ObjectStream stream = loader.openStream(); InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); return Optional.of(new BufferedReader(reader)); } } // No such file return Optional.empty(); } finally { treeWalk.release(); } } } else { Path path = identityValidator.getDirectoryPath().resolve(filename); if (Files.exists(path)) { return Optional.of(Files.newBufferedReader(path)); } else { return Optional.empty(); } } }
From source file:co.bledo.gitmin.servlet.Review.java
License:Apache License
private String getFileContent(Repository repo, RevCommit commit, String path) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { // and using commit's tree find the path RevTree tree = commit.getTree();/*from w w w. ja va 2s. co m*/ TreeWalk treeWalk = new TreeWalk(repo); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) { return ""; } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repo.open(objectId); // and then one can use either //InputStream in = loader.openStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); loader.copyTo(out); return out.toString(); }
From source file:com.beck.ep.team.git.GitListBuilder.java
License:BSD License
public String[] createList(Shell shell, IProject project) throws Exception { error = null;//from www . j a v a2 s .co m RepositoryMapping mapping = RepositoryMapping.getMapping(project); if (mapping == null) { error = "Git Repository not found (project: " + project.getName() + ")"; return null; } Repository rep = mapping.getRepository(); String pjPath = project.getLocation().toFile().getAbsolutePath(); String repPath = rep.getDirectory().getParentFile().getAbsolutePath(); int cutPrefixLen = 0; int pathDiffLen = pjPath.length() - repPath.length(); if (pathDiffLen > 0) { cutPrefixLen = pathDiffLen;//for repositoy is not in "parent folder of project" } RevWalk rw = new RevWalk(rep); TreeWalk tw = new TreeWalk(rep); int ignoreOpt = pref.getInt(GitModeMenuMgr.PREF_TYPE_IGNORE, GitModeMenuMgr.IGNORE_EXCLUDE); if (ignoreOpt == GitModeMenuMgr.IGNORE_ONLY) { String[] sa = listIgnored(rep, rw, tw, cutPrefixLen); if (cutPrefixLen > 0) { for (int i = 0; i < sa.length; i++) { if (sa[i].length() > cutPrefixLen) { sa[i] = sa[i].substring(cutPrefixLen); } } } return sa; } int mode = pref.getInt(GitModeMenuMgr.PREF_TYPE_MODE, GitModeMenuMgr.MODE_BRANCH_HEAD); int sourceMode = pref.getInt(GitModeMenuMgr.PREF_TYPE_SOURCE, GitModeMenuMgr.MODE_WORKING_AREA); if (mode == sourceMode) { return new String[0]; } int index = addTree(mode, shell, rep, rw, tw); if (index == -2) { return null; } int stageIndex = -1; boolean excludeUntrackOpt = false; if (!pref.getBoolean(GitModeMenuMgr.PREF_TYPE_UNTRACK, true) && sourceMode == GitModeMenuMgr.MODE_WORKING_AREA) { excludeUntrackOpt = true; if (mode == GitModeMenuMgr.MODE_STAGING) { stageIndex = 0; } } int idx2 = addTree(sourceMode, shell, rep, rw, tw); if (excludeUntrackOpt && stageIndex == -1) { addTree(GitModeMenuMgr.MODE_STAGING, shell, rep, rw, tw); stageIndex = 2; } // skip ignored resources if (ignoreOpt == GitModeMenuMgr.IGNORE_EXCLUDE && (index != -1 || idx2 != -1)) { int pos = (index != -1) ? index : idx2; NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(pos);//index = tw.addTree(?) tw.setFilter(notIgnoredFilter); } tw.setRecursive(true); ArrayList<String> sa = new ArrayList<String>(); while (tw.next()) { if (!tw.idEqual(0, 1)) { if (excludeUntrackOpt) { if (tw.getTree(stageIndex, AbstractTreeIterator.class) == null) { WorkingTreeIterator t = tw.getTree(1, WorkingTreeIterator.class); if (t != null && !t.isEntryIgnored()) {//file is untrack... continue; } } } String relPath = tw.getPathString(); if (cutPrefixLen > 0 && relPath.length() > cutPrefixLen) { relPath = relPath.substring(cutPrefixLen); } sa.add(relPath); } } return sa.toArray(new String[sa.size()]); }
From source file:com.beck.ep.team.git.GitListBuilder.java
License:BSD License
private String[] listIgnored(Repository rep, RevWalk rw, TreeWalk tw, int cutPrefixLen) throws Exception { addTree(GitModeMenuMgr.MODE_STAGING, null, rep, rw, tw); int workingTreeIndex = addTree(GitModeMenuMgr.MODE_WORKING_AREA, null, rep, rw, tw); IndexDiffFilter filter = new IndexDiffFilter(0, workingTreeIndex, true); tw.setFilter(filter);/*from w w w.j a v a2 s . co m*/ tw.setRecursive(true); while (tw.next()) { //just loop it... } Set<String> s = filter.getIgnoredPaths(); return s.toArray(new String[s.size()]); }
From source file:com.gitblit.build.BuildGhPages.java
License:Apache License
/** * Creates an in-memory index of the issue change. * /*from ww w . j a v a 2 s . com*/ * @param repo * @param headId * @param sourceFolder * @param obliterate * if true the source folder tree is used as the new tree for * gh-pages and non-existent files are considered deleted * @return an in-memory index * @throws IOException */ private static DirCache createIndex(Repository repo, ObjectId headId, File sourceFolder, boolean obliterate) throws IOException { DirCache inCoreIndex = DirCache.newInCore(); DirCacheBuilder dcBuilder = inCoreIndex.builder(); ObjectInserter inserter = repo.newObjectInserter(); try { // Add all files to the temporary index Set<String> ignorePaths = new TreeSet<String>(); List<File> files = listFiles(sourceFolder); for (File file : files) { // create an index entry for the file final DirCacheEntry dcEntry = new DirCacheEntry( StringUtils.getRelativePath(sourceFolder.getPath(), file.getPath())); dcEntry.setLength(file.length()); dcEntry.setLastModified(file.lastModified()); dcEntry.setFileMode(FileMode.REGULAR_FILE); // add this entry to the ignore paths set ignorePaths.add(dcEntry.getPathString()); // insert object InputStream inputStream = new FileInputStream(file); try { dcEntry.setObjectId(inserter.insert(Constants.OBJ_BLOB, file.length(), inputStream)); } finally { inputStream.close(); } // add to temporary in-core index dcBuilder.add(dcEntry); } if (!obliterate) { // Traverse HEAD to add all other paths TreeWalk treeWalk = new TreeWalk(repo); int hIdx = -1; if (headId != null) hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId)); treeWalk.setRecursive(true); while (treeWalk.next()) { String path = treeWalk.getPathString(); CanonicalTreeParser hTree = null; if (hIdx != -1) hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class); if (!ignorePaths.contains(path)) { // add entries from HEAD for all other paths if (hTree != null) { // create a new DirCacheEntry with data retrieved // from // HEAD final DirCacheEntry dcEntry = new DirCacheEntry(path); dcEntry.setObjectId(hTree.getEntryObjectId()); dcEntry.setFileMode(hTree.getEntryFileMode()); // add to temporary in-core index dcBuilder.add(dcEntry); } } } // release the treewalk treeWalk.release(); } // finish temporary in-core index used for this commit dcBuilder.finish(); } finally { inserter.release(); } return inCoreIndex; }
From source file:com.gitblit.LuceneExecutor.java
License:Apache License
/** * This completely indexes the repository and will destroy any existing * index.//w w w . j av a 2s . c om * * @param repositoryName * @param repository * @return IndexResult */ public IndexResult reindex(RepositoryModel model, Repository repository) { IndexResult result = new IndexResult(); if (!deleteIndex(model.name)) { return result; } try { String[] encodings = storedSettings.getStrings(Keys.web.blobEncodings).toArray(new String[0]); FileBasedConfig config = getConfig(repository); Set<String> indexedCommits = new TreeSet<String>(); IndexWriter writer = getIndexWriter(model.name); // build a quick lookup of tags Map<String, List<String>> tags = new HashMap<String, List<String>>(); for (RefModel tag : JGitUtils.getTags(repository, false, -1)) { if (!tag.isAnnotatedTag()) { // skip non-annotated tags continue; } if (!tags.containsKey(tag.getObjectId())) { tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>()); } tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName); } ObjectReader reader = repository.newObjectReader(); // get the local branches List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1); // sort them by most recently updated Collections.sort(branches, new Comparator<RefModel>() { @Override public int compare(RefModel ref1, RefModel ref2) { return ref2.getDate().compareTo(ref1.getDate()); } }); // reorder default branch to first position RefModel defaultBranch = null; ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository); for (RefModel branch : branches) { if (branch.getObjectId().equals(defaultBranchId)) { defaultBranch = branch; break; } } branches.remove(defaultBranch); branches.add(0, defaultBranch); // walk through each branch for (RefModel branch : branches) { boolean indexBranch = false; if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH) && branch.equals(defaultBranch)) { // indexing "default" branch indexBranch = true; } else if (IssueUtils.GB_ISSUES.equals(branch)) { // skip the GB_ISSUES branch because it is indexed later // note: this is different than updateIndex indexBranch = false; } else { // normal explicit branch check indexBranch = model.indexedBranches.contains(branch.getName()); } // if this branch is not specifically indexed then skip if (!indexBranch) { continue; } String branchName = branch.getName(); RevWalk revWalk = new RevWalk(reader); RevCommit tip = revWalk.parseCommit(branch.getObjectId()); String tipId = tip.getId().getName(); String keyName = getBranchKey(branchName); config.setString(CONF_ALIAS, null, keyName, branchName); config.setString(CONF_BRANCH, null, keyName, tipId); // index the blob contents of the tree TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tip.getTree()); treeWalk.setRecursive(true); Map<String, ObjectId> paths = new TreeMap<String, ObjectId>(); while (treeWalk.next()) { // ensure path is not in a submodule if (treeWalk.getFileMode(0) != FileMode.GITLINK) { paths.put(treeWalk.getPathString(), treeWalk.getObjectId(0)); } } ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] tmp = new byte[32767]; RevWalk commitWalk = new RevWalk(reader); commitWalk.markStart(tip); RevCommit commit; while ((paths.size() > 0) && (commit = commitWalk.next()) != null) { TreeWalk diffWalk = new TreeWalk(reader); int parentCount = commit.getParentCount(); switch (parentCount) { case 0: diffWalk.addTree(new EmptyTreeIterator()); break; case 1: diffWalk.addTree(getTree(commitWalk, commit.getParent(0))); break; default: // skip merge commits continue; } diffWalk.addTree(getTree(commitWalk, commit)); diffWalk.setFilter(ANY_DIFF); diffWalk.setRecursive(true); while ((paths.size() > 0) && diffWalk.next()) { String path = diffWalk.getPathString(); if (!paths.containsKey(path)) { continue; } // remove path from set ObjectId blobId = paths.remove(path); result.blobCount++; // index the blob metadata String blobAuthor = getAuthor(commit); String blobCommitter = getCommitter(commit); String blobDate = DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE); Document doc = new Document(); doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.blob.name(), Store.YES, Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field(FIELD_BRANCH, branchName, Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_COMMIT, commit.getName(), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_PATH, path, Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_DATE, blobDate, Store.YES, Index.NO)); doc.add(new Field(FIELD_AUTHOR, blobAuthor, Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_COMMITTER, blobCommitter, Store.YES, Index.ANALYZED)); // determine extension to compare to the extension // blacklist String ext = null; String name = path.toLowerCase(); if (name.indexOf('.') > -1) { ext = name.substring(name.lastIndexOf('.') + 1); } // index the blob content if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) { ObjectLoader ldr = repository.open(blobId, Constants.OBJ_BLOB); InputStream in = ldr.openStream(); int n; while ((n = in.read(tmp)) > 0) { os.write(tmp, 0, n); } in.close(); byte[] content = os.toByteArray(); String str = StringUtils.decodeString(content, encodings); doc.add(new Field(FIELD_CONTENT, str, Store.YES, Index.ANALYZED)); os.reset(); } // add the blob to the index writer.addDocument(doc); } } os.close(); // index the tip commit object if (indexedCommits.add(tipId)) { Document doc = createDocument(tip, tags.get(tipId)); doc.add(new Field(FIELD_BRANCH, branchName, Store.YES, Index.ANALYZED)); writer.addDocument(doc); result.commitCount += 1; result.branchCount += 1; } // traverse the log and index the previous commit objects RevWalk historyWalk = new RevWalk(reader); historyWalk.markStart(historyWalk.parseCommit(tip.getId())); RevCommit rev; while ((rev = historyWalk.next()) != null) { String hash = rev.getId().getName(); if (indexedCommits.add(hash)) { Document doc = createDocument(rev, tags.get(hash)); doc.add(new Field(FIELD_BRANCH, branchName, Store.YES, Index.ANALYZED)); writer.addDocument(doc); result.commitCount += 1; } } } // finished reader.release(); // this repository has a gb-issues branch, index all issues if (IssueUtils.getIssuesBranch(repository) != null) { List<IssueModel> issues = IssueUtils.getIssues(repository, null); if (issues.size() > 0) { result.branchCount += 1; } for (IssueModel issue : issues) { result.issueCount++; Document doc = createDocument(issue); writer.addDocument(doc); } } // commit all changes and reset the searcher config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION); config.save(); writer.commit(); resetIndexSearcher(model.name); result.success(); } catch (Exception e) { logger.error("Exception while reindexing " + model.name, e); } return result; }
From source file:com.gitblit.service.LuceneService.java
License:Apache License
/** * This completely indexes the repository and will destroy any existing * index.// w ww. jav a 2s.c om * * @param repositoryName * @param repository * @return IndexResult */ public IndexResult reindex(RepositoryModel model, Repository repository) { IndexResult result = new IndexResult(); if (!deleteIndex(model.name)) { return result; } try { String[] encodings = storedSettings.getStrings(Keys.web.blobEncodings).toArray(new String[0]); FileBasedConfig config = getConfig(repository); Set<String> indexedCommits = new TreeSet<String>(); IndexWriter writer = getIndexWriter(model.name); // build a quick lookup of tags Map<String, List<String>> tags = new HashMap<String, List<String>>(); for (RefModel tag : JGitUtils.getTags(repository, false, -1)) { if (!tag.isAnnotatedTag()) { // skip non-annotated tags continue; } if (!tags.containsKey(tag.getReferencedObjectId().getName())) { tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>()); } tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName); } ObjectReader reader = repository.newObjectReader(); // get the local branches List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1); // sort them by most recently updated Collections.sort(branches, new Comparator<RefModel>() { @Override public int compare(RefModel ref1, RefModel ref2) { return ref2.getDate().compareTo(ref1.getDate()); } }); // reorder default branch to first position RefModel defaultBranch = null; ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository); for (RefModel branch : branches) { if (branch.getObjectId().equals(defaultBranchId)) { defaultBranch = branch; break; } } branches.remove(defaultBranch); branches.add(0, defaultBranch); // walk through each branch for (RefModel branch : branches) { boolean indexBranch = false; if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH) && branch.equals(defaultBranch)) { // indexing "default" branch indexBranch = true; } else if (branch.getName().startsWith(com.gitblit.Constants.R_META)) { // skip internal meta branches indexBranch = false; } else { // normal explicit branch check indexBranch = model.indexedBranches.contains(branch.getName()); } // if this branch is not specifically indexed then skip if (!indexBranch) { continue; } String branchName = branch.getName(); RevWalk revWalk = new RevWalk(reader); RevCommit tip = revWalk.parseCommit(branch.getObjectId()); String tipId = tip.getId().getName(); String keyName = getBranchKey(branchName); config.setString(CONF_ALIAS, null, keyName, branchName); config.setString(CONF_BRANCH, null, keyName, tipId); // index the blob contents of the tree TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tip.getTree()); treeWalk.setRecursive(true); Map<String, ObjectId> paths = new TreeMap<String, ObjectId>(); while (treeWalk.next()) { // ensure path is not in a submodule if (treeWalk.getFileMode(0) != FileMode.GITLINK) { paths.put(treeWalk.getPathString(), treeWalk.getObjectId(0)); } } ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] tmp = new byte[32767]; RevWalk commitWalk = new RevWalk(reader); commitWalk.markStart(tip); RevCommit commit; while ((paths.size() > 0) && (commit = commitWalk.next()) != null) { TreeWalk diffWalk = new TreeWalk(reader); int parentCount = commit.getParentCount(); switch (parentCount) { case 0: diffWalk.addTree(new EmptyTreeIterator()); break; case 1: diffWalk.addTree(getTree(commitWalk, commit.getParent(0))); break; default: // skip merge commits continue; } diffWalk.addTree(getTree(commitWalk, commit)); diffWalk.setFilter(ANY_DIFF); diffWalk.setRecursive(true); while ((paths.size() > 0) && diffWalk.next()) { String path = diffWalk.getPathString(); if (!paths.containsKey(path)) { continue; } // remove path from set ObjectId blobId = paths.remove(path); result.blobCount++; // index the blob metadata String blobAuthor = getAuthor(commit); String blobCommitter = getCommitter(commit); String blobDate = DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE); Document doc = new Document(); doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.blob.name(), StringField.TYPE_STORED)); doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED)); doc.add(new Field(FIELD_COMMIT, commit.getName(), TextField.TYPE_STORED)); doc.add(new Field(FIELD_PATH, path, TextField.TYPE_STORED)); doc.add(new Field(FIELD_DATE, blobDate, StringField.TYPE_STORED)); doc.add(new Field(FIELD_AUTHOR, blobAuthor, TextField.TYPE_STORED)); doc.add(new Field(FIELD_COMMITTER, blobCommitter, TextField.TYPE_STORED)); // determine extension to compare to the extension // blacklist String ext = null; String name = path.toLowerCase(); if (name.indexOf('.') > -1) { ext = name.substring(name.lastIndexOf('.') + 1); } // index the blob content if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) { ObjectLoader ldr = repository.open(blobId, Constants.OBJ_BLOB); InputStream in = ldr.openStream(); int n; while ((n = in.read(tmp)) > 0) { os.write(tmp, 0, n); } in.close(); byte[] content = os.toByteArray(); String str = StringUtils.decodeString(content, encodings); doc.add(new Field(FIELD_CONTENT, str, TextField.TYPE_STORED)); os.reset(); } // add the blob to the index writer.addDocument(doc); } } os.close(); // index the tip commit object if (indexedCommits.add(tipId)) { Document doc = createDocument(tip, tags.get(tipId)); doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED)); writer.addDocument(doc); result.commitCount += 1; result.branchCount += 1; } // traverse the log and index the previous commit objects RevWalk historyWalk = new RevWalk(reader); historyWalk.markStart(historyWalk.parseCommit(tip.getId())); RevCommit rev; while ((rev = historyWalk.next()) != null) { String hash = rev.getId().getName(); if (indexedCommits.add(hash)) { Document doc = createDocument(rev, tags.get(hash)); doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED)); writer.addDocument(doc); result.commitCount += 1; } } } // finished reader.close(); // commit all changes and reset the searcher config.save(); writer.commit(); resetIndexSearcher(model.name); result.success(); } catch (Exception e) { logger.error("Exception while reindexing " + model.name, e); } return result; }
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 {// w w w.java 2 s . 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; }