Example usage for org.eclipse.jgit.lib Repository shortenRefName

List of usage examples for org.eclipse.jgit.lib Repository shortenRefName

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository shortenRefName.

Prototype

@NonNull
public static String shortenRefName(String refName) 

Source Link

Document

Get a shortened more user friendly ref name

Usage

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/** The second time a repository is setup and its on the correct branch, no changes should have to be made. */
@Test/*ww w .ja  v a2s. co m*/
public void testInitGitSecondClean() throws Exception {
    Ref workRef = git.branchCreate().setName(WORK_BRANCH).call();

    source.initialize(directory, ORIGIN);

    assertNotNull("Branch must exist after init", git.getRepository().resolve(WORK_BRANCH));
    Ref headRef = git.getRepository().getRef(Constants.HEAD).getTarget();
    assertEquals("Head should point to the work branch", WORK_BRANCH,
            Repository.shortenRefName(headRef.getName()));
    assertEquals("Work branch should not have moved", workRef.getObjectId(), headRef.getObjectId());
}

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/** On first initialization of a repository with bad files, they should be removed. */
@Test/*from www . j  a v a  2s.  c o  m*/
public void testInitGitWithBadFiles() throws Exception {
    File.createTempFile("SomeClass", ".class", directory);
    File.createTempFile("some-jar", ".jar", directory);

    File subdir = new File(directory, "subdir");
    subdir.mkdirs();
    File.createTempFile("AnotherClass", ".class", subdir);
    File.createTempFile("another-jar", ".jar", subdir);

    git.add().addFilepattern(".").call();
    git.commit().setMessage("Add bad files for test").call();

    Ref oldHead = git.getRepository().getRef(Constants.HEAD).getLeaf();

    source.initialize(directory, ORIGIN);

    DirCache dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".jar")));
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".class")));
    }

    Ref oldBranch = git.getRepository().getRef(Repository.shortenRefName(oldHead.getName()));
    assertEquals("Keep history that jars were there", oldHead.getObjectId(), oldBranch.getObjectId());
}

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/**
 * If there are bad files on another branch, but the repository has been setup, i.e. bad files removed on the work branch,
 * then we should ignore them./*from ww w.  j  av a  2 s  .  c o m*/
 */
@Test
public void testInitBadFilesSecond() throws Exception {
    File.createTempFile("SomeClass", ".class", directory);
    File.createTempFile("some-jar", ".jar", directory);

    File subdir = new File(directory, "subdir");
    subdir.mkdirs();
    File.createTempFile("AnotherClass", ".class", subdir);
    File.createTempFile("another-jar", ".jar", subdir);

    git.add().addFilepattern(".").call();
    git.commit().setMessage("Add bad files for test").call();

    Ref oldHead = git.getRepository().getRef(Constants.HEAD).getLeaf();

    // setup work branch with bad files
    git.checkout().setCreateBranch(true).setName(WORK_BRANCH).call();

    RmCommand removeCommand = git.rm();
    DirCache dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        String path = dirCache.getEntry(i).getPathString();
        if (path.endsWith(".jar") || path.endsWith(".class")) {
            removeCommand.addFilepattern(path);
        }
    }
    removeCommand.call();
    git.commit().setMessage("Remove bad jars for testing").call();

    // perform test
    source.initialize(directory, ORIGIN);

    dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".jar")));
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".class")));
    }

    Ref oldBranch = git.getRepository().getRef(Repository.shortenRefName(oldHead.getName()));
    assertEquals("Keep history that jars were there", oldHead.getObjectId(), oldBranch.getObjectId());
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public Branch branchCreate(BranchCreateRequest request) throws GitException {
    CreateBranchCommand createBranchCommand = getGit().branchCreate().setName(request.getName());
    String start = request.getStartPoint();
    if (start != null) {
        createBranchCommand.setStartPoint(start);
    }/*from  www  .j  a  v a 2s .co  m*/
    try {
        Ref brRef = createBranchCommand.call();
        String refName = brRef.getName();
        String displayName = Repository.shortenRefName(refName);
        return newDto(Branch.class).withName(refName).withDisplayName(displayName).withActive(false)
                .withRemote(false);
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public List<Branch> branchList(BranchListRequest request) throws GitException {
    String listMode = request.getListMode();
    if (listMode != null && !BranchListRequest.LIST_ALL.equals(listMode)
            && !BranchListRequest.LIST_REMOTE.equals(listMode)) {
        throw new IllegalArgumentException(String.format(ERROR_UNSUPPORTED_LIST_MODE, listMode));
    }//from w  w  w.j a v  a  2  s  .  co m

    ListBranchCommand listBranchCommand = getGit().branchList();
    if (BranchListRequest.LIST_ALL.equals(listMode)) {
        listBranchCommand.setListMode(ListMode.ALL);
    } else if (BranchListRequest.LIST_REMOTE.equals(listMode)) {
        listBranchCommand.setListMode(ListMode.REMOTE);
    }
    List<Ref> refs;
    String currentRef;
    try {
        refs = listBranchCommand.call();
        String headBranch = getRepository().getBranch();
        Optional<Ref> currentTag = getGit().tagList().call().stream()
                .filter(tag -> tag.getObjectId().getName().equals(headBranch)).findFirst();
        if (currentTag.isPresent()) {
            currentRef = currentTag.get().getName();
        } else {
            currentRef = "refs/heads/" + headBranch;
        }

    } catch (GitAPIException | IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
    List<Branch> branches = new ArrayList<>();
    for (Ref ref : refs) {
        String refName = ref.getName();
        boolean isCommitOrTag = Constants.HEAD.equals(refName);
        String branchName = isCommitOrTag ? currentRef : refName;
        String branchDisplayName;
        if (isCommitOrTag) {
            branchDisplayName = "(detached from " + Repository.shortenRefName(currentRef) + ")";
        } else {
            branchDisplayName = Repository.shortenRefName(refName);
        }
        Branch branch = newDto(Branch.class).withName(branchName)
                .withActive(isCommitOrTag || refName.equals(currentRef)).withDisplayName(branchDisplayName)
                .withRemote(refName.startsWith("refs/remotes"));
        branches.add(branch);
    }
    return branches;
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

private PushResponse addCommandOutputUpdates(PushResponse pushResponseDto, final PushRequest request,
        final PushResult result) throws GitException {
    String commandOutput = result.getMessages();
    final Collection<RemoteRefUpdate> refUpdates = result.getRemoteUpdates();
    final List<Map<String, String>> updates = new ArrayList<>();
    final String currentBranch = getCurrentBranch();

    for (RemoteRefUpdate remoteRefUpdate : refUpdates) {
        final String remoteRefName = remoteRefUpdate.getRemoteName();
        // check status only for branch given in the URL or tags - (handle special "refs/for" case)
        String shortenRefFor = remoteRefName.startsWith("refs/for/")
                ? remoteRefName.substring("refs/for/".length())
                : remoteRefName;//from  w ww . j av  a 2s  .c o m
        if (currentBranch.equals(Repository.shortenRefName(remoteRefName))
                || currentBranch.equals(shortenRefFor) || remoteRefName.startsWith(Constants.R_TAGS)) {
            Map<String, String> update = new HashMap<>();
            RemoteRefUpdate.Status status = remoteRefUpdate.getStatus();
            if (status != RemoteRefUpdate.Status.UP_TO_DATE || !remoteRefName.startsWith(Constants.R_TAGS)) {
                update.put(KEY_COMMIT_MESSAGE, remoteRefUpdate.getMessage());
                update.put(KEY_RESULT, status.name());
                TrackingRefUpdate refUpdate = remoteRefUpdate.getTrackingRefUpdate();
                if (refUpdate != null) {
                    update.put(KEY_REMOTENAME, Repository.shortenRefName(refUpdate.getLocalName()));
                    update.put(KEY_LOCALNAME, Repository.shortenRefName(refUpdate.getRemoteName()));
                } else {
                    update.put(KEY_REMOTENAME, Repository.shortenRefName(remoteRefUpdate.getSrcRef()));
                    update.put(KEY_LOCALNAME, Repository.shortenRefName(remoteRefUpdate.getRemoteName()));
                }
                updates.add(update);
            }
            if (status != RemoteRefUpdate.Status.OK) {
                commandOutput = buildPushFailedMessage(request, remoteRefUpdate, currentBranch);
            }
        }
    }
    return pushResponseDto.withCommandOutput(commandOutput).withUpdates(updates);
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

private String getCurrentBranch() throws GitException {
    try {//from  www . j a v a 2  s  .  c o m
        return Repository.shortenRefName(repository.exactRef(Constants.HEAD).getLeaf().getName());
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.bc.BeyondCompareWithHeadActionHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    final Repository repository = getRepository(true, event);
    // assert all resources map to the same repository
    if (repository == null)
        return null;
    final IResource[] resources = getSelectedResources(event);

    if (resources.length == 1 && resources[0] instanceof IFile) {
        final IFile baseFile = (IFile) resources[0];

        try {/*from w  ww  . j a  va2  s  .  c  o  m*/
            RepositoryMapping mapping = RepositoryMapping.getMapping(baseFile.getProject());
            String repoRelativeBasePath = mapping.getRepoRelativePath(baseFile);
            Repository localRepo = mapping.getRepository();
            RevCommit commit = getHeadRevision(repository, repoRelativeBasePath);

            String rightFilePath = BeyondCompareUtil.getCompareFilePath(repoRelativeBasePath, commit,
                    localRepo);
            String leftFilePath = baseFile.getLocation().toFile().getAbsolutePath();
            BeyondCompareUtil.execBeyondCompare(leftFilePath, rightFilePath);
        } catch (Exception e) {
            Activator.handleError(UIText.CompareWithHeadActionHandler_onError, e, true);
        }
        return null;

    } else {
        CompareTreeView view;
        try {
            view = (CompareTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .showView(CompareTreeView.ID);
            try {
                Ref head = repository.getRef(Constants.HEAD);
                if (head == null || head.getObjectId() == null) {
                    // Initial commit case
                    Shell shell = HandlerUtil.getActiveShell(event);
                    MessageDialog.openInformation(shell, UIText.CompareWithHeadActionHandler_NoHeadTitle,
                            UIText.CompareWithHeadActionHandler_NoHeadMessage);
                } else
                    view.setInput(resources, Repository.shortenRefName(head.getTarget().getName()));
            } catch (IOException e) {
                Activator.handleError(e.getMessage(), e, true);
                return null;
            }
        } catch (PartInitException e) {
            Activator.handleError(e.getMessage(), e, true);
            return null;
        }
        return null;
    }
}

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

License:Open Source License

/**
 * Creates a new branch//from   ww  w  .ja v a  2s.  co m
 *
 * @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.common.LocalRepositoryTestCase.java

License:Open Source License

protected static void createStableBranch(Repository myRepository) throws IOException {
    // let's create a stable branch temporarily so
    // that we push two branches to remote
    String newRefName = "refs/heads/stable";
    RefUpdate updateRef = myRepository.updateRef(newRefName);
    Ref sourceBranch = myRepository.getRef("refs/heads/master");
    ObjectId startAt = sourceBranch.getObjectId();
    String startBranch = Repository.shortenRefName(sourceBranch.getName());
    updateRef.setNewObjectId(startAt);//from   w w  w  .  j a va 2s .c o m
    updateRef.setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$
    updateRef.update();
}