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.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Creates a new branch./*from   w  w w.j  a v  a 2s. co  m*/
 * 
 * @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();
}

From source file:org.eclipse.mylyn.internal.github.core.pr.PullRequestUtils.java

License:Open Source License

/**
 * Is given branch name the currently checked out branch?
 *
 * @param name/*  ww w .  j  ava2 s. c  o m*/
 * @param repo
 * @return true if checked out branch, false otherwise
 */
public static boolean isCurrentBranch(String name, Repository repo) {
    try {
        return name.equals(Repository.shortenRefName(repo.getFullBranch()));
    } catch (IOException e) {
        return false;
    }
}

From source file:org.eclipse.oomph.gitbash.decorators.BranchDecorator.java

License:Open Source License

private String getDecoration(org.eclipse.egit.ui.internal.repository.tree.RefNode node) {
    String branchName = Repository.shortenRefName(node.getObject().getName());
    Repository repository = node.getRepository();
    StoredConfig config = repository.getConfig();

    String branch = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_MERGE);

    if (branch != null) {
        String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
                ConfigConstants.CONFIG_KEY_REMOTE);

        boolean rebaseFlag = config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
                ConfigConstants.CONFIG_KEY_REBASE, false);

        if (branch.startsWith(DEFAULT_PATH)) {
            branch = branch.substring(DEFAULT_PATH.length());
        }//  w w w . j av a2  s  . com

        String prefix = ".".equals(remote) ? "" : remote + "/";
        String result = (rebaseFlag ? "REBASE" : "MERGE") + ": " + prefix + branch;

        try {
            BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branchName);
            if (trackingStatus != null
                    && (trackingStatus.getAheadCount() != 0 || trackingStatus.getBehindCount() != 0)) {
                result += " " + formatBranchTrackingStatus(trackingStatus);
            }
        } catch (Throwable t) {
            //$FALL-THROUGH$
        }

        return result;
    }

    return null;
}

From source file:org.eclipse.orion.server.git.BranchToJSONConverter.java

License:Open Source License

public static JSONObject toJSON(Ref ref, Repository db, URI baseLocation, int segmentsToRemove)
        throws JSONException, URISyntaxException, IOException, CoreException {
    JSONObject result = new JSONObject();
    String shortName = Repository.shortenRefName(ref.getName());
    result.put(ProtocolConstants.KEY_NAME, shortName);
    result.put(ProtocolConstants.KEY_TYPE, GitConstants.KEY_BRANCH_NAME);

    IPath basePath = new Path(baseLocation.getPath());
    IPath newPath = new Path(GitServlet.GIT_URI).append(GitConstants.BRANCH_RESOURCE).append(shortName)
            .append(basePath.removeFirstSegments(segmentsToRemove));
    URI location = new URI(baseLocation.getScheme(), baseLocation.getUserInfo(), baseLocation.getHost(),
            baseLocation.getPort(), newPath.toString(), baseLocation.getQuery(), baseLocation.getFragment());
    result.put(ProtocolConstants.KEY_LOCATION, location);

    // add Git Clone URI
    result.put(GitConstants.KEY_CLONE,/*from   w  w  w.  j  a v  a  2s. co m*/
            BaseToCloneConverter.getCloneLocation(location, BaseToCloneConverter.BRANCH));

    // add Git Commit URI
    result.put(GitConstants.KEY_COMMIT,
            BaseToCommitConverter.getCommitLocation(location, shortName, BaseToCommitConverter.REMOVE_FIRST_3));

    result.put(GitConstants.KEY_REMOTE, BaseToRemoteConverter.getRemoteBranchLocation(location, shortName, db,
            BaseToRemoteConverter.REMOVE_FIRST_3));
    result.put(GitConstants.KEY_HEAD, BaseToCommitConverter.getCommitLocation(location, Constants.HEAD,
            BaseToCommitConverter.REMOVE_FIRST_3));
    result.put(GitConstants.KEY_BRANCH_CURRENT, shortName.equals(db.getBranch()));

    return result;
}

From source file:org.eclipse.orion.server.git.jobs.PushJob.java

License:Open Source License

private IStatus doPush() throws IOException, CoreException, URISyntaxException, GitAPIException {
    // /git/remote/{remote}/{branch}/file/{path}
    File gitDir = GitUtils.getGitDir(path.removeFirstSegments(2));
    Repository db = new FileRepository(gitDir);
    Git git = new Git(db);

    PushCommand pushCommand = git.push();

    RemoteConfig remoteConfig = new RemoteConfig(git.getRepository().getConfig(), remote);
    credentials.setUri(remoteConfig.getURIs().get(0));
    pushCommand.setCredentialsProvider(credentials);

    RefSpec spec = new RefSpec(srcRef + ':' + Constants.R_HEADS + branch);
    pushCommand.setRemote(remote).setRefSpecs(spec);
    if (tags)//from  ww w. java 2 s  . co m
        pushCommand.setPushTags();
    pushCommand.setForce(force);
    Iterable<PushResult> resultIterable = pushCommand.call();
    PushResult pushResult = resultIterable.iterator().next();
    // this set will contain only OK status or UP_TO_DATE status
    Set<RemoteRefUpdate.Status> statusSet = new HashSet<RemoteRefUpdate.Status>();
    for (final RemoteRefUpdate rru : pushResult.getRemoteUpdates()) {
        final String rm = rru.getRemoteName();
        // check status only for branch given in the URL or tags
        if (branch.equals(Repository.shortenRefName(rm)) || rm.startsWith(Constants.R_TAGS)) {
            RemoteRefUpdate.Status status = rru.getStatus();
            // any status different from UP_TO_DATE and OK should generate warning
            if (status != RemoteRefUpdate.Status.OK && status != RemoteRefUpdate.Status.UP_TO_DATE)
                return new Status(IStatus.WARNING, GitActivator.PI_GIT, status.name(),
                        new Throwable(rru.getMessage()));
            // add OK or UP_TO_DATE status to the set
            statusSet.add(status);
        }
        // TODO: return results for all updated branches once push is available for remote, see bug 352202
    }
    if (statusSet.contains(RemoteRefUpdate.Status.OK))
        // if there is OK status in the set -> something was updated
        return Status.OK_STATUS;
    else
        // if there is no OK status in the set -> only UP_TO_DATE status is possible
        return new Status(IStatus.WARNING, GitActivator.PI_GIT, RemoteRefUpdate.Status.UP_TO_DATE.name());
}

From source file:org.eclipse.orion.server.git.objects.Branch.java

License:Open Source License

@PropertyDescription(name = GitConstants.KEY_REMOTE)
private JSONArray getRemotes() throws URISyntaxException, JSONException, IOException, CoreException {
    String branchName = Repository.shortenRefName(ref.getName());
    JSONArray result = new JSONArray();
    String remoteName = getConfig().getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_REMOTE);
    if (remoteName != null) {
        RemoteConfig remoteConfig = new RemoteConfig(getConfig(), remoteName);
        if (!remoteConfig.getFetchRefSpecs().isEmpty()) {
            Remote remote = new Remote(cloneLocation, db, remoteName);
            remote.setNewBranch(branchName);
            result.put(remote.toJSON());
        }/*from  w ww.  j a va 2  s .co m*/
    } else {
        List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(getConfig());
        for (RemoteConfig remoteConfig : remoteConfigs) {
            if (!remoteConfig.getFetchRefSpecs().isEmpty()) {
                Remote r = new Remote(cloneLocation, db, remoteConfig.getName());
                r.setNewBranch(branchName);
                if (db.resolve(Constants.R_REMOTES + remoteConfig.getName() + "/" + branchName) != null) { //$NON-NLS-1$
                    // it's an existing branch, not a new one, use it as filter
                    return new JSONArray().put(r.toJSON());
                }
                result.put(r.toJSON());
            }
        }
    }
    return result;
}

From source file:org.eclipse.orion.server.git.objects.Branch.java

License:Open Source License

public String getName(boolean fullName, boolean encode) {
    String name = ref.getName();//  w  w w. jav  a  2 s .c o m
    if (!fullName)
        name = Repository.shortenRefName(ref.getName());
    if (encode)
        name = GitUtils.encode(name);
    return name;
}

From source file:org.eclipse.orion.server.git.objects.Log.java

License:Open Source License

@PropertyDescription(name = ProtocolConstants.KEY_PREVIOUS_LOCATION)
private URI getPreviousPageLocation() throws URISyntaxException {
    if (page > 0) {
        StringBuilder c = new StringBuilder(""); //$NON-NLS-1$
        if (fromRefId != null)
            c.append(fromRefId.getName());
        if (fromRefId != null && toRefId != null)
            c.append(".."); //$NON-NLS-1$
        if (toRefId != null)
            c.append(Repository.shortenRefName(toRefId.getName()));
        final String q = "page=%d&pageSize=%d"; //$NON-NLS-1$
        if (page > 1) {
            return BaseToCommitConverter.getCommitLocation(cloneLocation, c.toString(), pattern,
                    BaseToCommitConverter.REMOVE_FIRST_2.setQuery(String.format(q, page - 1, pageSize)));
        }/*from ww  w .  j a va 2s .  co  m*/
    }
    return null;
}

From source file:org.eclipse.orion.server.git.objects.Log.java

License:Open Source License

@PropertyDescription(name = ProtocolConstants.KEY_NEXT_LOCATION)
private URI getNextPageLocation() throws URISyntaxException {
    if (hasNextPage()) {
        StringBuilder c = new StringBuilder(""); //$NON-NLS-1$
        if (fromRefId != null)
            c.append(fromRefId.getName());
        if (fromRefId != null && toRefId != null)
            c.append(".."); //$NON-NLS-1$
        if (toRefId != null)
            c.append(Repository.shortenRefName(toRefId.getName()));
        final String q = "page=%d&pageSize=%d"; //$NON-NLS-1$
        return BaseToCommitConverter.getCommitLocation(cloneLocation, c.toString(), pattern,
                BaseToCommitConverter.REMOVE_FIRST_2.setQuery(String.format(q, page + 1, pageSize)));
    }/*from w w w  .  j ava 2s  .  co m*/
    return null;
}

From source file:org.eclipse.orion.server.git.objects.Remote.java

License:Open Source License

@PropertyDescription(name = ProtocolConstants.KEY_CHILDREN)
private JSONArray getChildren() throws IOException, JSONException, URISyntaxException, CoreException {
    JSONArray children = new JSONArray();
    boolean branchFound = false;
    List<Ref> refs = new ArrayList<Ref>();
    String currentBranch = db.getBranch();
    for (Entry<String, Ref> refEntry : db.getRefDatabase().getRefs(Constants.R_REMOTES + name + "/") //$NON-NLS-1$
            .entrySet()) {//from ww  w  .j  a  v a  2s  .c o m
        if (!refEntry.getValue().isSymbolic()) {
            Ref ref = refEntry.getValue();
            String name = ref.getName();
            name = Repository.shortenRefName(name).substring(Constants.DEFAULT_REMOTE_NAME.length() + 1);
            if (currentBranch.equals(name)) {
                refs.add(0, ref);
            } else {
                refs.add(ref);
            }
        }
    }
    for (Ref ref : refs) {
        String remoteBranchName = Repository.shortenRefName(ref.getName());
        remoteBranchName = remoteBranchName.substring((this.name + "/").length()); //$NON-NLS-1$
        RemoteBranch remoteBranch = new RemoteBranch(cloneLocation, db, this, remoteBranchName);
        children.put(remoteBranch.toJSON());
        if (newBranch != null && !newBranch.isEmpty() && remoteBranchName.equals(newBranch)) {
            children = new JSONArray().put(remoteBranch.toJSON());
            branchFound = true;
            break;
        }
    }

    if (!branchFound && newBranch != null && !newBranch.isEmpty()) {
        JSONObject o = new JSONObject();
        // TODO: this should be a RemoteBranch
        String name = Constants.R_REMOTES + getName() + "/" + newBranch; //$NON-NLS-1$
        o.put(ProtocolConstants.KEY_NAME, name.substring(Constants.R_REMOTES.length()));
        o.put(ProtocolConstants.KEY_FULL_NAME, name);
        o.put(ProtocolConstants.KEY_TYPE, RemoteBranch.TYPE);
        o.put(ProtocolConstants.KEY_LOCATION,
                BaseToRemoteConverter.REMOVE_FIRST_2.baseToRemoteLocation(cloneLocation, "", //$NON-NLS-1$
                        /*short name is {remote}/{branch}*/getName() + "/" + GitUtils.encode(newBranch)));
        children.put(o);
    }

    return children;
}