List of usage examples for org.eclipse.jgit.treewalk WorkingTreeOptions KEY
Config.SectionParser KEY
To view the source code for org.eclipse.jgit.treewalk WorkingTreeOptions KEY.
Click Source Link
From source file:com.mangosolutions.rcloud.rawgist.repository.git.BareCommitCommand.java
private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw) throws IOException { ObjectInserter inserter = null;//from www . jav a 2 s. co m // get DirCacheBuilder for existing index DirCacheBuilder existingBuilder = index.builder(); // get DirCacheBuilder for newly created in-core index to build a // temporary index for this commit DirCache inCoreIndex = DirCache.newInCore(); DirCacheBuilder tempBuilder = inCoreIndex.builder(); onlyProcessed = new boolean[only.size()]; boolean emptyCommit = true; try (TreeWalk treeWalk = new TreeWalk(repo)) { treeWalk.setOperationType(OperationType.CHECKIN_OP); int dcIdx = treeWalk.addTree(new DirCacheBuildIterator(existingBuilder)); FileModeStrategy fileModeStrategy = this.getRepository().getConfig().get(WorkingTreeOptions.KEY) .isDirNoGitLinks() ? NoGitlinksStrategy.INSTANCE : DefaultFileModeStrategy.INSTANCE; FileTreeIterator fti = new FileTreeIterator(this.workingFolder, this.getRepository().getFS(), this.getRepository().getConfig().get(WorkingTreeOptions.KEY), fileModeStrategy); fti.setDirCacheIterator(treeWalk, 0); int fIdx = treeWalk.addTree(fti); int hIdx = -1; if (headId != null) { hIdx = treeWalk.addTree(rw.parseTree(headId)); } treeWalk.setRecursive(true); String lastAddedFile = null; while (treeWalk.next()) { String path = treeWalk.getPathString(); // check if current entry's path matches a specified path int pos = lookupOnly(path); CanonicalTreeParser hTree = null; if (hIdx != -1) { hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class); } DirCacheIterator dcTree = treeWalk.getTree(dcIdx, DirCacheIterator.class); if (pos >= 0) { // include entry in commit FileTreeIterator fTree = treeWalk.getTree(fIdx, FileTreeIterator.class); // check if entry refers to a tracked file boolean tracked = dcTree != null || hTree != null; if (!tracked) { continue; } // for an unmerged path, DirCacheBuildIterator will yield 3 // entries, we only want to add one if (path.equals(lastAddedFile)) { continue; } lastAddedFile = path; if (fTree != null) { // create a new DirCacheEntry with data retrieved from // disk final DirCacheEntry dcEntry = new DirCacheEntry(path); long entryLength = fTree.getEntryLength(); dcEntry.setLength(entryLength); dcEntry.setLastModified(fTree.getEntryLastModified()); dcEntry.setFileMode(fTree.getIndexFileMode(dcTree)); boolean objectExists = (dcTree != null && fTree.idEqual(dcTree)) || (hTree != null && fTree.idEqual(hTree)); if (objectExists) { dcEntry.setObjectId(fTree.getEntryObjectId()); } else { if (FileMode.GITLINK.equals(dcEntry.getFileMode())) { dcEntry.setObjectId(fTree.getEntryObjectId()); } else { // insert object if (inserter == null) { inserter = repo.newObjectInserter(); } long contentLength = fTree.getEntryContentLength(); InputStream inputStream = fTree.openEntryStream(); try { dcEntry.setObjectId( inserter.insert(Constants.OBJ_BLOB, contentLength, inputStream)); } finally { inputStream.close(); } } } // add to existing index existingBuilder.add(dcEntry); // add to temporary in-core index tempBuilder.add(dcEntry); if (emptyCommit && (hTree == null || !hTree.idEqual(fTree) || hTree.getEntryRawMode() != fTree.getEntryRawMode())) { // this is a change emptyCommit = false; } } else { // if no file exists on disk, neither add it to // index nor to temporary in-core index if (emptyCommit && hTree != null) { // this is a change emptyCommit = false; } } // keep track of processed path onlyProcessed[pos] = true; } else { // 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 tempBuilder.add(dcEntry); } // preserve existing entry in index if (dcTree != null) { existingBuilder.add(dcTree.getDirCacheEntry()); } } } } // there must be no unprocessed paths left at this point; otherwise an // untracked or unknown path has been specified for (int i = 0; i < onlyProcessed.length; i++) { if (!onlyProcessed[i]) { throw new JGitInternalException( MessageFormat.format(JGitText.get().entryNotFoundByPath, only.get(i))); } } // there must be at least one change if (emptyCommit) { // Would like to throw a EmptyCommitException. But this would break the API // TODO(ch): Change this in the next release // throw new JGitInternalException(JGitText.get().emptyCommit); } // update index existingBuilder.commit(); // finish temporary in-core index used for this commit tempBuilder.finish(); return inCoreIndex; }
From source file:com.mangosolutions.rcloud.rawgist.repository.git.CreateOrUpdateGistOperation.java
private FileTreeIterator getFileTreeIterator(Git jgit, File workingFolder) { FileModeStrategy fileModeStrategy = jgit.getRepository().getConfig().get(WorkingTreeOptions.KEY) .isDirNoGitLinks() ? NoGitlinksStrategy.INSTANCE : DefaultFileModeStrategy.INSTANCE; FileTreeIterator iterator = new FileTreeIterator(workingFolder, jgit.getRepository().getFS(), jgit.getRepository().getConfig().get(WorkingTreeOptions.KEY), fileModeStrategy); return iterator; }
From source file:org.eclipse.egit.core.AdaptableFileTreeIterator.java
License:Open Source License
/** * Create a new iterator to traverse the work tree of the given repository * <p>/*from w w w .ja va2 s .c om*/ * The iterator will automatically adapt to a {@link ContainerTreeIterator} * when encountering directories what can be mapped into the given workspace * root. * * @param repository * the repository this iterator should traverse the working tree * of * @param workspaceRoot * the workspace root to check resource mapping against. */ public AdaptableFileTreeIterator(final Repository repository, final IWorkspaceRoot workspaceRoot) { super(repository.getWorkTree(), FS.DETECTED, repository.getConfig().get(WorkingTreeOptions.KEY)); root = workspaceRoot; }
From source file:org.eclipse.egit.core.ContainerTreeIterator.java
License:Open Source License
/** * Construct a new iterator from a container in the workspace. * <p>/* w ww . ja v a 2s .com*/ * The iterator will support traversal over the named container, but only if * it is contained within a project which has the Git repository provider * connected and this resource is mapped into a Git repository. During the * iteration the paths will be automatically generated to match the proper * repository paths for this container's children. * * @param repository * repository the given base is mapped to * @param base * the part of the workspace the iterator will walk over. */ public ContainerTreeIterator(final Repository repository, final IContainer base) { super(computePrefix(base), repository.getConfig().get(WorkingTreeOptions.KEY)); node = base; init(entries()); }
From source file:org.eclipse.egit.core.ContainerTreeIterator.java
License:Open Source License
/** * Construct a new iterator from the workspace root. * <p>/* w ww. j a va 2s . com*/ * The iterator will support traversal over workspace projects that have * a Git repository provider connected and is mapped into a Git repository. * During the iteration the paths will be automatically generated to match * the proper repository paths for this container's children. * * @param repository * repository the given base is mapped to * @param root * the workspace root to walk over. */ public ContainerTreeIterator(final Repository repository, final IWorkspaceRoot root) { super("", repository.getConfig().get(WorkingTreeOptions.KEY)); //$NON-NLS-1$ node = root; init(entries()); }
From source file:org.eclipse.egit.core.storage.GitBlobStorage.java
License:Open Source License
private InputStream open() throws IOException, CoreException, IncorrectObjectTypeException { if (blobId == null) return new ByteArrayInputStream(new byte[0]); try {/*from w ww . j av a 2s . com*/ WorkingTreeOptions workingTreeOptions = db.getConfig().get(WorkingTreeOptions.KEY); final InputStream objectInputStream = db.open(blobId, Constants.OBJ_BLOB).openStream(); switch (workingTreeOptions.getAutoCRLF()) { case INPUT: // When autocrlf == input the working tree could be either CRLF or LF, i.e. the comparison // itself should ignore line endings. case FALSE: return objectInputStream; case TRUE: default: return new AutoCRLFInputStream(objectInputStream, true); } } catch (MissingObjectException notFound) { throw new CoreException( Activator.error(NLS.bind(CoreText.BlobStorage_blobNotFound, blobId.name(), path), notFound)); } }
From source file:org.ms123.common.git.GitServiceImpl.java
License:Open Source License
public Map getWorkingTree(@PName("name") String repoName, @PName("path") @POptional String path, @PName("depth") @POptional @PDefaultInt(100) Integer depth, @PName("includeTypeList") @POptional List<String> includeTypeList, @PName("includePathList") @POptional List<String> includePathList, @PName("excludePathList") @POptional List<String> excludePathList, @PName("mapping") @POptional Map mapping) throws RpcException { try {//from ww w . j a v a 2s .c o m if (depth == null) depth = 100; String gitSpace = System.getProperty("git.repos"); File gitDir = new File(gitSpace, repoName + "/.git"); if (!gitDir.exists()) { throw new RpcException(ERROR_FROM_METHOD, 100, "GitService.getWorkingTree:Repo(" + repoName + ") not exists"); } File repoDir = new File(gitSpace, repoName); Git gitObject = Git.open(new File(gitSpace, repoName)); TreeWalk treeWalk = new TreeWalk(gitObject.getRepository()); FileTreeIterator newTree = null; String rootPath = "root"; String type = "sw.project"; File basePath = repoDir; String title = repoName; if (path == null) { newTree = new FileTreeIterator(gitObject.getRepository()); } else { File f = new File(gitObject.getRepository().getDirectory().getParentFile(), path); debug("f:" + f); newTree = new FileTreeIterator(f, FS.detect(), gitObject.getRepository().getConfig().get(WorkingTreeOptions.KEY)); rootPath = path; type = "sw.directory"; title += "/" + path; basePath = new File(basePath, path); } treeWalk.addTree(newTree); treeWalk.setRecursive(true); Collection<TreeFilter> filterList = new HashSet(); TreeFilter pathFilter = PathPatternFilter.create("^[a-zA-Z].*$", includePathList, excludePathList, 0); filterList.add(pathFilter); if (includeTypeList != null && includeTypeList.size() > 0) { filterList.add(TypeFilter.create(basePath, includeTypeList)); } if (filterList.size() > 1) { TreeFilter andFilter = AndTreeFilter.create(filterList); treeWalk.setFilter(andFilter); } else { treeWalk.setFilter(pathFilter); } treeWalk.setPostOrderTraversal(true); Map<String, Map> parentMap = new HashMap(); Map root = new HashMap(); root.put("path", rootPath); root.put("title", repoName); root.put("value", rootPath); root.put("type", type); root.put("children", new ArrayList()); parentMap.put("root", root); while (true) { if (!treeWalk.next()) { break; } String pathString = new String(treeWalk.getRawPath()); // String pathString = treeWalk.getPathString(); Node[] nodes = getNodes(pathString); for (int i = 0; i < nodes.length && i < depth; i++) { if (parentMap.get(nodes[i].path) == null) { Map map = getNodeMap(nodes[i], i < (nodes.length - 1), basePath, mapping); map.put("children", new ArrayList()); parentMap.put(nodes[i].path, map); Map pmap = parentMap.get(nodes[i].parent); if (pmap != null) { List<Map> children = (List) pmap.get("children"); children.add(map); } } } } // m_js.prettyPrint(true); // String ser = m_js.deepSerialize(parentMap.get("root")); // debug("Tree" + ser); return parentMap.get("root"); } catch (Exception e) { if (e instanceof RpcException) throw (RpcException) e; throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.getWorkingTree:", e); } finally { } }