List of usage examples for org.eclipse.jgit.lib Repository getRefDatabase
@NonNull public abstract RefDatabase getRefDatabase();
From source file:org.kuali.student.git.importer.IdentifyBranchTipsMain.java
License:Educational Community License
/** * @param args/*from w w w . j ava 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 w w. j a v a 2 s . c o m */ 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.ReportBlobSizePerBranch.java
License:Educational Community License
/** * @param args//from w w w . j a v a 2s . c o 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 o m 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.ref.utils.GitRefUtils.java
License:Educational Community License
public static void batchRefUpdate(Repository repo, List<ReceiveCommand> updates, ProgressMonitor progressMonitor) throws IOException { BatchRefUpdate batchUpdate = repo.getRefDatabase().newBatchUpdate(); batchUpdate.addCommand(updates);// w w w . j a v a 2 s . co m batchUpdate.execute(new RevWalk(repo), progressMonitor); }
From source file:org.kuali.student.git.model.ref.utils.GitRefUtils.java
License:Educational Community License
public static Result deleteRef(Repository repo, Ref ref, boolean force) throws IOException { RefUpdate refUpdate = repo.getRefDatabase().newUpdate(ref.getName(), false); refUpdate.setForceUpdate(force);/*www.j a v a2 s . c o m*/ return refUpdate.delete(); }
From source file:org.kuali.student.repository.viewer.ViewerMain.java
License:Educational Community License
/** * @param args//from w w w . j av a 2 s . com */ public static void main(String[] args) { if (args.length != 1 && args.length != 2 && args.length != 3) { System.err.println("USAGE: <git meta directory> [--simplify] [--report]"); System.exit(-1); } String gitDirectory = args[0]; File gitDir = new File(gitDirectory); if (!gitDir.exists()) { System.err.println(gitDirectory + " does not exist"); System.exit(-1); } boolean simplify = false; boolean report = false; if (args.length >= 2) { if (args[1].toLowerCase().equals("--simplify")) simplify = true; else if (args[1].toLowerCase().equals("--report")) report = true; } if (args.length == 3) { if (args[2].toLowerCase().equals("--simplify")) simplify = true; else if (args[2].toLowerCase().equals("--report")) report = true; } try { Repository repo = GitRepositoryUtils.buildFileRepository(gitDir, false, true); Graph<RevCommit, String> graph = new DirectedSparseMultigraph<RevCommit, String>(); RevWalk rw = new RevWalk(repo); Map<String, Ref> branchHeads = repo.getRefDatabase().getRefs(Constants.R_HEADS); Map<RevCommit, String> branchHeadCommitToBranchNameMap = new HashMap<RevCommit, String>(); for (Map.Entry<String, Ref> entry : branchHeads.entrySet()) { RevCommit branchHeadCommit = rw.parseCommit(entry.getValue().getObjectId()); rw.markStart(branchHeadCommit); branchHeadCommitToBranchNameMap.put(branchHeadCommit, entry.getValue().getName()); } // tags Map<String, Ref> tags = repo.getRefDatabase().getRefs(Constants.R_TAGS); for (Map.Entry<String, Ref> entry : tags.entrySet()) { RevTag tag = rw.parseTag(entry.getValue().getObjectId()); RevCommit branchHeadCommit = rw.parseCommit(tag.getObject().getId()); rw.markStart(branchHeadCommit); branchHeadCommitToBranchNameMap.put(branchHeadCommit, entry.getValue().getName()); } rw.sort(RevSort.TOPO); rw.sort(RevSort.REVERSE, true); RevCommit currentCommit = null; Set<RevCommit> commits = new HashSet<RevCommit>(); while ((currentCommit = rw.next()) != null) { commits.add(currentCommit); } for (RevCommit commit : commits) { if (simplify) { /* * The only vertexes will be the parentless commits, multi-parent commits and branch head commits. * * For each commit we need to walk the available parents backwards so that we find the candidate vertex to associate with. * */ if (RevCommitVertexUtils.isSimplifiedVertex(branchHeadCommitToBranchNameMap, commit)) { for (RevCommit parentCommit : commit.getParents()) { // find the parent commit that is a simplified vertex RevCommit currentVertex = RevCommitVertexUtils .findSimplifiedVertex(branchHeadCommitToBranchNameMap, parentCommit); graph.addEdge(commit.getId().name() + " to " + currentVertex.getId().name(), commit, currentVertex); } } } else { /* * For non-simplified mode we an edge between each commit and its parent commit. */ for (RevCommit parentCommit : commit.getParents()) { graph.addEdge(commit.getId().name() + " to " + parentCommit.getId().name(), commit, parentCommit); } } } if (report) { PrintWriter reporter = new PrintWriter(new File("vertex-report.txt")); List<RevCommitVertexCount> vertexes = new ArrayList<RevCommitVertexCount>(); for (RevCommit vertex : graph.getVertices()) { Collection<String> inEdges = graph.getInEdges(vertex); int size = inEdges.size(); if (size > 1) vertexes.add(new RevCommitVertexCount(vertex, size)); } Collections.sort(vertexes, new Comparator<RevCommitVertexCount>() { /* (non-Javadoc) * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ @Override public int compare(RevCommitVertexCount o1, RevCommitVertexCount o2) { // descending return new Integer(o2.getCount()).compareTo(new Integer(o1.getCount())); } }); reporter.println("Object Id:child count"); for (RevCommitVertexCount vertexCount : vertexes) { reporter.println(vertexCount.getVertex().getId().name() + ":" + vertexCount.getCount()); } reporter.close(); } new GitGraphFrame(gitDir, graph, branchHeadCommitToBranchNameMap, simplify); } catch (Exception e) { log.error("viewing failed", e); } }
From source file:org.uberfire.java.nio.fs.jgit.util.commands.RefTreeUpdateCommand.java
License:Apache License
private void commit(final Repository repo, final RevCommit original, final BiFunction fun) throws IOException { try (final ObjectReader reader = repo.newObjectReader(); final ObjectInserter inserter = repo.newObjectInserter(); final RevWalk rw = new RevWalk(reader)) { final RefTreeDatabase refdb = (RefTreeDatabase) repo.getRefDatabase(); final RefDatabase bootstrap = refdb.getBootstrap(); final RefUpdate refUpdate = bootstrap.newUpdate(refdb.getTxnCommitted(), false); final CommitBuilder cb = new CommitBuilder(); final Ref ref = bootstrap.exactRef(refdb.getTxnCommitted()); final RefTree tree; if (ref != null && ref.getObjectId() != null) { tree = RefTree.read(reader, rw.parseTree(ref.getObjectId())); cb.setParentId(ref.getObjectId()); refUpdate.setExpectedOldObjectId(ref.getObjectId()); } else {//from ww w . j av a 2s . c o m tree = RefTree.newEmptyTree(); refUpdate.setExpectedOldObjectId(ObjectId.zeroId()); } if (fun.apply(reader, tree)) { final Ref ref2 = bootstrap.exactRef(refdb.getTxnCommitted()); if (ref2 == null || ref2.getObjectId().equals(ref != null ? ref.getObjectId() : null)) { cb.setTreeId(tree.writeTree(inserter)); if (original != null) { cb.setAuthor(original.getAuthorIdent()); cb.setCommitter(original.getAuthorIdent()); } else { final PersonIdent personIdent = new PersonIdent("user", "user@example.com"); cb.setAuthor(personIdent); cb.setCommitter(personIdent); } refUpdate.setNewObjectId(inserter.insert(cb)); inserter.flush(); final RefUpdate.Result result = refUpdate.update(rw); switch (result) { case NEW: case FAST_FORWARD: break; default: throw new RuntimeException( repo.getDirectory() + " -> " + result.toString() + " : " + refUpdate.getName()); } final File commited = new File(repo.getDirectory(), refdb.getTxnCommitted()); final File accepted = new File(repo.getDirectory(), refdb.getTxnNamespace() + "accepted"); Files.copy(commited.toPath(), accepted.toPath(), StandardCopyOption.REPLACE_EXISTING); } } } }