List of usage examples for org.eclipse.jgit.api Git log
public LogCommand log()
From source file:org.modeshape.connector.git.GitFunction.java
License:Apache License
/** * Add an additional page of commits in the history names of the tags as children of the current node. * /* www. j av a 2s. c om*/ * @param git the Git object; may not be null * @param repository the Repository object; may not be null * @param spec the call specification; may not be null * @param writer the page writer for the current node; may not be null * @param pageKey the page key for this page; may not be null * @throws GitAPIException if there is a problem accessing the Git repository * @throws IOException if there is a problem reading the Git repository */ protected void addCommitsAsPageOfChildren(Git git, Repository repository, CallSpecification spec, PageWriter writer, PageKey pageKey) throws GitAPIException, IOException { RevWalk walker = new RevWalk(repository); try { // The offset is the ID of the last commit we read, so we'll need to skip the first commit String lastCommitIdName = pageKey.getOffsetString(); ObjectId lastCommitId = repository.resolve(lastCommitIdName); int pageSize = (int) pageKey.getBlockSize(); // int offset = pageKey.getOffsetInt(); // int maxCount = pageSize + offset; LogCommand command = git.log(); command.add(lastCommitId); command.setMaxCount(pageSize + 1); // Add the first set of commits ... int actual = 0; String commitId = null; for (RevCommit commit : command.call()) { commitId = commit.getName(); if (commitId.equals(lastCommitIdName)) continue; writer.addChild(spec.childId(commitId), commitId); ++actual; } if (actual == pageSize) { assert commitId != null; // We wrote the maximum number of commits, so there's (probably) another page ... writer.addPage(pageKey.getParentId(), commitId, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE); } } finally { walker.dispose(); } }
From source file:org.modeshape.connector.git.GitHistory.java
License:Apache License
@Override public Document execute(Repository repository, Git git, CallSpecification spec, DocumentWriter writer, Values values) throws GitAPIException, IOException { if (spec.parameterCount() == 0) { // This is the top-level "/commits" node writer.setPrimaryType(GitLexicon.COMMITS); // Generate the child references to the branches, tags, and commits in the history ... addBranchesAsChildren(git, spec, writer); addTagsAsChildren(git, spec, writer); addCommitsAsChildren(git, spec, writer, pageSize); } else if (spec.parameterCount() == 1) { // This is the top-level "/commits/{branchOrTagNameOrObjectId}" node writer.setPrimaryType(GitLexicon.OBJECT); // Generate the child references to the (latest) commits on this branch/tag ... String branchOrTagNameOrObjectId = spec.parameter(0); ObjectId objId = resolveBranchOrTagOrCommitId(repository, branchOrTagNameOrObjectId); RevWalk walker = new RevWalk(repository); try {// www. ja v a 2s .c o m RevCommit commit = walker.parseCommit(objId); LogCommand command = git.log(); command.add(commit.getId()); command.setMaxCount(pageSize); for (RevCommit rev : command.call()) { String commitId = rev.getId().getName(); writer.addChild(spec.childId(commitId), commitId); } // Handle paging ... writer.addPage(spec.getParentId(), pageSize, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE); } finally { walker.dispose(); } } else if (spec.parameterCount() == 2) { // This is a specific commit in the history, via "/commits/{branchOrTagNameOrObjectId}/{objectId}" writer.setPrimaryType(GitLexicon.COMMIT); // so we need to show the commit information ... RevWalk walker = new RevWalk(repository); try { String commitId = spec.parameter(1); ObjectId objId = repository.resolve(commitId); RevCommit commit = walker.parseCommit(objId); writer.addProperty(GitLexicon.OBJECT_ID, objId.name()); writer.addProperty(GitLexicon.AUTHOR, authorName(commit)); writer.addProperty(GitLexicon.COMMITTER, commiterName(commit)); writer.addProperty(GitLexicon.COMMITTED, values.dateFrom(commit.getCommitTime())); writer.addProperty(GitLexicon.TITLE, commit.getShortMessage()); writer.addProperty(GitLexicon.TREE, GitTree.referenceToTree(objId, objId.name(), values)); writer.addProperty(GitLexicon.DETAIL, GitCommitDetails.referenceToCommit(objId, values)); // And there are no children } finally { walker.dispose(); } } else { return null; } return writer.document(); }
From source file:org.modeshape.connector.git.GitHistory.java
License:Apache License
@Override public Document execute(Repository repository, Git git, CallSpecification spec, PageWriter writer, Values values, PageKey pageKey) throws GitAPIException, IOException { if (spec.parameterCount() == 0) { // List the next page of commits ... addCommitsAsPageOfChildren(git, repository, spec, writer, pageKey); } else {/* w ww. j a va 2s .c o m*/ // We know the branch, tag, or commit for the history ... String branchOrTagNameOrObjectId = spec.parameter(0); ObjectId objId = repository.resolve(branchOrTagNameOrObjectId); RevWalk walker = new RevWalk(repository); try { int offset = pageKey.getOffsetInt(); RevCommit commit = walker.parseCommit(objId); LogCommand command = git.log(); command.add(commit.getId()); command.setSkip(offset); command.setMaxCount(pageSize); for (RevCommit rev : command.call()) { String commitId = rev.getId().toString(); writer.addChild(spec.childId(commitId), commitId); } // Handle paging ... int nextOffset = offset + pageSize; writer.addPage(pageKey.getParentId(), nextOffset, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE); } finally { walker.dispose(); } } return writer.document(); }
From source file:org.modeshape.connector.git.GitTree.java
License:Apache License
protected void addInformationForPath(Repository repository, Git git, DocumentWriter writer, RevCommit commit, String path, CallSpecification spec, Values values) throws GitAPIException, IOException { // Make sure the path is in the canonical form we need ... if (path.startsWith("/")) { if (path.length() == 1) path = ""; else//from w w w . ja va2 s .c o m path = path.substring(1); } // Now see if we're actually referring to the "jcr:content" node ... boolean isContentNode = false; if (path.endsWith(JCR_CONTENT_SUFFIX)) { isContentNode = true; path = path.substring(0, path.length() - JCR_CONTENT_SUFFIX.length()); } // Create the TreeWalk that we'll use to navigate the files/directories ... final TreeWalk tw = new TreeWalk(repository); tw.addTree(commit.getTree()); if ("".equals(path)) { // This is the top-level directory, so we don't need to pre-walk to find anything ... tw.setRecursive(false); while (tw.next()) { String childName = tw.getNameString(); String childId = spec.childId(childName); writer.addChild(childId, childName); } } else { // We need to first find our path *before* we can walk the children ... PathFilter filter = PathFilter.create(path); tw.setFilter(filter); while (tw.next()) { if (filter.isDone(tw)) { break; } else if (tw.isSubtree()) { tw.enterSubtree(); } } // Now that the TreeWalk is the in right location given by the 'path', we can get the if (tw.isSubtree()) { // The object at the 'path' is a directory, so go into it ... tw.enterSubtree(); // Find the commit in which this folder was last modified ... // This may not be terribly efficient, but it seems to work faster on subsequent runs ... RevCommit folderCommit = git.log().addPath(path).call().iterator().next(); writer.setPrimaryType(GitLexicon.FOLDER); // could happen if not enough permissions, for example if (folderCommit != null) { // Add folder-related properties ... String committer = commiterName(folderCommit); String author = authorName(folderCommit); DateTime committed = values.dateFrom(folderCommit.getCommitTime()); writer.addProperty(JcrLexicon.CREATED, committed); writer.addProperty(JcrLexicon.CREATED_BY, committer); writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name()); writer.addProperty(GitLexicon.AUTHOR, author); writer.addProperty(GitLexicon.COMMITTER, committer); writer.addProperty(GitLexicon.COMMITTED, committed); writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage()); } else { connector.getLogger().warn(GitI18n.cannotReadCommit, path); } // And now walk the contents of the directory ... while (tw.next()) { String childName = tw.getNameString(); String childId = spec.childId(childName); writer.addChild(childId, childName); } } else { // The path specifies a file (or a content node) ... // Find the commit in which this folder was last modified ... // This may not be terribly efficient, but it seems to work faster on subsequent runs ... RevCommit fileCommit = git.log().addPath(path).call().iterator().next(); if (isContentNode) { writer.setPrimaryType(GitLexicon.RESOURCE); if (fileCommit == null) { // could happen if not enough permissions, for example connector.getLogger().warn(GitI18n.cannotReadCommit, path); return; } // Add file-related properties ... String committer = commiterName(fileCommit); String author = authorName(fileCommit); DateTime committed = values.dateFrom(fileCommit.getCommitTime()); writer.addProperty(JcrLexicon.LAST_MODIFIED, committed); writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer); writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name()); writer.addProperty(GitLexicon.AUTHOR, author); writer.addProperty(GitLexicon.COMMITTER, committer); writer.addProperty(GitLexicon.COMMITTED, committed); writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage()); // Create the BinaryValue ... ObjectId fileObjectId = tw.getObjectId(0); ObjectLoader fileLoader = repository.open(fileObjectId); BinaryKey key = new BinaryKey(fileObjectId.getName()); BinaryValue value = values.binaryFor(key, fileLoader.getSize()); if (value == null) { // It wasn't found in the binary store ... if (fileLoader.isLarge()) { // Too large to hold in memory, so use the binary store (which reads the file immediately) ... value = values.binaryFrom(fileLoader.openStream()); } else { // This is small enough to fit into a byte[], but it still may be pretty big ... value = new GitBinaryValue(fileObjectId, fileLoader, connector.getSourceName(), name, connector.getMimeTypeDetector()); } } writer.addProperty(JcrLexicon.DATA, value); if (connector.includeMimeType()) { try { String filename = spec.parameter(spec.parameterCount() - 1); // the last is 'jcr:content' String mimeType = value.getMimeType(filename); if (mimeType != null) writer.addProperty(JcrLexicon.MIMETYPE, mimeType); } catch (RepositoryException e) { // do nothing } catch (IOException e) { // do nothing } } } else { writer.setPrimaryType(GitLexicon.FILE); if (fileCommit == null) { // could happen if not enough permissions, for example connector.getLogger().warn(GitI18n.cannotReadCommit, path); return; } // Add file-related properties ... String committer = commiterName(fileCommit); String author = authorName(fileCommit); DateTime committed = values.dateFrom(fileCommit.getCommitTime()); writer.addProperty(JcrLexicon.CREATED, committed); writer.addProperty(JcrLexicon.CREATED_BY, committer); writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name()); writer.addProperty(GitLexicon.AUTHOR, author); writer.addProperty(GitLexicon.COMMITTER, committer); writer.addProperty(GitLexicon.COMMITTED, committed); writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage()); // Add the "jcr:content" child node ... String childId = spec.childId(JCR_CONTENT); writer.addChild(childId, JCR_CONTENT); } } } }
From source file:org.n52.wps.repository.git.GitAlgorithmRepositoryTest.java
License:Open Source License
@Test public void initLocalGitRepository() throws IOException, GitAPIException { File file = cleanRepository.resolve("hello_world.txt").toFile(); file.createNewFile();//w w w .jav a 2 s . co m Git repoGit = Git.open(cleanRepository.toFile()); repoGit.add().addFilepattern(file.getPath()); repoGit.commit().setMessage("initial commit").call(); File wc = testRoot.newFolder("workingCopy"); Git wcGit = Git.cloneRepository().setURI(cleanRepository.toUri().toString()).setDirectory(wc).call(); int i = 0; Iterator<RevCommit> commits = wcGit.log().all().call().iterator(); while (commits.hasNext()) { i++; commits.next(); } MatcherAssert.assertThat(i, Is.is(1)); }
From source file:org.ocpsoft.redoculous.model.impl.GitRepository.java
License:Open Source License
public List<String> getLogForCurrentBranch() throws GitAPIException { Git git = null; try {// w w w . j ava2s . co m git = git(getBaseDir()); List<String> results = new ArrayList<String>(); Iterable<RevCommit> commits = git.log().call(); for (RevCommit commit : commits) results.add(commit.getFullMessage()); return results; } catch (Exception e) { throw new RuntimeException("Failed to get log for repository [" + getUrl() + "] [" + getKey() + "]", e); } finally { if (git != null) git.getRepository().close(); } }
From source file:org.openengsb.connector.git.internal.GitServiceImpl.java
License:Apache License
@Override public List<CommitRef> update() { List<CommitRef> commits = new ArrayList<CommitRef>(); try {/* w ww . j a v a 2 s. c o m*/ if (repository == null) { prepareWorkspace(); initRepository(); } Git git = new Git(repository); AnyObjectId oldHead = repository.resolve(Constants.HEAD); if (oldHead == null) { LOGGER.debug("Local repository is empty. Fetching remote repository."); FetchResult fetchResult = doRemoteUpdate(); if (fetchResult.getTrackingRefUpdate(Constants.R_REMOTES + "origin/" + watchBranch) == null) { LOGGER.debug("Nothing to fetch from remote repository."); return null; } doCheckout(fetchResult); } else { LOGGER.debug("Local repository exists. Pulling remote repository."); git.pull().call(); } AnyObjectId newHead = repository.resolve(Constants.HEAD); if (newHead == null) { LOGGER.debug("New HEAD of local repository doesnt exist."); return null; } if (newHead != oldHead) { LogCommand logCommand = git.log(); if (oldHead == null) { LOGGER.debug("Retrieving revisions from HEAD [{}] on", newHead.name()); logCommand.add(newHead); } else { LOGGER.debug("Retrieving revisions in range [{}, {}]", newHead.name(), oldHead.name()); logCommand.addRange(oldHead, newHead); } Iterable<RevCommit> revisions = logCommand.call(); for (RevCommit revision : revisions) { commits.add(new GitCommitRef(revision)); } } } catch (Exception e) { throw new ScmException(e); } return commits; }
From source file:org.ossmeter.platform.vcs.git.GitManager.java
License:Open Source License
/** * To set the startRevision to the first commit, use 'null' * FIXME: This should HANDLE the exception probably.. *//*www. j a va2 s .c om*/ @Override public VcsRepositoryDelta getDelta(VcsRepository repository, String startRevision, String endRevision) throws Exception { // Clone into local repo Git git = getGit((GitRepository) repository); VcsRepositoryDelta vcsDelta = new VcsRepositoryDelta(); vcsDelta.setRepository(repository); Repository repo = git.getRepository(); RevWalk walk = new RevWalk(repo); Iterable<RevCommit> logs = git.log().call(); Iterator<RevCommit> iterator = logs.iterator(); boolean foundStart = false; boolean foundEnd = false; List<RevCommit> commits = new ArrayList<RevCommit>(); // Reorder the commits (currently they are latest first) while (iterator.hasNext()) { commits.add(0, walk.parseCommit(iterator.next())); } for (int i = 0; i < commits.size(); i++) { RevCommit commit = commits.get(i); RevCommit prevCommit = i - 1 < 0 ? null : commits.get(i - 1); if (startRevision == null || commit.getId().getName().equals(startRevision)) { foundStart = true; } if (commit.getId().getName().equals(endRevision)) { foundEnd = true; } VcsCommit vcsCommit = null; if (foundStart) { // Setup the meta data for the commit vcsCommit = new VcsCommit(); vcsCommit.setRevision(commit.getId().getName()); vcsCommit.setMessage(commit.getFullMessage()); vcsCommit.setAuthor(commit.getAuthorIdent().getName()); vcsCommit.setDelta(vcsDelta); int date = commit.getCommitTime(); long datelong = (long) date * (long) 1000; vcsCommit.setJavaDate(new java.util.Date(datelong)); vcsDelta.getCommits().add(vcsCommit); if (prevCommit != null) { // Do a diff against the succeeding commit ObjectId thisCommitId = repo.resolve(commit.getId().getName() + "^{tree}"); ObjectId prevCommitId = repo.resolve(prevCommit.getId().getName() + "^{tree}"); ObjectReader reader = repo.newObjectReader(); CanonicalTreeParser currentTreeIter = new CanonicalTreeParser(); currentTreeIter.reset(reader, thisCommitId); CanonicalTreeParser prevTreeIter = new CanonicalTreeParser(); prevTreeIter.reset(reader, prevCommitId); List<DiffEntry> diffs = git.diff().setNewTree(currentTreeIter).setOldTree(prevTreeIter).call(); for (DiffEntry diff : diffs) { VcsChangeType change = convertChangeType(diff.getChangeType()); if (change == null) continue; VcsCommitItem item = new VcsCommitItem(); String path = diff.getNewPath(); if (change.equals(VcsChangeType.DELETED)) { path = diff.getOldPath(); } item.setPath(path); item.setChangeType(change); item.setCommit(vcsCommit); vcsCommit.getItems().add(item); } } else { // First commit: everything is ADDED // vcsCommit = new VcsCommit(); TreeWalk treeWalk = new TreeWalk(repo); treeWalk.addTree(commit.getTree()); while (treeWalk.next()) { if (treeWalk.isSubtree()) { treeWalk.enterSubtree(); } else { VcsCommitItem item = new VcsCommitItem(); item.setPath(treeWalk.getPathString()); item.setChangeType(VcsChangeType.ADDED); item.setCommit(vcsCommit); vcsCommit.getItems().add(item); } } } } if (foundEnd) { break; } } return vcsDelta; }
From source file:org.ossmeter.platform.vcs.git.GitManager.java
License:Open Source License
@Override public String getFirstRevision(VcsRepository repository) throws Exception { Git git = getGit((GitRepository) repository); Repository repo = git.getRepository(); RevWalk walk = new RevWalk(repo); Iterator<RevCommit> iterator = git.log().call().iterator(); walk.parseCommit(iterator.next());//from w ww .j av a2 s .c o m String revision = null; // The commits are ordered latest first, so we want the last one. while (iterator.hasNext()) { RevCommit commit = iterator.next(); if (!iterator.hasNext()) { revision = commit.getId().getName(); } } return revision; }
From source file:org.ossmeter.platform.vcs.git.GitManager.java
License:Open Source License
@Override public int compareVersions(VcsRepository repository, String versionOne, String versionTwo) throws Exception { Git git = getGit((GitRepository) repository); Repository repo = git.getRepository(); RevWalk walk = new RevWalk(repo); Iterator<RevCommit> iterator = git.log().call().iterator(); walk.parseCommit(iterator.next());/*w ww .ja v a 2 s . c o m*/ List<String> revisions = new ArrayList<String>(); // The commits are ordered latest first, so we want the last one. while (iterator.hasNext()) { RevCommit commit = iterator.next(); revisions.add(commit.getId().getName()); } Integer oneIndex = revisions.indexOf(versionOne); Integer twoIndex = revisions.indexOf(versionTwo); // System.out.println(oneIndex); // System.out.println(twoIndex); // System.out.println(revisions); // Because the revision list is reversed, we compare two to one instead of the other way around return twoIndex.compareTo(oneIndex); }