Java Code Examples for org.eclipse.jgit.dircache.DirCache#getEntry()

The following examples show how to use org.eclipse.jgit.dircache.DirCache#getEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResolveMerger.java    From onedev with MIT License 6 votes vote down vote up
/**
 * Reverts the worktree after an unsuccessful merge. We know that for all
 * modified files the old content was in the old index and the index
 * contained only stage 0. In case if inCore operation just clear the
 * history of modified files.
 *
 * @throws java.io.IOException
 * @throws org.eclipse.jgit.errors.CorruptObjectException
 * @throws org.eclipse.jgit.errors.NoWorkTreeException
 * @since 3.4
 */
protected void cleanUp() throws NoWorkTreeException,
		CorruptObjectException,
		IOException {
	if (inCore) {
		modifiedFiles.clear();
		return;
	}

	DirCache dc = nonNullRepo().readDirCache();
	Iterator<String> mpathsIt=modifiedFiles.iterator();
	while(mpathsIt.hasNext()) {
		String mpath = mpathsIt.next();
		DirCacheEntry entry = dc.getEntry(mpath);
		if (entry != null) {
			DirCacheCheckout.checkoutEntry(db, entry, reader, false,
					checkoutMetadata.get(mpath));
		}
		mpathsIt.remove();
	}
}
 
Example 2
Source File: CleanCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void deleteIfUnversioned(DirCache cache, String path, WorkingTreeIterator f, Repository repository, TreeWalk treeWalk) throws IOException, NoWorkTreeException {
    if (cache.getEntry(path) == null &&  // not in index 
        !f.isEntryIgnored() &&             // not ignored
        !Utils.isFromNested(f.getEntryFileMode().getBits()))
    {            
        File file = new File(repository.getWorkTree().getAbsolutePath() + File.separator + path);                        
        if(file.isDirectory()) {
            String[] s = file.list();
            if(s != null && s.length > 0) { // XXX is there no better way to find out if empty?
                // not empty
                return; 
            }
        }
        file.delete();
        listener.notifyFile(file, treeWalk.getPathString());
    }
}
 
Example 3
Source File: AbstractGitTestCase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected static void assertDirCacheEntry (Repository repository, File workDir, Collection<File> files) throws IOException {
    DirCache cache = repository.lockDirCache();
    for (File f : files) {
        String relativePath = Utils.getRelativePath(workDir, f);
        DirCacheEntry e = cache.getEntry(relativePath);
        assertNotNull(e);
        assertEquals(relativePath, e.getPathString());
        if (f.lastModified() != e.getLastModified()) {
            assertEquals((f.lastModified() / 1000) * 1000, (e.getLastModified() / 1000) * 1000);
        }
        try (InputStream in = new FileInputStream(f)) {
            assertEquals(e.getObjectId(), repository.newObjectInserter().idFor(Constants.OBJ_BLOB, f.length(), in));
        }
        if (e.getLength() == 0 && f.length() != 0) {
            assertTrue(e.isSmudged());
        } else {
            assertEquals(f.length(), e.getLength());
        }
    }
    cache.unlock();
}
 
Example 4
Source File: GitManagerImpl.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private DirCacheEntry[] findEntrys(Repository repository, String path) throws IOException {
    DirCache dirCache = repository.readDirCache();

    int eIdx = dirCache.findEntry(path);
    if (eIdx < 0) {
        throw new GitInvalidPathException(format("%s is not found in git index", path));
    }

    int lastIdx = dirCache.nextEntry(eIdx);

    final DirCacheEntry[] entries = new DirCacheEntry[lastIdx - eIdx];
    for (int i=0; i<entries.length; i++) {
        entries[i] = dirCache.getEntry(eIdx + i);
    }

    return entries;
}
 
Example 5
Source File: CleanTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertNullDirCacheEntry (Collection<File> files) throws Exception {
    DirCache cache = repository.lockDirCache();
    for (File f : files) {
        DirCacheEntry e = cache.getEntry(Utils.getRelativePath(workDir, f));
        assertNull(e);
    }
    cache.unlock();
}
 
Example 6
Source File: CheckoutTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLargeFile () throws Exception {
    unpack("large.dat.zip");
    File large = new File(workDir, "large.dat");
    assertTrue(large.exists());
    assertEquals(2158310, large.length());
    add();
    DirCache cache = repository.readDirCache();
    DirCacheEntry e = cache.getEntry("large.dat");
    WindowCacheConfig cfg = new WindowCacheConfig();
    cfg.setStreamFileThreshold((int) large.length() - 1);
    cfg.install();
    DirCacheCheckout.checkoutEntry(repository, e, repository.newObjectReader());
}
 
Example 7
Source File: ResetTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testResetConflict () throws Exception {
    File file = new File(workDir, "file");
    write(file, "init");
    File[] files = new File[] { file };
    add(files);
    commit(files);

    DirCache index = repository.lockDirCache();
    DirCacheBuilder builder = index.builder();
    DirCacheEntry e = index.getEntry(file.getName());
    DirCacheEntry e1 = new DirCacheEntry(file.getName(), 1);
    e1.setCreationTime(e.getCreationTime());
    e1.setFileMode(e.getFileMode());
    e1.setLastModified(e.getLastModified());
    e1.setLength(e.getLength());
    e1.setObjectId(e.getObjectId());
    builder.add(e1);
    builder.finish();
    builder.commit();
    
    GitClient client = getClient(workDir);
    Map<File, GitStatus> status = client.getStatus(files, NULL_PROGRESS_MONITOR);
    assertTrue(status.get(file).isConflict());
    assertEquals(GitConflictDescriptor.Type.BOTH_DELETED, status.get(file).getConflictDescriptor().getType());
    
    client.reset(files, "HEAD", true, NULL_PROGRESS_MONITOR);
    status = client.getStatus(files, NULL_PROGRESS_MONITOR);
    assertFalse(status.get(file).isConflict());
}
 
Example 8
Source File: AddTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertDirCacheEntryModified (Collection<File> files) throws IOException {
    DirCache cache = repository.lockDirCache();
    for (File f : files) {
        String relativePath = Utils.getRelativePath(workDir, f);
        DirCacheEntry e = cache.getEntry(relativePath);
        assertNotNull(e);
        assertEquals(relativePath, e.getPathString());
        try (InputStream in = new FileInputStream(f)) {
            assertNotSame(e.getObjectId(), repository.newObjectInserter().idFor(Constants.OBJ_BLOB, f.length(), in));
        }
    }
    cache.unlock();
}
 
Example 9
Source File: AddTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertNullDirCacheEntry (Collection<File> files) throws Exception {
    DirCache cache = repository.lockDirCache();
    for (File f : files) {
        DirCacheEntry e = cache.getEntry(Utils.getRelativePath(workDir, f));
        assertNull(e);
    }
    cache.unlock();
}
 
Example 10
Source File: Index.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public ObjectStream toObjectStream() throws IOException {
	DirCache cache = db.readDirCache();
	DirCacheEntry entry = cache.getEntry(pattern);
	if (entry == null) {
		return null;
	}
	try {
		ObjectId blobId = entry.getObjectId();
		return db.open(blobId, Constants.OBJ_BLOB).openStream();
	} catch (MissingObjectException e) {
		return null;
	}
}
 
Example 11
Source File: GitCloneHandlerV1.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private DirCacheEntry getEntry(Repository db, String path) throws IOException {
	DirCache dc = DirCache.read(db.getIndexFile(), db.getFS());
	return dc.getEntry(path);
}
 
Example 12
Source File: Status.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private JSONArray toJSONArray(Set<String> set, IPath basePath, URI baseLocation, String diffType) throws JSONException, URISyntaxException {
	JSONArray result = new JSONArray();
	DirCache cache = null;
	try{
		cache = db.readDirCache();
	}catch(Exception ex){
		
	}
	for (String s : set) {
		JSONObject object = new JSONObject();
		boolean isSubmodule = false, isDirectory = false;
		if(cache!=null){
			DirCacheEntry entry = cache.getEntry(s);
			int fileMode=-1;
			if(entry!=null){
				fileMode = entry.getRawMode();
			}
			//isSubmodule = this.submoduleStatuses.keySet().contains(s);
			isSubmodule = fileMode!=-1 && fileMode== FileMode.TYPE_GITLINK;
			isDirectory = fileMode!= -1 && (fileMode & FileMode.TYPE_TREE) != 0;
			
		}

		object.put(ProtocolConstants.KEY_NAME, s);
		IPath relative = new Path(s).makeRelativeTo(basePath);
		object.put(ProtocolConstants.KEY_PATH, relative);
		URI fileLocation = statusToFileLocation(baseLocation);
		object.put(ProtocolConstants.KEY_LOCATION, URIUtil.append(fileLocation, relative.toString()));
		if(isDirectory){
			object.put(ProtocolConstants.KEY_DIRECTORY, true);
		}
		if(isSubmodule){
			object.put(GitConstants.KEY_IS_SUBMODULE, true);
		}

		JSONObject gitSection = new JSONObject();
		URI diffLocation = statusToDiffLocation(baseLocation, diffType);
		gitSection.put(GitConstants.KEY_DIFF, URIUtil.append(diffLocation, relative.toString()));
		object.put(GitConstants.KEY_GIT, gitSection);

		URI commitLocation = statusToCommitLocation(baseLocation, Constants.HEAD);
		gitSection.put(GitConstants.KEY_COMMIT, URIUtil.append(commitLocation, relative.toString()));
		object.put(GitConstants.KEY_GIT, gitSection);

		URI indexLocation = statusToIndexLocation(baseLocation);
		gitSection.put(GitConstants.KEY_INDEX, URIUtil.append(indexLocation, relative.toString()));
		object.put(GitConstants.KEY_GIT, gitSection);

		result.put(object);
	}
	return result;
}