Example usage for org.eclipse.jgit.lib AnyObjectId name

List of usage examples for org.eclipse.jgit.lib AnyObjectId name

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib AnyObjectId name.

Prototype

public final String name() 

Source Link

Document

name.

Usage

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

License:Eclipse Distribution License

private void downloadObject(final ProgressMonitor pm, final AnyObjectId id) throws TransportException {
    if (alreadyHave(id))
        return;/*from  w  w w  .  j a v  a2 s.  co  m*/

    for (;;) {
        // Try a pack file we know about, but don't have yet. Odds are
        // that if it has this object, it has others related to it so
        // getting the pack is a good bet.
        //
        if (downloadPackedObject(pm, id))
            return;

        // Search for a loose object over all alternates, starting
        // from the one we last successfully located an object through.
        //
        final String idStr = id.name();
        final String subdir = idStr.substring(0, 2);
        final String file = idStr.substring(2);
        final String looseName = subdir + "/" + file;

        for (int i = lastRemoteIdx; i < remotes.size(); i++) {
            if (downloadLooseObject(id, looseName, remotes.get(i))) {
                lastRemoteIdx = i;
                return;
            }
        }
        for (int i = 0; i < lastRemoteIdx; i++) {
            if (downloadLooseObject(id, looseName, remotes.get(i))) {
                lastRemoteIdx = i;
                return;
            }
        }

        // Try to obtain more pack information and search those.
        //
        while (!noPacksYet.isEmpty()) {
            final WalkRemoteObjectDatabase wrr = noPacksYet.removeFirst();
            final Collection<String> packNameList;
            try {
                pm.beginTask("Listing packs", ProgressMonitor.UNKNOWN);
                packNameList = wrr.getPackNames();
            } catch (IOException e) {
                // Try another repository.
                //
                recordError(id, e);
                continue;
            } finally {
                pm.endTask();
            }

            if (packNameList == null || packNameList.isEmpty())
                continue;
            for (final String packName : packNameList) {
                if (packsConsidered.add(packName))
                    unfetchedPacks.add(new RemotePack(wrr, packName));
            }
            if (downloadPackedObject(pm, id))
                return;
        }

        // Try to expand the first alternate we haven't expanded yet.
        //
        Collection<WalkRemoteObjectDatabase> al = expandOneAlternate(id, pm);
        if (al != null && !al.isEmpty()) {
            for (final WalkRemoteObjectDatabase alt : al) {
                remotes.add(alt);
                noPacksYet.add(alt);
                noAlternatesYet.add(alt);
            }
            continue;
        }

        // We could not obtain the object. There may be reasons why.
        //
        List<Throwable> failures = fetchErrors.get(id);
        final TransportException te;

        te = new TransportException(MessageFormat.format(JGitText.get().cannotGet, id.name()));
        if (failures != null && !failures.isEmpty()) {
            if (failures.size() == 1)
                te.initCause(failures.get(0));
            else
                te.initCause(new CompoundException(failures));
        }
        throw te;
    }
}

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

License:Eclipse Distribution License

private boolean alreadyHave(final AnyObjectId id) throws TransportException {
    try {/*from w w  w  .  j  a  v  a  2  s. c  o  m*/
        return reader.has(id);
    } catch (IOException error) {
        throw new TransportException(MessageFormat.format(JGitText.get().cannotReadObject, id.name()), error);
    }
}

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

License:Eclipse Distribution License

private boolean downloadPackedObject(final ProgressMonitor monitor, final AnyObjectId id)
        throws TransportException {
    // Search for the object in a remote pack whose index we have,
    // but whose pack we do not yet have.
    ////w  w  w.j a v a2s  . c  om
    final Iterator<RemotePack> packItr = unfetchedPacks.iterator();
    while (packItr.hasNext() && !monitor.isCancelled()) {
        final RemotePack pack = packItr.next();
        try {
            pack.openIndex(monitor);
        } catch (IOException err) {
            // If the index won't open its either not found or
            // its a format we don't recognize. In either case
            // we may still be able to obtain the object from
            // another source, so don't consider it a failure.
            //
            recordError(id, err);
            packItr.remove();
            continue;
        }

        if (monitor.isCancelled()) {
            // If we were cancelled while the index was opening
            // the open may have aborted. We can't search an
            // unopen index.
            //
            return false;
        }

        if (!pack.index.hasObject(id)) {
            // Not in this pack? Try another.
            //
            continue;
        }

        // It should be in the associated pack. Download that
        // and attach it to the local repository so we can use
        // all of the contained objects.
        //
        try {
            pack.downloadPack(monitor);
        } catch (IOException err) {
            // If the pack failed to download, index correctly,
            // or open in the local repository we may still be
            // able to obtain this object from another pack or
            // an alternate.
            //
            recordError(id, err);
            continue;
        } finally {
            // If the pack was good its in the local repository
            // and Repository.hasObject(id) will succeed in the
            // future, so we do not need this data anymore. If
            // it failed the index and pack are unusable and we
            // shouldn't consult them again.
            //
            try {
                if (pack.tmpIdx != null)
                    FileUtils.delete(pack.tmpIdx);
            } catch (IOException e) {
                throw new TransportException(e.getMessage(), e);
            }
            packItr.remove();
        }

        if (!alreadyHave(id)) {
            // What the hell? This pack claimed to have
            // the object, but after indexing we didn't
            // actually find it in the pack.
            //
            recordError(id, new FileNotFoundException(
                    MessageFormat.format(JGitText.get().objectNotFoundIn, id.name(), pack.packName)));
            continue;
        }

        // Complete any other objects that we can.
        //
        final Iterator<ObjectId> pending = swapFetchQueue();
        while (pending.hasNext()) {
            final ObjectId p = pending.next();
            if (pack.index.hasObject(p)) {
                pending.remove();
                process(p);
            } else {
                workQueue.add(p);
            }
        }
        return true;

    }
    return false;
}

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

License:Eclipse Distribution License

private boolean downloadLooseObject(final AnyObjectId id, final String looseName,
        final WalkRemoteObjectDatabase remote) throws TransportException {
    try {/*from  w  w w.j a v a 2  s. c o  m*/
        final byte[] compressed = remote.open(looseName).toArray();
        verifyAndInsertLooseObject(id, compressed);
        return true;
    } catch (FileNotFoundException e) {
        // Not available in a loose format from this alternate?
        // Try another strategy to get the object.
        //
        recordError(id, e);
        return false;
    } catch (IOException e) {
        throw new TransportException(MessageFormat.format(JGitText.get().cannotDownload, id.name()), e);
    }
}

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

License:Eclipse Distribution License

private void verifyAndInsertLooseObject(final AnyObjectId id, final byte[] compressed) throws IOException {
    final ObjectLoader uol;
    try {//from w  w w .j  av a  2 s.  c o  m
        uol = UnpackedObject.parse(compressed, id);
    } catch (CorruptObjectException parsingError) {
        // Some HTTP servers send back a "200 OK" status with an HTML
        // page that explains the requested file could not be found.
        // These servers are most certainly misconfigured, but many
        // of them exist in the world, and many of those are hosting
        // Git repositories.
        //
        // Since an HTML page is unlikely to hash to one of our loose
        // objects we treat this condition as a FileNotFoundException
        // and attempt to recover by getting the object from another
        // source.
        //
        final FileNotFoundException e;
        e = new FileNotFoundException(id.name());
        e.initCause(parsingError);
        throw e;
    }

    final int type = uol.getType();
    final byte[] raw = uol.getCachedBytes();
    if (objCheck != null) {
        try {
            objCheck.check(type, raw);
        } catch (CorruptObjectException e) {
            throw new TransportException(MessageFormat.format(JGitText.get().transportExceptionInvalid,
                    Constants.typeString(type), id.name(), e.getMessage()));
        }
    }

    ObjectId act = inserter.insert(type, raw);
    if (!AnyObjectId.equals(id, act)) {
        throw new TransportException(MessageFormat.format(JGitText.get().incorrectHashFor, id.name(),
                act.name(), Constants.typeString(type), compressed.length));
    }
    inserter.flush();
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.commitInfo.GitCommitsInfoBuilder.java

License:Apache License

private void includeSubModules(@NotNull final Repository db, @NotNull final CommitTreeProcessor proc,
        @NotNull final RevCommit commit, @NotNull final CommitDataBean bean) {
    final SubInfo tree = proc.processCommitTree(commit);
    if (tree == null)
        return;/*from  w  w w .  j ava2s .com*/

    final SubmodulesConfig config = tree.getConfig();
    final Map<String, AnyObjectId> modules = tree.getSubmoduleToPath();

    for (Submodule sub : config.getSubmodules()) {
        final AnyObjectId mountedCommit = modules.get(sub.getPath());

        if (mountedCommit == null)
            continue;

        final String url;
        try {
            url = SubmoduleResolverImpl.resolveSubmoduleUrl(db, sub.getUrl());
        } catch (URISyntaxException e) {
            continue;
        }

        bean.addMountPoint(
                new CommitMountPointDataBean(Constants.VCS_NAME, url, sub.getPath(), mountedCommit.name()));
    }
}

From source file:org.eclipse.egit.ui.internal.push.PushBranchWizardTest.java

License:Open Source License

@Test
public void pushHeadToExistingRemote() throws Exception {
    try (Git git = new Git(repository)) {
        AnyObjectId head = repository.resolve(Constants.HEAD);
        git.checkout().setName(head.name()).call();
    }//from   w  w w.j a  va  2s. c  o m

    PushBranchWizardTester wizard = PushBranchWizardTester.startWizard(selectProject(), Constants.HEAD);
    wizard.selectRemote("fetch");
    wizard.enterBranchName("foo");
    wizard.next();
    wizard.finish();

    assertBranchPushed("foo", remoteRepository);
}

From source file:org.kuali.student.git.importer.FixImportRepo.java

License:Educational Community License

private static void updateRef(Repository repo, String branchName, String revision, AnyObjectId branchId)
        throws IOException {

    RefUpdate u = repo.updateRef(branchName, true);

    u.setForceUpdate(true);/*from  w w w .  ja v a 2  s . co  m*/

    String resultMessage = "resetting branch " + branchName + " to revision " + revision + " at "
            + branchId.name();

    u.setNewObjectId(branchId);

    u.setRefLogMessage(resultMessage, true);

    Result result = u.update();

    log.info(resultMessage + " result = " + result);

}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public List<CommitRef> update() {
    List<CommitRef> commits = new ArrayList<CommitRef>();
    try {/*from  ww w.  j  av a2 s  .c  o  m*/
        if (repository == null) {
            prepareWorkspace();
            initRepository();
        }
        Git git = new Git(repository);
        AnyObjectId oldHead = repository.resolve(Constants.HEAD);
        if (oldHead == null) {
            LOGGER.debug("Local repository is empty. Fetching remote repository.");
            FetchResult fetchResult = doRemoteUpdate();
            if (fetchResult.getTrackingRefUpdate(Constants.R_REMOTES + "origin/" + watchBranch) == null) {
                LOGGER.debug("Nothing to fetch from remote repository.");
                return null;
            }
            doCheckout(fetchResult);
        } else {
            LOGGER.debug("Local repository exists. Pulling remote repository.");
            git.pull().call();
        }
        AnyObjectId newHead = repository.resolve(Constants.HEAD);
        if (newHead == null) {
            LOGGER.debug("New HEAD of local repository doesnt exist.");
            return null;
        }
        if (newHead != oldHead) {
            LogCommand logCommand = git.log();
            if (oldHead == null) {
                LOGGER.debug("Retrieving revisions from HEAD [{}] on", newHead.name());
                logCommand.add(newHead);
            } else {
                LOGGER.debug("Retrieving revisions in range [{}, {}]", newHead.name(), oldHead.name());
                logCommand.addRange(oldHead, newHead);
            }
            Iterable<RevCommit> revisions = logCommand.call();
            for (RevCommit revision : revisions) {
                commits.add(new GitCommitRef(revision));
            }
        }
    } catch (Exception e) {
        throw new ScmException(e);
    }
    return commits;
}

From source file:org.openengsb.connector.git.internal.GitServiceImplTest.java

License:Apache License

@Test
public void tagHeadWithName_shouldReturnTagRefWithName() throws Exception {
    localRepository = RepositoryFixture.createRepository(localDirectory);
    String tagName = "newTag";
    TagRef tag = service.tagRepo(tagName);
    assertThat(tag, notNullValue());/*from  w  w  w.j  a  va 2  s . com*/
    assertThat(tagName, is(tag.getTagName()));
    AnyObjectId tagId = localRepository.resolve(tagName);
    assertThat(tagId.name(), is(tag.getStringRepresentation()));
    RevTag revTag = new RevWalk(localRepository).parseTag(tagId);
    AnyObjectId head = localRepository.resolve(Constants.HEAD);
    assertThat(revTag.getObject().name(), is(head.name()));
}