/** * Convert byte array to UTF-8 {@link String} * * @param repository * @param raw * @return UTF-8 string or null if bytes are null */ protected static String toString(final Repository repository, final byte[] raw) { if (raw == null) return null; try { return new String(raw, CHARACTER_ENCODING); } catch (UnsupportedEncodingException e) { throw new GitException(e, repository); } }
/** * Resolve the revision string to a commit object id * * @param repository * @param revision * @return commit id */ protected static ObjectId resolve(final Repository repository, final String revision) { try { return repository.resolve(revision); } catch (IOException e) { throw new GitException(e, repository); } }
/** * Resolve the revision string to a commit object id. * <p> * A {@link GitException} will be thrown when the revision can not be * resolved to an {@link ObjectId} * * @param repository * @param revision * @return commit id */ protected static ObjectId strictResolve(final Repository repository, final String revision) { final ObjectId resolved = resolve(repository, revision); if (resolved == null) throw new GitException(MessageFormat.format( "Revision ''{0}'' could not be resolved", revision), repository); return resolved; }
/** * Get the refs with prefix in repository * * @param repository * @param prefix * @return collection of refs */ protected static Collection<Ref> getRefs(final Repository repository, final String prefix) { try { return repository.getRefDatabase().getRefs(prefix).values(); } catch (IOException e) { throw new GitException(e, repository); } }
/** * Get the contents of the the blob with the given id as a byte array. * * @param repository * @param id * @return blob bytes */ protected static byte[] getBytes(final Repository repository, final ObjectId id) { try { return repository.open(id, OBJ_BLOB).getCachedBytes(MAX_VALUE); } catch (IOException e) { throw new GitException(e, repository); } }
/** * Get the contents of the the blob with the given id as a byte array. * * @param reader * @param id * @return blob bytes */ protected static byte[] getBytes(final ObjectReader reader, final ObjectId id) { try { return reader.open(id, OBJ_BLOB).getCachedBytes(MAX_VALUE); } catch (IOException e) { throw new GitException(e, null); } }
private static RevCommit lookupRef(final Repository repository, final Ref ref) { final RevWalk walk = new RevWalk(repository); try { return getRef(walk, ref); } catch (IOException e) { throw new GitException(e, repository); } finally { walk.close(); } }
/** * Create a tree walk with all the trees from the given commit's parents. * * @param walk * @param commit * @return tree walk */ public static TreeWalk withParents(final RevWalk walk, final RevCommit commit) { if (walk == null) throw new IllegalArgumentException(Assert.formatNotNull("Walk")); if (commit == null) throw new IllegalArgumentException(Assert.formatNotNull("Commit")); try { return withParents(walk.getObjectReader(), walk, commit); } catch (IOException e) { throw new GitException(e, null); } }
/** * Parse a commit from the object reader * * @param repository * @param reader * @param commit * @return commit */ protected static RevCommit parse(final Repository repository, final ObjectReader reader, final ObjectId commit) { final RevWalk walk = new RevWalk(reader); walk.setRetainBody(true); try { return walk.parseCommit(commit); } catch (IOException e) { throw new GitException(e, repository); } }
/** * Get all the commits that tags in the given repository reference. * * @param repository * @return non-null but possibly empty collection of commits */ public static Collection<RevCommit> getTags(final Repository repository) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); final Collection<RevCommit> commits = new HashSet<RevCommit>(); final RevWalk walk = new RevWalk(repository); final RefDatabase refDb = repository.getRefDatabase(); try { getRefCommits(walk, refDb, R_TAGS, commits); } catch (IOException e) { throw new GitException(e, repository); } finally { walk.close(); } return commits; }
/** * Create a tree walk configured with the given commit revisions * * @param repository * @param revisions * @return tree walk */ public static TreeWalk withCommits(final Repository repository, final String... revisions) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); if (revisions == null) throw new IllegalArgumentException( Assert.formatNotNull("Revisions")); if (revisions.length == 0) throw new IllegalArgumentException( Assert.formatNotEmpty("Revisions")); final TreeWalk walk = new TreeWalk(repository); try { for (String revision : revisions) walk.addTree(CommitUtils.getCommit(repository, revision) .getTree()); } catch (IOException e) { throw new GitException(e, repository); } return walk; }
/** * Get all the commits that branches in the given repository reference. * * @param repository * @return non-null but possibly empty collection of commits */ public static Collection<RevCommit> getBranches(final Repository repository) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); final Collection<RevCommit> commits = new HashSet<RevCommit>(); final RevWalk walk = new RevWalk(repository); final RefDatabase refDb = repository.getRefDatabase(); try { getRefCommits(walk, refDb, R_HEADS, commits); getRefCommits(walk, refDb, R_REMOTES, commits); } catch (IOException e) { throw new GitException(e, repository); } finally { walk.close(); } return commits; }
return repository.open(blobId, OBJ_BLOB).openStream(); } catch (IOException e) { throw new GitException(e, repository);
/** * Create a tree walk configured with the given commits * * @param repository * @param commits * @return tree walk */ public static TreeWalk withCommits(final Repository repository, final ObjectId... commits) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); if (commits == null) throw new IllegalArgumentException(Assert.formatNotNull("Commits")); if (commits.length == 0) throw new IllegalArgumentException(Assert.formatNotEmpty("Commits")); final TreeWalk walk = new TreeWalk(repository); try { for (ObjectId commit : commits) walk.addTree(CommitUtils.getCommit(repository, commit) .getTree()); } catch (IOException e) { throw new GitException(e, repository); } return walk; }
/** * Open a stream to the contents of the blob at the path in the given commit * * @param repository * @param commitId * @param path * @return stream, null if no blob at given path */ public static ObjectStream getStream(final Repository repository, final ObjectId commitId, final String path) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); if (commitId == null) throw new IllegalArgumentException( Assert.formatNotNull("Commit id")); if (path == null) throw new IllegalArgumentException(Assert.formatNotNull("Path")); if (path.length() == 0) throw new IllegalArgumentException(Assert.formatNotEmpty("Path")); final RevCommit commit = CommitUtils.parse(repository, commitId); final ObjectId blobId = lookupId(repository, commit, path); if (blobId == null) return null; try { return repository.open(blobId, OBJ_BLOB).openStream(); } catch (IOException e) { throw new GitException(e, repository); } }
/** * Parse a commit from the repository * * @param repository * @param commit * @return commit */ protected static RevCommit parse(final Repository repository, final ObjectId commit) { final RevWalk walk = new RevWalk(repository); walk.setRetainBody(true); try { return walk.parseCommit(commit); } catch (IOException e) { throw new GitException(e, repository); } finally { walk.close(); } }
/** * Create a tree walk with all the trees from the given revision's commit * parents. * * @param repository * @param revision * @return tree walk */ public static TreeWalk withParents(final Repository repository, final String revision) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); if (revision == null) throw new IllegalArgumentException(Assert.formatNotNull("Revision")); if (revision.length() == 0) throw new IllegalArgumentException( Assert.formatNotEmpty("Revision")); final ObjectId commit = CommitUtils.strictResolve(repository, revision); final ObjectReader reader = repository.newObjectReader(); final RevWalk walk = new RevWalk(reader); try { return withParents(reader, walk, walk.parseCommit(commit)); } catch (IOException e) { walk.close(); throw new GitException(e, repository); } }
/** * Search the commits starting at the commit that each tag is referencing. * <p> * Repositories that have no tags will be ignored. * * @return this finder */ public CommitFinder findInTags() { final Repository[] repos = repositories; final int repoCount = repositories.length; Repository repo; for (int i = 0; i < repoCount; i++) { repo = repos[i]; final Collection<RevCommit> commits = CommitUtils.getTags(repo); if (commits.isEmpty()) continue; final RevWalk walk = createWalk(repo); try { walk.markStart(commits); walk(walk); } catch (IOException e) { throw new GitException(e, repo); } finally { walk.close(); } } return this; }
/** * Create a tree walk with all the trees from the given commit's parents. * * @param repository * @param commitId * @return tree walk */ public static TreeWalk withParents(final Repository repository, final AnyObjectId commitId) { if (repository == null) throw new IllegalArgumentException( Assert.formatNotNull("Repository")); if (commitId == null) throw new IllegalArgumentException( Assert.formatNotNull("Commit id")); final ObjectReader reader = repository.newObjectReader(); final RevWalk walk = new RevWalk(reader); try { return withParents(reader, walk, walk.parseCommit(commitId)); } catch (IOException e) { walk.close(); throw new GitException(e, repository); } }
/** * Get the id of the blob at the path in the given commit. * * @param repository * @param commit * @param path * @return blob id, null if not present */ protected static ObjectId lookupId(final Repository repository, final RevCommit commit, final String path) { final TreeWalk walk; try { walk = TreeWalk.forPath(repository, path, commit.getTree()); } catch (IOException e) { throw new GitException(e, repository); } if (walk == null) return null; if ((walk.getRawMode(0) & TYPE_MASK) != TYPE_FILE) return null; return walk.getObjectId(0); }