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

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

Introduction

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

Prototype

String R_HEADS

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

Click Source Link

Document

Prefix for branch refs

Usage

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

License:Educational Community License

/**
 * @param args/*from   w ww.  ja v a 2 s . c o  m*/
 */
public static void main(String[] args) {

    if (args.length != 2 && args.length != 3) {
        System.err.println("USAGE: <git repository> <bare> [<ref prefix>]");
        System.err.println("\t<bare> : 0 (false) or 1 (true)");
        System.err.println("\t<ref prefix> : refs/heads (default) or say refs/remotes/origin (test clone)");
        System.exit(-1);
    }

    boolean bare = false;

    if (args[1].trim().equals("1")) {
        bare = true;
    }

    String refPrefix = Constants.R_HEADS;

    if (args.length == 3)
        refPrefix = args[2].trim();

    try {

        PrintWriter tipWriter = new PrintWriter("branch-tips.txt");

        PrintWriter toDeleteWriter = new PrintWriter("branch-to-delete.txt");

        Repository repo = GitRepositoryUtils.buildFileRepository(new File(args[0]).getAbsoluteFile(), false,
                bare);

        Collection<Ref> repositoryHeads = repo.getRefDatabase().getRefs(refPrefix).values();

        RevWalk rw = new RevWalk(repo);

        Git git = new Git(repo);

        Map<String, ObjectId> branchMap = new HashMap<>();

        Map<ObjectId, Set<String>> commitToBranchMap = new HashMap<>();

        Set<ObjectId> visitedCommits = new HashSet<>();

        Set<String> branchTips = new HashSet<>();

        Set<String> branchesToDelete = new HashSet<>();

        for (Ref ref : repositoryHeads) {

            String branchName = ref.getName().substring(refPrefix.length() + 1);

            branchMap.put(branchName, ref.getObjectId());

            Set<String> branches = new HashSet<>();

            branches.add(branchName);

            commitToBranchMap.put(ref.getObjectId(), branches);

            branchTips.add(branchName);

        }

        for (Ref ref : repositoryHeads) {

            rw.reset();

            // skip over the prefix and the trailing '/'

            String branchName = ref.getName().substring(refPrefix.length() + 1);

            if (visitedCommits.contains(ref.getObjectId())) {
                log.info("skipping {} because it has already been visited.", branchName);
                branchTips.remove(branchName);
                branchesToDelete.add(branchName);
                continue;
            }

            RevCommit currentCommit = rw.parseCommit(ref.getObjectId());

            rw.markStart(currentCommit);

            while ((currentCommit = rw.next()) != null) {

                //               if (visitedCommits.contains(currentCommit.getId())) {
                //                  log.info("branch = {}, commit = {} skipping since we have already visited this graph.", branchName, currentCommit.getId().getName());
                //                  break;
                //               }

                Set<String> currentBranchNames = commitToBranchMap.get(currentCommit.getId());

                //               visitedCommits.add(currentCommit.getId());

                if (currentBranchNames != null) {

                    for (String currentBranchName : currentBranchNames) {

                        if (currentBranchName.equals(branchName))
                            continue; // skip over our own branch name

                        branchTips.remove(currentBranchName);
                        branchesToDelete.add(currentBranchName);

                    }

                }

            }

        }

        log.info("found {} branch tips", branchTips.size());

        //         tipWriter.println("# " + branchTips.size() + " branch tips");

        List<String> orderedBranchTips = new ArrayList<>();

        orderedBranchTips.addAll(branchTips);

        Collections.sort(orderedBranchTips);

        for (String branch : orderedBranchTips) {

            tipWriter.println(branch);
        }

        log.info("found {} branches to delete", branchesToDelete.size());

        List<String> orderedBranchesToDelete = new ArrayList<>(branchesToDelete);

        Collections.sort(orderedBranchesToDelete);

        for (String branch : orderedBranchesToDelete) {

            toDeleteWriter.println(branch);
        }

        rw.release();

        tipWriter.close();

        toDeleteWriter.close();

    } catch (Exception e) {

        log.error("unexpected Exception ", e);
    }
}

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

License:Educational Community License

/**
 * @param args//from   w ww.  ja va 2s  . c om
 */
public static void main(String[] args) {

    if (args.length != 4 && args.length != 5) {
        System.err.println("USAGE: <git repository> <bare> <remote name> <branch prefixes> [<ref prefix>]");
        System.err.println(
                "\t<branch prefixes> : like enrollment_ks-enroll (colon seperated for multiple prefixes)");
        System.err.println(
                "\t<ref prefix> : like refs/heads/ or refs/remotes/origin/ needs to end with a trailing slash /");
        System.exit(-1);
    }

    boolean bare = false;

    if (args[1].trim().equals("true")) {
        bare = true;
    }

    String remoteName = args[2];

    String branchPrefixes[] = args[3].split(":");

    String refPrefix = Constants.R_HEADS;

    if (args.length == 5)
        refPrefix = args[4].trim();

    try {

        Repository repo = GitRepositoryUtils.buildFileRepository(new File(args[0]).getAbsoluteFile(), false,
                bare);

        Collection<Ref> refsToPush = repo.getRefDatabase().getRefs(refPrefix).values();

        List<RemoteRefUpdate> refsToUpdate = new ArrayList<>();

        for (String branchPrefix : branchPrefixes) {

            String adjustedBranchPrefix = refPrefix + branchPrefix;

            for (Ref candidateRef : refsToPush) {

                if (candidateRef.getName().startsWith(adjustedBranchPrefix)) {

                    String candidateBranchName = candidateRef.getName().substring(refPrefix.length());

                    String targetRefName = Constants.R_HEADS + candidateBranchName;

                    log.info("pushing " + adjustedBranchPrefix + " to " + targetRefName);

                    refsToUpdate.add(new RemoteRefUpdate(repo, candidateRef, targetRefName, true, null, null));
                }
            }
        }

        RevWalk rw = new RevWalk(repo);

        push(repo, remoteName, refsToUpdate, null, null);

        rw.release();

        repo.close();

    } catch (Exception e) {

        log.error("unexpected Exception ", e);
    }
}

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

License:Educational Community License

/**
 * @param args/* w  w w .  ja  v  a  2  s.c  o m*/
 */
public static void main(String[] args) {

    if (args.length != 6 && args.length != 7) {
        System.err.println(
                "USAGE: <git repository> <mode> <bare> <object id> <svn:externals containing file> <svn revision> [<ref prefix>]");
        System.err.println("\t<mode> : commit or branch");
        System.err.println("\t<bare> : 0 (false) or 1 (true)");
        System.err.println("\t<object id> : the sha1 of the commit or the name of the branch in branch mode");
        System.err.println(
                "\t<svn:externals file> : contains the content of the svn:externals property for the target");

        System.err.println("\t<ref prefix> : refs/heads (default) or say refs/remotes/origin (test clone)");
        System.exit(-1);
    }

    boolean bare = false;

    if (args[2].trim().equals("1")) {
        bare = true;
    }

    boolean branchMode = false;
    boolean commitMode = false;

    if (args[1].equals("branch"))
        branchMode = true;
    else if (args[1].equals("commit"))
        commitMode = true;

    String reference = args[3].trim();

    String svnExternalsDataFile = args[4].trim();

    Long svnRevision = Long.parseLong(args[5].trim());

    String refPrefix = Constants.R_HEADS;

    if (args.length == 7)
        refPrefix = args[6].trim();

    try {

        Repository repo = GitRepositoryUtils.buildFileRepository(new File(args[0]).getAbsoluteFile(), false,
                bare);

        SvnRevisionMapper revisionMapper = new SvnRevisionMapper(repo);

        if (commitMode) {

            /*
             * 
             */

            List<ExternalModuleInfo> externals = ExternalModuleUtils
                    .extractExternalModuleInfoFromSvnExternalsInputStream(svnRevision,
                            "https://svn.kuali.org/repos/student", new FileInputStream(svnExternalsDataFile));

            /*
             * Take the existing content of the commit pointed at and then materialize the externals within it.
             */

            RevWalk rw = new RevWalk(repo);

            ObjectInserter inserter = repo.newObjectInserter();

            RevCommit commit = rw.parseCommit(ObjectId.fromString(reference));

            TreeWalk tw = new TreeWalk(repo);

            tw.setRecursive(false);

            while (tw.next()) {

                if (tw.getNameString().equals("fusion-maven-plugin.dat")) {
                    ObjectId blobId = tw.getObjectId(0);

                    ObjectLoader loader = repo.newObjectReader().open(blobId, Constants.OBJ_BLOB);

                    List<String> lines = IOUtils.readLines(loader.openStream());

                    // pull out and use the sha1's from the stream to fuse the externals.

                }
            }
            CommitBuilder commitBuilder = new CommitBuilder();

            ObjectReader or;
            commitBuilder.setTreeId(ExternalModuleUtils.createFusedTree(or = repo.newObjectReader(), inserter,
                    rw, commit, externals));

            List<ObjectId> parentIds = new LinkedList<>();

            for (int i = 0; i < commit.getParentCount(); i++) {

                RevCommit parent = commit.getParent(i);

                parentIds.add(parent.getId());

            }

            commitBuilder.setParentIds(parentIds);

            commitBuilder.setAuthor(commit.getAuthorIdent());

            commitBuilder.setCommitter(commit.getCommitterIdent());

            commitBuilder.setMessage(commit.getFullMessage());

            ObjectId commitId = inserter.insert(commitBuilder);

            log.info("new commit id = " + commitId);

            rw.release();

            inserter.release();

            or.release();
        }

    } catch (Exception e) {

        log.error("unexpected Exception ", e);
    }
}

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

License:Educational Community License

/**
 * @param args//from  www .  j  a  va2  s  . co m
 */
public static void main(String[] args) {

    if (args.length != 3 && args.length != 4) {
        System.err.println(
                "USAGE: <git repository> <bare: 0 or 1> <output file name>[<refs prefix: default to refs/heads >]");
        System.exit(-1);
    }

    String gitRepositoryPath = args[0];

    String bareString = args[1].trim();

    boolean bare = false;

    if (bareString.equals("1"))
        bare = true;

    String refPrefix = Constants.R_HEADS;

    String outputFileName = args[2].trim();

    if (args.length == 4)
        refPrefix = args[3].trim();

    try {

        PrintWriter outputWriter = new PrintWriter(outputFileName);
        Repository repo = GitRepositoryUtils.buildFileRepository(new File(gitRepositoryPath).getAbsoluteFile(),
                false, bare);

        Map<String, Ref> branchHeads = repo.getRefDatabase().getRefs(refPrefix);

        ObjectReader objectReader = repo.newObjectReader();

        RevWalk rw = new RevWalk(objectReader);

        TreeWalk tw = new TreeWalk(objectReader);

        tw.setRecursive(true);

        String header = String.format(
                "Branch Name :: Total Commits in Graph :: Total Blob Size in Bytes :: Total Blob Size in Mega Bytes :: Total Blob Size in Giga Bytes");

        System.out.println(header);

        outputWriter.println(header);

        for (Map.Entry<String, Ref> entry : branchHeads.entrySet()) {

            String branchName = entry.getKey();
            Ref branchRef = entry.getValue();

            Set<ObjectId> blobIds = new HashSet<>();

            RevCommit commit = rw.parseCommit(branchRef.getObjectId());

            RevWalk commitHistoryWalk = new RevWalk(objectReader);

            commitHistoryWalk.markStart(commit);

            processCommit(commit, tw, blobIds);

            int totalReachableCommits = 0;

            while ((commit = commitHistoryWalk.next()) != null) {

                processCommit(commit, tw, blobIds);

                totalReachableCommits++;
            }

            long totalSize = 0L;

            AsyncObjectSizeQueue<ObjectId> sq = objectReader.getObjectSize(blobIds, true);

            while (sq.next())
                totalSize += sq.getSize();

            BigDecimal totalCounter = new BigDecimal(totalSize);

            String output = String.format("%s::%d::%s::%s::%s", branchName, totalReachableCommits,
                    totalCounter.toString(), getMB(totalCounter).toString(), getGB(totalCounter).toString());

            System.out.println(output);
            outputWriter.println(output);

            commitHistoryWalk.release();

        }

        tw.release();
        rw.release();
        objectReader.release();

        outputWriter.close();

    } catch (Exception e) {

        log.error("unexpected exception", e);

    }

}

From source file:org.kuali.student.git.model.AbstractGitImporterMainTestCase.java

License:Educational Community License

protected void runImporter(Repository repository, long importRevision, String dumpFilePath, String repoURL,
        String repoUUID) throws IOException {

    SvnRevisionMapper revisionMapper = new SvnRevisionMapper(repository);
    //      /*from w ww  .  j a  v  a  2 s . c  om*/
    revisionMapper.initialize();

    Map<String, Ref> heads = repository.getRefDatabase().getRefs(Constants.R_HEADS);

    if (heads.size() > 0) {
        revisionMapper.createRevisionMap(importRevision - 1L, new ArrayList<Ref>(heads.values()));
    }

    revisionMapper.shutdown();

    if (enablePlugin)
        System.getProperties().setProperty("spring.profiles.active", "configured-plugin");

    GitImporterMain.main(new String[] { dumpFilePath, repository.getDirectory().getAbsolutePath(),
            "target/" + name + "-r" + importRevision + "-veto.log",
            "target/" + name + "-r" + importRevision + "-copyFrom-skipped.log",
            "target/" + name + "-r" + importRevision + "-blob.log", "0", repoURL, repoUUID });

}

From source file:org.kuali.student.git.model.branch.utils.GitBranchUtils.java

License:Educational Community License

public static String getCanonicalBranchName(String branchPath, long revision,
        ILargeBranchNameProvider provider) {

    String branchName = convertPathToBranchName(branchPath);

    /*//from  ww w . j a  va  2s  .co m
     * Consider the
     */
    if ((Constants.R_HEADS.length() + branchPath.length()) >= FILE_SYSTEM_NAME_LIMIT) {
        // need to store the long name
        String shortBranchName = provider.storeLargeBranchName(branchName, revision);

        return shortBranchName;
    } else
        return branchName;

}

From source file:org.kuali.student.git.model.branch.utils.GitBranchUtils.java

License:Educational Community License

/**
 * In some cases a path could refer to a branch that is not detectable using our heuristics.
 * //from  w w w .  ja  va  2  s . c  o  m
 * In certain copy from cases we want to save the files even if the branch name is non sensical.
 * 
 * This provides a mechanism to find those branches later
 * 
 * @return the branch path part from the path.
 * 
 */
public static String extractBranchPath(Repository repo, String path) throws IOException {

    String pathPaths[] = path.split("/");

    for (int i = pathPaths.length - 1; i > 0; i--) {

        String candidatePath = StringUtils.join(pathPaths, "/", 0, i);

        Ref branchRef = repo.getRef(Constants.R_HEADS + candidatePath);

        if (branchRef != null) {
            // found a match
            return candidatePath;
        }

    }

    return null;

}

From source file:org.kuali.student.git.model.CopyFromTreeBlobVisitor.java

License:Educational Community License

@Override
public boolean visitBlob(ObjectId blobId, String blobPath, String name)
        throws MissingObjectException, IncorrectObjectTypeException, IOException {

    String alteredBlobPath = null;

    try {//from  ww w  .ja v a 2 s  . c  o  m

        String copyFromBranchPath = copyFromRevisionMapResults.getRevMap().getBranchPath();

        String adjustedCopyFromBranchPath = copyFromBranchPath.substring(Constants.R_HEADS.length());

        String copyFromBranchSubPath = copyFromRevisionMapResults.getSubPath();

        if (copyFromBranchSubPath.length() > 0) {
            adjustedCopyFromBranchPath = adjustedCopyFromBranchPath + "/" + copyFromBranchSubPath;
        }

        Long copyFromRevision = copyFromRevisionMapResults.getRevMap().getRevision();

        BranchData copyFromBranch = null;

        try {
            copyFromBranch = branchDetector.parseBranch(copyFromRevision, adjustedCopyFromBranchPath);
        } catch (VetoBranchException e) {
            // just use the name as given in the copy from
            copyFromBranch = new BranchData(copyFromRevision, copyFromBranchPath, copyFromBranchSubPath);
        }

        alteredBlobPath = GitBranchUtils.convertToTargetPath(path,
                copyFromRevisionMapResults.getRevMap().getRevision(), adjustedCopyFromBranchPath, blobPath,
                copyFromBranch);

        if (copyFromPath.length() > 0) {
            String adjustedPath = adjustedCopyFromBranchPath.substring(copyFromPath.length());

            /*
             * Insert the adjusted path between the new branch name and file
             * part
             */

            if (adjustedPath.length() > 0) {

                int insertionIndex = targetBranch.getBranchPath().length();

                StringBuilder blobPathBuilder = new StringBuilder();

                blobPathBuilder.append(targetBranch.getBranchPath());

                blobPathBuilder.append(adjustedPath);

                blobPathBuilder.append(alteredBlobPath.substring(insertionIndex));

                alteredBlobPath = blobPathBuilder.toString();

            }

        }
        /*
         * In most cases this blob path will live in the branch identified
         * as data.
         * 
         * But in some cases the branch could be different. i.e. the full
         * blob path shows a different branch.
         * 
         * Detect the branch path and if different from data then record
         * this blob separately.
         */

        BranchData alteredData = null;
        try {

            alteredData = branchDetector.parseBranch(currentRevision, alteredBlobPath);

        } catch (VetoBranchException e1) {

            if (!fallbackOnTargetBranch) {
                vetoLog.print("vetoed alteredBlobPath = " + alteredBlobPath);
                // even though this blob failed we still want to look at the
                // others in case any can be applied.
                return true;
            } else {

                // fallback on the target branch
                /*
                 * This is mostly to allow in certain copy from cases the copyfrom to work even though the branch detection heuristic fails.
                 */
                targetBranch.addBlob(alteredBlobPath, blobId, blobLog);

                targetBranch.addMergeParentId(
                        ObjectId.fromString(this.copyFromRevisionMapResults.getRevMap().getCommitId()));

                return true;
            }
        }

        if (alteredData.getBranchPath().equals(targetBranch.getBranchPath())) {

            // same branch
            targetBranch.addBlob(alteredBlobPath, blobId, blobLog);

            targetBranch.addMergeParentId(
                    ObjectId.fromString(this.copyFromRevisionMapResults.getRevMap().getCommitId()));

        } else {
            // a different branch
            GitBranchData alteredBranchData = branchDataProvider
                    .getBranchData(GitBranchUtils.getCanonicalBranchName(alteredData.getBranchPath(),
                            currentRevision, largeBranchNameProvider), currentRevision);

            alteredBranchData.addBlob(alteredBlobPath, blobId, blobLog);

            alteredBranchData.addMergeParentId(
                    ObjectId.fromString(this.copyFromRevisionMapResults.getRevMap().getCommitId()));
        }

    } catch (VetoBranchException e) {
        vetoLog.println(String.format(
                "tree walk add blob vetoed. CurrentRevision: %s, Current Branch Name: %s, Blob Path: %s",
                String.valueOf(currentRevision), targetBranch.getBranchName(), alteredBlobPath));
        // intentionally continue

    }

    // visit all of the blobs
    return true;
}

From source file:org.kuali.student.git.model.ExternalModuleUtils.java

License:Educational Community License

public static String createFusionMavenPluginDataFileString(long currentRevision, final Repository repo,
        List<ExternalModuleInfo> externals, ILargeBranchNameProvider largeBranchNameProvider) {
    return createFusionMavenPluginDataFileString(currentRevision, new IBranchHeadProvider() {

        @Override/*from   w  ww.  j a v a2 s .  c  o m*/
        public ObjectId getBranchHeadObjectId(String branchName) {

            ObjectId branchHead = null;
            try {

                // use the branch head
                Ref branchRef = repo.getRef(Constants.R_HEADS + branchName);

                if (branchRef != null)
                    branchHead = branchRef.getObjectId();
                else {
                    log.warn("createFusionMavenPluginDataFileString failed to resolve branch for: {}",
                            branchName);
                }

            } catch (IOException e) {
                // intentionally fall through
            }

            return branchHead;
        }
    }, externals, largeBranchNameProvider);
}

From source file:org.kuali.student.git.model.NodeProcessor.java

License:Educational Community License

public void processNode(String path, long currentRevision, Map<String, String> nodeProperties)
        throws IOException, VetoBranchException {

    /*//from  w  ww.j  a v  a  2 s . c  o  m
     * This catches cases that don't have file content.
     * 
     * This can be directory adds, file copies, directory copies, etc.
     */

    String kind = nodeProperties.get(SvnDumpFilter.SVN_DUMP_KEY_NODE_KIND);

    String action = nodeProperties.get(SvnDumpFilter.SVN_DUMP_KEY_NODE_ACTION);

    boolean validBranch = true;

    BranchData branchData = null;
    try {
        branchData = branchDetector.parseBranch(currentRevision, path);

        if (kind != null && kind.equals(FILE_KIND) && branchData.getPath().length() == 0)
            throw new VetoBranchException(
                    "A file add or change requires part of the path to be a subpath in the branch.");

    } catch (VetoBranchException e) {
        validBranch = false;

    }

    if (validBranch) {
        // check that there is actually a branch of this name
        String canonicalBranchName = GitBranchUtils.getCanonicalBranchName(branchData.getBranchPath(),
                currentRevision, largeBranchNameProvider);

        try {
            Ref mergeBranchRef = repo.getRef(Constants.R_HEADS + canonicalBranchName);

        } catch (IOException e) {
            validBranch = false;
            // intentionally fall through to the if statements below.
        }
    }

    GitBranchData data = null;

    if (validBranch) {
        data = getBranchData(GitBranchUtils.getCanonicalBranchName(branchData.getBranchPath(), currentRevision,
                largeBranchNameProvider), currentRevision);
    }

    if (ADD_ACTION.equals(action)) {

        if (FILE_KIND.equals(kind)) {

            /*
             * No content length means we add the blob from the copy from
             * revision
             */

            if (!validBranch) {
                // an add on an invalid branch means we have a gap and the
                // path should be stored in a default branch
                // for now this will be the first directory in the path.

                data = getDefaultBranchData(path, currentRevision);
            }

            if (data != null)
                applyBlobAdd(data, path, currentRevision, nodeProperties);

        } else if (DIR_KIND.equals(kind)) {

            /*
             * We care if the directory was copied from somewhere else
             */

            loadRevisionProperties(currentRevision, data, path, nodeProperties);

            applyDirectoryAdd(data, path, currentRevision, nodeProperties);

        } else {
            // skip this case
        }

    } else if (CHANGE_ACTION.equals(action)) {

        /*
         * This can happen I think for property changes Not sure if we are
         * doing the right thing here.
         */
        if (FILE_KIND.equals(kind)) {

            if (!validBranch)
                data = getDefaultBranchData(path, currentRevision);

            if (data != null)
                applyBlobAdd(data, path, currentRevision, nodeProperties);

        } else if (DIR_KIND.equals(kind)) {
            loadRevisionProperties(currentRevision, data, path, nodeProperties);
        } else {
            // skip this case
        }

    } else if (REPLACE_ACTION.equals(action)) {

        /*
         * Copy of add action section to start with.
         */
        if (FILE_KIND.equals(kind)) {

            log.info("file replace on " + path);

            deletePath(data, currentRevision, path);

            /*
             * No content length means we add the blob from the copy from
             * revision
             */

            if (!validBranch) {
                // an add on an invalid branch means we have a gap and the
                // path should be stored in a default branch
                // for now this will be the first directory in the path.

                data = getDefaultBranchData(path, currentRevision);
            }

            if (data != null)
                applyBlobAdd(data, path, currentRevision, nodeProperties);

        } else if (DIR_KIND.equals(kind)) {

            log.info("directory replace on " + path);

            deletePath(data, currentRevision, path);

            /*
             * We care if the directory was copied from somewhere else
             */

            loadRevisionProperties(currentRevision, data, path, nodeProperties);

            applyDirectoryAdd(data, path, currentRevision, nodeProperties);

        } else {
            // skip this case
        }
    } else if (DELETE_ACTION.equals(action)) {
        /*
         * We make no distinction between file and directory deletes.
         * 
         * Just that the delete is occurring on a valid branch.
         */

        deletePath(data, currentRevision, path);

    }
}