Example usage for org.eclipse.jgit.lib Constants OBJECT_ID_STRING_LENGTH

List of usage examples for org.eclipse.jgit.lib Constants OBJECT_ID_STRING_LENGTH

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants OBJECT_ID_STRING_LENGTH.

Prototype

int OBJECT_ID_STRING_LENGTH

To view the source code for org.eclipse.jgit.lib Constants OBJECT_ID_STRING_LENGTH.

Click Source Link

Document

A Git object can be expressed as a 40 character string of hexadecimal digits.

Usage

From source file:com.google.gerrit.server.git.ReceiveCommitsAdvertiseRefsHook.java

License:Apache License

private Set<ObjectId> advertiseHistory(Iterable<Ref> sending, BaseReceivePack rp) {
    Set<ObjectId> toInclude = Sets.newHashSet();

    // Advertise some recent open changes, in case a commit is based one.
    final int limit = 32;
    try {// www  . j  av  a2  s  .  c om
        Set<PatchSet.Id> toGet = Sets.newHashSetWithExpectedSize(limit);
        for (ChangeData cd : queryProvider.get().enforceVisibility(true).setLimit(limit)
                .byProjectOpen(projectName)) {
            PatchSet.Id id = cd.change().currentPatchSetId();
            if (id != null) {
                toGet.add(id);
            }
        }
        for (PatchSet ps : db.patchSets().get(toGet)) {
            if (ps.getRevision() != null && ps.getRevision().get() != null) {
                toInclude.add(ObjectId.fromString(ps.getRevision().get()));
            }
        }
    } catch (OrmException err) {
        log.error("Cannot list open changes of " + projectName, err);
    }

    // Size of an additional ".have" line.
    final int haveLineLen = 4 + Constants.OBJECT_ID_STRING_LENGTH + 1 + 5 + 1;

    // Maximum number of bytes to "waste" in the advertisement with
    // a peek at this repository's current reachable history.
    final int maxExtraSize = 8192;

    // Number of recent commits to advertise immediately, hoping to
    // show a client a nearby merge base.
    final int base = 64;

    // Number of commits to skip once base has already been shown.
    final int step = 16;

    // Total number of commits to extract from the history.
    final int max = maxExtraSize / haveLineLen;

    // Scan history until the advertisement is full.
    Set<ObjectId> alreadySending = Sets.newHashSet();
    RevWalk rw = rp.getRevWalk();
    for (Ref ref : sending) {
        try {
            if (ref.getObjectId() != null) {
                alreadySending.add(ref.getObjectId());
                rw.markStart(rw.parseCommit(ref.getObjectId()));
            }
        } catch (IOException badCommit) {
            continue;
        }
    }

    int stepCnt = 0;
    RevCommit c;
    try {
        while ((c = rw.next()) != null && toInclude.size() < max) {
            if (alreadySending.contains(c) || toInclude.contains(c) || c.getParentCount() > 1) {
                // Do nothing
            } else if (toInclude.size() < base) {
                toInclude.add(c);
            } else {
                stepCnt = ++stepCnt % step;
                if (stepCnt == 0) {
                    toInclude.add(c);
                }
            }
        }
    } catch (IOException err) {
        log.error("Error trying to advertise history on " + projectName, err);
    }
    rw.reset();
    return toInclude;
}

From source file:com.surevine.gateway.scm.git.jgit.BundleWriter.java

License:Eclipse Distribution License

/**
 * Generate and write the bundle to the output stream.
 * <p>/*from  w  ww.  j  a va  2 s. c om*/
 * This method can only be called once per BundleWriter instance.
 *
 * @param monitor
 *            progress monitor to report bundle writing status to.
 * @param os
 *            the stream the bundle is written to. The stream should be
 *            buffered by the caller. The caller is responsible for closing
 *            the stream.
 * @throws IOException
 *             an error occurred reading a local object's data to include in
 *             the bundle, or writing compressed object data to the output
 *             stream.
 */
public void writeBundle(ProgressMonitor monitor, OutputStream os) throws IOException {
    PackConfig pc = packConfig;
    if (pc == null)
        pc = new PackConfig(db);
    PackWriter packWriter = new PackWriter(pc, db.newObjectReader());
    try {
        final HashSet<ObjectId> inc = new HashSet<ObjectId>();
        final HashSet<ObjectId> exc = new HashSet<ObjectId>();
        inc.addAll(include.values());
        for (final RevCommit r : assume)
            exc.add(r.getId());
        packWriter.setIndexDisabled(true);
        packWriter.setDeltaBaseAsOffset(true);
        packWriter.setThin(exc.size() > 0);
        packWriter.setReuseValidatingObjects(false);
        if (exc.size() == 0)
            packWriter.setTagTargets(tagTargets);
        packWriter.preparePack(monitor, inc, exc);

        final Writer w = new OutputStreamWriter(os, Constants.CHARSET);
        w.write(TransportBundle.V2_BUNDLE_SIGNATURE);
        w.write('\n');

        final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
        for (final RevCommit a : assume) {
            w.write('-');
            a.copyTo(tmp, w);
            if (a.getRawBuffer() != null) {
                w.write(' ');
                w.write(a.getShortMessage());
            }
            w.write('\n');
        }
        for (final Map.Entry<String, ObjectId> e : include.entrySet()) {
            e.getValue().copyTo(tmp, w);
            w.write(' ');
            w.write(e.getKey());
            w.write('\n');
        }

        w.write('\n');
        w.flush();
        packWriter.writePack(monitor, monitor, os);
    } finally {
        packWriter.release();
    }
}

From source file:it.com.atlassian.labs.speakeasy.util.jgit.WalkRemoteObjectDatabase.java

License:Eclipse Distribution License

/**
 * Overwrite (or create) a loose ref in the remote repository.
 * <p>//from   w  w w . j ava 2s  .  c o  m
 * This method creates any missing parent directories, if necessary.
 *
 * @param name
 *            name of the ref within the ref space, for example
 *            <code>refs/heads/pu</code>.
 * @param value
 *            new value to store in this ref. Must not be null.
 * @throws IOException
 *             writing is not supported, or attempting to write the file
 *             failed, possibly due to permissions or remote disk full, etc.
 */
void writeRef(final String name, final ObjectId value) throws IOException {
    final ByteArrayOutputStream b;

    b = new ByteArrayOutputStream(Constants.OBJECT_ID_STRING_LENGTH + 1);
    value.copyTo(b);
    b.write('\n');

    writeFile(ROOT_DIR + name, b.toByteArray());
}

From source file:org.eclipse.mylyn.gerrit.tests.core.GerritConnectorTest.java

License:Open Source License

@Test
public void testPerformQueryAnonymous() throws Exception {
    // XXX some test repositories require OpenID auth which is not supported when running tests
    repository.setCredentials(AuthenticationType.REPOSITORY, null, false);

    IRepositoryQuery query = new RepositoryQuery(repository.getConnectorKind(), "query"); //$NON-NLS-1$
    query.setAttribute(GerritQuery.TYPE, GerritQuery.ALL_OPEN_CHANGES);
    query.setAttribute(GerritQuery.QUERY_STRING, GerritQuery.ALL_OPEN_CHANGES);
    InMemoryTaskDataCollector resultCollector = new InMemoryTaskDataCollector();

    IStatus status = connector.performQuery(repository, query, resultCollector, null,
            new NullProgressMonitor());
    assertEquals(Status.OK_STATUS, status);
    assertTrue(resultCollector.getResults().size() > 0);
    for (TaskData result : resultCollector.getResults()) {
        assertTrue(result.isPartial());//from  w ww .j  a  v  a  2s . c  om
        assertNull(result.getRoot().getAttribute(GerritTaskSchema.getDefault().UPLOADED.getKey()));
        TaskAttribute key = result.getRoot().getAttribute(GerritTaskSchema.getDefault().KEY.getKey());
        assertNotNull(key);
        String value = key.getValue();
        assertNotNull(value);
        assertTrue(value.startsWith("I")); // Change-Ids are prefixed with an uppercase I
        // 'expand' the abbreviated SHA-1 with 'a's
        String objId = StringUtils.rightPad(value.substring(1), Constants.OBJECT_ID_STRING_LENGTH, 'a');
        assertTrue(ObjectId.isId(objId));
    }
}