org.eclipse.jgit.lib.ObjectDatabase Java Examples

The following examples show how to use org.eclipse.jgit.lib.ObjectDatabase. 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: PullRequest.java    From onedev with MIT License 6 votes vote down vote up
public boolean isValid() {
	if (valid == null) {
		Repository repository = targetProject.getRepository();
		ObjectDatabase objDb = repository.getObjectDatabase();
		try {
			if (!objDb.has(ObjectId.fromString(baseCommitHash))) {
				valid = false;
			} else {
				for (PullRequestUpdate update: updates) {
					if (!objDb.has(ObjectId.fromString(update.getTargetHeadCommitHash()))
							|| !objDb.has(ObjectId.fromString(update.getHeadCommitHash()))) {
						valid = false;
						break;
					}
				}
				if (valid == null)
					valid = true;
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	} 
	return valid;
}
 
Example #2
Source File: CheckoutRevisionCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void mergeConflicts (List<String> conflicts, DirCache cache) throws GitException {
    DirCacheBuilder builder = cache.builder();
    DirCacheBuildIterator dci = new DirCacheBuildIterator(builder);
    ObjectDatabase od = null;
    DiffAlgorithm.SupportedAlgorithm diffAlg = getRepository().getConfig().getEnum(
                    ConfigConstants.CONFIG_DIFF_SECTION, null,
                    ConfigConstants.CONFIG_KEY_ALGORITHM,
                    DiffAlgorithm.SupportedAlgorithm.HISTOGRAM);
    MergeAlgorithm merger = new MergeAlgorithm(DiffAlgorithm.getAlgorithm(diffAlg));
    try (TreeWalk walk = new TreeWalk(getRepository());) {
        od = getRepository().getObjectDatabase();
        walk.addTree(dci);
        walk.setFilter(PathFilterGroup.create(Utils.getPathFilters(conflicts)));
        String lastPath = null;
        DirCacheEntry[] entries = new DirCacheEntry[3];
        walk.setRecursive(true);
        while (walk.next()) {
            DirCacheEntry e = walk.getTree(0, DirCacheIterator.class).getDirCacheEntry();
            String path = e.getPathString();
            if (lastPath != null && !lastPath.equals(path)) {
                resolveEntries(merger, lastPath, entries, od, builder);
            }
            if (e.getStage() == 0) {
                DirCacheIterator c = walk.getTree(0, DirCacheIterator.class);
                builder.add(c.getDirCacheEntry());
            } else {
                entries[e.getStage() - 1] = e;
                lastPath = path;
            }
        }
        resolveEntries(merger, lastPath, entries, od, builder);
        builder.commit();
    } catch (IOException ex) {
        throw new GitException(ex);
    } finally {
        if (od != null) {
            od.close();
        }
    }
}
 
Example #3
Source File: WorkerTest.java    From github-bucket with ISC License 5 votes vote down vote up
public WorkerTest() {
    URIish fetchUrl = new URIish();
    SyncableRepository pushRepository = mock(SyncableRepository.class);

    when(config.getFetchUrl()).thenReturn(fetchUrl);
    when(config.getPushRepository()).thenReturn(pushRepository);

    ObjectDatabase database = mock(ObjectDatabase.class);
    when(database.exists()).thenReturn(false);
    Repository repository = mock(Repository.class);
    when(repository.getObjectDatabase()).thenReturn(database);
    when(config.getWorkingFileRepository()).thenReturn(repository);

    uut = new Worker(config);
}
 
Example #4
Source File: WorkerTest.java    From github-bucket with ISC License 5 votes vote down vote up
public WorkerTest() {
    URIish fetchUrl = new URIish();
    SyncableRepository pushRepository = mock(SyncableRepository.class);

    when(config.getFetchUrl()).thenReturn(fetchUrl);
    when(config.getPushRepository()).thenReturn(pushRepository);

    ObjectDatabase database = mock(ObjectDatabase.class);
    when(database.exists()).thenReturn(false);
    Repository repository = mock(Repository.class);
    when(repository.getObjectDatabase()).thenReturn(database);
    when(config.getWorkingFileRepository()).thenReturn(repository);

    uut = new Worker(config);
}
 
Example #5
Source File: Utils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static RawText getRawText (ObjectId id, ObjectDatabase db) throws IOException {
    if (id.equals(ObjectId.zeroId())) {
        return RawText.EMPTY_TEXT;
    }
    return new RawText(db.open(id, Constants.OBJ_BLOB).getCachedBytes());
}
 
Example #6
Source File: GitIndexEntry.java    From git-code-format-maven-plugin with MIT License 4 votes vote down vote up
private void doFormat(DirCacheEntry dirCacheEntry, CodeFormatter formatter) {
  LineRanges lineRanges = computeLineRanges(dirCacheEntry);
  if (lineRanges.isAll()) {
    log.info("Formatting '" + dirCacheEntry.getPathString() + "'");
  } else {
    log.info("Formatting lines " + lineRanges + " of '" + dirCacheEntry.getPathString() + "'");
  }

  try (TemporaryFile temporaryFormattedFile =
      TemporaryFile.create(log, dirCacheEntry.getPathString() + ".formatted")) {
    ObjectId unformattedObjectId = dirCacheEntry.getObjectId();
    log.debug("Unformatted object id is '" + unformattedObjectId + "'");
    ObjectDatabase objectDatabase = repository.getObjectDatabase();
    ObjectLoader objectLoader = objectDatabase.open(unformattedObjectId);

    logObjectContent(objectLoader, dirCacheEntry.getPathString() + ".unformatted");

    try (InputStream content = objectLoader.openStream();
        OutputStream formattedContent = temporaryFormattedFile.newOutputStream()) {
      formatter.format(content, lineRanges, formattedContent);
    }

    long formattedSize = temporaryFormattedFile.size();
    ObjectId formattedObjectId;
    try (InputStream formattedContent = temporaryFormattedFile.newInputStream();
        ObjectInserter objectInserter = objectDatabase.newInserter()) {
      formattedObjectId = objectInserter.insert(OBJ_BLOB, formattedSize, formattedContent);
      objectInserter.flush();
    }

    log.debug("Formatted size is " + formattedSize);
    dirCacheEntry.setLength(formattedSize);
    log.debug("Formatted object id is '" + formattedObjectId + "'");
    dirCacheEntry.setObjectId(formattedObjectId);
  } catch (IOException e) {
    throw new MavenGitCodeFormatException(e);
  }

  if (lineRanges.isAll()) {
    log.info("Formatted '" + dirCacheEntry.getPathString() + "'");
  } else {
    log.info("Formatted lines " + lineRanges + " of '" + dirCacheEntry.getPathString() + "'");
  }
}
 
Example #7
Source File: JGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
private Repository stubbedRepo() {
	return spy(new Repository(new BaseRepositoryBuilder()) {
		@Override
		public void create(boolean bare) throws IOException {

		}

		@Override
		public ObjectDatabase getObjectDatabase() {
			return null;
		}

		@Override
		public RefDatabase getRefDatabase() {
			return JGitEnvironmentRepositoryTests.this.database;
		}

		@Override
		public StoredConfig getConfig() {
			return null;
		}

		@Override
		public AttributesNodeProvider createAttributesNodeProvider() {
			return null;
		}

		@Override
		public void scanForRepoChanges() throws IOException {

		}

		@Override
		public void notifyIndexChanged(boolean internal) {

		}

		@Override
		public ReflogReader getReflogReader(String refName) throws IOException {
			return null;
		}
	});
}