List of usage examples for org.eclipse.jgit.lib ObjectId fromString
public static ObjectId fromString(String str)
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static ObjectId resolveObjectId(final Git git, final String name) { try {//from w w w . ja v a2 s . c o m final Ref refName = getBranch(git, name); if (refName != null) { return refName.getObjectId(); } try { final ObjectId id = ObjectId.fromString(name); if (git.getRepository().getObjectDatabase().has(id)) { return id; } } catch (final IllegalArgumentException ex) { } return null; } catch (java.io.IOException e) { } return null; }
From source file:org.kuali.student.git.cleaner.AbstractRepositoryCleaner.java
License:Educational Community License
protected void loadGrafts(String graftsFileName) throws IOException { List<String> graftLines = FileUtils.readLines(new File(graftsFileName)); for (String graftLine : graftLines) { String parts[] = graftLine.split(" "); // part zero is the target commit Set<ObjectId> parents = new HashSet<>(); for (int i = 1; i < parts.length; i++) { ObjectId parent = ObjectId.fromString(parts[i]); parents.add(parent);/* w ww. ja v a2 s . co m*/ } ObjectId targetCommitId = ObjectId.fromString(parts[0]); GitGraft graft = new GitGraft(targetCommitId, parents); grafts.put(targetCommitId, graft); } }
From source file:org.kuali.student.git.cleaner.model.ObjectTranslationDataSource.java
License:Educational Community License
public void storeObjectTranslation(String originalObjectId, String newObjectId) { ObjectId original = ObjectId.fromString(originalObjectId); ObjectId translated = ObjectId.fromString(newObjectId); this.objectTranslationMap.put(original, translated); }
From source file:org.kuali.student.git.cleaner.RepositoryBlobRewriter.java
License:Educational Community License
@Override public void validateArgs(List<String> args) throws Exception { if (args.size() != 2 && args.size() != 4) { log.error(//from w w w. j a v a 2 s. co m "USAGE: <source git repository meta directory> <blob replacement input file> [<branchRefSpec> <git command path>]"); log.error("\t<git repo meta directory> : the path to the meta directory of the source git repository"); log.error( "\t<blob replacement input file> : format: blob id <double colon ::> replacement message (single line)"); log.error("\t<branchRefSpec> : git refspec from which to source the graph to be rewritten"); log.error("\t<git command path> : the path to a native git "); throw new IllegalArgumentException("invalid arguments"); } setRepo(GitRepositoryUtils.buildFileRepository(new File(args.get(0)).getAbsoluteFile(), false)); List<String> lines = FileUtils.readLines(new File(args.get(1))); for (String line : lines) { String[] parts = line.split("::"); ObjectId blobId = ObjectId.fromString(parts[0].trim()); String replacementContent = parts[1].trim(); this.blobIdToReplacementContentMap.put(blobId, replacementContent); } if (args.size() >= 3) setBranchRefSpec(args.get(2).trim()); if (args.size() == 4) setExternalGitCommandPath(args.get(3).trim()); }
From source file:org.kuali.student.git.importer.FixImportRepo.java
License:Educational Community License
/** * @param args/*w w w.ja v a2 s . c o m*/ */ public static void main(final String[] args) { if (args.length != 2) { log.error("USAGE: <git repository> <reset branches to revision>"); System.exit(-1); } try { File gitRepository = new File(args[0]).getAbsoluteFile(); if (!gitRepository.getParentFile().exists()) throw new FileNotFoundException(args[1] + "path not found"); final Repository repo = GitRepositoryUtils.buildFileRepository(gitRepository, false); String revision = args[1]; long longRevision = Long.parseLong(revision); SvnRevisionMapper mapper = new SvnRevisionMapper(repo); mapper.initialize(); Map<String, Ref> allRefs = new HashMap<String, Ref>(); Map<String, Ref> existingRefs = repo.getRefDatabase().getRefs(Constants.R_HEADS); allRefs.putAll(existingRefs); for (SvnRevisionMap entry : mapper.getRevisionHeads(longRevision)) { updateRef(repo, entry.getBranchName(), revision, ObjectId.fromString(entry.getCommitId())); allRefs.remove(entry.getBranchName().substring(Constants.R_HEADS.length())); } if (allRefs.size() > 0) { // delete all of the left over refs that weren't updated. for (Ref ref : allRefs.values()) { deleteRef(repo, ref.getName(), revision); } } mapper.truncateTo(longRevision); mapper.shutdown(); } catch (Exception e) { log.error("Processing failed", e); } }
From source file:org.kuali.student.git.importer.GitImporterParseOptions.java
License:Educational Community License
@Override public void onRevisionContentLength(long currentRevision, long contentLength, long propContentLength, ReadLineData lineData) {//from w ww. ja va 2 s. com // flush logs for the last revision blobLog.flush(); copyFromSkippedLog.flush(); vetoLog.flush(); // for any branch with a blob added create a git commit object pointing // at the git tree for the change. if (this.currentRevision != -1) flushPendingBranchCommits(); if (gcEnabled && this.currentRevision != 0 && this.currentRevision % 500 == 0) { // every five hundred revisions garbage collect the repository to // keep it fast log.info("Garbage collecting git repository"); if (externalGitCommandPath != null) { ExternalGitUtils.runGarbageCollection(externalGitCommandPath, repo, System.out); } else { try { GarbageCollectCommand gc = new Git(repo).gc(); // should not matter but anything loose can be collected. gc.setExpire(new Date()); gc.call(); } catch (GitAPIException e) { } } /* * Make sure JGit knows where the ref is located after loose refs are put into the pack file. */ repo.getRefDatabase().refresh(); } // if (gcEnabled && this.currentRevision != 0 && this.currentRevision % // 10000 == 0) { // // repack the revision map file every 10000 revs // try { // revisionMapper.repackMapFile(); // } catch (IOException e) { // throw new RuntimeException("failed to repack revision mapper", e); // } // } if (this.currentRevision == -1) { /* * In the initialization case check that we haven't already imported this revision. * * This will prevent clobbering an existing import. */ try { List<SvnRevisionMap> knownHeads = revisionMapper.getRevisionHeads(currentRevision); if (knownHeads != null && knownHeads.size() > 0) throw new RuntimeException( "Aborting: Target Git Repository(" + repo.getDirectory().getAbsolutePath() + ") already contains an export of revision: " + currentRevision); } catch (IOException e) { throw new RuntimeException( "failed to check existing revision heads for revision = " + currentRevision, e); } } this.currentRevision = currentRevision; log.info("starting on Revision: " + currentRevision); // at this point we should be able to read // propContentLength and parse out the try { Map<String, String> revisionProperties = org.kuali.student.common.io.IOUtils .extractRevisionProperties(inputStream, propContentLength, contentLength); // read out the author and commit message String author = revisionProperties.get("svn:author"); String commitDate = revisionProperties.get("svn:date"); String commitMessage = revisionProperties.get("svn:log"); String userName = author; if (userName == null) userName = "unknown"; String emailAddress = userName + "@kuali.org"; Date actualCommitDate = null; if (commitDate != null) { actualCommitDate = GitImporterDateUtils.convertDateString(commitDate); } else { log.warn("Missing commit date"); if (commitMessage == null) { commitMessage = MISSING_COMMIT_DATE_MESSAGE; } else { commitMessage = commitMessage + "\n" + MISSING_COMMIT_DATE_MESSAGE; } /* * Get the commit time of the previous commit and add 5 minutes * to it. */ List<SvnRevisionMap> heads = revisionMapper.getRevisionHeads(currentRevision - 1L); if (heads.size() == 0) { actualCommitDate = new DateTime(0L).toDate(); } else { SvnRevisionMap head = heads.get(0); RevWalk rw = new RevWalk(repo); RevCommit lastCommit = rw.parseCommit(ObjectId.fromString(head.getCommitId())); DateTime dt = new DateTime(lastCommit.getAuthorIdent().getWhen()).plusMinutes(5); actualCommitDate = dt.toDate(); } } TimeZone tz = GitImporterDateUtils.extractTimeZone(actualCommitDate); commitData = new GitCommitData(new PersonIdent(userName, emailAddress, actualCommitDate, tz), commitMessage); nodeProcessor.setCommitData(commitData); // also consider copyfrom or other details that // suggest a merge at this point. } catch (Exception e) { throw new RuntimeException("onRevisionContentLength failed to read revision properties.", e); } }
From source file:org.kuali.student.git.importer.ModuleMergeToolMain.java
License:Educational Community License
/** * @param args/* w w w .jav a 2s .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.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 w w w.j av a2 s. co 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 List<ExternalModuleInfo> extractFusionMavenPluginData(List<String> lines) { List<ExternalModuleInfo> externals = new ArrayList<>(); for (int i = 0; i < lines.size(); i += 2) { String commentLine = lines.get(i); String commentParts[] = commentLine.split("revision = "); String branchPathParts[] = commentParts[0].split("branch Path = "); String branchPath = branchPathParts[1].trim(); String revisionString = commentParts[1].trim(); String dataLine = lines.get(i + 1); String dataParts[] = dataLine.split("::"); String moduleName = dataParts[0].trim(); String branchName = dataParts[1].trim(); String branchHeadObjectId = dataParts[2].trim(); String subTreePath = null; if (dataParts.length == 4) subTreePath = dataParts[3].trim(); long revision = -1; try {// w w w . jav a 2 s. co m revision = Long.parseLong(revisionString); } catch (NumberFormatException e) { // intentionally do nothing, use revision = 0 } ExternalModuleInfo emi = null; if (subTreePath != null) emi = new ExternalModuleInfo(moduleName, branchPath, branchName, revision, subTreePath); else emi = new ExternalModuleInfo(moduleName, branchPath, branchName, revision); if (!branchHeadObjectId.equals("UNKNOWN")) { emi.setBranchHeadId(ObjectId.fromString(branchHeadObjectId)); } externals.add(emi); } return externals; }
From source file:org.kuali.student.git.model.NodeProcessor.java
License:Educational Community License
private void applyDirectoryAdd(final GitBranchData data, final String path, final long currentRevision, Map<String, String> nodeProperties) { /*//from ww w . j a v a 2s . c o m * A directory add can occur within a branch * * but it can also take place above, in which case we need to look at * the path in relationship to the branches that it matches and apply * the blobs accordingly. * * It can also take place within an existing branch in which case we * need to navigate the subtree to find the blobs. */ try { CopyFromOperation copyFromOperation = computeTargetBranches(data, path, currentRevision); computeCopyFromBranches(copyFromOperation, nodeProperties); for (GitBranchData targetBranch : copyFromOperation.getTargetBranches()) { List<SvnRevisionMapResults> copyFromBranches = copyFromOperation.getCopyFromBranches(); if (copyFromOperation.getType().equals(OperationType.SINGLE_NEW)) { /* * If the parent exists then this is the same as a delete * and copy. * * We are no longer connected to the old parent only the * copyfrom data. */ if (data.getParentId() != null) { deleteBranch(data.getBranchName(), currentRevision); data.reset(); } if (copyFromBranches.size() == 1) { // this is a new branch copied from the old branch // copy the svn:externals and svn:mergeinfo data aswell. SvnRevisionMapResults results = copyFromBranches.get(0); if (results != null && results.getSubPath() != null && results.getSubPath().length() == 0) { GitBranchDataUtils.extractAndStoreBranchMerges(results.getRevMap().getRevision(), results.getRevMap().getBranchName(), data, revisionMapper); ObjectId parentId = ObjectId.fromString(results.getRevMap().getCommitId()); // make a shallow copy of the parent tree. only the // blobs in the root directory GitTreeData parentTreeData = treeProcessor.extractExistingTreeDataFromCommit(parentId); // shallow because this method extract the externals from a // fusion-maven-plugin.dat file at the root of the // tree. // then it will set the details into the target branch. GitBranchDataUtils.extractExternalModules(repo, parentTreeData, data, treeProcessor); } } else if (copyFromBranches.size() > 1) { log.warn("multiple copy from case at rev = " + currentRevision + " path = " + path); } } else if (copyFromOperation.getType().equals(OperationType.INVALID_SINGLE_NEW)) { if (copyFromBranches.size() > 0) { /* * Normally we would not store into the path given but * because it is a copy from we will do it. */ String branchName = GitBranchUtils.getCanonicalBranchName(path, currentRevision, largeBranchNameProvider); if (targetBranch != null) { log.warn( "overriting target branch at rev = " + currentRevision + " and path = " + path); } targetBranch = getBranchData(branchName, currentRevision); } } /* * In this case the number of copyfrom branches can be very * high. * * And we need an efficient way to perform the copy. * * To start with we will ignore the possible child branches and * just use the existing git tree objects represented by the * copyfrom branches. */ for (SvnRevisionMapResults revisionMapResults : copyFromBranches) { ObjectId copyFromBranchCommitId = ObjectId .fromString(revisionMapResults.getRevMap().getCommitId()); String copyFromPath = revisionMapResults.getCopyFromPath(); String copyFromBranchPath = revisionMapResults.getRevMap().getBranchPath() .substring(Constants.R_HEADS.length()); String copyFromBranchSubPath = revisionMapResults.getSubPath(); String targetPath = GitBranchUtils.convertToTargetPath(path, copyFromPath, new BranchData(currentRevision, copyFromBranchPath, copyFromBranchSubPath)); try { BranchData adjustedBranch = branchDetector.parseBranch(currentRevision, targetPath); String branchName = GitBranchUtils.getCanonicalBranchName(adjustedBranch.getBranchPath(), currentRevision, largeBranchNameProvider); GitBranchData adjustedTargetBranch = getBranchData(branchName, currentRevision); applyCopy(adjustedTargetBranch, targetPath, copyFromBranchSubPath, copyFromBranchCommitId); // create the new branch Ref ref = repo.getRef(Constants.R_HEADS + branchName); if (ref == null || copyFromOperation.getType().equals(OperationType.INVALID_SINGLE_NEW) || copyFromOperation.getType().equals(OperationType.SINGLE_NEW)) { adjustedTargetBranch.setCreated(true); // copy over any svn merge data GitBranchDataUtils.extractAndStoreBranchMerges( revisionMapResults.getRevMap().getRevision(), revisionMapResults.getRevMap().getBranchName(), adjustedTargetBranch, revisionMapper); ObjectId parentId = ObjectId.fromString(revisionMapResults.getRevMap().getCommitId()); // make a shallow copy of the parent tree. only the // blobs in the root directory GitTreeData parentTreeData = treeProcessor.extractExistingTreeDataFromCommit(parentId); // shallow because this method will insert a // fusion-maven-plugin.dat file at the root of the // tree. GitBranchDataUtils.extractExternalModules(repo, parentTreeData, adjustedTargetBranch, treeProcessor); } adjustedTargetBranch.addMergeParentId(copyFromBranchCommitId); } catch (VetoBranchException e) { // add the branch tree into the target branch applyCopy(targetBranch, targetPath, copyFromBranchSubPath, copyFromBranchCommitId); targetBranch.setCreated(true); targetBranch.addMergeParentId(copyFromBranchCommitId); } } if (copyFromOperation.getType().equals(OperationType.SINGLE_NEW) || copyFromOperation.getType().equals(OperationType.INVALID_SINGLE_NEW)) { if (targetBranch != null && (targetBranch.getBlobsAdded() > 0 || targetBranch.isTreeDirty())) targetBranch.setCreated(true); } } } catch (IOException e) { log.error(String.format("failed to process directory add %s at %d", path, currentRevision), e); } }