List of usage examples for org.eclipse.jgit.lib Constants typeString
public static String typeString(int typeCode)
From source file:com.google.gitiles.TreeJsonData.java
License:Open Source License
static Tree toJsonData(ObjectId id, TreeWalk tw) throws IOException { Tree tree = new Tree(); tree.id = id.name();//from ww w . j a v a2 s . co m tree.entries = Lists.newArrayList(); while (tw.next()) { Entry e = new Entry(); FileMode mode = tw.getFileMode(0); e.mode = mode.getBits(); e.type = Constants.typeString(mode.getObjectType()); e.id = tw.getObjectId(0).name(); e.name = tw.getNameString(); tree.entries.add(e); } return tree; }
From source file:it.com.atlassian.labs.speakeasy.util.jgit.WalkFetchConnection.java
License:Eclipse Distribution License
private void verifyAndInsertLooseObject(final AnyObjectId id, final byte[] compressed) throws IOException { final ObjectLoader uol; try {/*from ww w. j av a2 s. co m*/ uol = UnpackedObject.parse(compressed, id); } catch (CorruptObjectException parsingError) { // Some HTTP servers send back a "200 OK" status with an HTML // page that explains the requested file could not be found. // These servers are most certainly misconfigured, but many // of them exist in the world, and many of those are hosting // Git repositories. // // Since an HTML page is unlikely to hash to one of our loose // objects we treat this condition as a FileNotFoundException // and attempt to recover by getting the object from another // source. // final FileNotFoundException e; e = new FileNotFoundException(id.name()); e.initCause(parsingError); throw e; } final int type = uol.getType(); final byte[] raw = uol.getCachedBytes(); if (objCheck != null) { try { objCheck.check(type, raw); } catch (CorruptObjectException e) { throw new TransportException(MessageFormat.format(JGitText.get().transportExceptionInvalid, Constants.typeString(type), id.name(), e.getMessage())); } } ObjectId act = inserter.insert(type, raw); if (!AnyObjectId.equals(id, act)) { throw new TransportException(MessageFormat.format(JGitText.get().incorrectHashFor, id.name(), act.name(), Constants.typeString(type), compressed.length)); } inserter.flush(); }
From source file:org.test.RewriteGitHistory.java
License:Apache License
/** * In this case the svn repository has been cloned but there are no seperate * branches. Master points at the root with all the directories contained * beneath it.//from ww w . j a v a 2s .com * * What we want to accomplish is to create new commits for each 'branch' * that will contain only their contents. * * We do not erase or modify the original commits. * * We locate all of the commits that contain the includePath. * * Then we locate the first commit that included it. We create new commits for each of the existing commits * but we modify the tree to only contain the matched tree of each commit. * @throws NoHeadException * @throws WrongRepositoryStateException * @throws ConcurrentRefUpdateException * @throws NoMessageException * @throws NoFilepatternException * * */ public void extractBranch(String sourceBranchName, String newBranchName, String includePath) throws JGitInternalException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, AmbiguousObjectException, IOException, NoHeadException, NoMessageException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoFilepatternException { Ref branchId = git.checkout().setName(sourceBranchName).call(); RevWalk rwalk = new RevWalk(repository); RevCommit tip = rwalk.lookupCommit(branchId.getObjectId()); rwalk.markStart(tip); rwalk.setTreeFilter(PathFilter.create(includePath)); Iterator<RevCommit> commitIter = rwalk.iterator(); List<CommitBuilder> newBranchCommits = new ArrayList<CommitBuilder>(); RevCommit branchPoint = null; while (commitIter.hasNext()) { RevCommit commit = (RevCommit) commitIter.next(); printCommit(commit); final TreeWalk walk = new TreeWalk(repository); walk.setRecursive(false); //only look at the top level entries in the commit tree walk.addTree(commit.getTree().getId()); // match all of the paths on this commit List<ObjectId> matchedPaths = new ArrayList<ObjectId>(); while (walk.next()) { final FileMode mode = walk.getFileMode(0); // if (mode == FileMode.TREE) // log.info("0"); // log.info(mode.toString()); // log.info(" "); if (mode == FileMode.TREE) { String path = walk.getPathString(); if (path.startsWith(includePath)) { log.info("matched " + Constants.typeString(mode.getObjectType()) + " sha1: " + walk.getObjectId(0).name() + " path: " + walk.getPathString()); matchedPaths.add(walk.getObjectId(0)); branchPoint = commit; } else { log.debug("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: " + walk.getObjectId(0).name() + " path: " + walk.getPathString()); } } else { log.debug("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: " + walk.getObjectId(0).name() + " path: " + walk.getPathString()); } } if (matchedPaths.size() > 1) { // create a tree to hold the tree log.warn("multiple matches found for path = " + includePath + " commit = " + commit.getId().name()); } else if (matchedPaths.size() == 1) { PersonIdent author = commitFilter.filterPersonIdent(commit.getAuthorIdent()); PersonIdent committer = commitFilter.filterPersonIdent(commit.getAuthorIdent()); CommitBuilder commitBuilder = new CommitBuilder(); // set parent id later once we have the full path // commitBuilder.setParentId(newBaseId); commitBuilder.setTreeId(matchedPaths.get(0)); commitBuilder.setAuthor(author); commitBuilder.setCommitter(committer); commitBuilder.setEncoding(commit.getEncoding()); commitBuilder.setMessage(commitFilter.filterCommitMessage(commit.getFullMessage())); newBranchCommits.add(0, commitBuilder); } else { // do nothing } } // done with the commits. ObjectId parent = branchPoint; if (branchPoint.getParentCount() > 0) parent = branchPoint.getParent(0); Ref branch = git.checkout().setName(newBranchName).setCreateBranch(true).setStartPoint(parent.name()) .call(); // first step is that we need to delete all of the files that do not match the pattern RmCommand rmCommand = new RmCommand(repository); File workTree = git.getRepository().getWorkTree(); // only get the top level files and directories File[] topLevelFiles = workTree.listFiles(); for (File file : topLevelFiles) { if (!file.getName().startsWith(includePath)) { log.info("removing file = " + file.getAbsolutePath()); rmCommand.addFilepattern(file.getAbsolutePath()); } } rmCommand.call(); RevCommit ref = git.commit().setAuthor(selfCommitIdent).setCommitter(selfCommitIdent) .setMessage("Delete uneeded content from newly extracted branch '" + newBranchName + "'").call(); ObjectId parentId = ref.getId(); for (CommitBuilder commitBuilder : newBranchCommits) { commitBuilder.setParentId(parentId); parentId = executeCommit(commitBuilder); } }
From source file:org.test.RewriteGitHistory.java
License:Apache License
/** * We look backwards from the head commit on the branch named. We are looking for the path given. * // w w w. jav a 2 s . c o m * We will accumulate the list of commits where it exists. We go all the way back to the root of the branch to account for all branches. * * * @param branchName * @param filePrefix * @throws MissingObjectException * @throws IncorrectObjectTypeException * @throws IOException * @throws JGitInternalException * @throws RefAlreadyExistsException * @throws RefNotFoundException * @throws InvalidRefNameException */ public void listMatchingCommits(String branchName, String filePrefix) throws MissingObjectException, IncorrectObjectTypeException, IOException, JGitInternalException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException { Ref branchId = git.checkout().setName(branchName).call(); RevWalk rwalk = new RevWalk(repository); RevCommit tip = rwalk.lookupCommit(branchId.getObjectId()); rwalk.markStart(tip); rwalk.setTreeFilter(PathFilter.create(filePrefix)); Iterator<RevCommit> commitIter = rwalk.iterator(); while (commitIter.hasNext()) { RevCommit commit = (RevCommit) commitIter.next(); printCommit(commit); final TreeWalk walk = new TreeWalk(repository); walk.setRecursive(false); walk.addTree(commit.getTree().getId()); List<String> matchedPaths = new ArrayList<String>(); while (walk.next()) { final FileMode mode = walk.getFileMode(0); // if (mode == FileMode.TREE) // log.info("0"); // log.info(mode.toString()); // log.info(" "); if (mode == FileMode.TREE) { String path = walk.getPathString(); if (path.startsWith(filePrefix)) { log.info("matched " + Constants.typeString(mode.getObjectType()) + " sha1: " + walk.getObjectId(0).name() + " path: " + walk.getPathString()); } else { log.info("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: " + walk.getObjectId(0).name() + " path: " + walk.getPathString()); } } else { log.info("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: " + walk.getObjectId(0).name() + " path: " + walk.getPathString()); } } // while (objWalk.n) // // objWalk. } }