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

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

Introduction

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

Prototype

public final String name() 

Source Link

Document

name.

Usage

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

public BareGitRepository createTag(final String tagSource, final String tagName, final String message,
        final boolean force) throws GitWrapException {
    try {//from  ww  w . j a va  2s.c  o  m
        final ObjectId src = repository.resolve(tagSource);
        if (src == null) {
            throw new GitWrapException("Cannot resolve tag-source: %s", tagSource);
        }

        if (!force && repository.resolve(tagName) != null) {
            throw new GitWrapException("Tag: %s already exists!", tagName);
        }

        String dest = tagName;
        if (!dest.startsWith(Constants.R_TAGS)) {
            dest = Constants.R_TAGS + tagName;
        }

        final String tagShort = dest.substring(Constants.R_TAGS.length());

        final ObjectLoader sourceLoader = repository.open(src);
        final ObjectInserter inserter = repository.newObjectInserter();

        final TagBuilder tag = new TagBuilder();
        tag.setTag(tagShort);
        tag.setTagger(new PersonIdent(repository));
        tag.setObjectId(src, sourceLoader.getType());
        tag.setMessage(message);

        final ObjectId tagId = inserter.insert(tag);
        tag.setTagId(tagId);

        final String refName = Constants.R_TAGS + tag.getTag();

        final RefUpdate tagRef = repository.updateRef(refName);
        tagRef.setNewObjectId(tag.getTagId());
        tagRef.setForceUpdate(force);
        tagRef.setRefLogMessage("Tagging source: " + src.name() + " as " + tagName, false);

        final Result updateResult = tagRef.update();

        switch (updateResult) {
        case NEW:
        case FAST_FORWARD:
        case FORCED: {
            break;
        }
        case REJECTED: {
            throw new GitWrapException("Tag already exists: %s", tagName);
        }
        default: {
            throw new GitWrapException("Cannot lock tag: %s", tagName);
        }
        }
    } catch (final IOException e) {
        throw new GitWrapException("Failed to add tag: %s", e, e.getMessage());
    }

    return this;
}

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

public BareGitRepository createBranch(final String source, final String name) throws GitWrapException {
    final String refName = (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) ? name
            : Constants.R_HEADS + name;/* w ww.  j  a  v a 2 s .c om*/

    try {
        String src;
        final Ref from = repository.getRef(source);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Creating branch: " + refName + " from: " + from);
        }

        final ObjectId startAt = repository.resolve(source + "^0");
        if (from != null) {
            src = from.getName();
        } else {
            src = startAt.name();
        }
        src = repository.shortenRefName(src);

        if (!Repository.isValidRefName(refName)) {
            throw new GitWrapException("Invalid branch name: " + refName);
        }

        if (repository.resolve(refName) != null) {
            throw new GitWrapException("Branch: " + refName + " already exists!");
        }

        final RefUpdate updateRef = repository.updateRef(refName);
        updateRef.setNewObjectId(startAt);
        updateRef.setRefLogMessage("branch: Created from " + source, false);
        final Result updateResult = updateRef.update();

        if (updateResult == Result.REJECTED) {
            throw new GitWrapException("Branch creation rejected for: %s", refName);
        }
    } catch (final IOException e) {
        throw new GitWrapException("Failed to create branch: %s from: %s.\nReason: %s", e, refName, source,
                e.getMessage());
    }

    return this;
}

From source file:org.craftercms.deployer.impl.ProcessedCommitsStoreImpl.java

License:Open Source License

@Override
public void store(String targetId, ObjectId commitId) throws DeployerException {
    File commitFile = getCommitFile(targetId);
    try {//w w w  .  ja  v a  2s .  c  om
        logger.debug("Storing processed commit ID {} for target '{}'", commitId.name(), targetId);

        FileUtils.write(commitFile, commitId.name(), "UTF-8", false);
    } catch (IOException e) {
        throw new DeployerException("Error saving processed commit ID to " + commitFile, e);
    }
}

From source file:org.craftercms.deployer.impl.processors.GitDiffProcessor.java

License:Open Source License

protected ChangeSet resolveChangeSetFromCommits(Git git, ObjectId fromCommitId, ObjectId toCommitId)
        throws DeployerException {
    String fromCommitIdStr = fromCommitId != null ? fromCommitId.name() : "{empty}";
    String toCommitIdStr = toCommitId != null ? toCommitId.name() : "{empty}";

    if (!Objects.equals(fromCommitId, toCommitId)) {
        logger.info("Calculating change set from commits: {} -> {}", fromCommitIdStr, toCommitIdStr);

        try (ObjectReader reader = git.getRepository().newObjectReader()) {
            AbstractTreeIterator fromTreeIter = getTreeIteratorForCommit(git, reader, fromCommitId);
            AbstractTreeIterator toTreeIter = getTreeIteratorForCommit(git, reader, toCommitId);

            List<DiffEntry> diffEntries = git.diff().setOldTree(fromTreeIter).setNewTree(toTreeIter).call();

            return processDiffEntries(diffEntries);
        } catch (IOException | GitAPIException e) {
            throw new DeployerException(
                    "Failed to calculate change set from commits: " + fromCommitIdStr + " -> " + toCommitIdStr,
                    e);/*from   w  w w.j  a v  a 2 s. com*/
        }
    } else {
        logger.info("Commits are the same. No change set will be calculated", fromCommitIdStr, toCommitIdStr);

        return null;
    }
}

From source file:org.eclipse.egit.core.test.GitTestCase.java

License:Open Source License

protected ObjectId createFileCorruptShort(Repository repository, IProject actProject, String name,
        String content) throws IOException {
    ObjectId id = createFile(repository, actProject, name, content);
    File file = new File(repository.getDirectory(),
            "objects/" + id.name().substring(0, 2) + "/" + id.name().substring(2));
    byte[] readFully = IO.readFully(file);
    FileUtils.delete(file);/*  ww w .j ava 2  s.  c  o  m*/
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    byte[] truncatedData = new byte[readFully.length - 1];
    System.arraycopy(readFully, 0, truncatedData, 0, truncatedData.length);
    fileOutputStream.write(truncatedData);
    fileOutputStream.close();
    return id;
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

/**
 * Creates a new branch/* w  w w  .  j a v a 2 s .  c  om*/
 *
 * @param refName
 *            starting point for the new branch
 * @param newRefName
 * @throws IOException
 */
public void createBranch(String refName, String newRefName) throws IOException {
    RefUpdate updateRef;
    updateRef = repository.updateRef(newRefName);
    Ref startRef = repository.getRef(refName);
    ObjectId startAt = repository.resolve(refName);
    String startBranch;
    if (startRef != null)
        startBranch = refName;
    else
        startBranch = startAt.name();
    startBranch = Repository.shortenRefName(startBranch);
    updateRef.setNewObjectId(startAt);
    updateRef.setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$
    updateRef.update();
}

From source file:org.eclipse.egit.ui.internal.commit.command.StashDropHandler.java

License:Open Source License

private int getStashIndex(Repository repo, ObjectId id) throws ExecutionException {
    int index = 0;
    try {/*from  ww  w  .j a  v  a2 s.  com*/
        for (RevCommit commit : Git.wrap(repo).stashList().call())
            if (commit.getId().equals(id))
                return index;
            else
                index++;
        throw new IllegalStateException(
                MessageFormat.format(UIText.StashDropCommand_stashCommitNotFound, id.name()));
    } catch (Exception e) {
        String message = MessageFormat.format(UIText.StashDropCommand_dropFailed, id.name());
        Activator.showError(message, e);
        throw new ExecutionException(message, e);
    }
}

From source file:org.eclipse.egit.ui.internal.merge.MergeResultDialog.java

License:Open Source License

private String abbreviate(ObjectId id, boolean addBrackets) {
    StringBuilder result = new StringBuilder(EMPTY);
    if (addBrackets)
        result.append("["); //$NON-NLS-1$
    try {/*  ww w .  j a  va 2 s .  c  om*/
        result.append(objectReader.abbreviate(id).name());
    } catch (IOException e) {
        result.append(id.name());
    }
    if (addBrackets)
        result.append("]"); //$NON-NLS-1$
    return result.toString();
}

From source file:org.eclipse.egit.ui.internal.preferences.GitProjectPropertyPage.java

License:Open Source License

private void fillValues(Repository repository) throws IOException {
    gitDir.setText(repository.getDirectory().getAbsolutePath());
    branch.setText(repository.getBranch());
    workDir.setText(repository.getWorkTree().getAbsolutePath());

    state.setText(repository.getRepositoryState().getDescription());

    final ObjectId objectId = repository.resolve(repository.getFullBranch());
    if (objectId == null) {
        if (repository.getAllRefs().size() == 0)
            id.setText(UIText.GitProjectPropertyPage_ValueEmptyRepository);
        else//w w  w.  j  a  va  2 s .  c om
            id.setText(UIText.GitProjectPropertyPage_ValueUnbornBranch);
    } else
        id.setText(objectId.name());
}

From source file:org.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Creates a new branch./*ww  w  .j  a va 2s. c  om*/
 * 
 * @param refName
 *            Starting point for the new branch.
 * @param newRefName
 *            Name of the new branch.
 */
public void createBranch(String refName, String newRefName) throws IOException {
    RefUpdate updateRef;
    updateRef = repository.updateRef(newRefName);
    Ref startRef = repository.getRef(refName);
    ObjectId startAt = repository.resolve(refName);
    String startBranch;
    if (startRef != null) {
        startBranch = refName;
    } else {
        startBranch = startAt.name();
    }
    startBranch = Repository.shortenRefName(startBranch);
    updateRef.setNewObjectId(startAt);
    updateRef.setRefLogMessage("branch: Created from " + startBranch, false);
    updateRef.update();
}